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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.linear;

  22. import java.io.IOException;
  23. import java.io.ObjectInputStream;
  24. import java.io.ObjectOutputStream;
  25. import java.io.Serializable;

  26. import org.hipparchus.analysis.MultivariateFunction;
  27. import org.hipparchus.linear.ArrayRealVector;
  28. import org.hipparchus.linear.RealVector;
  29. import org.hipparchus.optim.OptimizationData;

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

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

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

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

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

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

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

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public boolean equals(Object other) {
  105.         if (this == other) {
  106.             return true;
  107.         }
  108.         if (other instanceof LinearObjectiveFunction) {
  109.             LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
  110.           return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
  111.         }

  112.         return false;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public int hashCode() {
  117.         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
  118.     }

  119.     /**
  120.      * Serialize the instance.
  121.      * @param oos stream where object should be written
  122.      * @throws IOException if object cannot be written to stream
  123.      */
  124.     private void writeObject(ObjectOutputStream oos)
  125.         throws IOException {
  126.         oos.defaultWriteObject();
  127.         final int n = coefficients.getDimension();
  128.         oos.writeInt(n);
  129.         for (int i = 0; i < n; ++i) {
  130.             oos.writeDouble(coefficients.getEntry(i));
  131.         }
  132.     }

  133.     /**
  134.      * Deserialize the instance.
  135.      * @param ois stream from which the object should be read
  136.      * @throws ClassNotFoundException if a class in the stream cannot be found
  137.      * @throws IOException if object cannot be read from the stream
  138.      */
  139.     private void readObject(ObjectInputStream ois)
  140.       throws ClassNotFoundException, IOException {
  141.         ois.defaultReadObject();

  142.         // read the vector data
  143.         final int n = ois.readInt();
  144.         final double[] data = new double[n];
  145.         for (int i = 0; i < n; ++i) {
  146.             data[i] = ois.readDouble();
  147.         }

  148.         try {
  149.             // create the instance
  150.             ArrayRealVector vector = new ArrayRealVector(data, false);
  151.             final java.lang.reflect.Field f = getClass().getDeclaredField("coefficients");
  152.             f.setAccessible(true); // NOPMD
  153.             f.set(this, vector);
  154.         } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
  155.             IOException ioe = new IOException();
  156.             ioe.initCause(e);
  157.             throw ioe;
  158.         }
  159.     }
  160. }