LinearConstraint.java

  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. package org.apache.commons.math3.optimization.linear;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;

  22. import org.apache.commons.math3.linear.MatrixUtils;
  23. import org.apache.commons.math3.linear.RealVector;
  24. import org.apache.commons.math3.linear.ArrayRealVector;


  25. /**
  26.  * A linear constraint for a linear optimization problem.
  27.  * <p>
  28.  * A linear constraint has one of the forms:
  29.  * <ul>
  30.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
  31.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
  32.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
  33.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
  34.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  35.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
  36.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  37.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
  38.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  39.  * </ul>
  40.  * 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>
  41.  * are the coordinates of the current point and v is the value of the constraint.
  42.  * </p>
  43.  * @deprecated As of 3.1 (to be removed in 4.0).
  44.  * @since 2.0
  45.  */
  46. @Deprecated
  47. public class LinearConstraint implements Serializable {

  48.     /** Serializable version identifier. */
  49.     private static final long serialVersionUID = -764632794033034092L;

  50.     /** Coefficients of the constraint (left hand side). */
  51.     private final transient RealVector coefficients;

  52.     /** Relationship between left and right hand sides (=, &lt;=, >=). */
  53.     private final Relationship relationship;

  54.     /** Value of the constraint (right hand side). */
  55.     private final double value;

  56.     /**
  57.      * Build a constraint involving a single linear equation.
  58.      * <p>
  59.      * A linear constraint with a single linear equation has one of the forms:
  60.      * <ul>
  61.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
  62.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
  63.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
  64.      * </ul>
  65.      * </p>
  66.      * @param coefficients The coefficients of the constraint (left hand side)
  67.      * @param relationship The type of (in)equality used in the constraint
  68.      * @param value The value of the constraint (right hand side)
  69.      */
  70.     public LinearConstraint(final double[] coefficients, final Relationship relationship,
  71.                             final double value) {
  72.         this(new ArrayRealVector(coefficients), relationship, value);
  73.     }

  74.     /**
  75.      * Build a constraint involving a single linear equation.
  76.      * <p>
  77.      * A linear constraint with a single linear equation has one of the forms:
  78.      * <ul>
  79.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
  80.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
  81.      *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> >= v</li>
  82.      * </ul>
  83.      * </p>
  84.      * @param coefficients The coefficients of the constraint (left hand side)
  85.      * @param relationship The type of (in)equality used in the constraint
  86.      * @param value The value of the constraint (right hand side)
  87.      */
  88.     public LinearConstraint(final RealVector coefficients, final Relationship relationship,
  89.                             final double value) {
  90.         this.coefficients = coefficients;
  91.         this.relationship = relationship;
  92.         this.value        = value;
  93.     }

  94.     /**
  95.      * Build a constraint involving two linear equations.
  96.      * <p>
  97.      * A linear constraint with two linear equation has one of the forms:
  98.      * <ul>
  99.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
  100.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  101.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
  102.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  103.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
  104.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  105.      * </ul>
  106.      * </p>
  107.      * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
  108.      * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
  109.      * @param relationship The type of (in)equality used in the constraint
  110.      * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
  111.      * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
  112.      */
  113.     public LinearConstraint(final double[] lhsCoefficients, final double lhsConstant,
  114.                             final Relationship relationship,
  115.                             final double[] rhsCoefficients, final double rhsConstant) {
  116.         double[] sub = new double[lhsCoefficients.length];
  117.         for (int i = 0; i < sub.length; ++i) {
  118.             sub[i] = lhsCoefficients[i] - rhsCoefficients[i];
  119.         }
  120.         this.coefficients = new ArrayRealVector(sub, false);
  121.         this.relationship = relationship;
  122.         this.value        = rhsConstant - lhsConstant;
  123.     }

  124.     /**
  125.      * Build a constraint involving two linear equations.
  126.      * <p>
  127.      * A linear constraint with two linear equation has one of the forms:
  128.      * <ul>
  129.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
  130.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  131.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
  132.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  133.      *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> >=
  134.      *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  135.      * </ul>
  136.      * </p>
  137.      * @param lhsCoefficients The coefficients of the linear expression on the left hand side of the constraint
  138.      * @param lhsConstant The constant term of the linear expression on the left hand side of the constraint
  139.      * @param relationship The type of (in)equality used in the constraint
  140.      * @param rhsCoefficients The coefficients of the linear expression on the right hand side of the constraint
  141.      * @param rhsConstant The constant term of the linear expression on the right hand side of the constraint
  142.      */
  143.     public LinearConstraint(final RealVector lhsCoefficients, final double lhsConstant,
  144.                             final Relationship relationship,
  145.                             final RealVector rhsCoefficients, final double rhsConstant) {
  146.         this.coefficients = lhsCoefficients.subtract(rhsCoefficients);
  147.         this.relationship = relationship;
  148.         this.value        = rhsConstant - lhsConstant;
  149.     }

  150.     /**
  151.      * Get the coefficients of the constraint (left hand side).
  152.      * @return coefficients of the constraint (left hand side)
  153.      */
  154.     public RealVector getCoefficients() {
  155.         return coefficients;
  156.     }

  157.     /**
  158.      * Get the relationship between left and right hand sides.
  159.      * @return relationship between left and right hand sides
  160.      */
  161.     public Relationship getRelationship() {
  162.         return relationship;
  163.     }

  164.     /**
  165.      * Get the value of the constraint (right hand side).
  166.      * @return value of the constraint (right hand side)
  167.      */
  168.     public double getValue() {
  169.         return value;
  170.     }

  171.     @Override
  172.     public boolean equals(Object other) {

  173.       if (this == other) {
  174.         return true;
  175.       }

  176.       if (other instanceof LinearConstraint) {
  177.           LinearConstraint rhs = (LinearConstraint) other;
  178.           return (relationship == rhs.relationship) &&
  179.                  (value        == rhs.value) &&
  180.                  coefficients.equals(rhs.coefficients);
  181.       }
  182.       return false;
  183.     }

  184.     @Override
  185.     public int hashCode() {
  186.         return relationship.hashCode() ^
  187.                Double.valueOf(value).hashCode() ^
  188.                coefficients.hashCode();
  189.     }

  190.     /**
  191.      * Serialize the instance.
  192.      * @param oos stream where object should be written
  193.      * @throws IOException if object cannot be written to stream
  194.      */
  195.     private void writeObject(ObjectOutputStream oos)
  196.         throws IOException {
  197.         oos.defaultWriteObject();
  198.         MatrixUtils.serializeRealVector(coefficients, oos);
  199.     }

  200.     /**
  201.      * Deserialize the instance.
  202.      * @param ois stream from which the object should be read
  203.      * @throws ClassNotFoundException if a class in the stream cannot be found
  204.      * @throws IOException if object cannot be read from the stream
  205.      */
  206.     private void readObject(ObjectInputStream ois)
  207.       throws ClassNotFoundException, IOException {
  208.         ois.defaultReadObject();
  209.         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
  210.     }

  211. }