简体   繁体   中英

Gurobi Jupyter Notebook solution not showing

I typed the following code in my jupyter notebook but its not giving me any optimal solution. It's just giving me this Image . Could someone help me out?

import gurobipy as gp
from gurobipy import GRB

try:

    # Create a new model
    m = gp.Model("Diet")

    # Create variables
    x = m.addVar(vtype="C", name="x")
    y = m.addVar(vtype="C", name="y")
    z = m.addVar(vtype="C", name="z")
    a = m.addVar(vtype="C", name="a")

    # Set objective
    m.setObjective(50*x + 20*y + 30*z + 80*a, GRB.MAXIMIZE)

    # Add constraint:
    m.addConstr(400 * x + 200 * y + 150 * z + 500 * a >= 500, "c0")

    # Add constraint: 
    m.addConstr(3 * x + 2 * y >= 6, "c1")
    
    # Add constraint: 
    m.addConstr(2 * x + 2 * y + 4 * z + 4 * a >= 10, "c2")
    
    # Add constraint: 
    m.addConstr(2 * x + 4 * y + z + 5 * a >= 8, "c3")
    

    # Optimize model
    m.optimize()

    for v in m.getVars():
        print('%s %g' % (v.varName, v.x))

    print('Obj: %g' % m.objVal)

except gp.GurobiError as e:
    print('Error code ' + str(e.errno) + ': ' + str(e))

except AttributeError:
    print('Encountered an attribute error')

The solution isn't printing because it didn't find an optimal solution. Infeasible or unbounded model means that either it's infeasible ie no solution exists, or it's unbounded ie it can continue increasing/decreasing the values of the variables without limit to improve the objective without limit.

Looking at the model, it does indeed appear to be unbounded. You have as the objective

m.setObjective(50*x + 20*y + 30*z + 80*a, GRB.MAXIMIZE)

and there is no upper limit on any of these variables. If you change this to

m.setObjective(50*x + 20*y + 30*z + 80*a, GRB.MINIMIZE)

then you should get the optimal solution for minimizing this objective.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM