LinExpr Type Error float() argument must be a string or a number

918 views
Skip to first unread message

Saravana Murthy

unread,
Jun 18, 2018, 10:07:37 AM6/18/18
to Gurobi Optimization
Hello,

I am a beginner to gurobipy. I have an objective function which is a sum of 3 LinExpr expressions. I have to maximize this function for "n" sets and I do this in a for loop.  I have a set of constraints as well.

The problem here is I get an error sometime (***NOT ALWAYS***) but exactly at the same line of code saying: File "linexpr.pxi", line 62, in gurobipy.LinExpr.__init__ (../../src/python/gurobipy.c:27663)
TypeError: float() argument must be a string or a number.

The code i have looks something like this.

for i in range(n):

     get inputs (code not shown here) - These are the number of variables
     model = Model("mymodel")
     x = model.addVars(5,vtype=GRB.BINARY,name= "x")
     y = model.addVars(5,vtype=GRB.BINARY,name= "y")
     z = model.addVars(6,vtype=GRB.BINARY,name= "z")
    coeff_x = [1 for j in range 5]
    coeff_y = [-1 for j in range 4]
    coeff_z = [1 for j in range 6]

    for each_i in range(len(y)):
           model.addConstr(z[i] == or_(x[i],y[i]))

Line 1    model.setObjective (LinExpr(coeff_x,x) +
Line 2                                     LinExpr(coeff_y,y) + 
Line 3                                   LinExpr(coeff_z,z)) 

    model.update()
    model.optimize()


When I run this, the error is always at line 3 telling 
"File "linexpr.pxi", line 62, in gurobipy.LinExpr.__init__ (../../src/python/gurobipy.c:27663)
TypeError: float() argument must be a string or a number "

This error does not happen always, quite random. For example if my n is set to 100, i get this error at i= 2 and sometimes at i = 65 etc. No real pattern. 

What can be the source of error.??


Thanks in advance

Michael Winkler

unread,
Jun 18, 2018, 10:10:27 AM6/18/18
to gur...@googlegroups.com, Saravana Murthy
Hi Saravana,


>     for each_i in range(len(y)):
>            model.addConstr(z[i] == or_(x[i],y[i]))
>

above it should be model.addConstr(z[each_i] == or_(x[each_ii],y[each_i]))

Hopefully this fixes your problem.

Best,
Michael

Saravana Murthy

unread,
Jun 18, 2018, 1:09:01 PM6/18/18
to Gurobi Optimization
Hello Michael Winkler,

Thats just a typo. The error would be list index out of range. But still even if i fix that the problem persists

Robert Luce

unread,
Jun 20, 2018, 7:24:25 AM6/20/18
to Gurobi Optimization
Hello Saravana,

would it be possible to reduce your code to a minimal, executable python script that reproduces the behaviour you ar reporting?

Thanks,

Robert
Reply all
Reply to author
Forward
0 new messages