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
018package org.apache.commons.math3.optimization.linear;
019
020import java.io.IOException;
021import java.io.ObjectInputStream;
022import java.io.ObjectOutputStream;
023import java.io.Serializable;
024
025import org.apache.commons.math3.linear.MatrixUtils;
026import org.apache.commons.math3.linear.RealVector;
027import 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 * @deprecated As of 3.1 (to be removed in 4.0).
040 * @since 2.0
041 */
042@Deprecated
043public class LinearObjectiveFunction implements Serializable {
044
045    /** Serializable version identifier. */
046    private static final long serialVersionUID = -4531815507568396090L;
047
048    /** Coefficients of the constraint (c<sub>i</sub>). */
049    private final transient RealVector coefficients;
050
051    /** Constant term of the linear equation. */
052    private final double constantTerm;
053
054    /**
055     * @param coefficients The coefficients for the linear equation being optimized
056     * @param constantTerm The constant term of the linear equation
057     */
058    public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
059        this(new ArrayRealVector(coefficients), constantTerm);
060    }
061
062    /**
063     * @param coefficients The coefficients for the linear equation being optimized
064     * @param constantTerm The constant term of the linear equation
065     */
066    public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
067        this.coefficients = coefficients;
068        this.constantTerm = constantTerm;
069    }
070
071    /**
072     * Get the coefficients of the linear equation being optimized.
073     * @return coefficients of the linear equation being optimized
074     */
075    public RealVector getCoefficients() {
076        return coefficients;
077    }
078
079    /**
080     * Get the constant of the linear equation being optimized.
081     * @return constant of the linear equation being optimized
082     */
083    public double getConstantTerm() {
084        return constantTerm;
085    }
086
087    /**
088     * Compute the value of the linear equation at the current point
089     * @param point point at which linear equation must be evaluated
090     * @return value of the linear equation at the current point
091     */
092    public double getValue(final double[] point) {
093        return coefficients.dotProduct(new ArrayRealVector(point, false)) + constantTerm;
094    }
095
096    /**
097     * Compute the value of the linear equation at the current point
098     * @param point point at which linear equation must be evaluated
099     * @return value of the linear equation at the current point
100     */
101    public double getValue(final RealVector point) {
102        return coefficients.dotProduct(point) + constantTerm;
103    }
104
105    @Override
106    public boolean equals(Object other) {
107
108      if (this == other) {
109        return true;
110      }
111
112      if (other instanceof LinearObjectiveFunction) {
113          LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
114          return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
115      }
116
117      return false;
118    }
119
120    @Override
121    public int hashCode() {
122        return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
123    }
124
125    /**
126     * Serialize the instance.
127     * @param oos stream where object should be written
128     * @throws IOException if object cannot be written to stream
129     */
130    private void writeObject(ObjectOutputStream oos)
131        throws IOException {
132        oos.defaultWriteObject();
133        MatrixUtils.serializeRealVector(coefficients, oos);
134    }
135
136    /**
137     * Deserialize the instance.
138     * @param ois stream from which the object should be read
139     * @throws ClassNotFoundException if a class in the stream cannot be found
140     * @throws IOException if object cannot be read from the stream
141     */
142    private void readObject(ObjectInputStream ois)
143      throws ClassNotFoundException, IOException {
144        ois.defaultReadObject();
145        MatrixUtils.deserializeRealVector(this, "coefficients", ois);
146    }
147
148}