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.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.analysis.MultivariateFunction;
  23. import org.apache.commons.math3.linear.MatrixUtils;
  24. import org.apache.commons.math3.linear.RealVector;
  25. import org.apache.commons.math3.linear.ArrayRealVector;
  26. import org.apache.commons.math3.optim.OptimizationData;

  27. /**
  28.  * An objective function for a linear optimization problem.
  29.  * <p>
  30.  * A linear objective function has one the form:
  31.  * <pre>
  32.  * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
  33.  * </pre>
  34.  * The c<sub>i</sub> and d are the coefficients of the equation,
  35.  * the x<sub>i</sub> are the coordinates of the current point.
  36.  * </p>
  37.  *
  38.  * @since 2.0
  39.  */
  40. public class LinearObjectiveFunction
  41.     implements MultivariateFunction,
  42.                OptimizationData,
  43.                Serializable {
  44.     /** Serializable version identifier. */
  45.     private static final long serialVersionUID = -4531815507568396090L;
  46.     /** Coefficients of the linear equation (c<sub>i</sub>). */
  47.     private final transient RealVector coefficients;
  48.     /** Constant term of the linear equation. */
  49.     private final double constantTerm;

  50.     /**
  51.      * @param coefficients Coefficients for the linear equation being optimized.
  52.      * @param constantTerm Constant term of the linear equation.
  53.      */
  54.     public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
  55.         this(new ArrayRealVector(coefficients), constantTerm);
  56.     }

  57.     /**
  58.      * @param coefficients Coefficients for the linear equation being optimized.
  59.      * @param constantTerm Constant term of the linear equation.
  60.      */
  61.     public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
  62.         this.coefficients = coefficients;
  63.         this.constantTerm = constantTerm;
  64.     }

  65.     /**
  66.      * Gets the coefficients of the linear equation being optimized.
  67.      *
  68.      * @return coefficients of the linear equation being optimized.
  69.      */
  70.     public RealVector getCoefficients() {
  71.         return coefficients;
  72.     }

  73.     /**
  74.      * Gets the constant of the linear equation being optimized.
  75.      *
  76.      * @return constant of the linear equation being optimized.
  77.      */
  78.     public double getConstantTerm() {
  79.         return constantTerm;
  80.     }

  81.     /**
  82.      * Computes the value of the linear equation at the current point.
  83.      *
  84.      * @param point Point at which linear equation must be evaluated.
  85.      * @return the value of the linear equation at the current point.
  86.      */
  87.     public double value(final double[] point) {
  88.         return value(new ArrayRealVector(point, false));
  89.     }

  90.     /**
  91.      * Computes the value of the linear equation at the current point.
  92.      *
  93.      * @param point Point at which linear equation must be evaluated.
  94.      * @return the value of the linear equation at the current point.
  95.      */
  96.     public double value(final RealVector point) {
  97.         return coefficients.dotProduct(point) + constantTerm;
  98.     }

  99.     @Override
  100.     public boolean equals(Object other) {
  101.         if (this == other) {
  102.             return true;
  103.         }
  104.         if (other instanceof LinearObjectiveFunction) {
  105.             LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
  106.           return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
  107.         }

  108.         return false;
  109.     }

  110.     @Override
  111.     public int hashCode() {
  112.         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
  113.     }

  114.     /**
  115.      * Serialize the instance.
  116.      * @param oos stream where object should be written
  117.      * @throws IOException if object cannot be written to stream
  118.      */
  119.     private void writeObject(ObjectOutputStream oos)
  120.         throws IOException {
  121.         oos.defaultWriteObject();
  122.         MatrixUtils.serializeRealVector(coefficients, oos);
  123.     }

  124.     /**
  125.      * Deserialize the instance.
  126.      * @param ois stream from which the object should be read
  127.      * @throws ClassNotFoundException if a class in the stream cannot be found
  128.      * @throws IOException if object cannot be read from the stream
  129.      */
  130.     private void readObject(ObjectInputStream ois)
  131.       throws ClassNotFoundException, IOException {
  132.         ois.defaultReadObject();
  133.         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
  134.     }
  135. }