1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.hipparchus.optim.nonlinear.vector.constrained;
18
19
20 import org.hipparchus.linear.ArrayRealVector;
21 import org.hipparchus.linear.DecompositionSolver;
22 import org.hipparchus.linear.EigenDecompositionSymmetric;
23 import org.hipparchus.linear.MatrixUtils;
24 import org.hipparchus.linear.RealMatrix;
25 import org.hipparchus.linear.RealVector;
26 import org.hipparchus.util.FastMath;
27
28
29
30
31 public class ADMMQPKKT implements KarushKuhnTuckerSolver<ADMMQPSolution> {
32
33
34
35
36 private double decompositionEpsilon;
37
38
39 private RealMatrix H;
40
41
42 private RealVector q;
43
44
45 private RealMatrix A;
46
47
48 private double sigma;
49
50
51 private RealMatrix R;
52
53
54 private RealMatrix Rinv;
55
56
57 private RealVector lb;
58
59
60 private RealVector ub;
61
62
63 private double alpha;
64
65
66 private RealMatrix M;
67
68
69 private DecompositionSolver dsX;
70
71
72
73
74
75
76
77
78 ADMMQPKKT() {
79 decompositionEpsilon = EigenDecompositionSymmetric.DEFAULT_EPSILON;
80 }
81
82
83 @Override
84 public ADMMQPSolution solve(RealVector b1, final RealVector b2) {
85 RealVector z = dsX.solve(new ArrayRealVector((ArrayRealVector) b1, b2));
86 return new ADMMQPSolution(z.getSubVector(0,b1.getDimension()), z.getSubVector(b1.getDimension(), b2.getDimension()));
87 }
88
89
90
91
92
93 public void updateDecompositionEpsilon(final double newDecompositionEpsilon) {
94 this.decompositionEpsilon = newDecompositionEpsilon;
95 }
96
97
98
99
100
101
102 public void updateSigmaRho(double newSigma, int me, double rho) {
103 this.sigma = newSigma;
104 this.H = H.add(MatrixUtils.createRealIdentityMatrix(H.getColumnDimension()).scalarMultiply(newSigma));
105 createPenaltyMatrix(me, rho);
106 M = MatrixUtils.createRealMatrix(H.getRowDimension() + A.getRowDimension(),
107 H.getRowDimension() + A.getRowDimension());
108 M.setSubMatrix(H.getData(), 0,0);
109 M.setSubMatrix(A.getData(), H.getRowDimension(),0);
110 M.setSubMatrix(A.transpose().getData(), 0, H.getRowDimension());
111 M.setSubMatrix(Rinv.scalarMultiply(-1.0).getData(), H.getRowDimension(),H.getRowDimension());
112 dsX = new EigenDecompositionSymmetric(M, decompositionEpsilon, true).getSolver();
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126 public void initialize(RealMatrix newH, RealMatrix newA, RealVector newQ,
127 int me, RealVector newLb, RealVector newUb,
128 double rho, double newSigma, double newAlpha) {
129 this.lb = newLb;
130 this.ub = newUb;
131 this.alpha = newAlpha;
132 this.sigma = newSigma;
133 this.H = newH.add(MatrixUtils.createRealIdentityMatrix(newH.getColumnDimension()).scalarMultiply(newSigma));
134 this.A = newA.copy();
135 this.q = newQ.copy();
136 createPenaltyMatrix(me, rho);
137
138 M = MatrixUtils.createRealMatrix(newH.getRowDimension() + newA.getRowDimension(),
139 newH.getRowDimension() + newA.getRowDimension());
140 M.setSubMatrix(newH.getData(),0,0);
141 M.setSubMatrix(newA.getData(),newH.getRowDimension(),0);
142 M.setSubMatrix(newA.transpose().getData(),0,newH.getRowDimension());
143 M.setSubMatrix(Rinv.scalarMultiply(-1.0).getData(),newH.getRowDimension(),newH.getRowDimension());
144 dsX = new EigenDecompositionSymmetric(M, decompositionEpsilon, true).getSolver();
145 }
146
147 private void createPenaltyMatrix(int me, double rho) {
148 this.R = MatrixUtils.createRealIdentityMatrix(A.getRowDimension());
149
150 for (int i = 0; i < R.getRowDimension(); i++) {
151 if (i < me) {
152 R.setEntry(i, i, rho * 1000.0);
153
154 } else {
155 R.setEntry(i, i, rho);
156
157 }
158 }
159 this.Rinv = MatrixUtils.inverse(R);
160 }
161
162
163 @Override
164 public ADMMQPSolution iterate(RealVector... previousSol) {
165 double onealfa = 1.0 - alpha;
166
167 RealVector xold = previousSol[0].copy();
168 RealVector yold = previousSol[1].copy();
169 RealVector zold = previousSol[2].copy();
170
171
172 RealVector b1 = previousSol[0].mapMultiply(sigma).subtract(q);
173 RealVector b2 = previousSol[2].subtract(Rinv.operate(previousSol[1]));
174
175
176 ADMMQPSolution sol = solve(b1, b2);
177 RealVector xtilde = sol.getX();
178 RealVector vtilde = sol.getV();
179
180
181 RealVector ztilde = zold.add(Rinv.operate(vtilde.subtract(yold)));
182
183 previousSol[0] = xtilde.mapMultiply(alpha).add(xold.mapMultiply(onealfa));
184
185
186 RealVector zpartial = ztilde.mapMultiply(alpha).add(zold.mapMultiply(onealfa)).add(Rinv.operate(yold));
187
188
189 for (int j = 0; j < previousSol[2].getDimension(); j++) {
190 previousSol[2].setEntry(j, FastMath.min(FastMath.max(zpartial.getEntry(j), lb.getEntry(j)), ub.getEntry(j)));
191 }
192
193
194 RealVector ytilde = ztilde.mapMultiply(alpha).add(zold.mapMultiply(onealfa).subtract(previousSol[2]));
195 previousSol[1] = yold.add(R.operate(ytilde));
196
197 return new ADMMQPSolution(previousSol[0], vtilde, previousSol[1], previousSol[2]);
198 }
199
200 }