【问题标题】:Using choco solver for solving equations [closed]使用 choco 求解器求解方程
【发布时间】:2016-02-28 22:00:59
【问题描述】:

我正在寻找一种在 Choco Solver 上对数学方程进行编码的方法。 我看到有一种方法可以对约束进行编码,例如:

x + y

但我正在尝试编码类似的东西

3x + 4y

其中 x 和 y 是 int 变量。

任何帮助将不胜感激。

【问题讨论】:

    标签: java api constraints smt choco


    【解决方案1】:

    我也是 Choco 的新手,但我可以解决这个问题。

    为此,您可以使用约束scalar(请参阅docs)。

    首先,您只需在两个IntVar 变量中定义xy。您可以使用VariableFactory.boundedVariable.enumerated。当您只想使用具有下限和上限的域时,它们非常相似,但user guide 中解释了区别。

    然后你需要用方程的系数定义一个数组,在本例中为{ 3, 4 }

    这是你的做法:

    Solver solver = new Solver();
    
    IntVar x = VariableFactory.bounded("x", 0, 50, solver);
    IntVar y = VariableFactory.bounded("y", 0, 50, solver);
    
    int[] coefficients = new int[]{ 3, 4 };
    
    solver.post(IntConstraintFactory.scalar(new IntVar[]{ x,  y }, coefficients, "<", VariableFactory.fixed(9, solver)));
    
    if (solver.findSolution()) {
        do {
            System.out.println("x = " + x.getValue() + ", y = " + y.getValue());
        } while (solver.nextSolution());
    } else {
        System.out.println("The equation has no solutions.");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多