巧克力解算器旧类

巧克力解算器旧类

本文介绍了巧克力解算器旧类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我找到了用choco求解器来求解幻方程序的代码:

public static void main(String[] args) {
    int n = 4;
    System.out.println("Magic Square Problem with n = " + n);

    Problem myPb = new Problem();

    IntVar[] vars = new IntVar[n * n];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
        vars[i * n + j] = myPb.makeEnumIntVar("C" + i + "_" + j, 1, n * n);
    }
    IntVar sum = myPb.makeEnumIntVar("S", 1, n * n * (n * n + 1) / 2);

    myPb.post(myPb.eq(sum, n * (n*n + 1) / 2));
    for (int i = 0; i < n * n; i++)
        for (int j = 0; j < i; j++)
        myPb.post(myPb.neq(vars[i], vars[j]));

    int[] coeffs = new int[n];
    for (int i = 0; i < n; i++) {
       coeffs[i] = 1;
    }

    for (int i = 0; i < n; i++) {
    IntVar[] col = new IntVar[n];
    IntVar[] row = new IntVar[n];

    for (int j = 0; j < n; j++) {
        col[j] = vars[i * n + j];
        row[j] = vars[j * n + i];
    }

    myPb.post(myPb.eq(myPb.scalar(coeffs, row), sum));
    myPb.post(myPb.eq(myPb.scalar(coeffs, col), sum));

    myPb.solve();
}

但是问题"类似乎已被模型"类替换.使用Model.intVar而不是Problem.makeEnumIntVar是否正确?替换Problem.neq,Problem.eq和Problem.scalar的当前函数是什么?

解决方案

似乎您那里有一些不推荐使用的代码.表达式

Problem.scalar and Problem.eq

可以表示为

int capacity = 34;  // max capacity
int[] volumes = new int[]{7, 5, 3};

 // Problem.scalar
model.scalar(new IntVar[]{obj1, obj2, obj3}, volumes, "=", capacity).post();

// Problem.eq
model.arithm(obj1, "=", obj2).post();

例如,以上代码表示标量乘积等于 capability obj1 必须等于 obj2 的约束./p>

进一步的阅读和资源:

在这里您将找到带有一些示例代码的最新教程: choco教程

最后,您还可以在github上查看测试用例: https://github.com/chocoteam/choco-solver/tree/master/src/test/java/org/chocosolver/solver

特别是对于变量表达式的测试可能对您来说很有趣.

可以在这里找到更多代码示例:更多代码示例

I found this code for solving the magic square program with the choco solver:

public static void main(String[] args) {
    int n = 4;
    System.out.println("Magic Square Problem with n = " + n);

    Problem myPb = new Problem();

    IntVar[] vars = new IntVar[n * n];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
        vars[i * n + j] = myPb.makeEnumIntVar("C" + i + "_" + j, 1, n * n);
    }
    IntVar sum = myPb.makeEnumIntVar("S", 1, n * n * (n * n + 1) / 2);

    myPb.post(myPb.eq(sum, n * (n*n + 1) / 2));
    for (int i = 0; i < n * n; i++)
        for (int j = 0; j < i; j++)
        myPb.post(myPb.neq(vars[i], vars[j]));

    int[] coeffs = new int[n];
    for (int i = 0; i < n; i++) {
       coeffs[i] = 1;
    }

    for (int i = 0; i < n; i++) {
    IntVar[] col = new IntVar[n];
    IntVar[] row = new IntVar[n];

    for (int j = 0; j < n; j++) {
        col[j] = vars[i * n + j];
        row[j] = vars[j * n + i];
    }

    myPb.post(myPb.eq(myPb.scalar(coeffs, row), sum));
    myPb.post(myPb.eq(myPb.scalar(coeffs, col), sum));

    myPb.solve();
}

But the class 'Problem' seems to have been replaced with the 'Model' class.Is it correct to use Model.intVar instead of Problem.makeEnumIntVar?What would be the current function that replaces Problem.neq, Problem.eq and Problem.scalar?

解决方案

It looks like you have some deprecated code there. The expressions

Problem.scalar and Problem.eq

can be expressed as

int capacity = 34;  // max capacity
int[] volumes = new int[]{7, 5, 3};

 // Problem.scalar
model.scalar(new IntVar[]{obj1, obj2, obj3}, volumes, "=", capacity).post();

// Problem.eq
model.arithm(obj1, "=", obj2).post();

The above code for example expresses the constraint that the scalar product is equal to capacity and that obj1 must be equal to obj2.

Further reading and resources:

Here you will find the latests tutorial with some sample code:choco tutorial

Finally you can also check out the Testcases at github:https://github.com/chocoteam/choco-solver/tree/master/src/test/java/org/chocosolver/solver

Especially the tests for variables and expression might be interesting for you.

Some more code examples can be found here:more code samples

这篇关于巧克力解算器旧类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:35