Example #2¶
Minimize A Constrained Function¶
This is problem 6 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 x+y <= 1 and x >= 0.6
f(x,y) = (x-1)2 + (y-1)2
where: x+y <= 1
and: x >= 0.6
The parasol file begins very much as in Example 1, except that there is a new, second result variable called “sumXY” with a limit placed on it.
The setResultVariableLimits statement sets a high limit of 1.0 on “sumXY” in order to satisfy the x+y <= 1 constraint.
The parasol file begins as shown below, by importing parasol and defining the problem.
from parasol import *
PS = ParametricSoln(taskName="Minimize F(x,y) with x>=0.6 and x+y<=1",
subtaskName="F(x,y)=(x-1)^2 + (y-1)^2",
author="Charlie Taylor",
constraintTolerance=0.001)
# design vars have:
# name, value, minVal, maxVal, NSteps, units, description
PS.addDesVars(
['x',0.6,.6,1.,50,'','X value'],
['y',0.2,-0.2,0.4,50,'','Y value'],
)
# result variables have:
# name, units, description
PS.addResultVars(
['F','','Function Value'],
['sumXY','','Sum of X and Y'],
)
PS.setResultVariableLimits(name="sumXY", hiLimit=1.)
The control routine is much like Example 1 except there is a second result variable to assign, “sumXY”.
# 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"] = (x-1.)**2 + (y-1.)**2
PS["sumXY"] = 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)
Again like Example 1, the statements below minimize “F”, save a summary, and make some plots to help visualize the results.
This example also introduces a carpet plot to display results.
# now optimize the system.
minimize(PS, figureOfMerit="F", desVars=[ 'x','y'], MaxLoop=500)
make2DParametricPlot(PS, sysParam="F", desVar="x",
paramVar=["y",0.0,.1,.2,.3,.4,] ,makeHTML=1, dpi=70, linewidth=2,
ptData=None, ptLegend='', logX=0, logY=0)
makeCarpetPlot(PS, sysParam="F", xResultVar="sumXY",
desVarL=[["x",0.6,0.8,1.0],["y",0.0,0.2,0.4]], angDesVarL=[40,0])
makeContourPlot(PS, sysParam="F", desVars=["x","y"])
# now save summary of system
PS.saveFullSummary()
The only remaining task is to tell parasol to close all files.
PS.close() # finish up with output files
The output from the minimize operation is shown below.
Minimize F(x,y) with x>=0.6 and x+y<=1 | |
F(x,y)=(x-1)^2 + (y-1)^2 | ParametricSoln v0.1.7 |
by: Charlie Taylor | August 01, 2016 |
PRIOR TO MINIMIZE OPTIMIZATION |
---|
ParametricSoln: F(x,y)=(x-1)^2 + (y-1)^2 ====================== OPTIMIZATION DESIGN VARIABLES ======================= name value minimum maximum x 0.6 0.6 1 X value y 0.2 -0.2 0.4 Y value Figure of Merit: Function Value (F) = 0.8 <== Minimize ========================== OPTIMIZATION CONSTRAINTS ========================= name value minimum maximum sumXY 0.8 ------------ 1 Sum of X and Y ============================================================================ |
AFTER MINIMIZE OPTIMIZATION |
---|
ParametricSoln: F(x,y)=(x-1)^2 + (y-1)^2 ====================== OPTIMIZATION DESIGN VARIABLES ======================= name value minimum maximum x 0.6 0.6 1 X value y 0.4 -0.2 0.4 Y value Figure of Merit: Function Value (F) = 0.52 <== Minimize ========================== OPTIMIZATION CONSTRAINTS ========================= name value minimum maximum sumXY 1 ------------ 1 Sum of X and Y ============================================================================ |
Design Variable Summary | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Design Variable Summary | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Parametric Solutions parasol v0.1.7 contact: C. Taylor, cet@appliedpython.com |