[英]creating two kinds of dependent variable using cplex and java
I am new to work by CPLEX. 我是CPLEX的新员工。 My simplified optimization problem is: 我的简化优化问题是:
objective function:
Maximize z1 + z2 + z3
Subject to:
c1: x1 - 3 x2 + x3 <= 30
c2: x1 + x2 + x3 >= z1
...
Bounds
x1=[0,1]
x2=[0,1]
...
To model this problem, my code is: 为了对此问题建模,我的代码是:
public static void main(String[] args) {
try {
IloCplex cplex = new IloCplex();
IloNumVar[] z = cplex.numVarArray(3, 0.0, 1.0);
IloLinearNumExpr objectiveExpr = cplex.linearNumExpr();
IloLinearNumExpr constraintExpr1 = cplex.linearNumExpr();
IloLinearNumExpr constraintExpr3 = cplex.linearNumExpr();
IloNumVar[] x = cplex.numVarArray(3, 0.0, 1.0);
for (int i = 1; i < 3; i++){
objectiveExpr.addTerm(1, z[i]);
}
IloObjective obj = cplex.maximize(objectiveExpr);
cplex.add(obj);
constraintExpr1.addTerm(1, x[1]);
constraintExpr1.addTerm(-3, x[2]);
constraintExpr1.addTerm(1, x[3]);
cplex.addLe(constraintExpr1, 30);
constraintExpr2.addTerm(-1, x[1]);
constraintExpr2.addTerm(1, x[2]);
constraintExpr2.addTerm(1, x[3]);
cplex.addGe(constraintExpr2, z[1]);
.
.
.
}
catch (IloException e){
System.err.println("Concert exception '" + e + "' caught");
}
}
The generated model of this code is: 该代码的生成模型为:
objective function:
Maximize x1 + 2 x2 + 3 x3
Subject To:
c1: x7 - 3 x8 + x9 <= 30
c2: - x1 - x7 + x8 + x9 <= 0
Bounds
0 <= x1 <= 1
...
If it starts to model the constraints from x4 instead of x7, I could distinguish x and z easily. 如果它开始从x4而不是x7建模约束,则可以轻松地区分x和z。
Unlike David, I do know CPLEX for about 17 years, but I also don't understand what your problem is. 与David不同,我确实了解CPLEX已有17年,但我也不了解您的问题所在。 Can you explain in what way the generated model is different from what you expected - we can probably work this out ourselves, but it seems daft for potentially many of us to have to spend extra time understanding your problem when you could explain better. 您能解释一下生成的模型与您期望的模型有什么不同吗?我们可以自己解决这个问题,但是当我们可能更好地解释时,可能我们许多人不得不花更多的时间来理解您的问题似乎很愚蠢。
Can I suggest that you name your variables and constraints using setName(...) so that you can see better what is happening in the generated model. 我可以建议您使用setName(...)来命名变量和约束,以便更好地了解生成的模型中发生了什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.