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