View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) 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 ASF 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    *      http://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  
18  package org.apache.commons.math3.optimization.linear;
19  
20  import java.io.IOException;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  import java.io.Serializable;
24  
25  import org.apache.commons.math3.linear.MatrixUtils;
26  import org.apache.commons.math3.linear.RealVector;
27  import org.apache.commons.math3.linear.ArrayRealVector;
28  
29  
30  /**
31   * A linear constraint for a linear optimization problem.
32   * <p>
33   * A linear constraint has one of the forms:
34   * <ul>
35   *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
36   *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
37   *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
38   *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
39   *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
40   *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
41   *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
42   *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
43   *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
44   * </ul>
45   * The c<sub>i</sub>, l<sub>i</sub> or r<sub>i</sub> are the coefficients of the constraints, the x<sub>i</sub>
46   * are the coordinates of the current point and v is the value of the constraint.
47   * </p>
48   * @version $Id: LinearConstraint.java 1422230 2012-12-15 12:11:13Z erans $
49   * @deprecated As of 3.1 (to be removed in 4.0).
50   * @since 2.0
51   */
52  @Deprecated
53  public class LinearConstraint implements Serializable {
54  
55      /** Serializable version identifier. */
56      private static final long serialVersionUID = -764632794033034092L;
57  
58      /** Coefficients of the constraint (left hand side). */
59      private final transient RealVector coefficients;
60  
61      /** Relationship between left and right hand sides (=, &lt;=, >=). */
62      private final Relationship relationship;
63  
64      /** Value of the constraint (right hand side). */
65      private final double value;
66  
67      /**
68       * Build a constraint involving a single linear equation.
69       * <p>
70       * A linear constraint with a single linear equation has one of the forms:
71       * <ul>
72       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
73       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
74       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
75       * </ul>
76       * </p>
77       * @param coefficients The coefficients of the constraint (left hand side)
78       * @param relationship The type of (in)equality used in the constraint
79       * @param value The value of the constraint (right hand side)
80       */
81      public LinearConstraint(final double[] coefficients, final Relationship relationship,
82                              final double value) {
83          this(new ArrayRealVector(coefficients), relationship, value);
84      }
85  
86      /**
87       * Build a constraint involving a single linear equation.
88       * <p>
89       * A linear constraint with a single linear equation has one of the forms:
90       * <ul>
91       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
92       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
93       *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
94       * </ul>
95       * </p>
96       * @param coefficients The coefficients of the constraint (left hand side)
97       * @param relationship The type of (in)equality used in the constraint
98       * @param value The value of the constraint (right hand side)
99       */
100     public LinearConstraint(final RealVector coefficients, final Relationship relationship,
101                             final double value) {
102         this.coefficients = coefficients;
103         this.relationship = relationship;
104         this.value        = value;
105     }
106 
107     /**
108      * Build a constraint involving two linear equations.
109      * <p>
110      * A linear constraint with two linear equation has one of the forms:
111      * <ul>
112      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
113      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
114      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
115      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
116      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
117      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
118      * </ul>
119      * </p>
120      * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
121      * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
122      * @param relationship The type of (in)equality used in the constraint
123      * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
124      * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
125      */
126     public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant,
127                             final Relationship relationship,
128                             final double[] rhsCoefficients, final double rhsConstant) {
129         double[] sub = new double[lhsCoefficients.length];
130         for (int i = 0; i < sub.length; ++i) {
131             sub[i] = lhsCoefficients[i] - rhsCoefficients[i];
132         }
133         this.coefficients = new ArrayRealVector(sub, false);
134         this.relationship = relationship;
135         this.value        = rhsConstant - lhsConstant;
136     }
137 
138     /**
139      * Build a constraint involving two linear equations.
140      * <p>
141      * A linear constraint with two linear equation has one of the forms:
142      * <ul>
143      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
144      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
145      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
146      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
147      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
148      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
149      * </ul>
150      * </p>
151      * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
152      * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
153      * @param relationship The type of (in)equality used in the constraint
154      * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
155      * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
156      */
157     public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant,
158                             final Relationship relationship,
159                             final RealVector rhsCoefficients, final double rhsConstant) {
160         this.coefficients = lhsCoefficients.subtract(rhsCoefficients);
161         this.relationship = relationship;
162         this.value        = rhsConstant - lhsConstant;
163     }
164 
165     /**
166      * Get the coefficients of the constraint (left hand side).
167      * @return coefficients of the constraint (left hand side)
168      */
169     public RealVector getCoefficients() {
170         return coefficients;
171     }
172 
173     /**
174      * Get the relationship between left and right hand sides.
175      * @return relationship between left and right hand sides
176      */
177     public Relationship getRelationship() {
178         return relationship;
179     }
180 
181     /**
182      * Get the value of the constraint (right hand side).
183      * @return value of the constraint (right hand side)
184      */
185     public double getValue() {
186         return value;
187     }
188 
189     @Override
190     public boolean equals(Object other) {
191 
192       if (this == other) {
193         return true;
194       }
195 
196       if (other instanceof LinearConstraint) {
197           LinearConstraint rhs = (LinearConstraint) other;
198           return (relationship == rhs.relationship) &&
199                  (value        == rhs.value) &&
200                  coefficients.equals(rhs.coefficients);
201       }
202       return false;
203     }
204 
205     @Override
206     public int hashCode() {
207         return relationship.hashCode() ^
208                Double.valueOf(value).hashCode() ^
209                coefficients.hashCode();
210     }
211 
212     /**
213      * Serialize the instance.
214      * @param oos stream where object should be written
215      * @throws IOException if object cannot be written to stream
216      */
217     private void writeObject(ObjectOutputStream oos)
218         throws IOException {
219         oos.defaultWriteObject();
220         MatrixUtils.serializeRealVector(coefficients, oos);
221     }
222 
223     /**
224      * Deserialize the instance.
225      * @param ois stream from which the object should be read
226      * @throws ClassNotFoundException if a class in the stream cannot be found
227      * @throws IOException if object cannot be read from the stream
228      */
229     private void readObject(ObjectInputStream ois)
230       throws ClassNotFoundException, IOException {
231         ois.defaultReadObject();
232         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
233     }
234 
235 }