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    *      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  
18  package org.apache.commons.math3.optimization.linear;
19  
20  import java.io.IOException;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  import java.io.Serializable;
24  
25  import org.apache.commons.math3.linear.MatrixUtils;
26  import org.apache.commons.math3.linear.RealVector;
27  import org.apache.commons.math3.linear.ArrayRealVector;
28  
29  /**
30   * An objective function for a linear optimization problem.
31   * <p>
32   * A linear objective function has one the form:
33   * <pre>
34   * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
35   * </pre>
36   * The c<sub>i</sub> and d are the coefficients of the equation,
37   * the x<sub>i</sub> are the coordinates of the current point.
38   * </p>
39   * @version $Id: LinearObjectiveFunction.java 1422230 2012-12-15 12:11:13Z erans $
40   * @deprecated As of 3.1 (to be removed in 4.0).
41   * @since 2.0
42   */
43  @Deprecated
44  public class LinearObjectiveFunction implements Serializable {
45  
46      /** Serializable version identifier. */
47      private static final long serialVersionUID = -4531815507568396090L;
48  
49      /** Coefficients of the constraint (c<sub>i</sub>). */
50      private final transient RealVector coefficients;
51  
52      /** Constant term of the linear equation. */
53      private final double constantTerm;
54  
55      /**
56       * @param coefficients The coefficients for the linear equation being optimized
57       * @param constantTerm The constant term of the linear equation
58       */
59      public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
60          this(new ArrayRealVector(coefficients), constantTerm);
61      }
62  
63      /**
64       * @param coefficients The coefficients for the linear equation being optimized
65       * @param constantTerm The constant term of the linear equation
66       */
67      public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
68          this.coefficients = coefficients;
69          this.constantTerm = constantTerm;
70      }
71  
72      /**
73       * Get the coefficients of the linear equation being optimized.
74       * @return coefficients of the linear equation being optimized
75       */
76      public RealVector getCoefficients() {
77          return coefficients;
78      }
79  
80      /**
81       * Get the constant of the linear equation being optimized.
82       * @return constant of the linear equation being optimized
83       */
84      public double getConstantTerm() {
85          return constantTerm;
86      }
87  
88      /**
89       * Compute the value of the linear equation at the current point
90       * @param point point at which linear equation must be evaluated
91       * @return value of the linear equation at the current point
92       */
93      public double getValue(final double[] point) {
94          return coefficients.dotProduct(new ArrayRealVector(point, false)) + constantTerm;
95      }
96  
97      /**
98       * Compute the value of the linear equation at the current point
99       * @param point point at which linear equation must be evaluated
100      * @return value of the linear equation at the current point
101      */
102     public double getValue(final RealVector point) {
103         return coefficients.dotProduct(point) + constantTerm;
104     }
105 
106     @Override
107     public boolean equals(Object other) {
108 
109       if (this == other) {
110         return true;
111       }
112 
113       if (other instanceof LinearObjectiveFunction) {
114           LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
115           return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
116       }
117 
118       return false;
119     }
120 
121     @Override
122     public int hashCode() {
123         return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
124     }
125 
126     /**
127      * Serialize the instance.
128      * @param oos stream where object should be written
129      * @throws IOException if object cannot be written to stream
130      */
131     private void writeObject(ObjectOutputStream oos)
132         throws IOException {
133         oos.defaultWriteObject();
134         MatrixUtils.serializeRealVector(coefficients, oos);
135     }
136 
137     /**
138      * Deserialize the instance.
139      * @param ois stream from which the object should be read
140      * @throws ClassNotFoundException if a class in the stream cannot be found
141      * @throws IOException if object cannot be read from the stream
142      */
143     private void readObject(ObjectInputStream ois)
144       throws ClassNotFoundException, IOException {
145         ois.defaultReadObject();
146         MatrixUtils.deserializeRealVector(this, "coefficients", ois);
147     }
148 
149 }