1

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:

  1. How can I remove all infeasible solutions and only evalute individuals that do not violate the constraints?
  2. 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!

  • Is there a hard requirement to use a genetic algorithm? Otherwise, I would use more suitable methods for solving a diet problem, which is a staple introductory problem for linear programming and can be solved with any of a multitude of solvers. – Zayenz Jun 27 '22 at 09:01
  • It does not not necessarily need to be GA, but I haven't come across other tools that could help me with this problem. I tried with IBMs CPLEX optimizer, but the solutions were not very diverse, which is what I hope GA can help me achieving. The problem is simple, but it is not so simple to setup the model (to me at least). So you have any specific tool in python in mind you would suggest me to look at? Or some online guides? – Caroline Herlev Gebara Jun 27 '22 at 12:26
  • 1
    I'm not sure if there is any way to guarantee that GA will give you diverse solutions. CPMpy is a new Python modelling layer with a focus on meta-level coding around optimization. You could combine hakank's Diet model (http://www.hakank.org/cpmpy/) with the CPMpy example on finding diverse solutions (https://github.com/CPMpy/cpmpy/blob/master/examples/advanced/diverse_solutions.py) In general, finding diverse solutions is a tricky problem with active research. See for example https://people.eng.unimelb.edu.au/pstuckey/papers/aaai20d.pdf – Zayenz Jun 28 '22 at 06:10
  • Thanks a lot @Zayenz! This could be exactly what I need. It looks like the model only can take integer or boolean variables. Do you know if I can have floating variables as well? E.g. so that in the example with the cost, the cost can take all values? – Caroline Herlev Gebara Jun 28 '22 at 09:38
  • No idea unfortunately, but it might very well be integers only. Often it is enough to scale all the values and round (say, multiplying by 10 or 100 or 1000) for most problems, at the cost of some potential loss of granularity. Or using another system, like pyomo or pulp (both linear programming oriented) or MiniZinc (general combinatorial modelling including floats) via minizinc-python. – Zayenz Jun 28 '22 at 19:59

0 Answers0