1 /*
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17 package org.apache.commons.math3.optim.linear;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import org.apache.commons.math3.analysis.MultivariateFunction;
24 import org.apache.commons.math3.linear.MatrixUtils;
25 import org.apache.commons.math3.linear.RealVector;
26 import org.apache.commons.math3.linear.ArrayRealVector;
27 import org.apache.commons.math3.optim.OptimizationData;
28
29 /**
30 * An objective function for a linear optimization problem.
31 * <p>
32 * A linear objective function has one the form:
33 * <pre>
34 * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
35 * </pre>
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.
38 * </p>
39 *
40 * @version $Id: LinearObjectiveFunction.java 1435539 2013-01-19 13:27:24Z tn $
41 * @since 2.0
42 */
43 public class LinearObjectiveFunction
44 implements MultivariateFunction,
45 OptimizationData,
46 Serializable {
47 /** Serializable version identifier. */
48 private static final long serialVersionUID = -4531815507568396090L;
49 /** Coefficients of the linear equation (c<sub>i</sub>). */
50 private final transient RealVector coefficients;
51 /** Constant term of the linear equation. */
52 private final double constantTerm;
53
54 /**
55 * @param coefficients Coefficients for the linear equation being optimized.
56 * @param constantTerm Constant term of the linear equation.
57 */
58 public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
59 this(new ArrayRealVector(coefficients), constantTerm);
60 }
61
62 /**
63 * @param coefficients Coefficients for the linear equation being optimized.
64 * @param constantTerm Constant term of the linear equation.
65 */
66 public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
67 this.coefficients = coefficients;
68 this.constantTerm = constantTerm;
69 }
70
71 /**
72 * Gets the coefficients of the linear equation being optimized.
73 *
74 * @return coefficients of the linear equation being optimized.
75 */
76 public RealVector getCoefficients() {
77 return coefficients;
78 }
79
80 /**
81 * Gets the constant of the linear equation being optimized.
82 *
83 * @return constant of the linear equation being optimized.
84 */
85 public double getConstantTerm() {
86 return constantTerm;
87 }
88
89 /**
90 * Computes the value of the linear equation at the current point.
91 *
92 * @param point Point at which linear equation must be evaluated.
93 * @return the value of the linear equation at the current point.
94 */
95 public double value(final double[] point) {
96 return value(new ArrayRealVector(point, false));
97 }
98
99 /**
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 }