Skip to content

Commit ce702a9

Browse files
author
Ahmed Gad
committed
Fix selecting a unique value from gene space
1 parent 3ba005f commit ce702a9

File tree

7 files changed

+11
-8
lines changed

7 files changed

+11
-8
lines changed

example2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def fitness_func(ga, solution, idx):
99
num_parents_mating=5,
1010
sol_per_pop=10,
1111
num_genes=10,
12-
random_seed=123,
12+
random_seed=2,
1313
# mutation_type=None,
1414
# crossover_type=None,
15-
random_mutation_min_val=1,
16-
random_mutation_max_val=100,
15+
# random_mutation_min_val=1,
16+
# random_mutation_max_val=100,
1717
fitness_func=fitness_func,
18-
gene_space=[30, None, 40, 50, None, 60, 70, None, None, None],
19-
gene_type=int,
18+
gene_space=[5],
19+
gene_type=float,
2020
allow_duplicate_genes=False,
2121
save_solutions=True)
2222

@@ -25,7 +25,7 @@ def fitness_func(ga, solution, idx):
2525
ga_instance.run()
2626

2727
# print(ga_instance.gene_space_unpacked)
28-
# print(ga_instance.population)
28+
print(ga_instance.population)
2929

3030
"""
3131
gene_space=[[0, 0],
0 Bytes
Binary file not shown.
40 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

pygad/helper/misc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ def generate_gene_value_from_space(self,
367367
# It has to be called here for all the missed cases.
368368
value_from_space = self.change_gene_dtype_and_round(gene_index=gene_idx,
369369
gene_value=value_from_space)
370+
if sample_size == 1 and type(value_from_space) not in pygad.GA.supported_int_float_types:
371+
value_from_space = value_from_space[0]
370372

371373
return value_from_space
372374

pygad/pygad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ def initialize_population(self,
13761376
# 4) Solve duplicates if not allowed.
13771377

13781378
# Create an empty population.
1379-
self.population = numpy.empty(shape=self.pop_size, dtype=object)
1379+
self.population = numpy.zeros(shape=self.pop_size, dtype=object)
13801380

13811381
# 1) Create the initial population either randomly or using the gene space.
13821382
if self.gene_space is None:

test_gene_space_allow_duplicate_genes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def number_respect_gene_space(gene_space=None,
3131
init_range_low=-4,
3232
init_range_high=4,
3333
initial_population=None,
34+
allow_duplicate_genes=False,
3435
parent_selection_type='sss',
3536
multi_objective=False):
3637

@@ -167,7 +168,7 @@ def test_gene_space_dict_with_step():
167168
assert num_outside == 0
168169

169170
def test_gene_space_list_single_value():
170-
num_outside, ga_instance = number_respect_gene_space(gene_space=[5])
171+
num_outside, ga_instance = number_respect_gene_space(gene_space=[5], allow_duplicate_genes=True)
171172

172173
assert num_outside == 0
173174

0 commit comments

Comments
 (0)