Linear Programming Error

24 views
Skip to first unread message

tvn

unread,
Jul 22, 2010, 3:46:04 PM7/22/10
to sage-support
I am trying to solve this simple linear programming prob using
MixedIntegerLinearProgram and it gives an AttributeError:
LinearFunction instance has no attribute '__float__' exception



sage: p =
MixedIntegerLinearProgram(maximization=True)
sage: x =
p.new_variable()
sage: p.add_constraint(x[0] + x[1] ,
min=3)
sage: p.add_constraint(20*x[0] + 10*x[1] ,
max=80)
sage: p.add_constraint(x[1] ,
max=2*x[0])
sage: p.set_objective(2*x[0]+3*x[1])
p.show()
Maximization:
2 x_0 +3 x_1
Constraints:
3 <= x_0 +x_1
20 x_0 +10 x_1 <= 80
x_1 <= 2 x_0
Variables:
x_0 is a real variable (min=0.0, max=+oo)
x_1 is a real variable (min=0.0, max=+oo)
sage: p.solve()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)

/Hanoi/Storage.Hanoi/DL/<ipython console> in <module>()

/home/tnguyen/Src/Devel/sage/local/lib/python2.6/site-packages/sage/
numerical/mip.so in sage.numerical.mip.MixedIntegerLinearProgram.solve
(sage/numerical/mip.c:8036)()

/home/tnguyen/Src/Devel/sage/local/lib/python2.6/site-packages/sage/
numerical/mip_glpk.so in sage.numerical.mip_glpk.solve_glpk (sage/
numerical/mip_glpk.cpp:725)()

/home/tnguyen/Src/Devel/sage/local/lib/python2.6/site-packages/sage/
numerical/mip_glpk.so in sage.numerical.mip_glpk.build_glp_prob (sage/
numerical/mip_glpk.cpp:2272)()

AttributeError: LinearFunction instance has no attribute '__float__'

Nathann Cohen

unread,
Jul 22, 2010, 9:57:49 PM7/22/10
to sage-support
Hello !!

> I am trying to solve this simple linear programming prob using
> MixedIntegerLinearProgram and it gives an AttributeError:
> LinearFunction instance has no attribute '__float__' exception

This is mainly my fault, and the reason is that min/max arguments do
not like to get something different from a real value :-)

sage: p = MixedIntegerLinearProgram(maximization=True)
sage: x = p.new_variable()
sage: p.add_constraint(x[0] + x[1] , min=3)
sage: p.add_constraint(20*x[0] + 10*x[1] , max=80)
sage: p.add_constraint(x[1] - 2*x[0], max = 0) # This line has
changed !
sage: p.set_objective(2*x[0]+3*x[1])
sage: p.solve()
16.0

I will immediately send a patch so that it returns an exception
otherwise, and update the manual accordingly. There is something else
I wrote which may interest you, though :

sage: p = MixedIntegerLinearProgram(maximization=True)
sage: x = p.new_variable()
sage: p.add_constraint(x[0] + x[1] >= 3) # This line has
changed !
sage: p.add_constraint(20*x[0] + 10*x[1] <= 80) # This line has
changed !
sage: p.add_constraint(x[1] <= 2*x[0]) # This line has
changed !
sage: p.set_objective(2*x[0]+3*x[1])
sage: p.solve()
16.0

Note that is it (very, very slightly) slower than the min/max syntax.
I do no think it will make any difference until your Linear Programs
have several hundreds of constraints, and even then it will only slow
(if at all) the construction of the LP, not the actual solving
process.

I will also try to take the time to rewrite an up-to-date tutorial for
LP in Sage... :-)

Have fuuuuuuuuun !!!

Nathann

Nathann Cohen

unread,
Jul 22, 2010, 10:38:58 PM7/22/10
to sage-support
This is now patch #9579, which is waiting for review :-)

Nathann

tvn

unread,
Jul 24, 2010, 1:21:16 PM7/24/10
to sage-support
thanks for the quick response ...
Reply all
Reply to author
Forward
0 new messages