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.legacy.optim.linear;
018
019import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
020import org.apache.commons.math4.legacy.linear.ArrayRealVector;
021import org.apache.commons.math4.legacy.linear.RealVector;
022import org.apache.commons.math4.legacy.optim.OptimizationData;
023
024/**
025 * An objective function for a linear optimization problem.
026 * <p>
027 * A linear objective function has one the form:
028 * <div style="white-space: pre"><code>
029 * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
030 * </code></div>
031 * The c<sub>i</sub> and d are the coefficients of the equation,
032 * the x<sub>i</sub> are the coordinates of the current point.
033 *
034 * @since 2.0
035 */
036public class LinearObjectiveFunction
037    implements MultivariateFunction,
038               OptimizationData {
039    /** Coefficients of the linear equation (c<sub>i</sub>). */
040    private final RealVector coefficients;
041    /** Constant term of the linear equation. */
042    private final double constantTerm;
043
044    /**
045     * @param coefficients Coefficients for the linear equation being optimized.
046     * @param constantTerm Constant term of the linear equation.
047     */
048    public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
049        this(new ArrayRealVector(coefficients), constantTerm);
050    }
051
052    /**
053     * @param coefficients Coefficients for the linear equation being optimized.
054     * @param constantTerm Constant term of the linear equation.
055     */
056    public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
057        this.coefficients = coefficients;
058        this.constantTerm = constantTerm;
059    }
060
061    /**
062     * Gets the coefficients of the linear equation being optimized.
063     *
064     * @return coefficients of the linear equation being optimized.
065     */
066    public RealVector getCoefficients() {
067        return coefficients;
068    }
069
070    /**
071     * Gets the constant of the linear equation being optimized.
072     *
073     * @return constant of the linear equation being optimized.
074     */
075    public double getConstantTerm() {
076        return constantTerm;
077    }
078
079    /**
080     * Computes the value of the linear equation at the current point.
081     *
082     * @param point Point at which linear equation must be evaluated.
083     * @return the value of the linear equation at the current point.
084     */
085    @Override
086    public double value(final double[] point) {
087        return value(new ArrayRealVector(point, false));
088    }
089
090    /**
091     * Computes the value of the linear equation at the current point.
092     *
093     * @param point Point at which linear equation must be evaluated.
094     * @return the value of the linear equation at the current point.
095     */
096    public double value(final RealVector point) {
097        return coefficients.dotProduct(point) + constantTerm;
098    }
099
100    /** {@inheritDoc} */
101    @Override
102    public boolean equals(Object other) {
103        if (this == other) {
104            return true;
105        }
106        if (other instanceof LinearObjectiveFunction) {
107            LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
108          return constantTerm == rhs.constantTerm && coefficients.equals(rhs.coefficients);
109        }
110
111        return false;
112    }
113
114    /** {@inheritDoc} */
115    @Override
116    public int hashCode() {
117        return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
118    }
119}