CONOPT
Loading...
Searching...
No Matches
tutorial.py
Go to the documentation of this file.
1
8
9import sys
10import os
11
12import conoptpy
13import adolc
14
15sys.path.append('../../common/')
16import std
17
18class TutModelData(conoptpy.ModelData):
19 def __init__(self):
20 self.Al = 0.16
21 self.Ak = 2.0
22 self.Ainp = 0.16
23 self.Rho = 1.0
24 self.K = 4.0
25 super().__init__()
26
27 def buildModel(self):
28 """
29 adding the variables and constraints to the model
30 @ingroup PYTHON1THREAD_AD_TUTORIAL
31 """
32 # adding the variables to the model
33 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
34 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
35 self.addVariable(0.0, conoptpy.CONOPT_INF)
36 self.addVariable(0.0, conoptpy.CONOPT_INF)
37
38 # adding the constraints to the model
39 self.addConstraint(conoptpy.ConstraintType_Free, -0.1, [0, 1, 2, 3],
40 [-1, -1, 0, 0], [0, 0, 1, 1])
41 self.addConstraint(conoptpy.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
42 [1, 1, 0])
43 self.addConstraint(conoptpy.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
44 [0, 0])
45
46 # setting the objective constraint
47 self.setObjectiveElement(conoptpy.ObjectiveElement_Constraint, 0)
48
49 # setting the optimisation direction
50 self.setOptimizationSense(conoptpy.Sense_Maximize)
51
52 # initialising the automatic differentiation
54
55
56 def tapeFunction(self, x, rowno):
57 """
58 @ingroup PYTHON1THREAD_AD_TUTORIAL
59 evaluates the nonlinear function and records a tape is necessary
60
61 @param x current point to be evaluated
62 @param rowno the index of the constraint. This is also used for the trace tag.
63 """
64 if rowno == 2:
65 return
66
67 adolc.trace_on(rowno)
68 ax = adolc.as_adouble(x)
69
70 # marking the x variables as independent
71 for item in iter(ax):
72 item.declareIndependent()
73
74 L = ax[0]
75 Inp = ax[1]
76 Out = ax[2]
77 P = ax[3]
78
79 if rowno == 0:
80 ay = P * Out
81 elif rowno == 1:
82 hold1 = (self.Al*pow(L,(-self.Rho)) + self.Ak*pow(self.K,(-self.Rho)) + self.Ainp*pow(Inp,(-self.Rho)))
83 hold2 = pow(hold1,( -1./self.Rho ))
84
85 ay = hold2
86
87 ay.declareDependent()
88 adolc.trace_off()
89
90
92 """
93 @ingroup PYTHON1THREAD_AD_TUTORIAL
94 initialises the automatic differentiation
95 """
96 x = []
97 for v in range(self.numVar()):
98 x.append(self.getVariable(v).curr)
99
100 for c in range(self.numCons()):
101 self.tapeFunction(x, c)
102
103
104 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
105 """
106 @ingroup PYTHON1THREAD_AD_TUTORIAL
107 """
108 try:
109 g = adolc.function(rowno, x)[0]
110 except adolc.BranchException:
111 self.tapeFunction(x, rowno)
112 g = adolc.function(rowno, x)[0]
113
114 return g
115
116
117 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
118 """
119 @ingroup PYTHON1THREAD_AD_TUTORIAL
120 """
121 jac = []
122 try:
123 jacres = adolc.gradient(rowno, x)
124 except adolc.BranchException:
125 self.tapeFunction(x, rowno)
126 jacres = adolc.gradient(rowno, x)
127
128 for i in jacnum:
129 jac.append(jacres[i])
130
131 return jac
132
133
134if __name__ == "__main__":
135 name = os.path.basename(__file__)[:-3]
136
137 conopt = conoptpy.Conopt(name)
138 model = TutModelData()
139 msghdlr = std.TutMessageHandler(name)
140
141 model.buildModel()
142
143 conopt.loadModel(model)
144 conopt.setMessageHandler(msghdlr)
145
146 # getting the license variables
147 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
148 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
149 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
150 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
151 if license_int_1 is not None and license_int_2 is not None \
152 and license_int_3 is not None and license_text is not None:
153 conopt.setLicense(int(license_int_1), int(license_int_2),
154 int(license_int_3), license_text)
155
156 coi_error = conopt.solve()
157
158 retcode = std.checkSolve(conopt, 0.572943, coi_error)
159
160 sys.exit(retcode)
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
tapeFunction(self, x, rowno)
evaluates the nonlinear function and records a tape is necessary
Definition tutorial.py:56
initialiseAutoDiff(self)
initialises the automatic differentiation
Definition tutorial.py:91
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
Definition tutorial.py:51
buildModel(self)
adding the variables and constraints to the model
Definition tutorial.py:25
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
Definition tutorial.py:71