简体   繁体   English

使用 CPMpy 进行优化时可以包含连续变量吗?

[英]Can I include continuous variable when optimising with CPMpy?

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 .我在这里找到了一个几乎符合我需要的饮食示例: 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.但是,就我而言,我的变量取连续值,因此在示例中,这将是所有营养价值和成本,而只有 x 取整数。 However, it seems like I can only define either 'intvar' or 'boolvar' when defining by variables with this model.但是,在使用此模型定义变量时,我似乎只能定义“intvar”或“boolvar”。 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.谢谢。

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. MIP 工具和求解器广泛可用。

The diet model is a classic example of an LP (Linear Programming) Model.饮食模型是 LP(线性规划)模型的经典示例。 When adding integer restrictions, you end up with a MIP model.添加整数限制时,您最终会得到一个 MIP 模型。

To answer your question: CPMpy does not support float variables (and I'm not sure that it's in the pipeline for future extensions).回答您的问题:CPMpy 不支持浮点变量(而且我不确定它是否在未来扩展的管道中)。

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.另一种方法——而不是像 Erwin 建议的那样使用 MIP 求解器——是编写问题的 MiniZinc ( https://www.minizinc.org/ ) 模型并使用它的一些求解器。 See my MiniZinc version of the diet problem: http://hakank.org/minizinc/diet1.mzn .请参阅我的 MiniZinc 版本的饮食问题: 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 .并查看 Stigler 饮食问题的 MiniZinc 版本,尽管它只是浮动变量: http ://hakank.org/minizinc/stigler.mzn。

There are some MiniZinc CP solvers that also supports float variables, eg Gecode, JaCoP, and OptimathSAT.有一些 MiniZinc CP 求解器也支持浮点变量,例如 Gecode、JaCoP 和 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.与一些 MIP 求解器相比,生成所有解是 CP 求解器的一般特征之一。

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.如果您提供有关问题的更多详细信息,可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个类中设置一个变量以包含另一个变量并在另一个值更改时更新? - How can I set a variable within a class to include another variable and update when the other value changes? 当输出变量是连续的时,我们如何调整神经网络的超参数? - How can we tune hyperparameters of neural network when the output variable is continuous? 如何测试 python 中离散和连续随机变量的相等性? - How can I test means equality of discrete and continuous random variable in python? 如何在字典中使用字典或其他方式将连续变量映射到离散字符串? - How can I map a continuous variable to discrete strings, using dictionaries or otherwise in Python? 我无法在迭代中正确包含第三个变量 - I can't include a 3rd variable in my iteration properly 我可以在数学表达式原始字符串中包含变量吗? - Can I include a variable inside a mathematical expression raw string? 单击矩形时连续移动矩形(pygame) - Continuous movement for a rectangle when I click on it (pygame) 如何在pygame中获得连续运动? - How can I get continuous motion in pygame? 如何创建数据集的连续分布? - How can I create a continuous distribution of a dataset? 绘制时间戳时如何包含2个特定的日期和刻度? - How can I include 2 specific dates and ticks when plotting Timestamps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM