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