简体   繁体   English

巧克力中的复变量

[英]Complex variable in choco

How to check in Choco if a complex variable satisfies the constraints?如果复杂变量满足约束,如何检查 Choco? For example, if I have the following list of configurations:例如,如果我有以下配置列表:

int[][] configurations = new int[][] {
        {20, 24, 10, 3, 4},  
        {20, 13, 1, 3, 4}};

where config1 = {20, 24, 10, 3, 4} and config2 = {20, 13, 1, 3, 4}其中 config1 = {20, 24, 10, 3, 4} 和 config2 = {20, 13, 1, 3, 4}

int[] constraints = new int[]{21, 15, 2, 10, 10};

is a list of constraints such that, for a given configuration each element in the configuration needs to be higher (or lower) than the coresponding constraint.是一个约束列表,对于给定的配置,配置中的每个元素都需要高于(或低于)相应的约束。 For example: config1 = {20, 24, 10, 3, 4} constraints = {21, 15, 2, 10, 10}例如:config1 = {20, 24, 10, 3, 4} 约束 = {21, 15, 2, 10, 10}

check if config1[0] < constraints[0] AND config1[1] < constraints[1] AND ... 

If all the constraints are satisfied then just mark it as a solution.如果满足所有约束,则将其标记为解决方案。 This is what I have这就是我所拥有的

// c = number of configurations
// q = number of elements in each configuration
// p = configurations matrix

for (int i = 0; i < c; i++) {
        for (int j = 0; j < q; j++) {
            model.arithm(model.intVar(p[i][j]), "<", model.intVar(k[j])).post();
        }
}

CP relies on variables and constraints. CP 依赖于变量和约束。 You should create variables first and then post constraints on them.您应该先创建变量,然后对它们发布约束。 Please do not call "constraints" an array of ints that is just some input of your problems.请不要将“约束”称为整数数组,这只是您问题的一些输入。

You can find a simple choco example here: - https://www.cosling.com/choco-solver/hello-world I think this hello word is sufficient for what you need.您可以在这里找到一个简单的 choco 示例: - https://www.cosling.com/choco-solver/hello-world我认为这个 hello 词足以满足您的需求。 If you want to go deeper, read this: http://choco-tuto.readthedocs.io/en/latest/如果你想更深入,请阅读: http : //choco-tuto.readthedocs.io/en/latest/

There is probably a better way to do this, but you could create an initial IntVar called config (bound between 0 and the number of configurations).可能有更好的方法来做到这一点,但您可以创建一个名为 config 的初始 IntVar(绑定在 0 和配置数量之间)。 Then separate IntVar's for each element (elementA, elementB, elementC etc).然后为每个元素(elementA、elementB、elementC 等)分隔 IntVar。

You can then add logic that says that:然后,您可以添加如下逻辑:

if config = 0 then elementA = 20
if config = 0 then elementB = 24
 ...

On top of this you can add further constraints based on the elements to return your solution.在此之上,您可以根据元素添加更多约束以返回您的解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM