Try our new documentation site (beta).
Filter Content By
Version
Text Search
${sidebar_list_label} - Back
Filter by Language
bilinear.py
#!/usr/bin/env python3.7 # Copyright 2020, 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 gurobipy as gp from gurobipy import GRB # Create a new model m = gp.Model("bilinear") # Create variables x = m.addVar(name="x") y = m.addVar(name="y") z = m.addVar(name="z") # Set objective: maximize x m.setObjective(1.0*x, GRB.MAXIMIZE) # Add linear constraint: x + y + z <= 10 m.addConstr(x + y + z <= 10, "c0") # Add bilinear inequality constraint: x * y <= 2 m.addConstr(x*y <= 2, "bilinear0") # Add bilinear equality constraint: x * z + y * z == 1 m.addConstr(x*z + y*z == 1, "bilinear1") # First optimize() call will fail - need to set NonConvex to 2 try: m.optimize() except gp.GurobiError: print("Optimize failed due to non-convexity") # Solve bilinear model m.params.NonConvex = 2 m.optimize() m.printAttr('x') # Constrain 'x' to be integral and solve again x.vType = GRB.INTEGER m.optimize() m.printAttr('x')