box_constrained.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from prism import *
from math import *

markdown_desc = """
This example is taken from the [ParaSol](http://parasol.readthedocs.io/en/latest/index.html) program's 
[Example #3](http://parasol.readthedocs.io/en/latest/example3.html)

The purpose of this example is to maximize the volume of an open topped box starting with a rectangle of cardboard measuring (10 x 20).

There is a constraint on the optimization, however, that the final resulting open topped box cannto exceed an L/W of 2.4.

The image below shows the basic cardboard shape, after the (h x h) squares have been removed from all four corners of the original rectangle.

<img src="./box.jpg" width="100" />
"""


# create system object (make sure author is correct... it's used for report)
S = SysModel(name="Box Design", type="misc", 
    author="C.N. Tainer", programName="Simple Optimization",
    markdown_desc=markdown_desc)


# add design variables to the system (these variables may be used to
# optimize the system or to create plots)
# design vars have: 
#     name, value, minVal, maxVal, step,  units,  description
S.addDesVars(
    ['Lmatl',20,10,30,1,'in','Length of Material'],
    ['Wmatl',10,8,12,0.2,'in','Width of Material'],
    ['hbox',1,0.2,3.8,0.18,'in','Height of Box'],
    )


# now add any Result Variables That might be plotted
# result variables have: 
#    name,      units,  description
S.addResultVars(
    ['Volume','cuin','Box Volume'],
    ['boxMass','lbm','Box Mass'],
    ['LoverW','','Box L/W Ratio', '<', 2.4], # constrain L/W
    )


SimpleEqnMass_1 = SimpleEqnMass(name="Simple Mass", type="inert",  mass_lbm=0.0,  eqn="1.2*1.1", desc='mass = simple eqn')


#=====  after they have been created, add the Mass Items to the system object ====
S.addMassItem( SimpleEqnMass_1 )



# the following control routine ties together the system components
#  with the system design variables
def myControlRoutine(S):
    # get current values of design variables    
    Lmatl,Wmatl,hbox = S("Lmatl","Wmatl","hbox")

    S.reCalc()

    # cut a Wmatl x Lmatl sheet to make a box.
    Volume = (Wmatl-2*hbox)*(Lmatl-2*hbox)*hbox
    
    boxMass = Wmatl*Lmatl - 4*hbox**2
    
    S['LoverW'] = (Lmatl-2*hbox) / (Wmatl-2*hbox)

    # "boxMass" is required
    S["boxMass"] = boxMass
    S["Volume"] = Volume

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

S.reCalcItems()



# now optimize the system... it should match up with the carpet plots.
optimize(S, figureOfMerit="Volume", desVars=['hbox'], findmin=0, useCOBYLA=0)

makeSensitivityPlot(S, 
    figureOfMerit="Volume", desVars=['hbox'],
    makeHTML=1, dpi=70, linewidth=2)

make2DPlot(S, sysParam="LoverW", desVar="hbox")


# now save summary of system
S.saveFullSummary()

# Be sure to wrap-up any files
S.close()