Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8888546

Browse files
Merge pull request #325 from ahmedfgad/github-actions
GitHub actions
2 parents 44a9070 + 33f443e commit 8888546

File tree

2 files changed

+53
-39
lines changed

2 files changed

+53
-39
lines changed

‎.github/workflows/main_py37.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
job_id_1:
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-latest
1212
name: PyTest Workflow Job
1313

1414
steps:

‎pygad/helper/unique.py

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,9 @@ def unique_int_gene_from_range(self,
184184
all_gene_values = numpy.asarray(all_gene_values,
185185
dtype[0])
186186

187-
values_to_select_from = list(set(list(all_gene_values)) - set(solution))
188-
189-
if len(values_to_select_from) == 0:
190-
# If there are no values, then keep the current gene value.
191-
selected_value = solution[gene_index]
192-
else:
193-
selected_value = random.choice(values_to_select_from)
187+
selected_value = self.select_unique_value(gene_values=all_gene_values,
188+
solution=solution,
189+
gene_index=gene_index)
194190

195191
selected_value = dtype[0](selected_value)
196192

@@ -224,39 +220,57 @@ def unique_float_gene_from_range(self,
224220
# The gene_type is of the form [type, precision]
225221
dtype = gene_type
226222

227-
for trial_index in range(num_trials):
228-
temp_val = numpy.random.uniform(low=min_val,
229-
high=max_val,
230-
size=1)[0]
223+
# We cannot have a list of all values out of a continous range.
224+
# Solution is to create a subset (e.g. 100) of all the values.
225+
some_gene_values = numpy.random.uniform(low=min_val,
226+
high=max_val,
227+
size=100)
231228

232-
# If mutation is by replacement, do not add the current gene value into the list.
233-
# This is to avoid replacing the value by itself again. We are doing nothing in this case.
234-
if mutation_by_replacement:
235-
pass
236-
else:
237-
temp_val = temp_val + solution[gene_index]
229+
# If mutation is by replacement, do not add the current gene value into the list.
230+
# This is to avoid replacing the value by itself again. We are doing nothing in this case.
231+
if mutation_by_replacement:
232+
pass
233+
else:
234+
some_gene_values = some_gene_values + solution[gene_index]
235+
236+
if not dtype[1] is None:
237+
# Precision is available and we have to round the number.
238+
# Convert the data type and round the number.
239+
some_gene_values = numpy.round(numpy.asarray(some_gene_values,
240+
dtype[0]),
241+
dtype[1])
242+
else:
243+
# There is no precision and rounding the number is not needed. The type is [type, None]
244+
# Just convert the data type.
245+
some_gene_values = numpy.asarray(some_gene_values,
246+
dtype[0])
247+
248+
selected_value = self.select_unique_value(gene_values=some_gene_values,
249+
solution=solution,
250+
gene_index=gene_index)
251+
return selected_value
238252

239-
if not dtype[1] is None:
240-
# Precision is available and we have to round the number.
241-
# Convert the data type and round the number.
242-
temp_val = numpy.round(dtype[0](temp_val),
243-
dtype[1])
244-
else:
245-
# There is no precision and rounding the number is not needed. The type is [type, None]
246-
# Just convert the data type.
247-
temp_val = dtype[0](temp_val)
248-
249-
if temp_val in solution and trial_index == (num_trials - 1):
250-
# If there are no values, then keep the current gene value.
251-
if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but cannot find a value to prevent duplicates.")
252-
selected_value = solution[gene_index]
253-
elif temp_val in solution:
254-
# Keep trying in the other remaining trials.
255-
continue
256-
else:
257-
# Unique gene value found.
258-
selected_value = temp_val
259-
break
253+
def select_unique_value(self, gene_values, solution, gene_index):
254+
255+
"""
256+
Select a unique value (if possible) from a list of gene values.
257+
258+
Args:
259+
gene_values (NumPy Array): An array of values from which a unique value should be selected.
260+
solution (list): A solution containing genes, potentially with duplicate values.
261+
262+
Returns:
263+
selected_gene: The new (hopefully unique) value of the gene. If no unique value can be found, the original gene value is returned.
264+
"""
265+
266+
values_to_select_from = list(set(list(gene_values)) - set(solution))
267+
268+
if len(values_to_select_from) == 0:
269+
# If there are no values, then keep the current gene value.
270+
if not self.suppress_warnings: warnings.warn("You set 'allow_duplicate_genes=False' but cannot find a value to prevent duplicates.")
271+
selected_value = solution[gene_index]
272+
else:
273+
selected_value = random.choice(values_to_select_from)
260274

261275
return selected_value
262276

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /