CONOPT
Loading...
Searching...
No Matches
tutorial2.py
Go to the documentation of this file.
7
8import os
9import sys
10
11import pyconopt
12
13sys.path.append('../common/')
14import std
15
16
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.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
35 self.addVariable(0.1, pyconopt.CONOPT_INF, 0.5)
36 self.addVariable(0.0, pyconopt.CONOPT_INF)
37 self.addVariable(0.0, pyconopt.CONOPT_INF)
38
39 # adding the constraints to the model
40 self.addConstraint(pyconopt.ConstraintType_Free, -0.1, [0, 1, 2, 3],
41 [-1, -1, 0, 0], [0, 0, 1, 1])
42 self.addConstraint(pyconopt.ConstraintType_Eq, 0.0, [0, 1, 2], [0, 0, -1],
43 [1, 1, 0])
44 self.addConstraint(pyconopt.ConstraintType_Eq, 4.0, [2, 3], [1, 2],
45 [0, 0])
46
47 # setting the objective constraint
48 self.setObjectiveElement(pyconopt.ObjectiveElement_Constraint, 0)
49
50 # setting the optimisation direction
51 self.setOptimizationSense(pyconopt.Sense_Maximize)
52
53 # setting the structure of the second derivative of the Lagrangian
54 self.setSDLagrangianStructure([0, 1, 1, 3], [0, 0, 1, 2])
55
56
57 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
58 """
59 @ingroup PYTHON1THREAD_TUTORIAL2
60 """
61 L = x[0]
62 Inp = x[1]
63 Out = x[2]
64 P = x[3]
65
66 g = 0
67 if rowno == 0:
68 g = P * Out
69 elif rowno == 1:
70 hold1 = (self.Al*pow(L,(-self.Rho)) + self.Ak*pow(self.K,(-self.Rho)) + self.Ainp*pow(Inp,(-self.Rho)))
71 hold2 = pow(hold1,( -1./self.Rho ))
72
73 g = hold2
74
75 return g
76
77 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
78 """
79 @ingroup PYTHON1THREAD_TUTORIAL2
80 """
81 L = x[0]
82 Inp = x[1]
83 Out = x[2]
84 P = x[3]
85
86 jac = []
87 if rowno == 0:
88 jac.append(P)
89 jac.append(Out)
90 elif rowno == 1:
91 hold1 = (self.Al*pow(L,(-self.Rho)) + self.Ak*pow(self.K,(-self.Rho)) + self.Ainp*pow(Inp,(-self.Rho)))
92 hold2 = pow(hold1,( -1./self.Rho ))
93 hold3 = hold2 / hold1
94
95 jac.append(hold3 * self.Al * pow(L ,(-self.Rho-1.)))
96 jac.append(hold3 * self.Ainp * pow(Inp,(-self.Rho-1.)))
97
98 return jac
99
100
101 def evaluateSDLagrangian(self, x, u, hessianrow, hessiancol):
102 """
103 @ingroup PYTHON1THREAD_TUTORIAL2
104 """
105 numhessian = len(hessianrow)
106 hessian = [0 for i in range(numhessian)]
107
108 hessian[3] = u[0]
109 if u[1] != 0.0:
110 L = x[0]
111 Inp = x[1]
112 Out = x[2]
113 P = x[3]
114 hold1 = (self.Al*pow(L,-self.Rho) + self.Ak*pow(self.K,-self.Rho) + self.Ainp*pow(Inp,-self.Rho))
115 hold2 = pow( hold1,-1.0/self.Rho)
116 hold3 = hold2 / hold1
117
118 hold4 = hold3 / hold1 * (-1.0/self.Rho-1.0)
119
120 hessian[0] = hold4 * (-self.Rho) * pow(self.Al*pow(L,-self.Rho-1.0),2) \
121 + hold3 * self.Al * (-self.Rho-1.0)*pow(L,-self.Rho-2.0)
122 hessian[1] = hold4 * (-self.Rho) * (self.Al*pow(L,-self.Rho-1.0)) * \
123 (self.Ainp*pow(Inp,-self.Rho-1.0))
124 hessian[2] = hold4 * (-self.Rho) * \
125 pow(self.Ainp*pow(Inp,-self.Rho-1.0),2) + \
126 hold3 * self.Ainp * (-self.Rho-1.0)*pow(Inp,-self.Rho-2.0)
127
128 for i in range(numhessian):
129 hessian[i] = hessian[i] * u[1]
130
131 return hessian
132
133if __name__ == "__main__":
134 name = os.path.basename(__file__)[:-3]
135
136 conopt = pyconopt.Conopt(name)
137 model = TutModelData()
138 msghdlr = std.TutMessageHandler(name)
139
140 model.buildModel()
141
142 conopt.loadModel(model)
143 conopt.setMessageHandler(msghdlr)
144
145 # getting the license variables
146 license_int_1 = os.environ.get('LICENSE_INT_1', None)
147 license_int_2 = os.environ.get('LICENSE_INT_2', None)
148 license_int_3 = os.environ.get('LICENSE_INT_3', None)
149 license_text = os.environ.get('LICENSE_TEXT', None)
150 if license_int_1 is not None and license_int_2 is not None \
151 and license_int_3 is not None and license_text is not None:
152 conopt.setLicense(int(license_int_1), int(license_int_2),
153 int(license_int_3), license_text)
154
155 coi_error = conopt.solve()
156
157 retcode = std.checkSolve(conopt, 0.572943, coi_error)
158
159 sys.exit(retcode)
setOptimizationSense(self, sense)
sets the optimisation direction.
Definition pyconopt.py:2119
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
Definition pyconopt.py:2111
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
Definition pyconopt.py:2207
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition pyconopt.py:2052
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition pyconopt.py:2011
The Conopt class.
Definition pyconopt.py:1416
A class that can be extended to build and solve a model using Conopt.
Definition pyconopt.py:2391
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:16
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition tutorial2.py:57
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:77
evaluateSDLagrangian(self, x, u, hessianrow, hessiancol)
Computes and returns the numerical values of the Lagrangian of the Hessian.
Definition tutorial2.py:101