我正在开发一个与Java一致的cplex模型,我需要一个条件约束,但似乎根本没有执行cplex.ifthen
代码。
我希望如果数组的元素等于1,那么同一列号的一个元素应该等于一。
这是我的代码。基本上,我创建了两个具有相同尺寸的矩阵。首先,我从第一个矩阵中选择了最小值,即4.现在我想从该列中选择最小元素。为此,我使用的是cplex.ifthen
,但它无效。
package sch;
import ilog.concert.*;
import ilog.cplex.*;
import ilog.concert.IloException;
import ilog.concert.IloLinearNumExpr;
import ilog.concert.IloNumVar;
import ilog.cplex.IloCplex;
public class Firsttry {
public static void main(String[] args) {
schedule();
}
public static void schedule() {
int exp = 3;
int imp = 5;
double[] volI = { 10.5, 5.0, 15.5, 16, 2.0 };
double shipv = 0.25;
double[][] days = { { 115, 7, 9, 8, 7 }, { 555, 14, 8, 7, 12 }, { 112, 6, 8, 10, 4 } };
try {
IloCplex cplex = new IloCplex();
// variable
IloNumVar[][] x = new IloNumVar[exp][imp];
IloNumVar[][] y = new IloNumVar[exp][imp];
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
x[e][i] = cplex.boolVar();
y[e][i] = cplex.boolVar();
}
}
IloLinearNumExpr objective = cplex.linearNumExpr();
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
objective.addTerm(days[e][i], x[e][i]);
objective.addTerm(days[e][i], y[e][i]);
}
}
cplex.addMinimize(objective);
// CONSTRAINTS
// constraint .....1
IloLinearNumExpr expr1 = cplex.linearNumExpr();
// IloLinearNumExpr expr2 = cplex.linearNumExpr();
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
expr1.addTerm(1, x[e][i]);
}
}
cplex.addEq(expr1, 1);
// constraint .....2
// if
IloLinearNumExpr[][] rowexp = new IloLinearNumExpr[exp][imp];
IloLinearNumExpr[] colexp = new IloLinearNumExpr[imp];
IloConstraint[][] rowconst = new IloConstraint[exp][imp];
IloConstraint[] colconst = new IloConstraint[imp];
// if
for (int e = 0; e < exp; ++e) {
// rowexp[e] = cplex.linearNumExpr();
for (int i = 0; i < imp; ++i) {
rowexp[e][i] = cplex.linearNumExpr();
rowexp[e][i].addTerm(1, x[e][i]);
rowconst[e][i] = cplex.eq(1, rowexp[e][i]);
}
// rowconst[e] = cplex.eq(1, rowexp[e]);
}
for (int i = 0; i < imp; ++i) {
colexp[i] = cplex.linearNumExpr();
for (int e = 0; e < exp; ++e) {
// colexp[i] = cplex.linearNumExpr();
colexp[i].addTerm(1, y[e][i]);
}
colconst[i] = cplex.eq(1, colexp[i]);
}
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
cplex.ifThen(rowconst[e][i], colconst[i]);
}
}
if (cplex.solve()) {
System.out.println("obje =" + cplex.getObjValue());
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
// System.out.println(" =" + cplex.getValue(y[e][i]));
System.out.print(cplex.getValue(y[e][i]) + " ");
}
System.out.println();
}
System.out.printf("%n%n%n");
for (int e = 0; e < exp; ++e) {
for (int i = 0; i < imp; ++i) {
// System.out.println(" =" + cplex.getValue(y[e][i]));
System.out.print(cplex.getValue(x[e][i]) + " ");
}
System.out.println();
}
System.out.println("obje =" + cplex.getObjValue());
}
} catch (IloException exc) {
exc.printStackTrace();
}
}
}
答案 0 :(得分:0)
我没有测试您的代码或研究您的配方,但看起来您的问题来自以下行:
cplex.ifThen(rowconst[e][i], colconst[i]);
正如ifThen的文档所述,会返回约束。它不会将其添加到模型中。要解决此问题,您可以执行以下操作:
cplex.add(cplex.ifThen(rowconst[e][i], colconst[i]));
另外,有关如何使用ifThen
的示例,您可能需要查看CPLEX附带的FoodManufact.java
程序。