Skip to content

Commit 901d7e1

Browse files
author
Ahmed Gad
committed
More tests
1 parent 2c6c6d8 commit 901d7e1

25 files changed

+531
-44
lines changed

.DS_Store

2 KB
Binary file not shown.

example2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ def fitness_func(ga_instance, solution, solution_idx):
2828
allow_duplicate_genes=False,
2929
# gene_space=numpy.unique(numpy.random.uniform(1, 100, size=100)),
3030
gene_space=[range(0, 100), {"low": 0, "high": 100, 'step': 1}, 2.5891221, [1,2,3,4], None, numpy.unique(numpy.random.uniform(1, 100, size=4))],
31-
gene_constraint=[lambda x: x[0]>=95,lambda x: x[1]>=95,lambda x: x[2]<98,lambda x: x[3]<98,lambda x: x[4]<98,lambda x: x[5]<98],
31+
gene_constraint=[lambda x: x[0]>=70,lambda x: x[1]>=70,lambda x: x[2]<98,lambda x: x[3]<98,lambda x: x[4]<98,lambda x: x[5]<98],
3232
)
3333

3434
print(ga_instance.initial_population)
3535

36-
# ga_instance.run()
36+
ga_instance.run()
3737

3838
# print(ga_instance.gene_space_unpacked)
3939
# print(ga_instance.population)

pygad/.DS_Store

8 KB
Binary file not shown.
-227 Bytes
Binary file not shown.
-77.8 KB
Binary file not shown.

pygad/helper/.DS_Store

6 KB
Binary file not shown.
-282 Bytes
Binary file not shown.
-14.8 KB
Binary file not shown.
-14.2 KB
Binary file not shown.

pygad/helper/misc.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def change_population_dtype_and_round(self,
1919
It returns the iterable with the data type changed for all genes.
2020
"""
2121

22-
population_new = population.copy()
22+
population_new = numpy.array(population.copy(), dtype=object)
2323

2424
# Forcing the iterable to have the data type assigned to the gene_type parameter.
2525
if self.gene_type_single == True:
@@ -49,8 +49,10 @@ def change_population_dtype_and_round(self,
4949
population_new[:, gene_idx] = numpy.round(numpy.array(population[:, gene_idx], float),
5050
self.gene_type[gene_idx][1])
5151
# Once rounding is done, change the data type.
52-
population_new[:, gene_idx] = numpy.asarray(population_new[:, gene_idx],
53-
dtype=self.gene_type[gene_idx][0])
52+
# population_new[:, gene_idx] = numpy.asarray(population_new[:, gene_idx], dtype=self.gene_type[gene_idx][0])
53+
# Use a for loop to maintain the data type of each individual gene.
54+
for sol_idx in range(population.shape[0]):
55+
population_new[sol_idx, gene_idx] = self.gene_type[gene_idx][0](population_new[sol_idx, gene_idx])
5456
return population_new
5557

5658
def change_gene_dtype_and_round(self,
@@ -153,7 +155,7 @@ def filter_gene_values_by_constraint(self,
153155
else:
154156
# No value found for the current gene that satisfies the constraint.
155157
if not self.suppress_warnings:
156-
warnings.warn(f"No value found for the gene at index {gene_idx} that satisfies its gene constraint.")
158+
warnings.warn(f"No value found for the gene at index {gene_idx} with value {solution[gene_idx]} that satisfies its gene constraint.")
157159
return None
158160

159161
filtered_values = values[filtered_values_indices]

0 commit comments

Comments
 (0)