Example #1

Minimize A Function

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) while constraining the value of y to be greater than 2.

f(x,y) = 6*x**2 + y**3 + x*y
    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. (To download the source, click the link below.)

download func_opt_a.py

The ParametricSoln object needs to know the title and author information as well as a description of the input and output variables.

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 (none in this case) and a long description.

from parasol import *

PS = ParametricSoln(subtaskName="F(x,y)=6x^2 + y^3 + xy",
    author="Charlie Taylor", taskName="Minimize F(x,y) with y>=-2",
    constraintTolerance=0.001)

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

# result variables have:
#    name,      units,  description
PS.addResultVars(
    ['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 task required for the setup are to give the ParametricSoln object the name of the control routine (myControlRoutine).

# need to tell system the name of the control routine
PS.setControlRoutine(myControlRoutine)

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
PS.saveFullSummary()

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.

Minimize F(x,y) with y>=-2

Minimize F(x,y) with y>=-2

F(x,y)=6x^2 + y^3 + xy ParametricSoln v0.1.6
by: Charlie Taylor July 31, 2016
PRIOR TO MINIMIZE OPTIMIZATION
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
============================================================================
AFTER MINIMIZE OPTIMIZATION
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
============================================================================
Design Variable Summary
Design Variables (nominal values)
NameValueUnitsDescription
y -2Y value
x 0.166669X value
Result Variables
NameValueUnitsDescriptionLow LimitHigh Limit
F -8.16667Function Value------

Design Variable Summary
Design Variables (nominal values)
NameValueUnitsDescription
y -2Y value
x 0.166669X value
Result Variables
NameValueUnitsDescriptionLow LimitHigh Limit
F -8.16667Function Value------

Parametric Solutions
parasol v0.1.6
contact: C. Taylor, cet@appliedpython.com