java 调用 gurobi 的一个例子

java 同时被 2 个专栏收录
59 篇文章 8 订阅
15 篇文章 10 订阅

Gurobi 也可以用 java 调用,但相对于 python, 对于 java 调用的文档说明很少。自己摸索了下,虽然对 java 调用的方法与类支持地比较少(不像 cplex 对 java 支持地那样多),但基本满足了需求。

官方文档自带的一个混合整数规划例子:

/* Copyright 2018, Gurobi Optimization, LLC */

/* This example formulates and solves the following simple MIP model:

     maximize    x +   y + 2 z
     subject to  x + 2 y + 3 z <= 4
                 x +   y       >= 1
     x, y, z binary
*/

import gurobi.*;

public class Mip1 {
  public static void main(String[] args) {
    try {
      GRBEnv    env   = new GRBEnv("mip1.log");
      GRBModel  model = new GRBModel(env);

      // Create variables

      GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
      GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
      GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

      // Set objective: maximize x + y + 2 z

      GRBLinExpr expr = new GRBLinExpr();
      expr.addTerm(1.0, x); expr.addTerm(1.0, y); expr.addTerm(2.0, z);
      model.setObjective(expr, GRB.MAXIMIZE);

      // Add constraint: x + 2 y + 3 z <= 4

      expr = new GRBLinExpr();
      expr.addTerm(1.0, x); expr.addTerm(2.0, y); expr.addTerm(3.0, z);
      model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

      // Add constraint: x + y >= 1

      expr = new GRBLinExpr();
      expr.addTerm(1.0, x); expr.addTerm(1.0, y);
      model.addConstr(expr, GRB.GREATER_EQUAL, 1.0, "c1");

      // Optimize model

      model.optimize();

      System.out.println(x.get(GRB.StringAttr.VarName)
                         + " " +x.get(GRB.DoubleAttr.X));
      System.out.println(y.get(GRB.StringAttr.VarName)
                         + " " +y.get(GRB.DoubleAttr.X));
      System.out.println(z.get(GRB.StringAttr.VarName)
                         + " " +z.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());
    }
  }
}

自己摸索发现,也可以定义求解变量的数组,代入模型中求解,这样就能求解一些复杂的模型了。

通过定义求解变量数组,改动上面的例子如下:

import gurobi.*;

public class Mip1 {
  public static void main(String[] args) {
    try {
      GRBEnv    env   = new GRBEnv("mip1.log");
      GRBModel  model = new GRBModel(env);

      // Create variables

     GRBVar[] x = new GRBVar[3];		    
	 x[0] = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "x");
	 x[1] = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "y");
	 x[2] = model.addVar(0.0, 1.0, 0.0, GRB.BINARY, "z");

	 // Set objective: maximize x + y + 2 z
	 GRBLinExpr expr = new GRBLinExpr();
	 expr.addTerm(1.0, x[0]);
	 expr.addTerm(1.0, x[1]);
	 expr.addTerm(2.0, x[2]);
	 model.setObjective(expr, GRB.MAXIMIZE);

	 // Add constraint: x + 2 y + 3 z <= 4
	 expr = new GRBLinExpr();
	 expr.addTerm(1.0, x[0]);
	 expr.addTerm(2.0, x[1]);
	 expr.addTerm(3.0, x[2]);
	 model.addConstr(expr, GRB.LESS_EQUAL, 4.0, "c0");

	 // Add constraint: x + y >= 1
	 expr = new GRBLinExpr();
	 expr.addTerm(1.0, x[0]);
	 expr.addTerm(1.0, x[1]);
	 model.addConstr(expr, GRB.GREATER_EQUAL, 1.0, "c1");

      // Optimize model

      model.optimize();

      System.out.println(x.get(GRB.StringAttr.VarName)
                         + " " +x.get(GRB.DoubleAttr.X));
      System.out.println(y.get(GRB.StringAttr.VarName)
                         + " " +y.get(GRB.DoubleAttr.X));
      System.out.println(z.get(GRB.StringAttr.VarName)
                         + " " +z.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());
    }
  }
}
  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值