Example 1


Example 1 is a simple function minimization.

This is problem 8 in Chapter 10 of the book "Numerical Methods in Engineering with Python" by Jaan Kiusalaas. The task is to minimize the function F(x,y) where y >= –2

F(x,y) = 6x2 + y3 + xy
    where:      y >= –2

While this example only operates on a simple function, parasol is capable of operating on very large math models with many inputs, outputs, and constraints.

The parasol file begins as shown below, by importing parasol and defining the problem. The ProblemDescription object needs to know the title and author information as well as a description of the input and output variables. (to download the source click here)

The addDesVars statement defines the "x", and "y" variables, their starting values, their parametric ranges, units and a long description. The constraint that y >= –2 is satisfied by using a range for y that starts at y = –2. Here we will look at both x and y over the range -2 to 2.

The addResultVars statement defines the function "F", its units and a long description.

from parasol import *

PrDesc = ProblemDescription(taskName="Minimize F(x,y) with y>=-2",
    subtaskName="F(x,y)=6x^2 + y^3 + xy",
    author="Charlie Taylor")

# design vars have: 
#     name, value, minVal, maxVal, NSteps,  units,  description
    ['x',1.0,-2,2.,50,'','X value'],
    ['y',0.2,-2,2.,50,'','Y value'],

# result variables have: 
#    name,      units,  description 
    ['F','','Function Value'],

The control routine describes the interaction of the input and output variables. It needs to have as input, a parasol object "PS". The current value of "x" and "y" are taken from PS, and the resulting value of "F" is set (A more complex problem would typically import a large math model).

# the following control routine ties together the system result variables
#  with the system design variables
def myControlRoutine(PS):
    # get current values of design variables    
    x,y = PS("x","y")
    # set output variable values
    PS["F"] = 6.*x**2 + y**3 + x*y

The final tasks required for the setup are to give the ProblemDescription object the name of the control routine (myControlRoutine) and to create the parasol object (PS).

# need to tell system the name of the control routine

PS = ParametricSoln(probDesc=PrDesc)

Now that the setup is done, we can run the code. The statements below minimize "F", save a summary, and make some plots to help visualize the results.

# now optimize the system.
minimize(PS, figureOfMerit="F", desVars=[ 'x','y'], MaxLoop=500)

# now save summary of system

makeContourPlot(PS, sysParam="F", desVars=["x","y"])
make2DParametricPlot(PS, sysParam="F", desVar="x", paramVar=["y",-2,-1,0,1,2])

The only remaining task is to tell parasol to finalize the output and close all files.

PS.close()  # finish up with output files

The output from the minimize operation is shown below. It shows that at the starting values for x and y of 1.0 and 0.2, the function value was 6.208. The optimizer improved that to a value of -8.16667 by changing x and y to 0.166669 and -2.0 respectively. The table below can be seen in the parasol output by opening the HTML output file that parasol creates.

ParametricSoln: F(x,y)=6x^2 + y^3 + xy
====================== OPTIMIZATION DESIGN VARIABLES =======================
      name         value        minimum   maximum
         x            1           -2            2 X value
         y          0.2           -2            2 Y value

 Figure of Merit: Function Value (F) = 6.208  <== Minimize
ParametricSoln: F(x,y)=6x^2 + y^3 + xy
====================== OPTIMIZATION DESIGN VARIABLES =======================
      name         value        minimum   maximum
         x     0.166669           -2            2 X value
         y           -2           -2            2 Y value

 Figure of Merit: Function Value (F) = -8.16667  <== Minimize

To verify the above results, a contour plot and a 2D parametric plot were made (click them to enlarge). Those plots are shown below. The contour plot is especially suggestive that the minimizer found a correct minimum.

Excel Screen Shot Matplotlib Plot

To view HTML output from Example 1 click here

Colorized listings thanks to ASPN Recipe at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/442482 by Peter Krantz