# Optimizing math functions

E

#### Esmail

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
-- 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

S

#### Steven D'Aprano

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

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

E

#### Esmail

Steven said:
Function minimization is a well-studied problem. You will find oodles of
references to doing function minimization if you google. In fact, if you

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

E

#### Esmail

Charlie said:

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

L

#### Lawrence D'Oliveiro

Steven D'Aprano said:
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.

C

#### Charlie

Esmail said:
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

E

#### Esmail

Charlie said:
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

B

#### Beni Cherniavsky

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.
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.-----------------------------------------------------
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 ###array([ 7.3418726 , 5.44153445])

That is x=7.3418726, y=5.44153445 gives f=-7.6132331733254421.
Makes sense?

B

#### Beni Cherniavsky

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).