LinearObjectiveFunction.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.analysis.MultivariateFunction;
  19. import org.apache.commons.math4.legacy.linear.ArrayRealVector;
  20. import org.apache.commons.math4.legacy.linear.RealVector;
  21. import org.apache.commons.math4.legacy.optim.OptimizationData;

  22. /**
  23.  * An objective function for a linear optimization problem.
  24.  * <p>
  25.  * A linear objective function has one the form:
  26.  * <div style="white-space: pre"><code>
  27.  * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
  28.  * </code></div>
  29.  * The c<sub>i</sub> and d are the coefficients of the equation,
  30.  * the x<sub>i</sub> are the coordinates of the current point.
  31.  *
  32.  * @since 2.0
  33.  */
  34. public class LinearObjectiveFunction
  35.     implements MultivariateFunction,
  36.                OptimizationData {
  37.     /** Coefficients of the linear equation (c<sub>i</sub>). */
  38.     private final RealVector coefficients;
  39.     /** Constant term of the linear equation. */
  40.     private final double constantTerm;

  41.     /**
  42.      * @param coefficients Coefficients for the linear equation being optimized.
  43.      * @param constantTerm Constant term of the linear equation.
  44.      */
  45.     public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
  46.         this(new ArrayRealVector(coefficients), constantTerm);
  47.     }

  48.     /**
  49.      * @param coefficients Coefficients for the linear equation being optimized.
  50.      * @param constantTerm Constant term of the linear equation.
  51.      */
  52.     public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
  53.         this.coefficients = coefficients;
  54.         this.constantTerm = constantTerm;
  55.     }

  56.     /**
  57.      * Gets the coefficients of the linear equation being optimized.
  58.      *
  59.      * @return coefficients of the linear equation being optimized.
  60.      */
  61.     public RealVector getCoefficients() {
  62.         return coefficients;
  63.     }

  64.     /**
  65.      * Gets the constant of the linear equation being optimized.
  66.      *
  67.      * @return constant of the linear equation being optimized.
  68.      */
  69.     public double getConstantTerm() {
  70.         return constantTerm;
  71.     }

  72.     /**
  73.      * Computes the value of the linear equation at the current point.
  74.      *
  75.      * @param point Point at which linear equation must be evaluated.
  76.      * @return the value of the linear equation at the current point.
  77.      */
  78.     @Override
  79.     public double value(final double[] point) {
  80.         return value(new ArrayRealVector(point, false));
  81.     }

  82.     /**
  83.      * Computes the value of the linear equation at the current point.
  84.      *
  85.      * @param point Point at which linear equation must be evaluated.
  86.      * @return the value of the linear equation at the current point.
  87.      */
  88.     public double value(final RealVector point) {
  89.         return coefficients.dotProduct(point) + constantTerm;
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public boolean equals(Object other) {
  94.         if (this == other) {
  95.             return true;
  96.         }
  97.         if (other instanceof LinearObjectiveFunction) {
  98.             LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
  99.           return constantTerm == rhs.constantTerm && coefficients.equals(rhs.coefficients);
  100.         }

  101.         return false;
  102.     }

  103.     /** {@inheritDoc} */
  104.     @Override
  105.     public int hashCode() {
  106.         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
  107.     }
  108. }