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.linear.ArrayRealVector;
20  import org.hipparchus.linear.RealMatrix;
21  import org.hipparchus.linear.RealVector;
22  
23  /**
24   * Adapter representing the inequality constraint set of an {@link SQPProblem}
25   * as an {@link InequalityConstraint} suitable for use by SQP solvers.
26   *
27   * <p>
28   * This wrapper delegates the evaluation and Jacobian computation of inequality
29   * constraints back to the underlying {@link SQPProblem}. The dimension of the
30   * constraint mapping and lower bounds are derived directly from the problem.
31   * </p>
32   *
33   * <p>
34   * Inequality constraints are assumed to follow the canonical form:
35   * </p>
36   * <pre>
37   *     {@code g(x) >= lb}
38   * </pre>
39   * <p>
40   * where {@code lb} is given by {@link SQPProblem#getIneqConstraintLB()}.
41   * </p>
42   *
43   * <p>
44   * An instance of this class is normally constructed automatically by SQP
45   * optimizers when processing {@link org.hipparchus.optim.OptimizationData}. It serves as a
46   * structural adapter layer between user-supplied models and solver APIs.
47   * </p>
48   *
49   * @since 4.1
50   */
51  public class SQPIneq extends InequalityConstraint {
52  
53      /** Reference to the originating SQP problem definition. */
54      private final SQPProblem problem;
55  
56      /**
57       * Creates an inequality constraint adapter for the given {@link SQPProblem}.
58       *
59       * @param problem the problem model supplying inequality constraint values
60       *                and their Jacobian
61       */
62      public SQPIneq(final SQPProblem problem) {
63          super(problem.getIneqConstraintLB());
64          this.problem = problem;
65      }
66  
67      /**
68       * {@inheritDoc}
69       *
70       * <p>
71       * Delegates to {@link SQPProblem#getdim()}.
72       * </p>
73       */
74      @Override
75      public int dim() {
76          return problem.getdim();
77      }
78  
79      /**
80       * {@inheritDoc}
81       *
82       * <p>
83       * Returns the number of inequality constraint components. If no lower
84       * bounds are supplied, the mapping is assumed empty.
85       * </p>
86       */
87      @Override
88      public int dimY() {
89          // getIneqConstraintLB is never null because it was checked at construction
90          return problem.getIneqConstraintLB().getDimension();
91      }
92  
93      /**
94       * {@inheritDoc}
95       *
96       * <p>
97       * Delegates evaluation to
98       * {@link SQPProblem#getIneqConstraintEvaluation(RealVector)}.
99       * </p>
100      */
101     @Override
102     public RealVector value(final RealVector rv) {
103         return problem.getIneqConstraintEvaluation(rv);
104     }
105 
106     /**
107      * {@inheritDoc}
108      *
109      * <p>
110      * Delegates to
111      * {@link SQPProblem#getIneqConstraintJacobian(RealVector)}.
112      * </p>
113      */
114     @Override
115     public RealMatrix jacobian(final RealVector rv) {
116         return problem.getIneqConstraintJacobian(rv);
117     }
118 
119     /**
120      * {@inheritDoc}
121      *
122      * <p>
123      * Convenience overload that evaluates constraints given a raw array input.
124      * Converts the input to {@link ArrayRealVector} and delegates to
125      * {@link SQPProblem#getIneqConstraintEvaluation(RealVector)}.
126      * </p>
127      *
128      */
129     @Override
130     public double[] value(final double[] doubles) {
131         return problem.getIneqConstraintEvaluation(new ArrayRealVector(doubles)).toArray();
132     }
133 }