View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim.linear;
23  
24  import java.io.IOException;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  import java.io.Serializable;
28  
29  import org.hipparchus.analysis.MultivariateFunction;
30  import org.hipparchus.linear.ArrayRealVector;
31  import org.hipparchus.linear.RealVector;
32  import org.hipparchus.optim.OptimizationData;
33  
34  /**
35   * An objective function for a linear optimization problem.
36   * <p>
37   * A linear objective function has one the form:
38   * \[
39   * c_1 x_1 + \ldots c_n x_n + d
40   * \]
41   * The c<sub>i</sub> and d are the coefficients of the equation,
42   * the x<sub>i</sub> are the coordinates of the current point.
43   * </p>
44   *
45   */
46  public class LinearObjectiveFunction
47      implements MultivariateFunction,
48                 OptimizationData,
49                 Serializable {
50      /** Serializable version identifier. */
51      private static final long serialVersionUID = -4531815507568396090L;
52      /** Coefficients of the linear equation (c<sub>i</sub>). */
53      private final transient RealVector coefficients;
54      /** Constant term of the linear equation. */
55      private final double constantTerm;
56  
57      /** Simple constructor.
58       * @param coefficients Coefficients for the linear equation being optimized.
59       * @param constantTerm Constant term of the linear equation.
60       */
61      public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
62          this(new ArrayRealVector(coefficients), constantTerm);
63      }
64  
65      /** Simple constructor.
66       * @param coefficients Coefficients for the linear equation being optimized.
67       * @param constantTerm Constant term of the linear equation.
68       */
69      public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
70          this.coefficients = coefficients;
71          this.constantTerm = constantTerm;
72      }
73  
74      /**
75       * Gets the coefficients of the linear equation being optimized.
76       *
77       * @return coefficients of the linear equation being optimized.
78       */
79      public RealVector getCoefficients() {
80          return coefficients;
81      }
82  
83      /**
84       * Gets the constant of the linear equation being optimized.
85       *
86       * @return constant of the linear equation being optimized.
87       */
88      public double getConstantTerm() {
89          return constantTerm;
90      }
91  
92      /**
93       * Computes the value of the linear equation at the current point.
94       *
95       * @param point Point at which linear equation must be evaluated.
96       * @return the value of the linear equation at the current point.
97       */
98      @Override
99      public double value(final double[] point) {
100         return value(new ArrayRealVector(point, false));
101     }
102 
103     /**
104      * Computes the value of the linear equation at the current point.
105      *
106      * @param point Point at which linear equation must be evaluated.
107      * @return the value of the linear equation at the current point.
108      */
109     public double value(final RealVector point) {
110         return coefficients.dotProduct(point) + constantTerm;
111     }
112 
113     /** {@inheritDoc} */
114     @Override
115     public boolean equals(Object other) {
116         if (this == other) {
117             return true;
118         }
119         if (other instanceof LinearObjectiveFunction) {
120             LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
121           return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
122         }
123 
124         return false;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public int hashCode() {
130         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
131     }
132 
133     /**
134      * Serialize the instance.
135      * @param oos stream where object should be written
136      * @throws IOException if object cannot be written to stream
137      */
138     private void writeObject(ObjectOutputStream oos)
139         throws IOException {
140         oos.defaultWriteObject();
141         final int n = coefficients.getDimension();
142         oos.writeInt(n);
143         for (int i = 0; i < n; ++i) {
144             oos.writeDouble(coefficients.getEntry(i));
145         }
146     }
147 
148     /**
149      * Deserialize the instance.
150      * @param ois stream from which the object should be read
151      * @throws ClassNotFoundException if a class in the stream cannot be found
152      * @throws IOException if object cannot be read from the stream
153      */
154     private void readObject(ObjectInputStream ois)
155       throws ClassNotFoundException, IOException {
156         ois.defaultReadObject();
157 
158         // read the vector data
159         final int n = ois.readInt();
160         final double[] data = new double[n];
161         for (int i = 0; i < n; ++i) {
162             data[i] = ois.readDouble();
163         }
164 
165         try {
166             // create the instance
167             ArrayRealVector vector = new ArrayRealVector(data, false);
168             final java.lang.reflect.Field f = getClass().getDeclaredField("coefficients");
169             f.setAccessible(true); // NOPMD
170             f.set(this, vector);
171         } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
172             IOException ioe = new IOException();
173             ioe.initCause(e);
174             throw ioe;
175         }
176     }
177 }