-
-
Notifications
You must be signed in to change notification settings - Fork 489
-
Hi,
I was checking the docs and could not find any details of the module support for variable-length chromosomes.
Is it doable with PyGAD? Can you provide code samples?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
Hi @RomekRJM,
The GitHub repository is updated to support changing the the chromosome length from one generation to another. This will be available in the next release 3.3.0
.
But still the individual chromosomes must all have the same length in the same generation.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi!
First and foremost, I want to express my gratitude for this incredible library and its clear documentation. Congratulations @ahmedfgad on the excellent work.
Thanks!
Now, version 3.3.0 is released... 👀 maybe we already have the variable chromossome feature? 👀
If not, my approach would simply have a big length gene_space
and consider some special values (like -1
) as the absent of that gene (I'm using "gene" as sinonym of "chromossome" here, but I know there's a small conceptual difference).
In my specific case, my optimization involves positioning weights in certain positions (which must be integers in a given range).
I don't know before hand how many weights I need, but I know it won't be bigger than, let's say, 5 weights. So I could have:
gene_space = [
range(-1, max_weight),
range(-1, max_weight),
range(-1, max_weight),
range(-1, max_weight),
range(-1, max_weight),
]
(I know I could just use gene_space=range(-1, max_weight),, but was to clarify that each individual would have 5 genes).
Then, on my fitness function I would have something like:
import pygad
from typing import Tuple, Literal
WeightPositionAsNone = Literal[-1]
MaxWeightPosition = Literal[5]
WeightPosition = Literal[WeightPositionAsNone,
0, 1, 2, 3, 4, MaxWeightPosition]
Solution = Tuple[
WeightPosition,
WeightPosition,
WeightPosition,
WeightPosition,
WeightPosition,
]
def fitness_func(
ga_instance: pygad.GA,
solution: Solution,
solution_idx: int
):
filtered_solution = [x for x in solution if x != -1]
# Calculate fitness solution for the problem
# desconsidering the weights that are not present
fitness = sum(filtered_solution)
return fitness
Does this seem like a valid approach?
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 1
-
This is absolutely a good idea. Still the feature is not fully merged. It is a major change.
Beta Was this translation helpful? Give feedback.