View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  /** Alternative Direction Method of Multipliers Solver.
29   * @since 3.1
30   */
31  public class ADMMQPKKT implements KarushKuhnTuckerSolver<ADMMQPSolution> {
32  
33      /** Tolerance for symmetric matrices decomposition.
34       * @since 4.1
35       */
36      private double decompositionEpsilon;
37  
38      /** Square matrix of weights for quadratic terms. */
39      private RealMatrix H;
40  
41      /** Vector of weights for linear terms. */
42      private RealVector q;
43  
44      /** Constraints coefficients matrix. */
45      private RealMatrix A;
46  
47      /** Regularization term sigma for Karush–Kuhn–Tucker solver. */
48      private double sigma;
49  
50      /** TBC. */
51      private RealMatrix R;
52  
53      /** Inverse of R. */
54      private RealMatrix Rinv;
55  
56      /** Lower bound. */
57      private RealVector lb;
58  
59      /** Upper bound. */
60      private RealVector ub;
61  
62      /** Alpha filter for ADMM iteration. */
63      private double alpha;
64  
65      /** Constrained problem KKT matrix. */
66      private RealMatrix M; // NOPMD
67  
68      /** Solver for M. */
69      private DecompositionSolver dsX;
70  
71      /** Simple constructor.
72       * <p>
73       * BEWARE, nothing is initialized here, it is {@link #initialize(RealMatrix, RealMatrix,
74       * RealVector, int, RealVector, RealVector, double, double, double) initialize} <em>must</em>
75       * be called before using the instance.
76       * </p>
77       */
78      ADMMQPKKT() {
79          decompositionEpsilon = EigenDecompositionSymmetric.DEFAULT_EPSILON;
80      }
81  
82      /** {@inheritDoc} */
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      /** Update tolerance for matrix decomposition
90       * @param newDecompositionEpsilon tolerance for symmetric matrix decomposition
91       * @since 4.1
92       */
93      public void updateDecompositionEpsilon(final double newDecompositionEpsilon) {
94          this.decompositionEpsilon = newDecompositionEpsilon;
95      }
96  
97      /** Update steps
98       * @param newSigma new regularization term sigma for Karush–Kuhn–Tucker solver
99       * @param me number of equality constraints
100      * @param rho new step size
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     /** Initialize problem
116      * @param newH square matrix of weights for quadratic term
117      * @param newA constraints coefficients matrix
118      * @param newQ TBD
119      * @param me number of equality constraints
120      * @param newLb lower bound
121      * @param newUb upper bound
122      * @param rho step size
123      * @param newSigma regularization term sigma for Karush–Kuhn–Tucker solver
124      * @param newAlpha alpha filter for ADMM iteration
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     /** {@inheritDoc} */
163     @Override
164     public ADMMQPSolution iterate(RealVector... previousSol) {
165         double onealfa = 1.0 - alpha;
166         //SAVE OLD VALUE
167         RealVector xold = previousSol[0].copy();
168         RealVector yold = previousSol[1].copy();
169         RealVector zold = previousSol[2].copy();
170 
171         //UPDATE RIGHT VECTOR
172         RealVector b1 = previousSol[0].mapMultiply(sigma).subtract(q);
173         RealVector b2 = previousSol[2].subtract(Rinv.operate(previousSol[1]));
174 
175         //SOLVE KKT SYSYEM
176         ADMMQPSolution sol = solve(b1, b2);
177         RealVector xtilde = sol.getX();
178         RealVector vtilde = sol.getV();
179 
180         //UPDATE ZTILDE
181         RealVector ztilde = zold.add(Rinv.operate(vtilde.subtract(yold)));
182         //UPDATE X
183         previousSol[0] = xtilde.mapMultiply(alpha).add(xold.mapMultiply(onealfa));
184 
185         //UPDATE Z PARTIAL
186         RealVector zpartial = ztilde.mapMultiply(alpha).add(zold.mapMultiply(onealfa)).add(Rinv.operate(yold));
187 
188         //PROJECT ZPARTIAL AND UPDATE Z
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         //UPDATE Y
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 }