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

Variable length chromosome #234

RomekRJM started this conversation in General
Sep 15, 2023 · 1 comments · 2 replies
Discussion options

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!

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

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.

You must be logged in to vote
2 replies
Comment options

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?

Comment options

This is absolutely a good idea. Still the feature is not fully merged. It is a major change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request

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