CONOPT
Loading...
Searching...
No Matches
pinadd2err.java
Go to the documentation of this file.
1
7
8import java.util.*;
9import java.lang.Math;
10import conopt.*;
11
14public class pinadd2err {
15 public static void main(String argv[]) {
16 System.loadLibrary("conoptjni4");
17
18 String name = "pinadd2err";
19
20 Conopt conopt = new Conopt(name);
22
23 // adding the message handler to the conopt interface
24 conopt.setMessageHandler(msghdlr);
25
26 // Turn debugging of FDEval on or off: -1 means first call only
27 conopt.debugFV(-1);
28
29 // try to set the license using the environment variables
30 try {
31 int license_int_1 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_1"));
32 int license_int_2 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_2"));
33 int license_int_3 = Integer.parseInt(System.getenv("CONOPT_LICENSE_INT_3"));
34 String license_text = System.getenv("CONOPT_LICENSE_TEXT");
35
38 } catch (Exception e) {
39 System.out.println("Unable to set license: " + e.getMessage());
40 }
41
42 std s = new std();
43 int retcode = 0;
44 int coi_error = 0;
45 double[] expectedSols = {1225.8643407378518, 1280.284354018754, 1333.7424507050348, 1386.2354828262864};
46
47 for (int i = 16; i <= 20; i++) {
48 PinAdd2ErrModelData model = new PinAdd2ErrModelData();
49
50 double[] xkeep = conopt.getVariableValues();
51 int[] xstat = conopt.getVariableStatus();
52 int[] estat = conopt.getConstraintStatus();
53
54 // building the model
55 model.buildModel(i, xkeep, xstat, estat);
56
57 // loading the model in the conopt object
58 conopt.loadModel(model);
59
60 if (i == 16) {
67 conopt.debug2D(-1);
68 coi_error = conopt.solve();
69
70 if (coi_error != 0) {
71 s.java_log(name, "Errors encountered during first solve");
72 retcode = -1;
73 }
74
75 else if (conopt.modelStatus() < 6 || conopt.modelStatus() > 7 ||
76 conopt.solutionStatus() != 5) {
77 s.java_log(name, "Incorrect Model or Solver Status during first solve");
78 retcode = -1;
79 };
80
81 conopt.printStatus();
82 }
83 else {
84 /* Turn debugging of 2nd derivative off again. */
85 /* We should expect a proper optimal solution even with an incorrect */
86 /* 2DLagr routine so we test for (local) optimality after the solve. */
87 conopt.debug2D(0);
88
89 conopt.solve();
90
91 retcode = s.checkSolve(name, conopt.modelStatus(), conopt.solutionStatus(),
92 conopt.objectiveValue(), expectedSols[i - 17], 0.001);
93
94 conopt.printStatus();
95 }
96 }
97 msghdlr.close();
98 System.exit(retcode);
99 }
100}
101
102class PinAdd2ErrModelData extends ModelData {
103 static final int CONS_SEQ = 0;
104 static final int CONS_DREV = 1;
105
106 private int T = 0;
107 private int Vpp = 7; // Variables per period
108 private int Epp = 6; // Equations per period
109
110 // the demand vector
111 private double[] demand;
112
113 // index arrays for the variables
114 private int[] vartd;
115 private int[] varcs;
116 private int[] vars;
117 private int[] vard;
118 private int[] varr;
119 private int[] varp;
120 private int[] varrev;
121
122 // index arrays for the constraints
123 private int[] consttdeq;
124 private int[] constseq;
125 private int[] constcseq;
126 private int[] constdeq;
127 private int[] constreq;
128 private int[] constdrev;
129
130 private int[] hessianstart;
131
132 public class IntPair {
133 int first_;
134 int second_;
135
136 public IntPair(int first, int second) {
137 first_ = first;
138 second_ = second;
139 }
140
141 public int first() { return first_; }
142 public int second() { return second_; }
143 };
144
145 private final Map<Integer, IntPair> consmapping = new HashMap<>();
146
147
148 public PinAdd2ErrModelData() {
149 super();
150 }
151
156 public void buildModel(int T, double[] xkeep, int[] xstat, int[] estat) {
157
158 this.T = T;
159
160 demand = new double[T];
161
162 vartd = new int[T];
163 varcs = new int[T];
164 vars = new int[T];
165 vard = new int[T];
166 varr = new int[T];
167 varp = new int[T];
168 varrev = new int[T];
169
170 consttdeq = new int[T];
171 constseq = new int[T];
172 constcseq = new int[T];
173 constdeq = new int[T];
174 constreq = new int[T];
175 constdrev = new int[T];
176 hessianstart = new int[T];
177
178
179 // adding the variables to the model
180 // defining the demand for each time period.
181 for (int t = 0; t < T; t++) {
182 demand[t] = 1.0 + 2.3 * Math.pow(1.015, t);
183 }
184
185 // other variables
186 int varidx;
187 for (int t = 0; t < T; t++) {
188 /* td: Lower=0, Upper=inf, Curr=18 */
189 varidx = addVariable(0, Conopt.Infinity, 18);
190 vartd[t] = varidx;
191
192 /* cs: Lower=0, Upper=inf, Curr=7*t */
193 varidx = addVariable(0, Conopt.Infinity, 7 * (t + 1));
194 varcs[t] = varidx;
195
196 /* s: Lower=0, Upper=inf, Curr=7 */
197 varidx = addVariable(0, Conopt.Infinity, 7);
198 vars[t] = varidx;
199
200 /* d: Lower=0, Upper=inf, Curr=td-s */
201 varidx =
202 addVariable(0, Conopt.Infinity, getVariable(vartd[t]).getCurr() - getVariable(vars[t]).getCurr());
203 vard[t] = varidx;
204
205 /* r: Lower=1, Upper=inf, Curr=r(t-1)-d */
206 if (t > 0)
207 varidx = addVariable(
208 1, Conopt.Infinity, getVariable(varr[t - 1]).getCurr() - getVariable(vard[t]).getCurr());
209 else
210 varidx = addVariable(1, Conopt.Infinity, 500 - getVariable(vard[t]).getCurr());
211 varr[t] = varidx;
212
213 /* p: Lower=1, Upper=inf, Curr=14 */
214 varidx = addVariable(1, Conopt.Infinity, 14);
215 varp[t] = varidx;
216
217 /* rev: Lower=-inf, Upper=inf, Curr=0 */
219 varrev[t] = varidx;
220 }
221
222 if (xkeep.length > 0) {
223 for (int i = 0; i < xkeep.length; i++) {
224 getVariable(i).setCurr(xkeep[i]);
225 }
226
227 // linear extrapolation for the last period of variable td
228 getVariable(vartd[T - 1]).setCurr(
229 2 * getVariable(vartd[T - 2]).getCurr() - getVariable(vartd[T - 3]).getCurr());
230 // cs
231 getVariable(varcs[T - 1]).setCurr(
232 2 * getVariable(varcs[T - 2]).getCurr() - getVariable(varcs[T - 3]).getCurr());
233 // s
234 getVariable(vars[T - 1]).setCurr(
235 2 * getVariable(vars[T - 2]).getCurr() - getVariable(vars[T - 3]).getCurr());
236 // d
237 getVariable(vard[T - 1]).setCurr(
238 2 * getVariable(vard[T - 2]).getCurr() - getVariable(vard[T - 3]).getCurr());
239 // r
240 getVariable(varr[T - 1]).setCurr(
241 2 * getVariable(varr[T - 2]).getCurr() - getVariable(varr[T - 3]).getCurr());
242 // p
243 getVariable(varp[T - 1]).setCurr(
244 2 * getVariable(varp[T - 2]).getCurr() - getVariable(varp[T - 3]).getCurr());
245 // rev
246 getVariable(varrev[T - 1]).setCurr(
247 2 * getVariable(varrev[T - 2]).getCurr() - getVariable(varrev[T - 3]).getCurr());
248
254 for (int i = 0; i < xstat.length; i++) {
255 getVariable(i).setVarstatus(xstat[i]);
256 }
257
258 /* td */
259 getVariable(vartd[T - 1]).setVarstatus(getVariable(vartd[T - 2]).getVarstatus());
260 /* cs */
261 getVariable(varcs[T - 1]).setVarstatus(getVariable(varcs[T - 2]).getVarstatus());
262 /* s */
263 getVariable(vars[T - 1]).setVarstatus(getVariable(vars[T - 2]).getVarstatus());
264 /* d */
265 getVariable(vard[T - 1]).setVarstatus(getVariable(vard[T - 2]).getVarstatus());
266 /* r */
267 getVariable(varr[T - 1]).setVarstatus(getVariable(varr[T - 2]).getVarstatus());
268 /* p */
269 getVariable(varp[T - 1]).setVarstatus(getVariable(varp[T - 2]).getVarstatus());
270 /* rev */
271 getVariable(varrev[T - 1]).setVarstatus(getVariable(varrev[T - 2]).getVarstatus());
272 }
273
274
275 // adding the objective
276 double[] objcoeff = new double[T];
277 int[] objnlflag = new int[T];
278
279 for (int t = 0; t < T; ++t) {
280 objcoeff[t] = Math.pow(1.05, 1 - (t + 1));
281 }
282 int objidx = addConstraint(ConstraintType.Free, 0.0, varrev, objcoeff, objnlflag);
283
284
285 // adding the constraints to the model
286 int considx;
287 for (int t = 0; t < T; t++) {
288
289 // adding the tdeq equations
290 if (t == 0) {
291 {
292 int[] index = {vartd[t], varp[t]}; // the variables
293 double[] value = {1.0, 0.13}; // the coefficients
294 int[] nlflag = {0, 0}; // nlflags
295 considx = addConstraint(ConstraintType.Eq, demand[t] + 0.87 * 18.0, index, value, nlflag);
296 }
297 }
298 else {
299 {
300 int[] index = {vartd[t], vartd[t - 1], varp[t]};
301 double[] value = {1.0, -0.87, 0.13};
302 int[] nlflag = {0, 0, 0};
303 considx = addConstraint(ConstraintType.Eq, demand[t], index, value, nlflag);
304 }
305 }
306 consttdeq[t] = considx;
307
308 // adding the seq Equations
309 if (t == 0) {
310 {
311 int[] index = {vars[t], varp[t], varcs[t]};
312 double[] value = {1.0, 0.0, 0.0};
313 int[] nlflag = {0, 1, 1};
314 considx = addConstraint(ConstraintType.Eq, 0.75 * 6.5, index, value, nlflag);
315 }
316 }
317 else {
318 {
319 int[] index = {vars[t], vars[t - 1], varp[t], varcs[t]};
320 double[] value = {1.0, -0.75, 0.0, 0.0};
321 int[] nlflag = {0, 0, 1, 1};
322 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
323 }
324 }
325 constseq[t] = considx;
326 consmapping.putIfAbsent(considx, new IntPair(CONS_SEQ, t));
327
328 // adding the cseq Equations
329 if (t == 0) {
330 {
331 int[] index = {varcs[t], vars[t]};
332 double[] value = {1.0, -1.0};
333 int[] nlflag = {0, 0};
334 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
335 }
336 }
337 else {
338 {
339 int[] index = {varcs[t], varcs[t - 1], vars[t]};
340 double[] value = {1.0, -1.0, -1.0};
341 int[] nlflag = {0, 0, 0};
342 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
343 }
344 }
345 constcseq[t] = considx;
346
347 // adding the deq equations
348 {
349 int[] index = {vard[t], vartd[t], vars[t]};
350 double[] value = {1.0, -1.0, 1.0};
351 int[] nlflag = {0, 0, 0};
352 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
353 }
354 constdeq[t] = considx;
355
356 // adding the req equations
357 if (t == 0) {
358 {
359 int[] index = {varr[t], vard[t]};
360 double[] value = {1.0, 1.0};
361 int[] nlflag = {0, 0};
362 considx = addConstraint(ConstraintType.Eq, 500.0, index, value, nlflag);
363 }
364 }
365 else {
366 {
367 int[] index = {varr[t], varr[t - 1], vard[t]};
368 double[] value = {1.0, -1.0, 1.0};
369 int[] nlflag = {0, 0, 0};
370 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
371 }
372 }
373 constreq[t] = considx;
374
375 // adding the drev equations
376 {
377 int[] index = {varrev[t], vard[t], varp[t], varr[t]};
378 double[] value = {1.0, 0.0, 0.0, 0.0};
379 int[] nlflag = {0, 1, 1, 1};
380 considx = addConstraint(ConstraintType.Eq, 0.0, index, value, nlflag);
381 }
382 constdrev[t] = considx;
383 consmapping.putIfAbsent(considx, new IntPair(CONS_DREV, t));
384 }
385
386 // Restore values from previous runs
387 if (estat.length > 0) {
388 // the equation status
389 for (int i = 0; i < estat.length; i++) {
390 getConstraint(i).setSlackstatus(estat[i]);
391 }
392
393 // tdeq
394 getConstraint(consttdeq[T - 1]).setSlackstatus(getConstraint(consttdeq[T - 2]).getSlackstatus());
395 // seq
396 getConstraint(constseq[T - 1]).setSlackstatus(getConstraint(constseq[T - 2]).getSlackstatus());
397 // cseq
398 getConstraint(constcseq[T - 1]).setSlackstatus(getConstraint(constcseq[T - 2]).getSlackstatus());
399 // deq
400 getConstraint(constdeq[T - 1]).setSlackstatus(getConstraint(constdeq[T - 2]).getSlackstatus());
401 // req
402 getConstraint(constreq[T - 1]).setSlackstatus(getConstraint(constreq[T - 2]).getSlackstatus());
403 // drev
404 getConstraint(constdrev[T - 1]).setSlackstatus(getConstraint(constdrev[T - 2]).getSlackstatus());
405 }
406
407 // setting the objective constraint
409
410 // setting the optimisation direction
412
413 // setting the structure of the hessian
414 int[] hsrow = new int[T * 5];
415 int[] hscol = new int[T * 5];
416
417 int idx = 0; // running index into hsrow/hscol
418 for (int t = 0; t < T; ++t) {
419 hessianstart[t] = idx;
420
421 // 1) (cs, cs)
422 hscol[idx] = varcs[t];
423 hsrow[idx] = varcs[t];
424 idx++;
425
426 // 2) (cs, p)
427 hscol[idx] = varcs[t];
428 hsrow[idx] = varp[t];
429 idx++;
430
431 // 3) (d, r) the row index for this entry is an error.
432 hscol[idx] = vard[t];
433 hsrow[idx] = varr[t];
434 idx++;
435
436 // 4) (d, p)
437 hscol[idx] = vard[t];
438 hsrow[idx] = varp[t];
439 idx++;
440
441 // 5) (r, r)
442 hscol[idx] = varr[t];
443 hsrow[idx] = varr[t];
444 idx++;
445 }
446 setSDLagrangianStructure(hsrow, hscol);
447 }
448
453 public double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread) {
454 IntPair conspair = consmapping.get(rowno);
455 if (conspair == null) return 0;
456
457 int consset = conspair.first();
458 int t = conspair.second();
459
460 assert (consset == CONS_SEQ || consset == CONS_DREV);
461 double g = 0;
462 if (consset == CONS_SEQ) {
463 // seq equation. Nonlinear term = -(1.1+0.1*p(t))*1.02**(-cs(t)/7)
464 double h1, h2;
465
466 h1 = (1.1 + 0.1 * x[varp[t]]);
467 h2 = Math.pow(1.02, -x[varcs[t]] / 7.0);
468 g = -h1 * h2;
469 }
470 else if (consset == CONS_DREV) {
471 g = -x[vard[t]] * (x[varp[t]] - 250. / x[varr[t]]);
472 }
473
474 return g;
475 }
476
481 public void evaluateNonlinearJacobian(double[] x, double[] jac, int rowno, int[] jacnum, boolean ignerr, int thread) {
482 assert x.length == jac.length;
483
484 IntPair conspair = consmapping.get(rowno);
485 if (conspair == null) return;
486
487 int consset = conspair.first();
488 int t = conspair.second();
489
490 assert (consset == CONS_SEQ || consset == CONS_DREV);
491
492 if (consset == CONS_SEQ) {
493 // seq equation. Nonlinear term = -(1.1+0.1*p(t))*1.02**(-cs(t)/7)
494 double h1, h2;
495
496 h1 = (1.1 + 0.1 * x[varp[t]]);
497 h2 = Math.pow(1.02, -x[varcs[t]] / 7.0);
498
499 jac[varcs[t]] = h1 * h2 * Math.log(1.02) / 7.0;
500 jac[varp[t]] = -h2 * 0.1;
501 }
502 else if (consset == CONS_DREV) {
503 jac[vard[t]] = -(x[varp[t]] - 250. / x[varr[t]]);
504 jac[varr[t]] = -x[vard[t]] * 250. / Math.pow(x[varr[t]], 2);
505 jac[varp[t]] = -x[vard[t]];
506 }
507 }
508
513 public void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval) {
514
515 double cs, d, r, p;
516 double h1, h2;
517
518 /* Normal Evaluation mode */
519 for (int t = 0; t < T; t++) {
520 int hessidx = hessianstart[t];
521 cs = x[varcs[t]];
522 d = x[vard[t]];
523 r = x[varr[t]];
524 p = x[varp[t]];
525
526 h1 = 1.1 + 0.1 * p;
527 h2 = Math.pow(1.02, -cs / 7.0);
528
529 /* the second derivative of the SEQ equations */
530 hessianval[hessidx] = -h1 * h2 * Math.pow(Math.log(1.02) / 7.0, 2) * u[constseq[t]];
531 hessianval[hessidx + 1] = 0.1 * h2 * (Math.log(1.02) / 7.) * u[constseq[t]];
532
533 /* the second derivative of the DREV equations */
534 hessianval[hessidx + 2] = -250.0 / Math.pow(r, 2) * u[constdrev[t]];
535 hessianval[hessidx + 3] = -1.0 * u[constdrev[t]];
536 hessianval[hessidx + 4] = 500.0 * d / Math.pow(r, 3) * u[constdrev[t]];
537 }
538 }
539}
IntPair(int first, int second)
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[])
static int checkSolve(String name, int model_status, int solution_status, double objective, double expected_objective, double tol)
Definition std.java:20
static void java_log(final String name, final String message)
Definition std.java:10
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
setSDLagrangianStructure(self, rownum, colnum)
sets the structure of the second derivatives of the Lagrangian
Definition conopt.py:2889
double evaluateNonlinearTerm(double[] x, int rowno, boolean ignerr, int thread)
callback method for evaluating the nonlinear terms in a given row
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
void evaluateSDLagrangian(double x[], double u[], int[] hessianrow, int[] hessiancol, double[] hessianval)
Computes and returns the numerical values of the Lagrangian of the Hessian.
void buildModel(int T, double[] xkeep, int[] xstat, int[] estat)
adds variables and constraints to the model
getVariable(self, *args)
Overload 1: returns a reference to the variable object
Definition conopt.py:2844
getConstraint(self, *args)
Overload 1: returns a reference to the constraint object
Definition conopt.py:2862
double xkeep[Tmax *Tmax *Vpp]
Definition std.py:1