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.optim.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.  *
  44.  * @since 2.0
  45.  */
  46. public class LinearConstraint implements Serializable {
  47.     /** Serializable version identifier. */
  48.     private static final long serialVersionUID = -764632794033034092L;
  49.     /** Coefficients of the constraint (left hand side). */
  50.     private final transient RealVector coefficients;
  51.     /** Relationship between left and right hand sides (=, &lt;=, >=). */
  52.     private final Relationship relationship;
  53.     /** Value of the constraint (right hand side). */
  54.     private final double value;

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

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

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

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

  159.     /**
  160.      * Gets the relationship between left and right hand sides.
  161.      *
  162.      * @return the relationship between left and right hand sides.
  163.      */
  164.     public Relationship getRelationship() {
  165.         return relationship;
  166.     }

  167.     /**
  168.      * Gets the value of the constraint (right hand side).
  169.      *
  170.      * @return the value of the constraint (right hand side).
  171.      */
  172.     public double getValue() {
  173.         return value;
  174.     }

  175.     @Override
  176.     public boolean equals(Object other) {
  177.         if (this == other) {
  178.             return true;
  179.         }
  180.         if (other instanceof LinearConstraint) {
  181.             LinearConstraint rhs = (LinearConstraint) other;
  182.             return relationship == rhs.relationship &&
  183.                 value == rhs.value &&
  184.                 coefficients.equals(rhs.coefficients);
  185.         }
  186.         return false;
  187.     }

  188.     @Override
  189.     public int hashCode() {
  190.         return relationship.hashCode() ^
  191.             Double.valueOf(value).hashCode() ^
  192.             coefficients.hashCode();
  193.     }

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

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