1

I need to run a model, where I optimise a diet within a set of constraints and call all integer solutions in the end. I have found a diet example matching almost what I need here: hakank.org. However, in my case, my variables take continuous values, so in the examples this would be all the nutritional values and the cost, while only x take integer. However, it seems like I can only define either 'intvar' or 'boolvar' when defining by variables with this model. Is there a way to overcome this? Other would there be other more suitable models with examples that I can read online?

I'm new to constraint programming, so any help would be appreaciated!

Thanks.

2 Answers2

2

Most Constraint Programming tools and solvers only work with integers. That is where their strength is. If you have a mixture of continuous and discrete variables, it is a good idea to have a look at Mixed Integer Programming. MIP tools and solvers are widely available.

The diet model is a classic example of an LP (Linear Programming) Model. When adding integer restrictions, you end up with a MIP model.

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
  • Thank you for the reply! Do you have experiences with some and can recommend which ones would be good in my case? I need to also be able to access the full solution pool (i.e. also non-optimal), and I struggle to find a tool that can help me doing both. – Caroline Herlev Gebara Jun 29 '22 at 15:39
  • MIP Solvers like Cplex, Gurobi, and Xpress have a solution pool. These are commercial solvers, however. For solvers without a solution pool, you can solve, add a constraint to prevent a solution to be found again, solve again, etc. This may be a bit cumbersome for integer solutions (opposed to binary variables). – Erwin Kalvelagen Jun 29 '22 at 16:13
  • I was using Cplex initially, but I experienced that the solutions I got were all close to the optimised soltuions, while I am interested in a more diverse range, including best and worst - although still soltuions. And I struggle to "tune" the model to get solutions that are more spread out. But in the end, it might sound like Cplex is still one of the strongest tools to use because of the solution pool, as you write, and that it is relatively quick. – Caroline Herlev Gebara Jun 30 '22 at 07:01
  • (1) I read your question as indicating you want all (feasible) integer solutions. In that case, diverse solutions are not an issue. (2) Cplex has some options to promote diverse solutions. See: https://www.ibm.com/docs/en/icos/20.1.0?topic=pool-example-diverse-solutions-through-replacement-parameter – Erwin Kalvelagen Jun 30 '22 at 07:46
1

To answer your question: CPMpy does not support float variables (and I'm not sure that it's in the pipeline for future extensions).

Another take - than using MIP solvers as Erwin suggest - would be to write a MiniZinc (https://www.minizinc.org/) model of the problem and use some of its solvers. See my MiniZinc version of the diet problem: http://hakank.org/minizinc/diet1.mzn. And see the MiniZinc version of Stigler's Diet problem though it's float vars only: http://hakank.org/minizinc/stigler.mzn.

There are some MiniZinc CP solvers that also supports float variables, e.g. Gecode, JaCoP, and OptimathSAT. However, depending on the exact constraints - such as the relation with the float vars and the integer vars - they might struggle to find solutions fast. In contrast to some MIP solvers, generating all solutions is one of the general features of CP solvers.

Perhaps all these diverse suggestions more confuse than help you. Sorry about that. It might help if you give some more details about your problem.

hakank
  • 6,629
  • 1
  • 17
  • 27