Open
Description
Hi, thank you for your all effort!
I encountered an error when I tried to run algorithm with batched fitness.
The solution shape change during run. In here, the batch size changed 10 to 9.
Could you help me to solve this?
Thanks.
Here is my PyGAD instance setting
----------------------------------------------------------------------
PyGAD Lifecycle
======================================================================
Step Handler Output Shape
======================================================================
on_start() on_start() None
----------------------------------------------------------------------
Fitness Function fitness_func() (1)
Fitness batch size: 10
----------------------------------------------------------------------
On Fitness on_fitness() None
----------------------------------------------------------------------
Parent Selection steady_state_selection() (10, 119)
Number of Parents: 10
----------------------------------------------------------------------
On Parents on_parents() None
----------------------------------------------------------------------
Crossover scattered_crossover() (10, 119)
----------------------------------------------------------------------
On Crossover on_crossover() None
----------------------------------------------------------------------
Mutation random_mutation() (10, 119)
Mutation Genes: 10
Random Mutation Range: (0.0, 2.0)
Gene Space: {'low': 0.0, 'high': 1.0}
Mutation by Replacement: True
Allow Duplicated Genes: True
----------------------------------------------------------------------
On Mutation on_mutation() None
----------------------------------------------------------------------
On Generation on_generation() None
----------------------------------------------------------------------
On Stop on_stop() None
----------------------------------------------------------------------
======================================================================
Population Size: (100, 119)
Number of Generations: 300
Initial Population Range: (0.5, 1.5)
Keep Elitism: 1
Gene DType: [<class 'float'>, None]
Save Best Solutions: False
Save Solutions: False
======================================================================
And here is python script
self.ga_inst = pygad.GA(
initial_population=self.initial_population(), # Just generate (100, 119) random number uisng numpy
init_range_low=0.5,
init_range_high=1.5,
gene_space={
"low": 0.0,
"high": 1.0
},
num_generations=300,
num_parents_mating=10,
fitness_func=self.fitness_func,
num_genes=119,
sol_per_pop=20,
parent_selection_type='sss',
crossover_type='scattered',
mutation_type='random',
mutation_num_genes=10,
random_mutation_min_val=0.0,
random_mutation_max_val=2.0,
mutation_by_replacement=True,
fitness_batch_size=10,
mutation_percent_genes=.05
)
And my error message was like this,
[INFO] GA start
0 (10, 119) -> I print out run count and solution.shape in fitness_funtion()
1 (10, 119)
2 (10, 119)
3 (10, 119)
4 (10, 119)
5 (10, 119)
6 (10, 119)
7 (10, 119)
8 (10, 119)
9 (10, 119)
10 (10, 119)
11 (10, 119)
12 (10, 119)
13 (10, 119)
14 (10, 119)
15 (10, 119)
16 (10, 119)
17 (10, 119)
18 (10, 119)
19 (9, 119)
There is a mismatch between the number of solutions passed to the fitness function (9) and the number of fitness values returned (10). They must match.
Traceback (most recent call last):
File "/mnt/dsk1/yhlee/workdir/torch_env/.venv/lib/python3.9/site-packages/pygad/pygad.py", line 1702, in cal_pop_fitness
raise ValueError(f"There is a mismatch between the number of solutions passed to the fitness function ({len(batch_indices)}) and the number of fitness values returned ({len(batch_fitness)}). They must match.")
ValueError: There is a mismatch between the number of solutions passed to the fitness function (9) and the number of fitness values returned (10). They must match.