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  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.hipparchus.linear.RealMatrix;
22  import org.hipparchus.linear.RealVector;
23  import org.hipparchus.optim.LocalizedOptimFormats;
24  import org.hipparchus.optim.OptimizationData;
25  import org.hipparchus.optim.nonlinear.scalar.ObjectiveFunction;
26  import org.hipparchus.util.MathUtils;
27  
28  /**
29   * Abstract class for Sequential Quadratic Programming solvers
30   * @since 3.1
31   * @deprecated as of 4.1, replaced by {@link AbstractSQPOptimizer2}; will be removed in 5.0
32   */
33  @Deprecated
34  public abstract class AbstractSQPOptimizer extends ConstraintOptimizer {
35  
36      /** Algorithm settings. */
37      private SQPOption settings;
38  
39      /** Objective function. */
40      private TwiceDifferentiableFunction obj;
41  
42      /** Equality constraint (may be null). */
43      private EqualityConstraint eqConstraint;
44  
45      /** Inequality constraint (may be null). */
46      private InequalityConstraint iqConstraint;
47  
48      /** Simple constructor.
49       * <p>
50       * The default settings use the 4.0.3 values of {@link SQPOption} defaults, which differ
51       * from the current {@link SQPOption} defaults (tuned for {@link SQPOptimizerS2}).
52       * </p>
53       */
54      protected AbstractSQPOptimizer() {
55          this.settings = new SQPOption();
56          settings.setEps(1.0e-9);
57          settings.setMu(0.1);
58          settings.setMaxLineSearchIteration(20);
59      }
60  
61      /** Getter for settings.
62       * @return settings
63       */
64      public SQPOption getSettings() {
65          return settings;
66      }
67  
68      /** Getter for objective function.
69       * @return objective function
70       */
71      public TwiceDifferentiableFunction getObj() {
72          return obj;
73      }
74  
75      /** Getter for equality constraint.
76       * @return equality constraint
77       */
78      public EqualityConstraint getEqConstraint() {
79          return eqConstraint;
80      }
81  
82      /** Getter for inequality constraint.
83       * @return inequality constraint
84       */
85      public InequalityConstraint getIqConstraint() {
86          return iqConstraint;
87      }
88  
89      @Override
90      public LagrangeSolution optimize(OptimizationData... optData) {
91          return super.optimize(optData);
92      }
93  
94      @Override
95      protected void parseOptimizationData(OptimizationData... optData) {
96          super.parseOptimizationData(optData);
97          for (OptimizationData data : optData) {
98  
99              if (data instanceof ObjectiveFunction) {
100                 obj = (TwiceDifferentiableFunction) ((ObjectiveFunction) data).getObjectiveFunction();
101                 continue;
102             }
103 
104             if (data instanceof EqualityConstraint) {
105                 eqConstraint = (EqualityConstraint) data;
106                 continue;
107             }
108             if (data instanceof InequalityConstraint) {
109                 iqConstraint = (InequalityConstraint) data;
110                 continue;
111             }
112 
113             if (data instanceof SQPOption) {
114                 settings = (SQPOption) data;
115             }
116 
117         }
118 
119         // if we got here, convexObjective exists
120         int n = obj.dim();
121         if (eqConstraint != null) {
122             int nDual = eqConstraint.dimY();
123             if (nDual >= n) {
124                 throw new MathIllegalArgumentException(LocalizedOptimFormats.CONSTRAINTS_RANK, nDual, n);
125             }
126             int nTest = eqConstraint.dim();
127             if (nDual == 0) {
128                 throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NOT_ALLOWED);
129             }
130             MathUtils.checkDimension(nTest, n);
131         }
132 
133     }
134 
135     /**
136      * Compute Lagrangian gradient for variable X
137      *
138      * @param currentGrad current gradient
139      * @param jacobConstraint Jacobian
140      * @param x value of x
141      * @param y value of y
142      * @return Lagrangian
143      */
144     protected RealVector lagrangianGradX(final RealVector currentGrad, final RealMatrix jacobConstraint,
145                                          final RealVector x, final RealVector y) {
146 
147         int me = 0;
148         int mi;
149         RealVector partial = currentGrad.copy();
150         if (getEqConstraint() != null) {
151             me = getEqConstraint().dimY();
152 
153             RealVector ye = y.getSubVector(0, me);
154             RealMatrix jacobe = jacobConstraint.getSubMatrix(0, me - 1, 0, x.getDimension() - 1);
155 
156             RealVector firstTerm = jacobe.transpose().operate(ye);
157 
158             // partial = partial.subtract(firstTerm).add(jacobe.transpose().operate(ge).mapMultiply(rho));
159             partial = partial.subtract(firstTerm);
160         }
161 
162         if (getIqConstraint() != null) {
163             mi = getIqConstraint().dimY();
164 
165             RealVector yi = y.getSubVector(me, mi);
166             RealMatrix jacobi = jacobConstraint.getSubMatrix(me, me + mi - 1, 0, x.getDimension() - 1);
167 
168             RealVector firstTerm = jacobi.transpose().operate(yi);
169 
170             partial = partial.subtract(firstTerm);
171         }
172         return partial;
173     }
174 
175 }