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 package org.apache.commons.math3.optim.linear;
018
019 import java.io.IOException;
020 import java.io.ObjectInputStream;
021 import java.io.ObjectOutputStream;
022 import java.io.Serializable;
023 import org.apache.commons.math3.analysis.MultivariateFunction;
024 import org.apache.commons.math3.linear.MatrixUtils;
025 import org.apache.commons.math3.linear.RealVector;
026 import org.apache.commons.math3.linear.ArrayRealVector;
027 import org.apache.commons.math3.optim.OptimizationData;
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 *
040 * @version $Id: LinearObjectiveFunction.java 1416643 2012-12-03 19:37:14Z tn $
041 * @since 2.0
042 */
043 public class LinearObjectiveFunction
044 implements MultivariateFunction,
045 OptimizationData,
046 Serializable {
047 /** Serializable version identifier. */
048 private static final long serialVersionUID = -4531815507568396090L;
049 /** Coefficients of the linear equation (c<sub>i</sub>). */
050 private final transient RealVector coefficients;
051 /** Constant term of the linear equation. */
052 private final double constantTerm;
053
054 /**
055 * @param coefficients Coefficients for the linear equation being optimized.
056 * @param constantTerm 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 Coefficients for the linear equation being optimized.
064 * @param constantTerm 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 * Gets the coefficients of the linear equation being optimized.
073 *
074 * @return coefficients of the linear equation being optimized.
075 */
076 public RealVector getCoefficients() {
077 return coefficients;
078 }
079
080 /**
081 * Gets the constant of the linear equation being optimized.
082 *
083 * @return constant of the linear equation being optimized.
084 */
085 public double getConstantTerm() {
086 return constantTerm;
087 }
088
089 /**
090 * Computes the value of the linear equation at the current point.
091 *
092 * @param point Point at which linear equation must be evaluated.
093 * @return the value of the linear equation at the current point.
094 */
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 @Override
110 public boolean equals(Object other) {
111 if (this == other) {
112 return true;
113 }
114 if (other instanceof LinearObjectiveFunction) {
115 LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
116 return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
117 }
118
119 return false;
120 }
121
122 @Override
123 public int hashCode() {
124 return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
125 }
126
127 /**
128 * Serialize the instance.
129 * @param oos stream where object should be written
130 * @throws IOException if object cannot be written to stream
131 */
132 private void writeObject(ObjectOutputStream oos)
133 throws IOException {
134 oos.defaultWriteObject();
135 MatrixUtils.serializeRealVector(coefficients, oos);
136 }
137
138 /**
139 * Deserialize the instance.
140 * @param ois stream from which the object should be read
141 * @throws ClassNotFoundException if a class in the stream cannot be found
142 * @throws IOException if object cannot be read from the stream
143 */
144 private void readObject(ObjectInputStream ois)
145 throws ClassNotFoundException, IOException {
146 ois.defaultReadObject();
147 MatrixUtils.deserializeRealVector(this, "coefficients", ois);
148 }
149 }