forked from xiongwq16/cplex-java-api-tutorial-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQCPDual.java
More file actions
404 lines (376 loc) · 19.8 KB
/
QCPDual.java
File metadata and controls
404 lines (376 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package examples;
/* --------------------------------------------------------------------------
* File: QCPDual.java
* Version 12.9.0
* --------------------------------------------------------------------------
* Licensed Materials - Property of IBM
* 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
* Copyright IBM Corporation 2003, 2019. All Rights Reserved.
*
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with
* IBM Corp.
* --------------------------------------------------------------------------
*/
/*
* QCPDual.java - Illustrates how to query and analyze dual values of QCPs
*/
import ilog.cplex.*;
import ilog.concert.*;
import java.util.HashMap;
public final class QCPDual {
/** Tolerance applied when testing values for zero. */
public static final double ZEROTOL = 1e-6;
/** Create a string representation of an array.
* This is similar to {@java.util.Arrays#toString(double[])} but
* uses a different output format for the individual elements.
*/
private static final String arrayToString(double[] array) {
final StringBuffer result = new StringBuffer();
result.append("[");
for (int i = 0; i < array.length; ++i)
result.append(String.format(" %7.3f", array[i]));
result.append(" ]");
return result.toString();
}
/* ***************************************************************** *
* *
* C A L C U L A T E D U A L S F O R Q U A D S *
* *
* CPLEX does not give us the dual multipliers for quadratic *
* constraints directly. This is because they may not be properly *
* defined at the cone top and deciding whether we are at the cone *
* top or not involves (problem specific) tolerance issues. CPLEX *
* instead gives us all the values we need in order to compute the *
* dual multipliers if we are not at the cone top. *
* *
* ***************************************************************** */
/** Calculate dual multipliers for quadratic constraints from dual
* slack vectors and optimal solutions.
* The dual multiplier is essentially the dual slack divided
* by the derivative evaluated at the optimal solution. If the optimal
* solution is 0 then the derivative at this point is not defined (we are
* at the cone top) and we cannot compute a dual multiplier.
* @param cplex The IloCplex instance that holds the optimal solution.
* @param xval The optimal solution vector.
* @param tol The tolerance used to decide whether we are at the cone
* top or not.
* @param x Array with all variables in the model.
* @param qs Array of quadratic constraints for which duals shall
* be calculated.
* @return An array with dual multipliers for all quadratic
* constraints in <code>qs</code>.
* @throw IloException if querying data from <code>cplex</code> fails.
* @throw RuntimeException if the optimal solution is at the cone top.
*/
private static double[] getqconstrmultipliers(IloCplex cplex, double[] xval, double tol, IloNumVar[] x, IloRange[] qs) throws IloException
{
// Store solution vector in hash map so that lookup is easy.
final HashMap<IloNumVar,Double> sol = new HashMap<IloNumVar,Double>();
for (int j = 0; j < x.length; ++j)
sol.put(x[j], xval[j]);
final double[] qpi = new double[qs.length];
for (int i = 0; i < qs.length; ++i) {
// Turn the dual slack vector in a map so that lookup is easy.
final HashMap<IloNumVar,Double> dslack = new HashMap<IloNumVar,Double>();
for (IloLinearNumExprIterator it = cplex.getQCDSlack(qs[i]).linearIterator(); it.hasNext(); /* nothing */) {
IloNumVar v = it.nextNumVar();
dslack.put(v, it.getValue());
}
// Sparse vector for gradient.
final HashMap<IloNumVar,Double> grad = new HashMap<IloNumVar,Double>();
boolean conetop = true;
for (IloQuadNumExprIterator it = ((IloLQNumExpr)qs[i].getExpr()).quadIterator();
it.hasNext(); /* nothing */)
{
it.next();
IloNumVar x1 = it.getNumVar1();
IloNumVar x2 = it.getNumVar2();
if ( sol.get(x1) > tol || sol.get(x2) > tol )
conetop = false;
final Double oldx1 = grad.get(x1);
grad.put(x1, (oldx1 != null ? oldx1.doubleValue() : 0.0) + sol.get(x2) * it.getValue());
final Double oldx2 = grad.get(x2);
grad.put(x2, (oldx2 != null ? oldx2.doubleValue() : 0.0) + sol.get(x1) * it.getValue());
}
if ( conetop )
throw new RuntimeException("Cannot compute dual multiplier at cone top!");
// Compute qpi[i] as slack/gradient.
// We may have several indices to choose from and use the one
// with largest absolute value in the denominator.
boolean ok = false;
double maxabs = -1.0;
for (int j = 0; j < x.length; ++j) {
if (grad.containsKey(x[j])) {
final double g = grad.get(x[j]);
if ( Math.abs(g) > tol ) {
if ( Math.abs(g) > maxabs ) {
final Double d = dslack.get(x[j]);
qpi[i] = (d != null ? d.doubleValue() : 0.0) / g;
maxabs = Math.abs(g);
}
ok = true;
}
}
}
if ( !ok ) {
// Dual slack is all 0. qpi[i] can be anything, just set to 0.
qpi[i] = 0.0;
}
}
return qpi;
}
/** The example's main function. */
public static void main(String[] args) {
IloCplex cplex = null;
try {
cplex = new IloCplex();
/* ***************************************************************** *
* *
* S E T U P P R O B L E M *
* *
* We create the following problem: *
* Minimize *
* obj: 3x1 - x2 + 3x3 + 2x4 + x5 + 2x6 + 4x7 *
* Subject To *
* c1: x1 + x2 = 4 *
* c2: x1 + x3 >= 3 *
* c3: x6 + x7 <= 5 *
* c4: -x1 + x7 >= -2 *
* q1: [ -x1^2 + x2^2 ] <= 0 *
* q2: [ 4.25x3^2 -2x3*x4 + 4.25x4^2 - 2x4*x5 + 4x5^2 ] + 2 x1 <= 9.0
* q3: [ x6^2 - x7^2 ] >= 4 *
* Bounds *
* 0 <= x1 <= 3 *
* x2 Free *
* 0 <= x3 <= 0.5 *
* x4 Free *
* x5 Free *
* x7 Free *
* End *
* *
* ***************************************************************** */
IloNumVar[] x = new IloNumVar[7];
x[0] = cplex.numVar(0, 3, "x1");
x[1] = cplex.numVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x2");
x[2] = cplex.numVar(0.0, 0.5, "x3");
x[3] = cplex.numVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x4");
x[4] = cplex.numVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x5");
x[5] = cplex.numVar(0.0, Double.POSITIVE_INFINITY, "x6");
x[6] = cplex.numVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x7");
IloRange[] linear = new IloRange[4];
linear[0] = cplex.addEq(cplex.sum(x[0], x[1]), 4.0, "c1");
linear[1] = cplex.addGe(cplex.sum(x[0], x[2]), 3.0, "c2");
linear[2] = cplex.addLe(cplex.sum(x[5], x[6]), 5.0, "c3");
linear[3] = cplex.addGe(cplex.diff(x[6], x[0]), -2.0, "c4");
IloRange[] quad = new IloRange[3];
quad[0] = cplex.addLe(cplex.sum(cplex.prod(-1, x[0], x[0]),
cplex.prod(x[1], x[1])), 0.0,
"q1");
quad[1] = cplex.addLe(cplex.sum(cplex.prod(4.25, x[2], x[2]),
cplex.prod(-2.0, x[2], x[3]),
cplex.prod(4.25, x[3], x[3]),
cplex.prod(-2.0, x[3], x[4]),
cplex.prod(4.0, x[4], x[4]),
cplex.prod(2.0, x[0])), 9.0,
"q2");
quad[2] = cplex.addGe(cplex.diff(cplex.prod(x[5], x[5]),
cplex.prod(x[6], x[6])), 4.0,
"q3");
cplex.addMinimize(cplex.sum(cplex.prod(3.0, x[0]),
cplex.prod(-1.0, x[1]),
cplex.prod(3, x[2]),
cplex.prod(2, x[3]),
cplex.prod(1, x[4]),
cplex.prod(2, x[5]),
cplex.prod(4, x[6])),
"obj");
/* ***************************************************************** *
* *
* O P T I M I Z E P R O B L E M *
* *
* ***************************************************************** */
cplex.setParam(IloCplex.Param.Barrier.QCPConvergeTol, 1e-10);
cplex.solve();
System.out.println("Solution status: " + cplex.getStatus());
/* ***************************************************************** *
* *
* Q U E R Y S O L U T I O N *
* *
* ***************************************************************** */
final double[] xval = cplex.getValues(x);
final double[] slack = cplex.getSlacks(linear);
final double[] qslack = cplex.getSlacks(quad);
final double[] cpi = cplex.getReducedCosts(x);
final double[] rpi = cplex.getDuals(linear);
final double[] qpi = getqconstrmultipliers (cplex, xval, ZEROTOL, x, quad);
// Also store the solution vector in a map since we need to look
// up solution values by variable and not only by index.
final HashMap<IloNumVar,Double> xmap = new HashMap<IloNumVar,Double>();
for (int j = 0; j < x.length; ++j)
xmap.put(x[j], xval[j]);
/* ***************************************************************** *
* *
* C H E C K K K T C O N D I T I O N S *
* *
* Here we verify that the optimal solution computed by CPLEX *
* (and the qpi[] values computed above) satisfy the KKT *
* conditions. *
* *
* ***************************************************************** */
// Primal feasibility: This example is about duals so we skip this test.
// Dual feasibility: We must verify
// - for <= constraints (linear or quadratic) the dual
// multiplier is non-positive.
// - for >= constraints (linear or quadratic) the dual
// multiplier is non-negative.
for (int i = 0; i < linear.length; ++i) {
if ( linear[i].getLB() <= Double.NEGATIVE_INFINITY ) {
// <= constraint
if ( rpi[i] > ZEROTOL )
throw new RuntimeException("Dual feasibility test failed for row "
+ linear[i] + ": " + rpi[i]);
}
else if ( linear[i].getUB() >= Double.POSITIVE_INFINITY ) {
// >= constraint
if ( rpi[i] < -ZEROTOL )
throw new RuntimeException("Dual feasibility test failed for row "
+ linear[i] + ": " + rpi[i]);
}
else {
// nothing to do for equality constraints
}
}
for (int i = 0; i < quad.length; ++i) {
if ( quad[i].getLB() <= Double.NEGATIVE_INFINITY ) {
// <= constraint
if ( qpi[i] > ZEROTOL )
throw new RuntimeException("Dual feasibility test failed for quad "
+ quad[i] + ": " + qpi[i]);
}
else if ( quad[i].getUB() >= Double.POSITIVE_INFINITY ) {
// >= constraint
if ( qpi[i] < -ZEROTOL )
throw new RuntimeException("Dual feasibility test failed for quad "
+ quad[i] + ": " + qpi[i]);
}
else {
// nothing to do for equality constraints
}
}
// Complementary slackness.
// For any constraint the product of primal slack and dual multiplier
// must be 0.
for (int i = 0; i < linear.length; ++i) {
if ( Math.abs(linear[i].getUB() - linear[i].getLB()) > ZEROTOL &&
Math.abs(slack[i] * rpi[i]) > ZEROTOL )
throw new RuntimeException("Complementary slackness test failed for row " + linear[i]
+ ": " + Math.abs(slack[i] * rpi[i]));
}
for (int i = 0; i < quad.length; ++i) {
if ( Math.abs(quad[i].getUB() - quad[i].getLB()) > ZEROTOL &&
Math.abs(qslack[i] * qpi[i]) > ZEROTOL )
throw new RuntimeException("Complementary slackness test failed for quad " + quad[i]
+ ": " + Math.abs(qslack[i] * qpi[i]));
}
for (int j = 0; j < x.length; ++j) {
if ( x[j].getUB() < Double.POSITIVE_INFINITY ) {
double slk = x[j].getUB() - xval[j];
double dual = cpi[j] < -ZEROTOL ? cpi[j] : 0.0;
if ( Math.abs(slk * dual) > ZEROTOL )
throw new RuntimeException("Complementary slackness test failed for column " + x[j]
+ ": " + Math.abs(slk * dual));
}
if ( x[j].getLB() > Double.NEGATIVE_INFINITY ) {
double slk = xval[j] - x[j].getLB();
double dual = cpi[j] > ZEROTOL ? cpi[j] : 0.0;
if ( Math.abs(slk * dual) > ZEROTOL )
throw new RuntimeException("Complementary slackness test failed for column " + x[j]
+ ": " + Math.abs(slk * dual));
}
}
// Stationarity.
// The difference between objective function and gradient at optimal
// solution multiplied by dual multipliers must be 0, i.e., for the
// optimal solution x
// 0 == c
// - sum(r in rows) r'(x)*rpi[r]
// - sum(q in quads) q'(x)*qpi[q]
// - sum(c in cols) b'(x)*cpi[c]
// where r' and q' are the derivatives of a row or quadratic constraint,
// x is the optimal solution and rpi[r] and qpi[q] are the dual
// multipliers for row r and quadratic constraint q.
// b' is the derivative of a bound constraint and cpi[c] the dual bound
// multiplier for column c.
final HashMap<IloNumVar,Double> kktsum = new HashMap<IloNumVar,Double>();
for (int j = 0; j < x.length; ++j)
kktsum.put(x[j], 0.0);
// Objective function.
for (IloLinearNumExprIterator it = ((IloLinearNumExpr)cplex.getObjective().getExpr()).linearIterator();
it.hasNext(); /* nothing */)
{
IloNumVar v = it.nextNumVar();
kktsum.put(v, it.getValue());
}
// Linear constraints.
// The derivative of a linear constraint ax - b (<)= 0 is just a.
for (int i = 0; i < linear.length; ++i) {
for (IloLinearNumExprIterator it = ((IloLinearNumExpr)linear[i].getExpr()).linearIterator();
it.hasNext(); /* nothing */)
{
IloNumVar v = it.nextNumVar();
kktsum.put(v, kktsum.get(v) - rpi[i] * it.getValue());
}
}
// Quadratic constraints.
// The derivative of a constraint xQx + ax - b <= 0 is
// Qx + Q'x + a.
for (int i = 0; i < quad.length; ++i) {
for (IloLinearNumExprIterator it = ((IloLinearNumExpr)quad[i].getExpr()).linearIterator();
it.hasNext(); /* nothing */)
{
IloNumVar v = it.nextNumVar();
kktsum.put(v, kktsum.get(v) - qpi[i] * it.getValue());
}
for (IloQuadNumExprIterator it = ((IloQuadNumExpr)quad[i].getExpr()).quadIterator();
it.hasNext(); /* nothing */)
{
it.next();
IloNumVar v1 = it.getNumVar1();
IloNumVar v2 = it.getNumVar2();
kktsum.put(v1, kktsum.get(v1) - qpi[i] * xmap.get(v2) * it.getValue());
kktsum.put(v2, kktsum.get(v2) - qpi[i] * xmap.get(v1) * it.getValue());
}
}
// Bounds.
// The derivative for lower bounds is -1 and that for upper bounds
// is 1.
// CPLEX already returns dj with the appropriate sign so there is
// no need to distinguish between different bound types here.
for (int j = 0; j < x.length; ++j) {
kktsum.put(x[j], kktsum.get(x[j]) - cpi[j]);
}
for (IloNumVar v : x) {
if ( Math.abs(kktsum.get(v)) > ZEROTOL )
throw new RuntimeException("Stationarity test failed at " + v
+ ": " + Math.abs(kktsum.get(v)));
}
// KKT conditions satisfied. Dump out the optimal solutions and
// the dual values.
System.out.println("Optimal solution satisfies KKT conditions.");
System.out.println(" x[] = " + arrayToString(xval));
System.out.println(" cpi[] = " + arrayToString(cpi));
System.out.println(" rpi[] = " + arrayToString(rpi));
System.out.println(" qpi[] = " + arrayToString(qpi));
}
catch (IloException e) {
System.err.println("IloException: " + e.getMessage());
e.printStackTrace();
System.exit(-1);
}
finally {
if ( cplex != null )
cplex.end();
}
}
}