CONOPT
Loading...
Searching...
No Matches
pindyck.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class pindyck {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "pindyck";
19
20 Conopt conopt = new Conopt(name);
21 PindyckModelData model = new PindyckModelData();
23
24 // adding the message handler to the conopt interface
25 conopt.setMessageHandler(msghdlr);
26
27 // building the model
28 model.buildModel();
29
30 // loading the model in the conopt object
31 conopt.loadModel(model);
32
33 // try to set the license using the environment variables
34 try {
35 int license_int_1 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_1"));
36 int license_int_2 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_2"));
37 int license_int_3 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_3"));
38 String license_text = System.getenv("CONOPT_LICENSE_TEXT");
39
42 } catch (Exception e) {
43 System.out.println("Unable to set license: " + e.getMessage());
44 }
45
46 conopt.solve();
47
48 conopt.printStatus();
49
50 std s = new std();
51 int retcode = s.checkSolve(name, conopt.modelStatus(), conopt.solutionStatus(),
52 conopt.objectiveValue(), 1170.486, 0.001);
53
54 msghdlr.close();
55
56 System.exit(retcode);
57 }
58}
59
60class PindyckModelData extends ModelData {
61 static final int CONS_SEQ = 0;
62 static final int CONS_DREV = 1;
63
64 private int T = 16; /* Number of time periods */
65 private int Vpp = 7; /* Variables per period */
66 private int Epp = 6; /* Equations per period */
67
68 /* the demand vector */
69 private double[] demand;
70
71 /* index arrays for the variables */
72 private int[] vartd;
73 private int[] varcs;
74 private int[] vars;
75 private int[] vard;
76 private int[] varr;
77 private int[] varp;
78 private int[] varrev;
79
80 public class IntPair {
81 int first_;
82 int second_;
83
84 public IntPair(int first, int second) {
85 first_ = first;
86 second_ = second;
87 }
88
89 public int first() { return first_; }
90 public int second() { return second_; }
91 };
92
93 private final Map<Integer, IntPair> consmapping = new HashMap<>();
94
95
96 public PindyckModelData() {
97 super();
98 InitializeLists();
99 }
100
101 private void InitializeLists() {
102 demand = new double[T];
103
104 vartd = new int[T];
105 varcs = new int[T];
106 vars = new int[T];
107 vard = new int[T];
108 varr = new int[T];
109 varp = new int[T];
110 varrev = new int[T];
111 }
112
113
118 public void buildModel() {
119 // adding the variables to the model
120
121 // defining the demand for each time period.
122 for (int t = 0; t < T; t++) {
123 demand[t] = 1.0 + 2.3 * Math.pow(1.015, t);
124 }
125
126 // other variables
127 int varidx;
128 for (int t = 0; t < T; t++) {
129 /* td: Lower=0, Upper=inf, Curr=18 */
130 varidx = addVariable(0, Conopt.Infinity, 18);
131 vartd[t] = varidx;
132
133 /* cs: Lower=0, Upper=inf, Curr=7*t */
134 varidx = addVariable(0, Conopt.Infinity, 7 * (t + 1));
135 varcs[t] = varidx;
136
137 /* s: Lower=0, Upper=inf, Curr=7 */
138 varidx = addVariable(0, Conopt.Infinity, 7);
139 vars[t] = varidx;
140
141 /* d: Lower=0, Upper=inf, Curr=td-s */
142 varidx =
143 addVariable(0, Conopt.Infinity, getVariable(vartd[t]).getCurr() - getVariable(vars[t]).getCurr());
144 vard[t] = varidx;
145
146 /* r: Lower=1, Upper=inf, Curr=r(t-1)-d */
147 if (t > 0)
148 varidx = addVariable(
149 1, Conopt.Infinity, getVariable(varr[t - 1]).getCurr() - getVariable(vard[t]).getCurr());
150 else
151 varidx = addVariable(1, Conopt.Infinity, 500 - getVariable(vard[t]).getCurr());
152 varr[t] = varidx;
153
154 /* p: Lower=1, Upper=inf, Curr=14 */
155 varidx = addVariable(1, Conopt.Infinity, 14);
156 varp[t] = varidx;
157
158 /* rev: Lower=-inf, Upper=inf, Curr=0 */
160 varrev[t] = varidx;
161 }
162
163
164 // adding the objective
165 double[] objcoeff = new double[T];
166 int[] objnlflag = new int[T];
167
168 for (int t = 0; t < T; ++t) {
169 objcoeff[t] = Math.pow(1.05, 1 - (t + 1));
170 }
171 int objidx = addConstraint(ConstraintType.Free, 0.0, varrev, objcoeff, objnlflag);
172
173
174 // adding the constraints to the model
175 int considx;
176 for (int t = 0; t < T; t++) {
177
178 // adding the tdeq equations
179 if (t == 0) {
180 {
181 int[] index = {vartd[t], varp[t]}; // the variables
182 double[] value = {1.0, 0.13}; // the coefficients
183 int[] nlflag = {0, 0}; // nlflags
184 addConstraint(ConstraintType.Eq, demand[t] + 0.87 * 18.0, index, value, nlflag);
185 }
186 }
187 else {
188 {
189 int[] index = {vartd[t], vartd[t - 1], varp[t]};
190 double[] value = {1.0, -0.87, 0.13};
191 int[] nlflag = {0, 0, 0};
192 addConstraint(ConstraintType.Eq, demand[t], index, value, nlflag);
193
194 }
195 }
196
197 // adding the seq Equations
198 if (t == 0) {
199 {
200 int[] index = {vars[t], varp[t], varcs[t]};
201 double[] value = {1.0, 0.0, 0.0};
202 int[] nlflag = {0, 1, 1};
203 considx = addConstraint(ConstraintType.Eq, 0.75 * 6.5, index, value, nlflag);
204 }
205 }
206 else {
207 {
208 int[] index = {vars[t], vars[t - 1], varp[t], varcs[t]};
209 double[] value = {1.0, -0.75, 0.0, 0.0};
210 int[] nlflag = {0, 0, 1, 1};
211 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
212 }
213 }
214 consmapping.putIfAbsent(considx, new IntPair(CONS_SEQ, t));
215
216 // adding the cseq Equations
217 if (t == 0) {
218 {
219 int[] index = {varcs[t], vars[t]};
220 double[] value = {1.0, -1.0};
221 int[] nlflag = {0, 0};
222 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
223 }
224 }
225 else {
226 {
227 int[] index = {varcs[t], varcs[t - 1], vars[t]};
228 double[] value = {1.0, -1.0, -1.0};
229 int[] nlflag = {0, 0, 0};
230 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
231 }
232 }
233
234 // adding the deq equations
235 {
236 int[] index = {vard[t], vartd[t], vars[t]};
237 double[] value = {1.0, -1.0, 1.0};
238 int[] nlflag = {0, 0, 0};
239 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
240 }
241
242 // adding the req equations
243 if (t == 0) {
244 {
245 int[] index = {varr[t], vard[t]};
246 double[] value = {1.0, 1.0};
247 int[] nlflag = {0, 0};
248 addConstraint(ConstraintType.Eq, 500.0, index, value, nlflag);
249 }
250 }
251 else {
252 {
253 int[] index = {varr[t], varr[t - 1], vard[t]};
254 double[] value = {1.0, -1.0, 1.0};
255 int[] nlflag = {0, 0, 0};
256 addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
257 }
258 }
259
260 // adding the drev equations
261 {
262 int[] index = {varrev[t], vard[t], varp[t], varr[t]};
263 double[] value = {1.0, 0.0, 0.0, 0.0};
264 int[] nlflag = {0, 1, 1, 1};
265 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
266 }
267 consmapping.putIfAbsent(considx, new IntPair(CONS_DREV, t));
268 }
269
270 // setting the objective constraint
272
273 // setting the optimisation direction
275 }
276
281 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
282 IntPair conspair = consmapping.get(rowno);
283 if (conspair == null) return 0;
284
285 int consset = conspair.first();
286 int t = conspair.second();
287
288 assert (consset == CONS_SEQ || consset == CONS_DREV);
289 double g = 0;
290 if (consset == CONS_SEQ) {
291 // seq equation. Nonlinear term = -(1.1+0.1*p(t))*1.02**(-cs(t)/7)
292 double h1, h2;
293
294 h1 = (1.1 + 0.1 * x[varp[t]]);
295 h2 = Math.pow(1.02, -x[varcs[t]] / 7.0);
296 g = -h1 * h2;
297 }
298 else if (consset == CONS_DREV) {
299 g = -x[vard[t]] * (x[varp[t]] - 250. / x[varr[t]]);
300 }
301
302 return g;
303 }
304
309 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
310 assert x.length == jac.length;
311
312 IntPair conspair = consmapping.get(rowno);
313 if (conspair == null) return;
314
315 int consset = conspair.first();
316 int t = conspair.second();
317
318 assert (consset == CONS_SEQ || consset == CONS_DREV);
319
320 if (consset == CONS_SEQ) {
321 // seq equation. Nonlinear term = -(1.1+0.1*p(t))*1.02**(-cs(t)/7)
322 double h1, h2;
323
324 h1 = (1.1 + 0.1 * x[varp[t]]);
325 h2 = Math.pow(1.02, -x[varcs[t]] / 7.0);
326
327 jac[varcs[t]] = h1 * h2 * Math.log(1.02) / 7.0;
328 jac[varp[t]] = -h2 * 0.1;
329 }
330 else if (consset == CONS_DREV) {
331 jac[vard[t]] = -(x[varp[t]] - 250. / x[varr[t]]);
332 jac[varr[t]] = -x[vard[t]] * 250. / Math.pow(x[varr[t]], 2);
333 jac[varp[t]] = -x[vard[t]];
334 }
335 }
336}
IntPair(int first, int second)
Definition pindyck.java:84
The Conopt class.
Definition conopt.py:1380
static final ConstraintType Eq
static final ConstraintType Free
A class that can be extended to build and solve a model using Conopt.
Definition conopt.py:2407
static final ObjectiveElement Constraint
static final Sense Maximize
Definition Sense.java:29
static void main(String argv[])
Definition pindyck.java:15
license_int_1
Definition pindyck.py:264
license_int_2
Definition pindyck.py:265
license_int_3
Definition pindyck.py:266
license_text
Definition pindyck.py:267
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
addConstraint(self, *args)
Overload 1: adds a constraint to the problem.
Definition conopt.py:2621
setObjectiveElement(self, elem, elemindex)
sets the index for the objective variable or constraint
Definition conopt.py:2766
addVariable(self, *args)
Overload 1: adds a variable to the model.
Definition conopt.py:2677
setOptimizationSense(self, sense)
sets the optimisation direction.
Definition conopt.py:2775
void buildModel()
adds variables and constraints to the model
Definition pindyck.java:118
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
Definition pindyck.java:281
void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread)
callback method for evaluating the jacobian for the nonlinear terms in a given row
Definition pindyck.java:309
getVariable(self, *args)
Overload 1: returns a reference to the variable object
Definition conopt.py:2844
Definition std.py:1