计算 Java 区间内的变量值

Calculate Values of Variables Found in an Interval with Java

我试图找到 xy 的值,因此以下不等式成立:

1/24 < 1/15*y < 1/10*x < 2/24 < 2/15*y < 3/24

有没有办法在Java中表述这样的问题? 约束编程可能会解决这样的问题,但有其他方法吗?

如果约束编程是唯一的方法,这看起来如何?

以下是我使用or-tools 进行约束编程的尝试。如何制定严格的不等式?

MPSolver solver = new MPSolver(
            "SimpleMipProgram", MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
    // [END solver]

    // [START variables]
    double infinity = java.lang.Double.POSITIVE_INFINITY;
    // x and y are float/double variables.
    MPVariable x = solver.makeNumVar(0,1,"x"); //makeIntVar(0.0, infinity, "x");
    MPVariable y = solver.makeNumVar(0,1,"y"); //makeIntVar(0.0, infinity, "y");

    System.out.println("Number of variables = " + solver.numVariables());
    // [END variables]

    // [START constraints]
    // x + 7 * y <= 17.5.
    /*MPConstraint c0 = solver.makeConstraint(-1, 17.5, "c0");
    c0.setCoefficient(x, 1);
    c0.setCoefficient(y, 7);

    // x <= 3.5.
    MPConstraint c1 = solver.makeConstraint(-infinity, 3.5, "c1");
    c1.setCoefficient(x, 1);
    c1.setCoefficient(y, 0);*/

    // 1/24 < 1/15*y ---> -1/15 * y < -1/24
    MPConstraint c0 = solver.makeConstraint(-1000,-1/24.0,"c0");
    c0.setCoefficient(y,-1/15.0);

    //  1/15*y < 1/10*x ---> 1/15*y - 1/10*x < 0
    MPConstraint c1 = solver.makeConstraint(-1000,0,"c1");
    c1.setCoefficient(y,1/15.0);
    c1.setCoefficient(x,-1/10.0);

    // 1/10*x < 2/24 ---> 1/10*x < 2/24
    MPConstraint c2 = solver.makeConstraint(-1000,2/24.0,"c2");
    c2.setCoefficient(x,1/10.0);

    // 2/24 < 2/15*y ---> -2/15*y < -2/24
    MPConstraint c3 = solver.makeConstraint(-1000, -2/24.0);
    c3.setCoefficient(y,-2/15.0);

    // 2/15*y < 3/24 ---> 2/15*y < 3/24
    MPConstraint c4 = solver.makeConstraint(-1000,3/24.0);
    c4.setCoefficient(y,2/15.0);

我找到了解决方案,方法是写下一个生成随机值的循环,直到满足所有语句。

现在我对 wolfram alpha 是如何快速解决此类问题感兴趣的。

public class inequalities {
private static double x;
private static double y;
private static double Ratio3 = 1/24.0;
private static double Ratio2 = 1/15.0;
private static double Ratio1 = 1/10.0;
public static void main(String[] args) {
    x = Math.random();
    y = Math.random();
    boolean loop = true;
    while (loop) {
        loop = calculatingTheInequalities();
        if (loop) {
            x = Math.random();
            y = Math.random();
        }
    }
    System.out.println("x value: " + x);
    System.out.println("y value: " + y);
}
public static boolean calculatingTheInequalities() {
    if (Ratio3<Ratio2*y && Ratio2*y<Ratio1*x &&
    Ratio1*x<2*Ratio3 && 2*Ratio3<2*Ratio2*y &&
    2*Ratio2*y<3*Ratio3) {
        return false;
    } else {
        return true;
    }
    /*if (Ratio3 < Ratio2 *y) {
        if (Ratio2 *y < Ratio1 *x) {
            if (Ratio1 *x<2* Ratio3) {
                if (2* Ratio3 < 2* Ratio2 *y) {
                    if (2* Ratio2 *y < 3* Ratio3) {
                        return false;
                    } else {
                        return true;
                    }
                } else {
                    return true;
                }
            } else {
                return true;
            }
        } else {
            return true;
        }
    } else {
        return true;
    }*/
}
}

这是使用整数求解器的工作代码

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from ortools.sat.python import cp_model


model = cp_model.CpModel()
scale = 1000

x = model.NewIntVar(0, scale, 'x')
y = model.NewIntVar(0, scale, 'y')

# 1/24 < 1/15*y < 1/10*x < 2/24 < 2/15*y < 3/24

model.Add(5 * scale < 8 * y)
model.Add(8 * y < 12 * x)
model.Add(12 * x < 10 * scale)
model.Add(10 * scale < 16 * y)
model.Add(16 * y < 15 * scale)

solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
status = solver.Solve(model)

if status == cp_model.FEASIBLE:
    print('x =', solver.Value(x) * 1.0 / scale)
    print('y =', solver.Value(y) * 1.0 / scale)

scale = 1000,输出:

x = 0.418
y = 0.626

scale = 100,输出:

x = 0.43
y = 0.63

scale = 10,输出

x = 0.5
y = 0.7