17sys.path.append(
'../../common/')
32 adding the variables and constraints to the model
33 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
36 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
37 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
38 self.addVariable(0.0, conoptpy.CONOPT_INF)
39 self.addVariable(0.0, conoptpy.CONOPT_INF)
42 self.addConstraint(conoptpy.ConstraintType_Free, -0.1, [0, 1, 2, 3],
43 [-1, -1, 0, 0], [0, 0, 1, 1])
44 self.addConstraint(conoptpy.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
46 self.addConstraint(conoptpy.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
50 self.setObjectiveElement(conoptpy.ObjectiveElement_Constraint, 0)
53 self.setOptimizationSense(conoptpy.Sense_Maximize)
64 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
65 evaluates the nonlinear function and records a tape is necessary
67 @param x current point to be evaluated
68 @param rowno the index of the constraint. This is also used for the trace tag.
71 ax = adolc.as_adouble(x)
75 item.declareIndependent()
82 ay = adolc.as_adouble(0)
86 hold1 = (self.
Al*pow(L,(-self.
Rho)) + self.
Ak*pow(self.
K,(-self.
Rho)) + self.
Ainp*pow(Inp,(-self.
Rho)))
87 hold2 = pow(hold1,( -1./self.
Rho ))
97 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
98 initialises the automatic differentiation
101 for v
in range(self.numVar()):
102 x.append(self.getVariable(v).curr)
104 for c
in range(self.numCons()):
110 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
111 uses the automatic differentiation methods to compute the Hessian
115 for v
in range(self.numVar()):
116 x.append(self.getVariable(v).curr)
119 for c
in range(self.numCons()):
120 hesspat = adolc.hess_pat(c, x, 0)
123 for i, n
in enumerate(hesspat[0]):
126 if i >= hesspat[1][index + j]:
127 if i
not in hessstr.keys():
129 hessstr[i].append(hesspat[1][index + j])
133 rowindex = [int(r)
for r
in range(self.numVar())
for _
in hessstr.get(r, [])]
134 colindex = [int(c)
for r
in range(self.numVar())
for c
in hessstr.get(r, [])]
137 self.setSDLagrangianStructure(rowindex, colindex)
142 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
145 g = adolc.function(rowno, x)[0]
146 except adolc.BranchException:
148 g = adolc.function(rowno, x)[0]
155 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
159 jacres = adolc.gradient(rowno, x)
160 except adolc.BranchException:
162 jacres = adolc.gradient(rowno, x)
165 jac.append(jacres[i])
172 @ingroup PYTHON1THREAD_AD_TUTORIAL2STR
174 numhessian = len(hessianrow)
175 hessian = [0
for i
in range(numhessian)]
177 for c
in range(self.numCons()):
179 hessres = adolc.hessian(c, x)
180 except adolc.BranchException:
182 hessres = adolc.hessian(c, x)
184 for i
in range(numhessian):
185 hessian[i] += u[c]*hessres[hessianrow[i]][hessiancol[i]]
189if __name__ ==
"__main__":
190 name = os.path.basename(__file__)[:-3]
192 conopt = conoptpy.Conopt(name)
198 conopt.loadModel(model)
199 conopt.setMessageHandler(msghdlr)
202 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
203 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
204 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
205 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
206 if license_int_1
is not None and license_int_2
is not None \
207 and license_int_3
is not None and license_text
is not None:
208 conopt.setLicense(int(license_int_1), int(license_int_2),
209 int(license_int_3), license_text)
211 coi_error = conopt.solve()
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
buildModel(self)
adding the variables and constraints to the model
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
initialiseAutoDiff(self)
initialises the automatic differentiation
tapeFunction(self, x, rowno)
evaluates the nonlinear function and records a tape is necessary
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
computeHessianStructure(self)
uses the automatic differentiation methods to compute the Hessian structure