How to use Gurobi to solve this optimization problem?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















the problem is follow:



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


or like this format:



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


This problem is from a paper, in this paper, the author used Gurobi to solve the problem. I download Gurobi and studied the LP examples, but the example's object is like min x + y + 2 z.
I want to know if this problem can be solved by Guribo,if the answer is yes, how to write the model.
Thank u very much.










share|improve this question



















  • 2





    You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

    – joni
    Nov 15 '18 at 18:07











  • Thank you for your comment,I use the Java api in Gurobi.

    – maqy
    Nov 16 '18 at 5:16











  • What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

    – maqy
    Nov 16 '18 at 5:27











  • To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

    – pushpen.paul
    Jan 6 at 13:32

















0















the problem is follow:



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


or like this format:



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


This problem is from a paper, in this paper, the author used Gurobi to solve the problem. I download Gurobi and studied the LP examples, but the example's object is like min x + y + 2 z.
I want to know if this problem can be solved by Guribo,if the answer is yes, how to write the model.
Thank u very much.










share|improve this question



















  • 2





    You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

    – joni
    Nov 15 '18 at 18:07











  • Thank you for your comment,I use the Java api in Gurobi.

    – maqy
    Nov 16 '18 at 5:16











  • What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

    – maqy
    Nov 16 '18 at 5:27











  • To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

    – pushpen.paul
    Jan 6 at 13:32













0












0








0








the problem is follow:



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


or like this format:



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


This problem is from a paper, in this paper, the author used Gurobi to solve the problem. I download Gurobi and studied the LP examples, but the example's object is like min x + y + 2 z.
I want to know if this problem can be solved by Guribo,if the answer is yes, how to write the model.
Thank u very much.










share|improve this question
















the problem is follow:



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


or like this format:



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


This problem is from a paper, in this paper, the author used Gurobi to solve the problem. I download Gurobi and studied the LP examples, but the example's object is like min x + y + 2 z.
I want to know if this problem can be solved by Guribo,if the answer is yes, how to write the model.
Thank u very much.







java gurobi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 10:37









joni

868168




868168










asked Nov 15 '18 at 17:20









maqymaqy

84




84







  • 2





    You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

    – joni
    Nov 15 '18 at 18:07











  • Thank you for your comment,I use the Java api in Gurobi.

    – maqy
    Nov 16 '18 at 5:16











  • What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

    – maqy
    Nov 16 '18 at 5:27











  • To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

    – pushpen.paul
    Jan 6 at 13:32












  • 2





    You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

    – joni
    Nov 15 '18 at 18:07











  • Thank you for your comment,I use the Java api in Gurobi.

    – maqy
    Nov 16 '18 at 5:16











  • What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

    – maqy
    Nov 16 '18 at 5:27











  • To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

    – pushpen.paul
    Jan 6 at 13:32







2




2





You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

– joni
Nov 15 '18 at 18:07





You didn't mention if you want to use Gurobi from a specific language. You could use it from Python, C, C++, Java, .NET, R and Matlab to create and solve your optimization problems. It's also possible to call it directly from the command line to solve your lp. I'd recommend to model your lp and solve it via gurobi_cl yourlp.lp.

– joni
Nov 15 '18 at 18:07













Thank you for your comment,I use the Java api in Gurobi.

– maqy
Nov 16 '18 at 5:16





Thank you for your comment,I use the Java api in Gurobi.

– maqy
Nov 16 '18 at 5:16













What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

– maqy
Nov 16 '18 at 5:27





What bothers me now is that the objective function is not a certain function.the object value 'z' depends on a series of funcitons, I don't konw how to model this lp, could you give me some suggestions?

– maqy
Nov 16 '18 at 5:27













To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

– pushpen.paul
Jan 6 at 13:32





To double up what @joni said, .lp files are easy to create, human readable and probably more versatile.

– pushpen.paul
Jan 6 at 13:32












1 Answer
1






active

oldest

votes


















0














I should admit I'm not a big fan of java and it's the first time I used Gurobi's Java Interface, so it might not be the most elegant solution. Anyway, here's a way to model and solve your problem in 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());





This will also create a model.lp file which contains your 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


For such a small problem I'd recommend to write your LP directly in such a model file. Then you can solve it from the command line via Gurobi's command line tool:



gurobi_cl ResultFile=model.sol model.lp 


where model.sol is the file containing the solution.



Note that you don't need to use Gurobi for such a simple LP. There are a handful of good non-commercial solvers (
lp_solve or GLPK for example) that can solve this problem easily. With GLPK you could solve it via



glpsol --cpxlp model.lp -o solution.txt


from the command line. The --cpxlp flag tells glpk that model.lp is written in the cplex format, while -o solution.txt tells glpk to write the solution to the file solution.txt.






share|improve this answer

























  • That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

    – maqy
    Nov 16 '18 at 15:06











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324808%2fhow-to-use-gurobi-to-solve-this-optimization-problem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I should admit I'm not a big fan of java and it's the first time I used Gurobi's Java Interface, so it might not be the most elegant solution. Anyway, here's a way to model and solve your problem in 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());





This will also create a model.lp file which contains your 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


For such a small problem I'd recommend to write your LP directly in such a model file. Then you can solve it from the command line via Gurobi's command line tool:



gurobi_cl ResultFile=model.sol model.lp 


where model.sol is the file containing the solution.



Note that you don't need to use Gurobi for such a simple LP. There are a handful of good non-commercial solvers (
lp_solve or GLPK for example) that can solve this problem easily. With GLPK you could solve it via



glpsol --cpxlp model.lp -o solution.txt


from the command line. The --cpxlp flag tells glpk that model.lp is written in the cplex format, while -o solution.txt tells glpk to write the solution to the file solution.txt.






share|improve this answer

























  • That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

    – maqy
    Nov 16 '18 at 15:06















0














I should admit I'm not a big fan of java and it's the first time I used Gurobi's Java Interface, so it might not be the most elegant solution. Anyway, here's a way to model and solve your problem in 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());





This will also create a model.lp file which contains your 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


For such a small problem I'd recommend to write your LP directly in such a model file. Then you can solve it from the command line via Gurobi's command line tool:



gurobi_cl ResultFile=model.sol model.lp 


where model.sol is the file containing the solution.



Note that you don't need to use Gurobi for such a simple LP. There are a handful of good non-commercial solvers (
lp_solve or GLPK for example) that can solve this problem easily. With GLPK you could solve it via



glpsol --cpxlp model.lp -o solution.txt


from the command line. The --cpxlp flag tells glpk that model.lp is written in the cplex format, while -o solution.txt tells glpk to write the solution to the file solution.txt.






share|improve this answer

























  • That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

    – maqy
    Nov 16 '18 at 15:06













0












0








0







I should admit I'm not a big fan of java and it's the first time I used Gurobi's Java Interface, so it might not be the most elegant solution. Anyway, here's a way to model and solve your problem in 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());





This will also create a model.lp file which contains your 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


For such a small problem I'd recommend to write your LP directly in such a model file. Then you can solve it from the command line via Gurobi's command line tool:



gurobi_cl ResultFile=model.sol model.lp 


where model.sol is the file containing the solution.



Note that you don't need to use Gurobi for such a simple LP. There are a handful of good non-commercial solvers (
lp_solve or GLPK for example) that can solve this problem easily. With GLPK you could solve it via



glpsol --cpxlp model.lp -o solution.txt


from the command line. The --cpxlp flag tells glpk that model.lp is written in the cplex format, while -o solution.txt tells glpk to write the solution to the file solution.txt.






share|improve this answer















I should admit I'm not a big fan of java and it's the first time I used Gurobi's Java Interface, so it might not be the most elegant solution. Anyway, here's a way to model and solve your problem in 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());





This will also create a model.lp file which contains your 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


For such a small problem I'd recommend to write your LP directly in such a model file. Then you can solve it from the command line via Gurobi's command line tool:



gurobi_cl ResultFile=model.sol model.lp 


where model.sol is the file containing the solution.



Note that you don't need to use Gurobi for such a simple LP. There are a handful of good non-commercial solvers (
lp_solve or GLPK for example) that can solve this problem easily. With GLPK you could solve it via



glpsol --cpxlp model.lp -o solution.txt


from the command line. The --cpxlp flag tells glpk that model.lp is written in the cplex format, while -o solution.txt tells glpk to write the solution to the file solution.txt.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 12:43

























answered Nov 16 '18 at 11:24









jonijoni

868168




868168












  • That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

    – maqy
    Nov 16 '18 at 15:06

















  • That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

    – maqy
    Nov 16 '18 at 15:06
















That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

– maqy
Nov 16 '18 at 15:06





That answer is great, and I found the the reason that caused me to go wrong is:1.I am not pay attention to the use of spaces. 2.I didn't write the variable 'z' to the left of the inequality.

– maqy
Nov 16 '18 at 15:06



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324808%2fhow-to-use-gurobi-to-solve-this-optimization-problem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo