CONOPT
Loading...
Searching...
No Matches
tutorial2.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import conopt as co
12
13sys.path.append('../common/')
14import std
15
16
17class TutModelData(co.ModelData):
18 """ """
19
20 def __init__(self):
21 self.Al = 0.16
22 self.Ak = 2.0
23 self.Ainp = 0.16
24 self.Rho = 1.0
25 self.K = 4.0
26 super().__init__()
27
28 def buildModel(self):
29 """
30 adding the variables and constraints to the model
31 @ingroup PYTHON1THREAD_TUTORIAL2
32 """
33 # adding the variables to the model
34 self.varl = self.addVariable(0.1, co.Conopt.Infinity, 0.5)
35 self.varinp = self.addVariable(0.1, co.Conopt.Infinity, 0.5)
36 self.varout = self.addVariable(0.0, co.Conopt.Infinity)
37 self.varp = self.addVariable(0.0, co.Conopt.Infinity)
38
39 # adding the constraints to the model
40 self.consobj = self.addConstraint(
41 co.ConstraintType_Free,
42 -0.1,
43 [self.varl, self.varinp, self.varout, self.varp],
44 [-1, -1, 0, 0],
45 [0, 0, 1, 1],
46 )
47 self.consprod = self.addConstraint(
48 co.ConstraintType_Eq,
49 0.0,
50 [self.varl, self.varinp, self.varout],
51 [0, 0, -1],
52 [1, 1, 0],
53 )
54 self.addConstraint(
55 co.ConstraintType_Eq, 4.0, [self.varout, self.varp], [1, 2], [0, 0]
56 )
57
58 # setting the objective constraint
59 self.setObjectiveElement(co.ObjectiveElement_Constraint, 0)
60
61 # setting the optimisation direction
62 self.setOptimizationSense(co.Sense_Maximize)
63
64 # setting the structure of the second derivative of the Lagrangian
65 self.setSDLagrangianStructure([0, 1, 1, 3], [0, 0, 1, 2])
66
67 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
68 """
69 @copydoc conopt.ModelData.evaluateNonlinearTerm
70 @ingroup PYTHON1THREAD_TUTORIAL2
71 """
72 L = x[self.varl]
73 Inp = x[self.varinp]
74 Out = x[self.varout]
75 P = x[self.varp]
76
77 g = 0
78 if rowno == self.consobj:
79 g = P * Out
80 elif rowno == self.consprod:
81 hold1 = (
82 self.Al * pow(L, (-self.Rho))
83 + self.Ak * pow(self.K, (-self.Rho))
84 + self.Ainp * pow(Inp, (-self.Rho))
85 )
86 hold2 = pow(hold1, (-1.0 / self.Rho))
87
88 g = hold2
89
90 return g
91
92 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
93 """
94 @copydoc conopt.ModelData.evaluateNonlinearJacobian
95
96 NOTE: The jacobian is returned as a list of length jacnum. In this
97 example, the returned list is constructed using `append`. It is also
98 possible to initially create a list of length jacnum containing only 0s,
99 then update the values by the variable indices.
100
101 @ingroup PYTHON1THREAD_TUTORIAL2
102 """
103 L = x[self.varl]
104 Inp = x[self.varinp]
105 Out = x[self.varout]
106 P = x[self.varp]
107
108 jac = []
109 if rowno == self.consobj:
110 jac.append(P)
111 jac.append(Out)
112 elif rowno == self.consprod:
113 hold1 = (
114 self.Al * pow(L, (-self.Rho))
115 + self.Ak * pow(self.K, (-self.Rho))
116 + self.Ainp * pow(Inp, (-self.Rho))
117 )
118 hold2 = pow(hold1, (-1.0 / self.Rho))
119 hold3 = hold2 / hold1
120
121 jac.append(hold3 * self.Al * pow(L, (-self.Rho - 1.0)))
122 jac.append(hold3 * self.Ainp * pow(Inp, (-self.Rho - 1.0)))
123
124 return jac
125
126 def evaluateSDLagrangian(self, x, u, hessianrow, hessiancol):
127 """
128 @copydoc conopt.ModelData.evaluateSDLagrangian
129 @ingroup PYTHON1THREAD_TUTORIAL2
130 """
131 numhessian = len(hessianrow)
132 hessian = [0 for i in range(numhessian)]
133
134 hessian[3] = u[self.consobj]
135 if u[self.consprod] != 0.0:
136 L = x[self.varl]
137 Inp = x[self.varinp]
138 Out = x[self.varout]
139 P = x[self.varp]
140 hold1 = (
141 self.Al * pow(L, -self.Rho)
142 + self.Ak * pow(self.K, -self.Rho)
143 + self.Ainp * pow(Inp, -self.Rho)
144 )
145 hold2 = pow(hold1, -1.0 / self.Rho)
146 hold3 = hold2 / hold1
147
148 hold4 = hold3 / hold1 * (-1.0 / self.Rho - 1.0)
149
150 hessian[0] = hold4 * (-self.Rho) * pow(
151 self.Al * pow(L, -self.Rho - 1.0), 2
152 ) + hold3 * self.Al * (-self.Rho - 1.0) * pow(L, -self.Rho - 2.0)
153 hessian[1] = (
154 hold4
155 * (-self.Rho)
156 * (self.Al * pow(L, -self.Rho - 1.0))
157 * (self.Ainp * pow(Inp, -self.Rho - 1.0))
158 )
159 hessian[2] = hold4 * (-self.Rho) * pow(
160 self.Ainp * pow(Inp, -self.Rho - 1.0), 2
161 ) + hold3 * self.Ainp * (-self.Rho - 1.0) * pow(Inp, -self.Rho - 2.0)
162
163 for i in range(numhessian):
164 hessian[i] = hessian[i] * u[self.consprod]
165
166 return hessian
167
168
169if __name__ == '__main__':
170 name = os.path.basename(__file__)[:-3]
171
172 conopt = co.Conopt(name)
173 model = TutModelData()
174 msghdlr = std.TutMessageHandler(name)
175
176 model.buildModel()
177
178 conopt.loadModel(model)
179 conopt.setMessageHandler(msghdlr)
180
181 # getting the license variables
182 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
183 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
184 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
185 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
186 if (
187 license_int_1 is not None
188 and license_int_2 is not None
189 and license_int_3 is not None
190 and license_text is not None
191 ):
192 conopt.setLicense(
193 int(license_int_1),
194 int(license_int_2),
195 int(license_int_3),
196 license_text,
197 )
198
199 coi_error = conopt.solve()
200
201 retcode = std.checkSolve(conopt, 0.572943, coi_error)
202
203 sys.exit(retcode)
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutorial2.py:67
buildModel(self)
adding the variables and constraints to the model
Definition tutorial2.py:28
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition tutorial2.py:92
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.
Definition tutorial2.py:126