-
-
Notifications
You must be signed in to change notification settings - Fork 481
Manipulate solution before saving it as parent #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @Overdrivr, Thanks for using PyGAD! Here are the answers to your questions:
|
Hi @ahmedfgad, thanks for your reply! |
It is straightforward.
You only need to edit the second argument. After you finish, you have to return:
This is an example. It changes all the parents to import pygad
import numpy
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
desired_output = 44 # Function output.
def fitness_func(ga_instance, solution, solution_idx):
output = numpy.sum(solution*function_inputs)
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
return fitness
last_fitness = 0
def on_parents(ga_instance, parents):
for idx in range(parents.shape[0]):
parents[idx, :] = [999]*parents.shape[1]
indices = [0]*parents.shape[0]
return parents, indices
ga_instance = pygad.GA(num_generations=20,
num_parents_mating=5,
sol_per_pop=10,
num_genes=len(function_inputs),
fitness_func=fitness_func,
on_parents=on_parents,
suppress_warnings=True)
ga_instance.run()
ga_instance.plot_fitness() |
Thanks @ahmedfgad , much clearer ! |
The above code works well only if you set both of these parameters to 0:
If at least one parameter is non-zero, then you are right. The code should not use zero as the index for all the new parents. Let me explain how this would be solved easily. To be at the safe side, you should update the following attributes according to the new parents:
This way you will 100% sure that the new parents will have no impact on the algorithm. |
Hi,
Thank you for this wonderful lib. I'm experimenting with PyGAD on a scheduling problem, and I'm facing a bit of difficulty.
My fitness function performs a multi-agent simulation (using python lib "mesa"), takes an input planning and returns a single score.
The challenge I'm facing is that the input (="theoretical") planning (that's provided by PyGad to the fitness function) is not usable 1:1 by the agents. For instance, if the planning tells the agents to do something that's impossible (for instance, starting operation B before operation A was complete), the agent will skip the operation and move to the next doable one.
At this point, the challenge I'm facing is that pygad converges to a solution, but it's really bad, and much worse than a planning generated with basic heuristics.
I have a couple of questions about this :
Thanks a lot for your help!
The text was updated successfully, but these errors were encountered: