2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.commons.math.optimization.linear;
20
import java.io.IOException;
21
import java.io.ObjectInputStream;
22
import java.io.ObjectOutputStream;
23
import java.io.Serializable;
25
import org.apache.commons.math.linear.MatrixUtils;
26
import org.apache.commons.math.linear.RealVector;
27
import org.apache.commons.math.linear.ArrayRealVector;
30
* An objective function for a linear optimization problem.
32
* A linear objective function has one the form:
34
* c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
36
* The c<sub>i</sub> and d are the coefficients of the equation,
37
* the x<sub>i</sub> are the coordinates of the current point.
39
* @version $Revision: 783702 $ $Date: 2009-06-11 04:54:02 -0400 (Thu, 11 Jun 2009) $
42
public class LinearObjectiveFunction implements Serializable {
44
/** Serializable version identifier. */
45
private static final long serialVersionUID = -4531815507568396090L;
47
/** Coefficients of the constraint (c<sub>i</sub>). */
48
private final transient RealVector coefficients;
50
/** Constant term of the linear equation. */
51
private final double constantTerm;
54
* @param coefficients The coefficients for the linear equation being optimized
55
* @param constantTerm The constant term of the linear equation
57
public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
58
this(new ArrayRealVector(coefficients), constantTerm);
62
* @param coefficients The coefficients for the linear equation being optimized
63
* @param constantTerm The constant term of the linear equation
65
public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
66
this.coefficients = coefficients;
67
this.constantTerm = constantTerm;
71
* Get the coefficients of the linear equation being optimized.
72
* @return coefficients of the linear equation being optimized
74
public RealVector getCoefficients() {
79
* Get the constant of the linear equation being optimized.
80
* @return constant of the linear equation being optimized
82
public double getConstantTerm() {
87
* Compute the value of the linear equation at the current point
88
* @param point point at which linear equation must be evaluated
89
* @return value of the linear equation at the current point
91
public double getValue(final double[] point) {
92
return coefficients.dotProduct(point) + constantTerm;
96
* Compute the value of the linear equation at the current point
97
* @param point point at which linear equation must be evaluated
98
* @return value of the linear equation at the current point
100
public double getValue(final RealVector point) {
101
return coefficients.dotProduct(point) + constantTerm;
106
public boolean equals(Object other) {
118
LinearObjectiveFunction rhs = (LinearObjectiveFunction) other;
119
return (constantTerm == rhs.constantTerm) && coefficients.equals(rhs.coefficients);
121
} catch (ClassCastException ex) {
130
public int hashCode() {
131
return Double.valueOf(constantTerm).hashCode() ^ coefficients.hashCode();
134
/** Serialize the instance.
135
* @param oos stream where object should be written
136
* @throws IOException if object cannot be written to stream
138
private void writeObject(ObjectOutputStream oos)
140
oos.defaultWriteObject();
141
MatrixUtils.serializeRealVector(coefficients, oos);
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
149
private void readObject(ObjectInputStream ois)
150
throws ClassNotFoundException, IOException {
151
ois.defaultReadObject();
152
MatrixUtils.deserializeRealVector(this, "coefficients", ois);