I am trying to apply GA for generating a set of solutions that fulfil a list of constraints. I would like to find solutions (individuals) of diets that fulfill nutritional constraints, for which I think GA could be a good way to do so. I have found a similar example here: A Genetic Algorithm to Optimize Your Diet, however, the example to not use constraints and it only finds the optimal solutions. Thus my questions are:
- How can I remove all infeasible solutions and only evalute individuals that do not violate the constraints?
- Is there a way to store/acess all the generated individuals that fulfil my constraints (also non-optimal)?
From the webpage of the DEAP library, I found that I can add a feasibility function, where I penalise the infeasible solutions, like in the example here:
def feasible(individual):
individual = individual[0]
if (7*1999. < sum(x*y for x,y in zip(kcal_data,individual)) < 7*2001. and
7*60.5 < sum(x*y for x,y in zip(prot_data,individual)) and
... and so on....):
return True
return False
toolbox.decorate("evaluate", tools.DeltaPenalty(feasible, 9999))
However, the solutions are still kept, although they violate the constraints. Would it be possible to insert the constraints directly in the evaluate function, to ensure that I only evaluate feaisble solutions? Or could I, instead of penalising, delete the solutions?
For my second question, I am interested in obtaining a diverse set of individuals, not just the optimal ones. But, when using the GA, the worse solutions are remove from the population resulting in a final population of the best individuals. Is it instead possible to store all generations from the algorithm (incl. intermediate solutions)?
Any help will be appreciated,
Thanks!