使用 Java 在 Cplex 中按列建模
Column wise modeling in Cplex using Java
我想使用按列建模解决 cplex 中的一个简单问题。
问题来了,
maximize 2x + 3y
subject to x<= 5
y<=2
x,y >=0
这是我必须编写的代码来解决它:
public static void Model_1() {
try {
//create new model
IloCplex cplex = new IloCplex();
//define variables
IloNumVar x;
IloNumVar y;
IloObjective objective;
objective = cplex.addMaximize();
IloRange cons01;
IloRange cons02;
cons01 = cplex.addRange(0, 5, "c1");
cons02 = cplex.addRange(0, 2, "c1");
IloColumn new_col = cplex.column(objective, 2);
IloColumn new_col2 = cplex.column(objective,3);
new_col = new_col.and(cplex.column(cons01,1));
new_col2 = new_col2.and(cplex.column(cons02,1));
x = cplex.numVar(new_col, 0, Double.MAX_VALUE);
y = cplex.numVar(new_col, 0, Double.MAX_VALUE);
//solve model
if (cplex.solve()) {
System.out.println("obj = "+cplex.getObjValue());
System.out.println("x = "+cplex.getValue(x));
System.out.println("y = "+cplex.getValue(y));
}
else {
System.out.println("Model not solved");
}
cplex.end();
}
catch (IloException exc) {
exc.printStackTrace();
}
}
但我没有得到正确的解决方案。我写代码有什么错误吗?
尝试调试此类问题时,将模型导出为 LP 格式以确保正确生成它总是很有用的。您可以通过在调用 cplex.solve
:
之前添加以下代码行来完成此操作
cplex.exportModel("model.lp");
如果这样做,model.lp 的内容将如下所示:
Maximize
obj: 2 x1 + 2 x2
Subject To
c1: x1 + x2 - Rgc1 = 0
c1: - Rgc1 = 0
Bounds
0 <= Rgc1 <= 5
0 <= Rgc1 <= 2
End
这说明了您在程序中的两个拼写错误。即,您应该替换:
cons02 = cplex.addRange(0, 2, "c1");
和
cons02 = cplex.addRange(0, 2, "c2");
并且,您应该替换:
y = cplex.numVar(new_col, 0, Double.MAX_VALUE);
和
y = cplex.numVar(new_col2, 0, Double.MAX_VALUE);
进行这两项更改后 model.lp 看起来像:
Maximize
obj: 2 x1 + 3 x2
Subject To
c1: x1 - Rgc1 = 0
c2: x2 - Rgc2 = 0
Bounds
0 <= Rgc1 <= 5
0 <= Rgc2 <= 2
End
并且,您从程序中得到以下输出:
obj = 16.0
x = 5.0
y = 2.0
我想使用按列建模解决 cplex 中的一个简单问题。 问题来了,
maximize 2x + 3y
subject to x<= 5
y<=2
x,y >=0
这是我必须编写的代码来解决它:
public static void Model_1() {
try {
//create new model
IloCplex cplex = new IloCplex();
//define variables
IloNumVar x;
IloNumVar y;
IloObjective objective;
objective = cplex.addMaximize();
IloRange cons01;
IloRange cons02;
cons01 = cplex.addRange(0, 5, "c1");
cons02 = cplex.addRange(0, 2, "c1");
IloColumn new_col = cplex.column(objective, 2);
IloColumn new_col2 = cplex.column(objective,3);
new_col = new_col.and(cplex.column(cons01,1));
new_col2 = new_col2.and(cplex.column(cons02,1));
x = cplex.numVar(new_col, 0, Double.MAX_VALUE);
y = cplex.numVar(new_col, 0, Double.MAX_VALUE);
//solve model
if (cplex.solve()) {
System.out.println("obj = "+cplex.getObjValue());
System.out.println("x = "+cplex.getValue(x));
System.out.println("y = "+cplex.getValue(y));
}
else {
System.out.println("Model not solved");
}
cplex.end();
}
catch (IloException exc) {
exc.printStackTrace();
}
}
但我没有得到正确的解决方案。我写代码有什么错误吗?
尝试调试此类问题时,将模型导出为 LP 格式以确保正确生成它总是很有用的。您可以通过在调用 cplex.solve
:
cplex.exportModel("model.lp");
如果这样做,model.lp 的内容将如下所示:
Maximize
obj: 2 x1 + 2 x2
Subject To
c1: x1 + x2 - Rgc1 = 0
c1: - Rgc1 = 0
Bounds
0 <= Rgc1 <= 5
0 <= Rgc1 <= 2
End
这说明了您在程序中的两个拼写错误。即,您应该替换:
cons02 = cplex.addRange(0, 2, "c1");
和
cons02 = cplex.addRange(0, 2, "c2");
并且,您应该替换:
y = cplex.numVar(new_col, 0, Double.MAX_VALUE);
和
y = cplex.numVar(new_col2, 0, Double.MAX_VALUE);
进行这两项更改后 model.lp 看起来像:
Maximize
obj: 2 x1 + 3 x2
Subject To
c1: x1 - Rgc1 = 0
c2: x2 - Rgc2 = 0
Bounds
0 <= Rgc1 <= 5
0 <= Rgc2 <= 2
End
并且,您从程序中得到以下输出:
obj = 16.0
x = 5.0
y = 2.0