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 exposing the equality constraint set of an {@link SQPProblem}
25   * through the {@link EqualityConstraint} abstraction used by SQP solvers.
26   *
27   * <p>
28   * This wrapper delegates equality constraint evaluation and Jacobian
29   * computation to the underlying {@link SQPProblem}. The dimensionality
30   * of the constraint mapping and the associated lower bounds are deduced
31   * directly from the problem instance.
32   * </p>
33   *
34   * <p>
35   * Equality constraints typically model conditions of the form:
36   * </p>
37   * <pre>
38   *     h(x) = 0
39   * </pre>
40   * <p>
41   * but some SQP formulations internally interpret equalities through
42   * a bounded form (e.g. {@code h(x) >= lb} and {@code h(x) <= ub}).
43   * Here, only the lower bound is provided via
44   * {@link SQPProblem#getEqConstraintLB()}, consistent with the
45   * {@link EqualityConstraint} superclass.
46   * </p>
47   *
48   * <p>
49   * Instances of this class are commonly created internally by SQP
50   * optimizers when parsing user-supplied {@link org.hipparchus.optim.OptimizationData}.
51   * It acts as an adapter layer between domain models and solver APIs.
52   * </p>
53   *
54   * @since 4.1
55   */
56  public class SQPEq extends EqualityConstraint {
57  
58      /** Reference to the originating SQP model. */
59      private final SQPProblem problem;
60  
61      /**
62       * Constructs an equality constraint wrapper around the supplied
63       * {@link SQPProblem}.
64       *
65       * @param problem optimization model providing equality constraint
66       *                functions and gradients
67       */
68      public SQPEq(final SQPProblem problem) {
69          super(problem.getEqConstraintLB());
70          this.problem = problem;
71      }
72  
73      /**
74       * {@inheritDoc}
75       *
76       * <p>
77       * Delegates to {@link SQPProblem#getdim()}.
78       * </p>
79       */
80      @Override
81      public int dim() {
82          return problem.getdim();
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public int dimY() {
90          // getEqConstraintLB is never null because it was checked at construction
91          return problem.getEqConstraintLB().getDimension();
92      }
93  
94      /**
95       * {@inheritDoc}
96       *
97       * <p>
98       * Delegates evaluation to
99       * {@link SQPProblem#getEqConstraintEvaluation(RealVector)}.
100      * </p>
101      */
102     @Override
103     public RealVector value(final RealVector rv) {
104         return problem.getEqConstraintEvaluation(rv);
105     }
106 
107     /**
108      * {@inheritDoc}
109      *
110      * <p>
111      * Delegates to
112      * {@link SQPProblem#getEqConstraintJacobian(RealVector)}.
113      * </p>
114      */
115     @Override
116     public RealMatrix jacobian(final RealVector rv) {
117         return problem.getEqConstraintJacobian(rv);
118     }
119 
120     /**
121      * {@inheritDoc}
122      *
123      * <p>
124      * Convenience overload evaluating equality constraints for a raw
125      * primitive array. Converts the input to {@link ArrayRealVector}
126      * before delegation.
127      * </p>
128      *
129      */
130     @Override
131     public double[] value(final double[] doubles) {
132         return problem.getEqConstraintEvaluation(new ArrayRealVector(doubles)).toArray();
133     }
134 }