AttributeError: 'gurobipy.LinExpr' object

2019-07-20 20:07发布

问题:

I am trying to model a MILP problem using Python with Gurobi Solver. I have the latest Gurobi solver version. My problem started after I added a constraint with new function of gurobi m.addGenConstrAbs which add the abs value of the function as a constraint. Here is my code which create a gurobi feedback as:

AttributeError: 'gurobipy.LinExpr' object has no attribute '__colno__'.

My code which results with this feedback is:

for t in range(0,Period): 
 m.addGenConstrAbs(PEN[t], EG [t]+STG[t]-XXX, "PEN Constraint") 

where EG[t], STD[t] and XXX are decision variables.

I don't understand why Gurobi or Python returns with this error. What do you think the problem came from? Thanks.

回答1:

The arguments to Model.addGenConstrAbs() must be variables (Var), not linear expressions (LinExpr). Try this:

for t in range(0,Period):
    z = m.addVar(lb=-GRB.INFINITY)
    m.addConstr(z == EG[t]+STG[t]-XXX)
    m.addGenConstrAbs(PEN[t], z, "PEN Constraint") 


标签: python gurobi