Sympy expressions and GEKKO

63 views
Skip to first unread message

Martin Sørland Festøy

unread,
Mar 28, 2018, 11:49:09 AM3/28/18
to sympy
I am somewhat new to programming, and what I know about Sympy, Numpy, Scipy and GEKKO have been mostly self-taught within the last six-seven weeks, so I apologize if this inquiry seem elementary.

I am writing my master's thesis within industrial economics, and am currently analyzing a Hotelling model. The model I am analyzing consists of two stages: first stage, firms establishes themselves along a Hotelling line (an unit interval), and on the second stage these firms compete in prices (the second stage will always be simultaneous). The goal is to have this model work for n-number of firms. I have found Sympy extremely useful in finding explicit expressions for prices wrt. locations. These prices are inserted into their respective profit functions. From here on out, I am dependent on solving the model numerically to arrive at equilibrium locations.

Since lambdifying these expressions and defining callable functions for them are straight-forward, solving the first stage simultaneous is not problematic using Scipy's minimize (or fsolve on the FOCs). But I am also analyzing what happens when the firms establishes one-by-one. Making this stage dynamic is quite another world of hurt for me, and so far I have only eyed hope the GEKKO-package could be my savior. Unfortunately this package is not compatible with Sympy expressions or the same methods that work with Scipy (See another inquiry I made regarding this on the APMonitor Google Group ).

This is where I hope this community can assist me: Either by suggesting an alternate way of dynamically optimize my first stage for n-number of firms where I may use my expressions, or, I assume more realistically, some way of converting my Sympy-expressions into expressions that can be used in GEKKO.

Kind regards,
Martin Festøy

Aaron Meurer

unread,
Mar 28, 2018, 2:43:55 PM3/28/18
to sy...@googlegroups.com
I'm not familiar with GEKKO. However, it should be possible to define
a lambdify printer to convert SymPy expressions to GEKKO expressions.
Basically you need to subclass sympy.printing.lambdarepr.LambdaPrinter
and define the relevant methods for the SymPy classes used in your
expressions. Judging from the example here
https://gekko.readthedocs.io/en/latest/quick_start.html, this
shouldn't be too difficult.

Here are some resources that should be helpful

Docs on printing: http://docs.sympy.org/dev/modules/printing.html
Existing lambdify printers:
https://github.com/sympy/sympy/blob/master/sympy/printing/lambdarepr.py
and https://github.com/sympy/sympy/blob/master/sympy/printing/python.py
Docs on how lambdify works: https://github.com/sympy/sympy/pull/13485

You can also do this using only a modules dictionary in lambdify. That
is, lambdify prints an expression like sin(x + 1) as "sin(x + 1)", and
you can define what sin points to like lambdify(x, sin(x + 1), {'sin':
custom_sin}). See the docs in the above ummerged pull request for more
information on how this works. This only works for things that prints
as functions, as it basically is just a Python namespace replacement.
So if you need to do something special for things like addition,
you'll have to use a custom printer. A custom printer is also much
more maintainable, but it's more work to build up compared to a
namespace dictionary.

Aaron Meurer
> --
> 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/2c94ef4a-a15b-43cc-9939-64428e6bbfcb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages