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.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.  * An objective function for a linear optimization problem.
  27.  * <p>
  28.  * A linear objective function has one the form:
  29.  * <pre>
  30.  * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
  31.  * </pre>
  32.  * The c<sub>i</sub> and d are the coefficients of the equation,
  33.  * the x<sub>i</sub> are the coordinates of the current point.
  34.  * </p>
  35.  * @deprecated As of 3.1 (to be removed in 4.0).
  36.  * @since 2.0
  37.  */
  38. @Deprecated
  39. public class LinearObjectiveFunction implements Serializable {

  40.     /** Serializable version identifier. */
  41.     private static final long serialVersionUID = -4531815507568396090L;

  42.     /** Coefficients of the constraint (c<sub>i</sub>). */
  43.     private final transient RealVector coefficients;

  44.     /** Constant term of the linear equation. */
  45.     private final double constantTerm;

  46.     /**
  47.      * @param coefficients The coefficients for the linear equation being optimized
  48.      * @param constantTerm The constant term of the linear equation
  49.      */
  50.     public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
  51.         this(new ArrayRealVector(coefficients), constantTerm);
  52.     }

  53.     /**
  54.      * @param coefficients The coefficients for the linear equation being optimized
  55.      * @param constantTerm The constant term of the linear equation
  56.      */
  57.     public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
  58.         this.coefficients = coefficients;
  59.         this.constantTerm = constantTerm;
  60.     }

  61.     /**
  62.      * Get the coefficients of the linear equation being optimized.
  63.      * @return coefficients of the linear equation being optimized
  64.      */
  65.     public RealVector getCoefficients() {
  66.         return coefficients;
  67.     }

  68.     /**
  69.      * Get the constant of the linear equation being optimized.
  70.      * @return constant of the linear equation being optimized
  71.      */
  72.     public double getConstantTerm() {
  73.         return constantTerm;
  74.     }

  75.     /**
  76.      * Compute the value of the linear equation at the current point
  77.      * @param point point at which linear equation must be evaluated
  78.      * @return value of the linear equation at the current point
  79.      */
  80.     public double getValue(final double[] point) {
  81.         return coefficients.dotProduct(new ArrayRealVector(point, false)) + constantTerm;
  82.     }

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

  91.     @Override
  92.     public boolean equals(Object other) {

  93.       if (this == other) {
  94.         return true;
  95.       }

  96.       if (other instanceof LinearObjectiveFunction) {
  97.           LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
  98.           return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
  99.       }

  100.       return false;
  101.     }

  102.     @Override
  103.     public int hashCode() {
  104.         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
  105.     }

  106.     /**
  107.      * Serialize the instance.
  108.      * @param oos stream where object should be written
  109.      * @throws IOException if object cannot be written to stream
  110.      */
  111.     private void writeObject(ObjectOutputStream oos)
  112.         throws IOException {
  113.         oos.defaultWriteObject();
  114.         MatrixUtils.serializeRealVector(coefficients, oos);
  115.     }

  116.     /**
  117.      * Deserialize the instance.
  118.      * @param ois stream from which the object should be read
  119.      * @throws ClassNotFoundException if a class in the stream cannot be found
  120.      * @throws IOException if object cannot be read from the stream
  121.      */
  122.     private void readObject(ObjectInputStream ois)
  123.       throws ClassNotFoundException, IOException {
  124.         ois.defaultReadObject();
  125.         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
  126.     }

  127. }