Optimizing math functions

Discussion in 'Python' started by Esmail, May 23, 2009.

  1. Esmail

    Esmail Guest

    Hello all,

    I would like to maximize or minimize a given math function over a
    specific set of values, in Python preferably.

    I was checking out Wolfram Alpha (http://www70.wolframalpha.com/)
    and it can do simple optimization problems in math, such as

    maximize 15*x - x**2 over 0 to 15

    (http://www70.wolframalpha.com/input/?i=maximize 15*x - x**2 over 0 to 15)

    which finds the maximum value and input for x in the range 0 to 15.
    Very cool. (This is of course a very simple example).

    What it apparently can't do is for maximize (or minimize) functions
    that contain two variables, x and y, or more. So for example a simple
    example would be

    maximize x**2 + y**2 over x:0 to 15, y:0-12 -- this does not work
    -- though I'm unclear about
    -- the correct syntax too.

    Is there some sort of simple Python module that would allow me to
    evaluate this type of function?

    In this particular instance I am interested in the minimum of

    x * sin(4*x) + 1.1 * sin(2*y), where x,y in range 0-10

    though in other problems the range may not be identical for x and y.

    Thanks,

    Esmail

    ps: Does anyone know if Octave or some other free Linux (or Windows)
    program might also do this in a simple way? My preference would
    still be a Python solution that would be simple to use, ie plug in
    the function, the ranges and have it pop out the solution :)
     
    Esmail, May 23, 2009
    #1
    1. Advertising

  2. On Sat, 23 May 2009 09:22:59 -0400, Esmail wrote:

    > Hello all,
    >
    > I would like to maximize or minimize a given math function over a
    > specific set of values, in Python preferably.

    ....
    > What it apparently can't do is for maximize (or minimize) functions that
    > contain two variables, x and y, or more.


    Function minimization is a well-studied problem. You will find oodles of
    references to doing function minimization if you google. In fact, if you
    google for "python function minimization" you should find about 63,000
    links.

    Minimizing functions of two variables is difficult, as a general rule.
    Nevertheless, there are tools for doing so. Check out SciPy.



    --
    Steven
     
    Steven D'Aprano, May 23, 2009
    #2
    1. Advertising

  3. Esmail

    Esmail Guest

    Steven D'Aprano wrote:
    > On Sat, 23 May 2009 09:22:59 -0400, Esmail wrote:
    >
    >> Hello all,
    >>
    >> I would like to maximize or minimize a given math function over a
    >> specific set of values, in Python preferably.

    > ...
    >> What it apparently can't do is for maximize (or minimize) functions that
    >> contain two variables, x and y, or more.

    >
    > Function minimization is a well-studied problem. You will find oodles of
    > references to doing function minimization if you google. In fact, if you
    > google for "python function minimization" you should find about 63,000
    > links.


    Hi Steven,

    Right you are (63,700!) ... I don't know what search string I used
    before, but clearly not this rather obvious one .. duh.


    > Minimizing functions of two variables is difficult, as a general rule.
    > Nevertheless, there are tools for doing so. Check out SciPy.


    I will - thanks. I have tools for 1D, I mostly am interested in 2D
    optimization at this point, mostly as a way to verifying some results
    I am calculating.

    Hopefully SciPy will provide a nice simple interface to help me do
    this.

    Thanks,
    Esmail
     
    Esmail, May 23, 2009
    #3
  4. Esmail

    Charlie Guest

    Steven D'Aprano <steve <at> REMOVE-THIS-cybersource.com.au> writes:

    >
    > On Sat, 23 May 2009 09:22:59 -0400, Esmail wrote:
    >
    > > Hello all,
    > >
    > > I would like to maximize or minimize a given math function over a
    > > specific set of values, in Python preferably.

    > ...
    > > What it apparently can't do is for maximize (or minimize) functions that
    > > contain two variables, x and y, or more.



    You might also look at:
    http://pyparasol.sourceforge.net/example_1.html
     
    Charlie, May 24, 2009
    #4
  5. Esmail

    Esmail Guest

    Charlie wrote:
    >
    > You might also look at:
    > http://pyparasol.sourceforge.net/example_1.html


    Thanks for this lead, I had never heard of parasol before. Do you know
    if this also works under Linux? The docs mention only the Windows platform,
    but given that this is Python perhaps it is save to assume this would also
    work under Linux?

    Esmail
     
    Esmail, May 24, 2009
    #5
  6. In message <0033dace$0$9725$>, Steven D'Aprano
    wrote:

    > Minimizing functions of two variables is difficult, as a general rule.
    > Nevertheless, there are tools for doing so. Check out SciPy.


    The name "Marquadt-Levenberg" comes to mind. As I recall, it involved
    finding zeroes of the various partial derivatives.
     
    Lawrence D'Oliveiro, May 25, 2009
    #6
  7. Esmail

    Charlie Guest

    Esmail <ebonak <at> hotmail.com> writes:

    >
    > Charlie wrote:
    > >
    > > You might also look at:
    > > http://pyparasol.sourceforge.net/example_1.html

    >
    > Thanks for this lead, I had never heard of parasol before. Do you know
    > if this also works under Linux? The docs mention only the Windows platform,
    > but given that this is Python perhaps it is save to assume this would also
    > work under Linux?
    >
    > Esmail
    >


    It might work under Linux, however, it was developed under Windows and, to my
    knowledge, has never been tested on a Linux machine. Basic operation only
    depends on installations of matplotlib, numpy, and scipy. Those packages are
    all available on Linux.

    If you try it, I'd like to know the outcome.

    The parasol options to launch Microsoft Office apps Excel, Power Point, and
    Word; or the ray tracing app POV-Ray, will very likely fail.

    Charlie
     
    Charlie, May 25, 2009
    #7
  8. Esmail

    Esmail Guest

    Charlie wrote:
    >
    > It might work under Linux, however, it was developed under Windows and, to my
    > knowledge, has never been tested on a Linux machine. Basic operation only
    > depends on installations of matplotlib, numpy, and scipy. Those packages are
    > all available on Linux.
    >
    > If you try it, I'd like to know the outcome.


    For sure, if I end up trying it I will post a message about my
    results.

    > The parasol options to launch Microsoft Office apps Excel, Power Point, and
    > Word; or the ray tracing app POV-Ray, will very likely fail.


    :)

    thanks again for the information about Parasol,

    Esmail
     
    Esmail, May 26, 2009
    #8
  9. On May 23, 4:22 pm, Esmail <> wrote:
    >
    > Is there some sort of simple Python module that would allow me to
    > evaluate this type of function?
    >
    > In this particular instance I am interested in the minimum of
    >
    >    x * sin(4*x) + 1.1 * sin(2*y), where x,y in range 0-10
    >
    > though in other problems the range may not be identical for x and y.


    Take a look at http://openopt.org (python-scikits-openopt on debian/
    ubuntu) - a lot of optimization engines under one interface, so you
    can try different engines easily and without learning many interfaces.

    >>> import openopt
    >>> from numpy import *

    # openopt passes parameters as single vector.
    >>> def f((x,y)):

    return x * sin(4*x) + 1.1 * sin(2*y)
    # GLP finds global minimum - can be hard, but let's try.
    # I'm not constraining the effort in hope it will converge quickly.
    >>> opt = openopt.GLP(f, lb=array([0, 0]), ub=array([10, 10]))

    # 'galileo' is one of the supported GLP solvers, included out of the
    box.
    >>> sol = opt.solve('galileo', plot=True)

    -----------------------------------------------------
    solver: galileo problem: unnamed
    iter objFunVal
    0 3.966e+00
    10 -6.190e+00
    20 -7.613e+00
    30 -7.613e+00
    35 -7.613e+00
    istop: 11 (Non-Success Number > maxNonSuccess = 15)
    Solver: Time Elapsed = 0.78 CPU Time Elapsed = 0.24
    Plotting: Time Elapsed = 8.04 CPU Time Elapsed = 2.21
    objFunValue: -7.6132332 (feasible, max constraint = 0)
    ### here you need to close the runtime-value graph to continue ###
    >>> sol.ff

    -7.6132331733254421
    >>> sol.xf

    array([ 7.3418726 , 5.44153445])

    That is x=7.3418726, y=5.44153445 gives f=-7.6132331733254421.
    Makes sense?
     
    Beni Cherniavsky, May 31, 2009
    #9
  10. On May 31, 12:41 pm, Beni Cherniavsky <>
    wrote:
    >
    > Take a look athttp://openopt.org(python-scikits-openopt on debian/
    > ubuntu) - a lot of optimization engines under one interface, so you
    > can try different engines easily and without learning many interfaces.
    >

    Correction: don't use the debian package, it uses an old snapshot of
    openopt.
    Install from svn, it's very easy (checkout, setup.py).
     
    Beni Cherniavsky, May 31, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chirs
    Replies:
    18
    Views:
    800
    Chris Uppal
    Mar 2, 2004
  2. AciD_X
    Replies:
    4
    Views:
    8,181
    Jonathan Turkanis
    Apr 1, 2004
  3. Mark Healey
    Replies:
    7
    Views:
    1,570
    Tim Prince
    May 22, 2006
  4. Esmail

    Re: Optimizing math functions

    Esmail, May 24, 2009, in forum: Python
    Replies:
    2
    Views:
    286
    Esmail
    May 24, 2009
  5. VK
    Replies:
    15
    Views:
    1,335
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page