问题如下:
object:min z
s.t.
r1 >= 0
r2 >= 0
r3 >= 0
r1 + r2 + r3 = 1
15 * (1 - r1) <= z
12 * (1 - r2) <= z
12 * (1 - r3) <= z
240 * r1 <= z
27 * r2 <= z
27 * r3 <= z
或类似这种格式:
object:
min z; z = max( 15 * (1 - r1), 12 * (1 - r2), 12 * (1 - r3) ,240 * r1, 27 * r2, 27 * r3)
s.t.
r1 >= 0
r2 >= 0
r3 >= 0
r1 + r2 + r3 = 1
这个问题来自一篇论文,在本文中,作者使用Gurobi来解决。我下载了Gurobi并研究了LP示例,但是示例的对象就像min x + y + 2 z
。
我想知道Guribo是否可以解决这个问题,如果答案是肯定的,那么如何编写模型。
非常感谢你。
答案 0 :(得分:0)
我应该承认我不是Java的忠实拥护者,这是我第一次使用Gurobi的Java接口,因此它可能不是最优雅的解决方案。无论如何,这是一种使用Java建模和解决问题的方法:
// example.java
import gurobi.*;
public class example {
public static void main(String[] args) {
try {
GRBEnv env = new GRBEnv("example.log");
GRBModel model = new GRBModel(env);
// Create variables
GRBVar r1 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "r1");
GRBVar r2 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "r2");
GRBVar r3 = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "r3");
GRBVar z = model.addVar(-GRB.INFINITY, GRB.INFINITY, 1.0, GRB.CONTINUOUS, "z");
// Set objective: minimize z
GRBLinExpr expr = new GRBLinExpr();
expr.addTerm(1.0, z);
model.setObjective(expr, GRB.MINIMIZE);
// Add constraint: r1 + r2 + r3 = 1
expr = new GRBLinExpr();
expr.addTerm(1.0, r1); expr.addTerm(1.0, r2); expr.addTerm(1.0, r3);
model.addConstr(expr, GRB.EQUAL, 1.0, "c0");
// Add constraint: 15 * (1-r1) <= z <-> -15 r1 - z <= -15
expr = new GRBLinExpr();
expr.addTerm(-15.0, r1); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, -15.0, "c1");
// Add constraint: 12 * (1-r2) <= z <-> -12 r2 - z <= -12
expr = new GRBLinExpr();
expr.addTerm(-12.0, r2); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, -12.0, "c1");
// Add constraint: 12 * (1-r3) <= z <-> -12 r3 - z <= -12
expr = new GRBLinExpr();
expr.addTerm(-12.0, r3); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, -12.0, "c1");
// Add constraint: 240 r1 <= z <-> 240 r1 - z <= 0
expr = new GRBLinExpr();
expr.addTerm(240.0, r1); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, 0.0, "c1");
// Add constraint: 27 r2 <= z <-> 27 r2 - z <= 0
expr = new GRBLinExpr();
expr.addTerm(27.0, r2); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, 0.0, "c1");
// Add constraint: 27 r3 <= z <-> 27 r3 - z <= 0
expr = new GRBLinExpr();
expr.addTerm(27.0, r3); expr.addTerm(-1.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, 0.0, "c1");
// Optimize model
model.write("model.lp");
model.optimize();
System.out.println(r1.get(GRB.StringAttr.VarName)
+ " " +r1.get(GRB.DoubleAttr.X));
System.out.println(r2.get(GRB.StringAttr.VarName)
+ " " +r2.get(GRB.DoubleAttr.X));
System.out.println(r3.get(GRB.StringAttr.VarName)
+ " " +r3.get(GRB.DoubleAttr.X));
System.out.println("Obj: " + model.get(GRB.DoubleAttr.ObjVal));
// Dispose of model and environment
model.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
这还将创建一个包含您的LP的model.lp
文件:
Minimize
obj: z
Subject To
c0: r1 + r2 + r3 = 1
c1: -15 r1 - z <= -15
c2: -12 r2 - z <= -12
c3: -12 r3 - z <= -12
c4: 240 r1 - z <= 0
c5: 27 r2 - z <= 0
c6: 27 r3 - z <= 0
Bounds
r1 >= 0
r2 >= 0
r3 >= 0
End
对于这么小的问题,我建议直接在这样的model file中编写您的LP。然后,您可以通过Gurobi's command line tool在命令行中解决它:
gurobi_cl ResultFile=model.sol model.lp
其中model.sol
是包含解决方案的文件。
请注意,您不需要将Gurobi用于如此简单的LP。有少数优秀的非商业求解器( lp_solve或GLPK)可以轻松解决此问题。有了GLPK,您可以通过
解决它glpsol --cpxlp model.lp -o solution.txt
从命令行。 --cpxlp
标志告诉glpk model.lp以cplex格式编写,而-o solution.txt
告诉glpk将解决方案写到文件solution.txt。