gpt4 book ai didi

java - CPLEX Java - 获取 IloNumExpr 和数字之间的最小值

转载 作者:行者123 更新时间:2023-12-02 01:19:35 25 4
gpt4 key购买 nike

我尝试使用以下简化代码查找 IloNumExpr 和数字之间的最小值:

import ilog.concert.IloException;
import ilog.concert.IloNumExpr;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;

public class Test {

public static void main(String[] args) {
try {
// Define model
IloCplex cplex = new IloCplex();

IloNumExpr objective = cplex.numExpr();
IloNumVar var = cplex.intVar(0, 10);

objective = cplex.sum(objective, cplex.prod(var, 10));
cplex.addMinimize(objective);

IloNumExpr aux1 = cplex.numExpr();
IloNumExpr aux2 = cplex.numExpr();
aux1 = cplex.prod(var, 1);
aux2 = cplex.min(var, 1);

if (cplex.solve()) {
System.out.println("var: " + cplex.getValue(var));
System.out.println("aux1: " + cplex.getValue(aux1));
System.out.println("aux2: " + cplex.getValue(aux2));
}

cplex.end();
}
catch (IloException exc) {
exc.printStackTrace();
}
}
}

但是,当我尝试打印 aux2 的值时,此代码返回错误。输出如下:

Tried aggregator 1 time.
LP Presolve eliminated 0 rows and 1 columns.
All rows and columns eliminated.
Presolve time = 0,00 sec. (0,00 ticks)
var: 0.0
aux1: 0.0
ilog.cplex.IloCplex$UnknownObjectException: CPLEX Error: object is unknown to IloCplex
at ilog.cplex.CpxNumVar.getVarIndexValue(CpxNumVar.java:289)
at ilog.cplex.EvalVisitor.visitNumVar(EvalVisitor.java:33)
at ilog.cplex.CpxNumVar.accept(CpxNumVar.java:57)
at ilog.cplex.IloCplex.getValue(IloCplex.java:8686)
at Test.main(Test.java:29)

为什么会发生这种情况?

提前致谢

最佳答案

CPLEX 只能返回属于模型一部分的变量的值。您的变量 aux2 不是模型的一部分,因此 CPLEX 不知道它。在求解模型之前尝试cplex.add(aux2)。这应该可以解决问题。

关于java - CPLEX Java - 获取 IloNumExpr 和数字之间的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com