CONOPT
Loading...
Searching...
No Matches
pindyck.py
Go to the documentation of this file.
7
8import os
9import sys
10import math
11
12import conopt as co
13
14sys.path.append('../common/')
15import std
16
17
18class PDModelData(co.ModelData):
19 """ """
20
21 def __init__(self):
22 self.CONS_SEQ = 0
23 self.CONS_DREV = 1
24 self.T = 16 # Number of time periods
25 self.Vpp = 7 # Variables per period
26 self.Epp = 6 # Equations per period
27
28 # index arrays for the variables
29 self.vartd = []
30 self.varcs = []
31 self.vars = []
32 self.vard = []
33 self.varr = []
34 self.varp = []
35 self.varrev = []
36
37 self.consmapping = {}
38 super().__init__()
39
40 def buildModel(self):
41 """
42 adding the variables and constraints to the model
43 @ingroup PYTHON1THREAD_PINDYCK
44 """
45
46 demand = [1.0 + 2.3 * pow(1.015, t) for t in range(self.T)]
47
48 # adding the variables to the model
49 for t in range(self.T):
50 # td: Lower=0, Upper=inf, Curr=18
51 varidx = self.addVariable(0, co.Conopt.Infinity, 18)
52 self.vartd.append(varidx)
53
54 # cs: Lower=0, Upper=inf, Curr=7*t
55 varidx = self.addVariable(0, co.Conopt.Infinity, 7 * (t + 1))
56 self.varcs.append(varidx)
57
58 # s: Lower=0, Upper=inf, Curr=7
59 varidx = self.addVariable(0, co.Conopt.Infinity, 7)
60 self.vars.append(varidx)
61
62 # d: Lower=0, Upper=inf, Curr=td-s
63 varidx = self.addVariable(
64 0,
65 co.Conopt.Infinity,
66 self.getVariable(self.vartd[t]).curr
67 - self.getVariable(self.vars[t]).curr,
68 )
69 self.vard.append(varidx)
70
71 # r: Lower=1, Upper=inf, Curr=r(t-1)-d
72 if t > 0:
73 varidx = self.addVariable(
74 1,
75 co.Conopt.Infinity,
76 self.getVariable(self.varr[t - 1]).curr
77 - self.getVariable(self.vard[t]).curr,
78 )
79 else:
80 varidx = self.addVariable(
81 1,
82 co.Conopt.Infinity,
83 500 - self.getVariable(self.vard[t]).curr,
84 )
85 self.varr.append(varidx)
86
87 # p: Lower=1, Upper=inf, Curr=14
88 varidx = self.addVariable(1, co.Conopt.Infinity, 14)
89 self.varp.append(varidx)
90
91 # rev: Lower=-inf, Upper=inf, Curr=0
92 varidx = self.addVariable(-co.Conopt.Infinity, co.Conopt.Infinity)
93 self.varrev.append(varidx)
94
95 # adding the objective
96 objcoeff = [pow(1.05, 1 - (t + 1)) for t in range(self.T)]
97 objnlflag = [0] * self.T
98 objidx = self.addConstraint(
99 co.ConstraintType_Free, 0.0, self.varrev, objcoeff, objnlflag
100 )
101
102 # adding the constraints to the model
103 for t in range(self.T):
104 if t == 0:
105 self.addConstraint(
106 co.ConstraintType_Eq,
107 demand[t] + 0.87 * 18.0,
108 [self.vartd[t], self.varp[t]], # the variables
109 [1.0, 0.13], # the coefficients
110 [0, 0], # nlflags
111 )
112 else:
113 self.addConstraint(
114 co.ConstraintType_Eq,
115 demand[t],
116 [self.vartd[t], self.vartd[t - 1], self.varp[t]],
117 [1.0, -0.87, 0.13],
118 [0, 0, 0],
119 )
120
121 if t == 0:
122 considx = self.addConstraint(
123 co.ConstraintType_Eq,
124 0.75 * 6.5,
125 [self.vars[t], self.varp[t], self.varcs[t]],
126 [1.0, 0.0, 0.0],
127 [0, 1, 1],
128 )
129 else:
130 considx = self.addConstraint(
131 co.ConstraintType_Eq,
132 0.0,
133 [
134 self.vars[t],
135 self.vars[t - 1],
136 self.varp[t],
137 self.varcs[t],
138 ],
139 [1.0, -0.75, 0.0, 0.0],
140 [0, 0, 1, 1],
141 )
142 self.consmapping[considx] = (self.CONS_SEQ, t)
143
144 if t == 0:
145 self.addConstraint(
146 co.ConstraintType_Eq,
147 0.0,
148 [self.varcs[t], self.vars[t]],
149 [1.0, -1.0],
150 [0, 0],
151 )
152 else:
153 self.addConstraint(
154 co.ConstraintType_Eq,
155 0.0,
156 [self.varcs[t], self.varcs[t - 1], self.vars[t]],
157 [1.0, -1.0, -1.0],
158 [0, 0, 0],
159 )
160
161 self.addConstraint(
162 co.ConstraintType_Eq,
163 0.0,
164 [self.vard[t], self.vartd[t], self.vars[t]],
165 [1.0, -1.0, 1.0],
166 [0, 0, 0],
167 )
168
169 if t == 0:
170 self.addConstraint(
171 co.ConstraintType_Eq,
172 500.0,
173 [self.varr[t], self.vard[t]],
174 [1.0, 1.0],
175 [0, 0],
176 )
177 else:
178 self.addConstraint(
179 co.ConstraintType_Eq,
180 0.0,
181 [self.varr[t], self.varr[t - 1], self.vard[t]],
182 [1.0, -1.0, 1.0],
183 [0, 0, 0],
184 )
185
186 considx = self.addConstraint(
187 co.ConstraintType_Eq,
188 0.0,
189 [self.varrev[t], self.vard[t], self.varp[t], self.varr[t]],
190 [1.0, 0.0, 0.0, 0.0],
191 [0, 1, 1, 1],
192 )
193 self.consmapping[considx] = (self.CONS_DREV, t)
194
195 # setting the objective constraint
196 self.setObjectiveElement(co.ObjectiveElement_Constraint, objidx)
197
198 # setting the optimisation direction
199 self.setOptimizationSense(co.Sense_Maximize)
200
201 def evaluateNonlinearTerm(self, x, rowno, ignerr, thread):
202 """
203 @copydoc conopt.ModelData.evaluateNonlinearTerm
204 @ingroup PYTHON1THREAD_PINDYCK
205 """
206 if rowno not in self.consmapping:
207 return
208
209 # unpack consset and time index
210 consset, t = self.consmapping[rowno]
211 assert consset in (self.CONS_SEQ, self.CONS_DREV)
212
213 g = 0
214 if consset == self.CONS_SEQ:
215 h1 = 1.1 + 0.1 * x[self.varp[t]]
216 h2 = pow(1.02, -x[self.varcs[t]] / 7.0)
217 g = -h1 * h2
218
219 elif consset == self.CONS_DREV:
220 g = -x[self.vard[t]] * (x[self.varp[t]] - 250.0 / x[self.varr[t]])
221
222 return g
223
224 def evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread):
225 """
226 @copydoc conopt.ModelData.evaluateNonlinearJacobian
227 @ingroup PYTHON1THREAD_PINDYCK
228 """
229 if rowno not in self.consmapping:
230 return
231
232 # unpack consset and time index
233 consset, t = self.consmapping[rowno]
234 assert consset in (self.CONS_SEQ, self.CONS_DREV)
235
236 jac = []
237 if consset == self.CONS_SEQ:
238 h1 = 1.1 + 0.1 * x[self.varp[t]]
239 h2 = pow(1.02, -x[self.varcs[t]] / 7.0)
240 jac.append(h1 * h2 * math.log(1.02) / 7.0)
241 jac.append(-h2 * 0.1)
242
243 elif consset == self.CONS_DREV:
244 jac.append(-(x[self.varp[t]] - 250.0 / x[self.varr[t]]))
245 jac.append(-x[self.vard[t]] * 250.0 / pow(x[self.varr[t]], 2))
246 jac.append(-x[self.vard[t]])
247
248 return jac
249
250
251if __name__ == '__main__':
252 name = os.path.basename(__file__)[:-3]
253
254 conopt = co.Conopt(name)
255 model = PDModelData()
256 msghdlr = std.TutMessageHandler(name)
257
258 model.buildModel()
259
260 conopt.loadModel(model)
261 conopt.setMessageHandler(msghdlr)
262
263 # getting the license variables
264 license_int_1 = os.environ.get('CONOPT_LICENSE_INT_1', None)
265 license_int_2 = os.environ.get('CONOPT_LICENSE_INT_2', None)
266 license_int_3 = os.environ.get('CONOPT_LICENSE_INT_3', None)
267 license_text = os.environ.get('CONOPT_LICENSE_TEXT', None)
268 if (
269 license_int_1 is not None
270 and license_int_2 is not None
271 and license_int_3 is not None
272 and license_text is not None
273 ):
274 conopt.setLicense(
275 int(license_int_1),
276 int(license_int_2),
277 int(license_int_3),
278 license_text,
279 )
280
281 coi_error = conopt.solve()
282
283 retcode = std.checkSolve(conopt, 1170.486, coi_error, tol=0.001)
284
285 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 pindyck.py:40
evaluateNonlinearTerm(self, x, rowno, ignerr, thread)
callback method for evaluating the nonlinear terms in a given row
Definition pindyck.py:201
evaluateNonlinearJacobian(self, x, rowno, jacnum, ignerr, thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition pindyck.py:224