You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yet it is not possible to set a constraint for more than 1 gene at the same time (e.g. all genes must sum to X, difference between 2 genes not exceeds X, etc).
Note that setting solution /= sum(solution) in the fitness function will not change the solution in the population. So, the best_solution() method will still use the unnormalized solution.
Given your problem, you might change the population after the mutation is done (given that the mutation is the last step in which the genes' values get changed). You can implement a function/method that makes the sum of all genes equal to 1. Then assign it to the on_mutation parameter.
defon_mutation(ga_instance, offspring_mutation):
# Changes the genes to make the sum = 1
...
ga_instance.population= ...
pygad.GA(...,
on_mutation=on_mutation,
...)
Activity
ahmedfgad commentedon Jun 27, 2023
Yet it is not possible to set a constraint for more than 1 gene at the same time (e.g. all genes must sum to X, difference between 2 genes not exceeds X, etc).
Note that setting
solution /= sum(solution)in the fitness function will not change the solution in the population. So, thebest_solution()method will still use the unnormalized solution.Given your problem, you might change the
populationafter the mutation is done (given that the mutation is the last step in which the genes' values get changed). You can implement a function/method that makes the sum of all genes equal to 1. Then assign it to theon_mutationparameter.nurettin commentedon Jun 27, 2023
Thanks, @ahmedfgad
I also found this which seems related
#37