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.math.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.math.linear.MatrixUtils;
026 import org.apache.commons.math.linear.RealVector;
027 import org.apache.commons.math.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 $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
040 * @since 2.0
041 */
042 public class LinearObjectiveFunction implements Serializable {
043
044 /** Serializable version identifier. */
045 private static final long serialVersionUID = -4531815507568396090L;
046
047 /** Coefficients of the constraint (c<sub>i</sub>). */
048 private final transient RealVector coefficients;
049
050 /** Constant term of the linear equation. */
051 private final double constantTerm;
052
053 /**
054 * @param coefficients The coefficients for the linear equation being optimized
055 * @param constantTerm The 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 The coefficients for the linear equation being optimized
063 * @param constantTerm The 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 * Get the coefficients of the linear equation being optimized.
072 * @return coefficients of the linear equation being optimized
073 */
074 public RealVector getCoefficients() {
075 return coefficients;
076 }
077
078 /**
079 * Get the constant of the linear equation being optimized.
080 * @return constant of the linear equation being optimized
081 */
082 public double getConstantTerm() {
083 return constantTerm;
084 }
085
086 /**
087 * Compute the value of the linear equation at the current point
088 * @param point point at which linear equation must be evaluated
089 * @return value of the linear equation at the current point
090 */
091 public double getValue(final double[] point) {
092 return coefficients.dotProduct(point) + constantTerm;
093 }
094
095 /**
096 * Compute the value of the linear equation at the current point
097 * @param point point at which linear equation must be evaluated
098 * @return value of the linear equation at the current point
099 */
100 public double getValue(final RealVector point) {
101 return coefficients.dotProduct(point) + constantTerm;
102 }
103
104 /** {@inheritDoc} */
105 @Override
106 public boolean equals(Object other) {
107
108 if (this == other) {
109 return true;
110 }
111
112 if (other == null) {
113 return false;
114 }
115
116 try {
117
118 LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
119 return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
120
121 } catch (ClassCastException ex) {
122 // ignore exception
123 return false;
124 }
125
126 }
127
128 /** {@inheritDoc} */
129 @Override
130 public int hashCode() {
131 return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
132 }
133
134 /** Serialize the instance.
135 * @param oos stream where object should be written
136 * @throws IOException if object cannot be written to stream
137 */
138 private void writeObject(ObjectOutputStream oos)
139 throws IOException {
140 oos.defaultWriteObject();
141 MatrixUtils.serializeRealVector(coefficients, oos);
142 }
143
144 /** Deserialize the instance.
145 * @param ois stream from which the object should be read
146 * @throws ClassNotFoundException if a class in the stream cannot be found
147 * @throws IOException if object cannot be read from the stream
148 */
149 private void readObject(ObjectInputStream ois)
150 throws ClassNotFoundException, IOException {
151 ois.defaultReadObject();
152 MatrixUtils.deserializeRealVector(this, "coefficients", ois);
153 }
154
155 }