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.EigenDecompositionSymmetric;
22  import org.hipparchus.optim.LocalizedOptimFormats;
23  import org.hipparchus.optim.OptimizationData;
24  import org.hipparchus.optim.SimpleBounds;
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   */
32  public abstract class AbstractSQPOptimizer2 extends ConstraintOptimizer {
33  
34      /** Algorithm settings. */
35      private SQPOption settings;
36  
37      /** Tolerance for symmetric matrix decomposition.
38       * @since 4.1
39       */
40      private MatrixDecompositionTolerance matrixDecompositionTolerance;
41  
42      /** Objective function. */
43      private TwiceDifferentiableFunction OBJ;
44  
45      /** Equality constraint (may be null). */
46      private EqualityConstraint EQ;
47  
48      /** Inequality constraint (may be null). */
49      private InequalityConstraint IQ;
50  
51      /** Inequality constraint (may be null). */
52      private BoundedConstraint BOX;
53  
54      /** Simple Bounds (may be null). */
55      private SimpleBounds SB;
56  
57      /** Default QPSolver. */
58      private QPOptimizer QPSolver = new QPDualActiveSolver();
59  
60      /** Simple constructor.
61       */
62      protected AbstractSQPOptimizer2() {
63          this.settings                     = new SQPOption();
64          this.matrixDecompositionTolerance = new MatrixDecompositionTolerance(EigenDecompositionSymmetric.DEFAULT_EPSILON);
65      }
66  
67      /** Getter for settings.
68       * @return settings
69       */
70      public SQPOption getSettings() {
71          return settings;
72      }
73  
74      /** Getter for matrix decomposition tolerance.
75       * @return matrix decomposition tolerance
76       * @since 4.1
77       */
78      public MatrixDecompositionTolerance getMatrixDecompositionTolerance() {
79          return matrixDecompositionTolerance;
80      }
81  
82      /** Getter for objective function.
83       * @return objective function
84       */
85      public TwiceDifferentiableFunction getObj() {
86          return OBJ;
87      }
88  
89      /** Getter for equality constraint.
90       * @return equality constraint
91       */
92      public EqualityConstraint getEqConstraint() {
93          return EQ;
94      }
95  
96      /** Getter for inequality constraint.
97       * @return inequality constraint
98       */
99      public InequalityConstraint getIqConstraint() {
100         return IQ;
101     }
102 
103      /** Getter for box constraint.
104      * @return inequality constraint
105      */
106     public BoundedConstraint getBoxConstraint() {
107         return BOX;
108     }
109 
110      /** Getter for simple bounds.
111      * @return simple bounds
112      */
113     public SimpleBounds getSimpleBounds() {
114         return SB;
115     }
116 
117     /** Getter for QP Solver.
118      * @return QP Solver
119      */
120     public QPOptimizer getQPSolver() {
121         return QPSolver;
122     }
123 
124     @Override
125     public LagrangeSolution optimize(OptimizationData... optData) {
126         return super.optimize(optData);
127     }
128 
129     @Override
130     public void parseOptimizationData(OptimizationData... optData) {
131         super.parseOptimizationData(optData);
132         for (OptimizationData data : optData) {
133 
134             if (data instanceof SQPProblem) {
135                 SQPProblem problem = (SQPProblem) data;
136                 OBJ = new SQPObj(problem);
137 
138                 EQ= (problem.hasEquality())?new SQPEq((SQPProblem) data):null;
139                 IQ= (problem.hasInequality())?new SQPIneq((SQPProblem) data):null;
140 
141                 double[] lb = ((SQPProblem) data).getBoundsLB();
142                 double[]ub = ((SQPProblem) data).getBoundsUB();
143                 SB = (problem.hasBounds())?new SimpleBounds(lb,ub):null;
144 
145                 continue;
146             }
147 
148             if (data instanceof ObjectiveFunction) {
149                 OBJ = (TwiceDifferentiableFunction) ((ObjectiveFunction) data).getObjectiveFunction();
150                 continue;
151             }
152 
153             if (data instanceof EqualityConstraint) {
154                 EQ = (EqualityConstraint) data;
155                 continue;
156             }
157             if (data instanceof InequalityConstraint) {
158                 IQ = (InequalityConstraint) data;
159                 continue;
160             }
161 
162             if (data instanceof BoundedConstraint) {
163                 BOX = (BoundedConstraint) data;
164                 continue;
165             }
166 
167             if (data instanceof SimpleBounds) {
168                 SB = (SimpleBounds) data;
169                 continue;
170             }
171 
172             if (data instanceof SQPOption) {
173                 settings = (SQPOption) data;
174             }
175 
176             if (data instanceof QPOptimizer) {
177                 QPSolver = (QPOptimizer) data;
178             }
179 
180             if (data instanceof MatrixDecompositionTolerance) {
181                 matrixDecompositionTolerance = (MatrixDecompositionTolerance) data;
182             }
183 
184         }
185 
186         // if we got here, convexObjective exists
187         int n = OBJ.dim();
188         if (EQ != null) {
189             int nDual = EQ.dimY();
190             if (nDual > n) {
191                 throw new MathIllegalArgumentException(LocalizedOptimFormats.CONSTRAINTS_RANK, nDual, n);
192             }
193             int nTest = EQ.dim();
194             if (nDual == 0) {
195                 throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_NOT_ALLOWED);
196             }
197             MathUtils.checkDimension(nTest, n);
198         }
199 
200     }
201 
202 }