Try our new documentation site (beta).

Filter Content By
Version

### Bilinear.java

/* Copyright 2024, Gurobi Optimization, LLC */

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

maximize    x
subject to  x + y + z <= 10
x * y <= 2          (bilinear inequality)
x * z + y * z == 1  (bilinear equality)
x, y, z non-negative (x integral in second version)
*/

import com.gurobi.gurobi.*;

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

// Create variables

GRBVar x = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "x");
GRBVar y = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "y");
GRBVar z = model.addVar(0.0, GRB.INFINITY, 0.0, GRB.CONTINUOUS, "z");

// Set objective

GRBLinExpr obj = new GRBLinExpr();
model.setObjective(obj, GRB.MAXIMIZE);

// Add linear constraint: x + y + z <= 10

GRBLinExpr expr = new GRBLinExpr();

// Add bilinear inequality: x * y <= 2

// Add bilinear equality: x * z + y * z == 1

// 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) + " " +
obj.getValue());
System.out.println();

// Constrain x to be integral and solve again

x.set(GRB.CharAttr.VType, GRB.INTEGER);
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) + " " +
obj.getValue());
System.out.println();

// Dispose of model and environment

model.dispose();
env.dispose();

} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}


Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
##### Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.