Convert sympy expressions to use a 3rd party linear programming solver (pulp)

71 views
Skip to first unread message

Matt Anonyme

unread,
May 28, 2016, 2:52:20 PM5/28/16
to sympy
Hi,

First of all, thanks for the fantastic open source library ! It has all I could dream of (export to latex etc...).

I use sympy to generate constraints of a linear programming problem. At the end of it, I want to find the solution for the system with https://github.com/coin-or/pulp, i.e., I want to convert my sympy.core.expr.Expr ( "x+y" etc..) into pulp.LpConstraint variables in order to do as in this tutorial: http://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html
===
# The five constraints are entered
prob += x1 + x2 == 100, "PercentagesSum"
prob += 0.100*x1 + 0.200*x2 >= 8.0, "ProteinRequirement"
prob += 0.080*x1 + 0.100*x2 >= 6.0, "FatRequirement"
prob += 0.001*x1 + 0.005*x2 <= 2.0, "FibreRequirement"
prob += 0.002*x1 + 0.005*x2 <= 0.4, "SaltRequirement"
===
So for instance in my code, I have a sympy expression 0.100*x1 + 0.200*x2 but I want to substitute x1 and x2 by pulp.LpVariable (similar to sympy symbol) so that the sympy.core.expr.Expr becomes a pulp.LpAffineExpression.
So far, I believe my best option is to use Expr.as_coefficients_dict to rebuild the whole thing.

I wonder if there is a best option (even with another library than pulp, I don't care) to solve this (optimization) problem.

Best regards



Ondřej Čertík

unread,
May 30, 2016, 10:05:55 AM5/30/16
to sympy
Hi Matt,

On Sat, May 28, 2016 at 7:40 PM, Matt Anonyme <matt...@gmail.com> wrote:
> Hi,
>
> First of all, thanks for the fantastic open source library ! It has all I
> could dream of (export to latex etc...).

I am glad you found it useful.

>
> I use sympy to generate constraints of a linear programming problem. At the
> end of it, I want to find the solution for the system with
> https://github.com/coin-or/pulp, i.e., I want to convert my
> sympy.core.expr.Expr ( "x+y" etc..) into pulp.LpConstraint variables in
> order to do as in this tutorial:
> http://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html
> ===
>
> # The five constraints are entered
> prob += x1 + x2 == 100, "PercentagesSum"
> prob += 0.100*x1 + 0.200*x2 >= 8.0, "ProteinRequirement"
> prob += 0.080*x1 + 0.100*x2 >= 6.0, "FatRequirement"
> prob += 0.001*x1 + 0.005*x2 <= 2.0, "FibreRequirement"
> prob += 0.002*x1 + 0.005*x2 <= 0.4, "SaltRequirement"
>
> ===
> So for instance in my code, I have a sympy expression 0.100*x1 + 0.200*x2
> but I want to substitute x1 and x2 by pulp.LpVariable (similar to sympy
> symbol) so that the sympy.core.expr.Expr becomes a pulp.LpAffineExpression.
> So far, I believe my best option is to use Expr.as_coefficients_dict to
> rebuild the whole thing.

You can use the traverse expression functions (preorder_traversal,
postorder_traversal) to rebuild the expressions using your classes.

Another option is to write a custom printer.

A third option is to use the lambdify functionality, we use it to use
NumPy or other libraries to evaluate things. Perhaps it could be used
for your case.

Ondrej

>
> I wonder if there is a best option (even with another library than pulp, I
> don't care) to solve this (optimization) problem.
>
> Best regards
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/7e0b3040-3581-48c0-a2a8-eef8cf051d55%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages