13sys.path.append(
'../../common/')
17class TutModelData(conoptpy.ModelData):
28 adding the variables and constraints to the model
29 @ingroup PYTHON1THREAD_AD_TUTORIAL2
32 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
33 self.addVariable(0.1, conoptpy.CONOPT_INF, 0.5)
34 self.addVariable(0.0, conoptpy.CONOPT_INF)
35 self.addVariable(0.0, conoptpy.CONOPT_INF)
38 self.addConstraint(conoptpy.ConstraintType_Free, -0.1, [0, 1, 2, 3],
39 [-1, -1, 0, 0], [0, 0, 1, 1])
40 self.addConstraint(conoptpy.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
42 self.addConstraint(conoptpy.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
46 self.setObjectiveElement(conoptpy.ObjectiveElement_Constraint, 0)
49 self.setOptimizationSense(conoptpy.Sense_Maximize)
52 self.setSDLagrangianStructure([0, 1, 1, 3], [0, 0, 1, 2])
60 @ingroup PYTHON1THREAD_AD_TUTORIAL2
61 evaluates the nonlinear function and records a tape is necessary
63 @param x current point to be evaluated
64 @param rowno the index of the constraint. This is also used for the trace tag.
67 ax = adolc.as_adouble(x)
71 item.declareIndependent()
78 ay = adolc.as_adouble(0)
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 ))
93 @ingroup PYTHON1THREAD_AD_TUTORIAL2
94 initialises the automatic differentiation
97 for v
in range(self.numVar()):
98 x.append(self.getVariable(v).curr)
100 for c
in range(self.numCons()):
106 @ingroup PYTHON1THREAD_AD_TUTORIAL2
109 g = adolc.function(rowno, x)[0]
110 except adolc.BranchException:
112 g = adolc.function(rowno, x)[0]
119 @ingroup PYTHON1THREAD_AD_TUTORIAL2
123 jacres = adolc.gradient(rowno, x)
124 except adolc.BranchException:
126 jacres = adolc.gradient(rowno, x)
129 jac.append(jacres[i])
136 @ingroup PYTHON1THREAD_AD_TUTORIAL2
138 numhessian = len(hessianrow)
139 hessian = [0
for i
in range(numhessian)]
141 for c
in range(self.numCons()):
143 hessres = adolc.hessian(c, x)
144 except adolc.BranchException:
146 hessres = adolc.hessian(c, x)
148 for i
in range(numhessian):
149 hessian[i] += u[c]*hessres[hessianrow[i]][hessiancol[i]]
154if __name__ ==
"__main__":
155 name = os.path.basename(__file__)[:-3]
157 conopt = conoptpy.Conopt(name)
163 conopt.loadModel(model)
164 conopt.setMessageHandler(msghdlr)
167 license_int_1 = os.environ.get(
'CONOPT_LICENSE_INT_1',
None)
168 license_int_2 = os.environ.get(
'CONOPT_LICENSE_INT_2',
None)
169 license_int_3 = os.environ.get(
'CONOPT_LICENSE_INT_3',
None)
170 license_text = os.environ.get(
'CONOPT_LICENSE_TEXT',
None)
171 if license_int_1
is not None and license_int_2
is not None \
172 and license_int_3
is not None and license_text
is not None:
173 conopt.setLicense(int(license_int_1), int(license_int_2),
174 int(license_int_3), license_text)
176 coi_error = conopt.solve()
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
initialiseAutoDiff(self)
initialises the automatic differentiation
tapeFunction(self, x, rowno)
evaluates the nonlinear function and records a tape is necessary
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
buildModel(self)
adding the variables and constraints to the model
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.