Objective Q not PSD (diagonal adjustment would be required)

576 views
Skip to first unread message

Heinu Schütte

unread,
Sep 22, 2016, 7:15:12 AM9/22/16
to Gurobi Optimization
Hello,

I am trying to solve my first model using Gurobi in Python.

It is a very simple minimisation problem and everything works as expected, until I add this constraint:
c4 = m.addConstr(l, GRB.EQUAL, 16.0373*w + 31.9586)

I am trying to set the load hauled by a locomotive equal to 16.0373*(the number of wagons) + 31.9586.

When running the model, I get:
gurobipy.GurobiError: Objective Q not PSD (diagonal adjustment of 1.8e+03 would be required)

I have read some other questions regarding the "Objective Q not PSD" error, but I don't understand what it means, nor how to fix it.

Any help will be greatly appreciated.

Regards,
Heinu

Daniel Espinoza

unread,
Sep 22, 2016, 10:10:08 AM9/22/16
to Gurobi Optimization
Hi Heinu,

Assuming that `w` and `I` are variables, did you try 

c4 = m.addConstr(16.0373*w + 31.9586, GRB.EQUAL, I)

instead?

or even simpler:

c4 = m.addConstr(I == 16.0373*w + 31.9586)

Let me know if they work

Best
Daniel

Daniel Espinoza

unread,
Sep 22, 2016, 10:17:33 AM9/22/16
to Gurobi Optimization
Still,

It seems strange to me that you are getting that behavior, could you share a more complete example of the problem?

Best
Daniel

Heinu Schütte

unread,
Sep 22, 2016, 12:10:47 PM9/22/16
to Gurobi Optimization
Hello Daniel,

Thank you for the response. I tried
c4 = m.addConstr(I == 16.0373*w + 31.9586)
and that seems to work.

However, now I get "gurobipy.GurobiError: Unable to retrieve attribute 'X' ". Which goes away when I take c4 out - but I get a useless calculation result.

Below is the full code:

from gurobipy import *

# Set calculation-loop lists
days = range(1,366)
speedlist = range(5,115,5)

# Repeat entire optimisation calculation for every increment of train operating
# speed = {5;10;15;...;110}
for i in speedlist:
    v = i
    print
    print "--- --- --- --- ---"
    print
    print "For train operating speed set to", v, "km/h:"
    print
    print "--- --- --- --- ---"
    print
    
    # Set calculation values for each iteration of operating speed:
    d = 117.70
    t = d/v
    
    # Create model
    m = Model('Swazilink Optimisation')
    
    # Define variables
    n = m.addVar(vtype=GRB.INTEGER, name='Number of trains per day')
    w = m.addVar(vtype=GRB.INTEGER, name='Number of wagons per train')
    c = m.addVar(vtype=GRB.INTEGER, name='Number of containers per train')
    l = m.addVar(vtype=GRB.CONTINUOUS, name='Load hauled per train')
    
    # Update Gurobi variables
    m.update()
    
    # Define objective function
    obj = quicksum(QuadExpr(n*l*v)
        for i in days)
    yearly = quicksum(n*w
        for j in days)
    
    # Maximise minimise objective function        
    m.setObjective(obj, GRB.MINIMIZE)
    
    # Set constraints
    c1 = m.addConstr(w, GRB.LESS_EQUAL, 50)
    c2 = m.addConstr(c, GRB.EQUAL, 2*w)
    c3 = m.addConstr(l, GRB.EQUAL, w)
    c4 = m.addConstr(l == 16.0373*w + 31.9586)
    c5 = m.addConstr(n*w, GRB.GREATER_EQUAL, 1305)
    c6 = m.addConstr(yearly, GRB.GREATER_EQUAL, 476463)
    c7 = m.addConstr(w, GRB.GREATER_EQUAL, 0)
    c8 = m.addConstr(c, GRB.GREATER_EQUAL, 0)
    
    # Run model optimization
    m.optimize()

    # Print attributes
    m.printAttr('obj')
    m.printAttr('X')

Regards,
Heinu

Daniel Espinoza

unread,
Sep 22, 2016, 12:30:49 PM9/22/16
to Gurobi Optimization
Dear Heinu,

It seems that your model is infeasible (even in the first instance where speed=5); thus when you query X you get an error. (see R2 and R3 and R4)

Best
Daniel

Heinu Schütte

unread,
Sep 22, 2016, 2:02:56 PM9/22/16
to Gurobi Optimization
Dear Daniel,

Oh dear. Thank you very much for your assistance.

Best regards
Heinu
Reply all
Reply to author
Forward
0 new messages