001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.math3.optimization.linear;
019    
020    import java.io.IOException;
021    import java.io.ObjectInputStream;
022    import java.io.ObjectOutputStream;
023    import java.io.Serializable;
024    
025    import org.apache.commons.math3.linear.MatrixUtils;
026    import org.apache.commons.math3.linear.RealVector;
027    import org.apache.commons.math3.linear.ArrayRealVector;
028    
029    /**
030     * An objective function for a linear optimization problem.
031     * <p>
032     * A linear objective function has one the form:
033     * <pre>
034     * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
035     * </pre>
036     * The c<sub>i</sub> and d are the coefficients of the equation,
037     * the x<sub>i</sub> are the coordinates of the current point.
038     * </p>
039     * @version $Id: LinearObjectiveFunction.java 1422230 2012-12-15 12:11:13Z erans $
040     * @deprecated As of 3.1 (to be removed in 4.0).
041     * @since 2.0
042     */
043    @Deprecated
044    public class LinearObjectiveFunction implements Serializable {
045    
046        /** Serializable version identifier. */
047        private static final long serialVersionUID = -4531815507568396090L;
048    
049        /** Coefficients of the constraint (c<sub>i</sub>). */
050        private final transient RealVector coefficients;
051    
052        /** Constant term of the linear equation. */
053        private final double constantTerm;
054    
055        /**
056         * @param coefficients The coefficients for the linear equation being optimized
057         * @param constantTerm The constant term of the linear equation
058         */
059        public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
060            this(new ArrayRealVector(coefficients), constantTerm);
061        }
062    
063        /**
064         * @param coefficients The coefficients for the linear equation being optimized
065         * @param constantTerm The constant term of the linear equation
066         */
067        public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
068            this.coefficients = coefficients;
069            this.constantTerm = constantTerm;
070        }
071    
072        /**
073         * Get the coefficients of the linear equation being optimized.
074         * @return coefficients of the linear equation being optimized
075         */
076        public RealVector getCoefficients() {
077            return coefficients;
078        }
079    
080        /**
081         * Get the constant of the linear equation being optimized.
082         * @return constant of the linear equation being optimized
083         */
084        public double getConstantTerm() {
085            return constantTerm;
086        }
087    
088        /**
089         * Compute the value of the linear equation at the current point
090         * @param point point at which linear equation must be evaluated
091         * @return value of the linear equation at the current point
092         */
093        public double getValue(final double[] point) {
094            return coefficients.dotProduct(new ArrayRealVector(point, false)) + constantTerm;
095        }
096    
097        /**
098         * Compute the value of the linear equation at the current point
099         * @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    }