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.math4.legacy.optim.linear;

  18. import org.apache.commons.math4.legacy.linear.ArrayRealVector;
  19. import org.apache.commons.math4.legacy.linear.RealVector;

  20. /**
  21.  * A linear constraint for a linear optimization problem.
  22.  * <p>
  23.  * A linear constraint has one of the forms:
  24.  * <ul>
  25.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> = v</li>
  26.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &lt;= v</li>
  27.  *   <li>c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> &gt;= v</li>
  28.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> =
  29.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  30.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &lt;=
  31.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  32.  *   <li>l<sub>1</sub>x<sub>1</sub> + ... l<sub>n</sub>x<sub>n</sub> + l<sub>cst</sub> &gt;=
  33.  *       r<sub>1</sub>x<sub>1</sub> + ... r<sub>n</sub>x<sub>n</sub> + r<sub>cst</sub></li>
  34.  * </ul>
  35.  * 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>
  36.  * are the coordinates of the current point and v is the value of the constraint.
  37.  *
  38.  * @since 2.0
  39.  */
  40. public class LinearConstraint {
  41.     /** Coefficients of the constraint (left hand side). */
  42.     private final RealVector coefficients;
  43.     /** Relationship between left and right hand sides {@code (=, <=, >=)}. */
  44.     private final Relationship relationship;
  45.     /** Value of the constraint (right hand side). */
  46.     private final double value;

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

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

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

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

  143.     /**
  144.      * Gets the coefficients of the constraint (left hand side).
  145.      *
  146.      * @return the coefficients of the constraint (left hand side).
  147.      */
  148.     public RealVector getCoefficients() {
  149.         return coefficients;
  150.     }

  151.     /**
  152.      * Gets the relationship between left and right hand sides.
  153.      *
  154.      * @return the relationship between left and right hand sides.
  155.      */
  156.     public Relationship getRelationship() {
  157.         return relationship;
  158.     }

  159.     /**
  160.      * Gets the value of the constraint (right hand side).
  161.      *
  162.      * @return the value of the constraint (right hand side).
  163.      */
  164.     public double getValue() {
  165.         return value;
  166.     }

  167.     /** {@inheritDoc} */
  168.     @Override
  169.     public boolean equals(Object other) {
  170.         if (this == other) {
  171.             return true;
  172.         }
  173.         if (other instanceof LinearConstraint) {
  174.             LinearConstraint rhs = (LinearConstraint) other;
  175.             return relationship == rhs.relationship &&
  176.                 value == rhs.value &&
  177.                 coefficients.equals(rhs.coefficients);
  178.         }
  179.         return false;
  180.     }

  181.     /** {@inheritDoc} */
  182.     @Override
  183.     public int hashCode() {
  184.         return relationship.hashCode() ^
  185.             Double.valueOf(value).hashCode() ^
  186.             coefficients.hashCode();
  187.     }
  188. }