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.math4.legacy.optim.linear;
18
19 import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
20 import org.apache.commons.math4.legacy.linear.ArrayRealVector;
21 import org.apache.commons.math4.legacy.linear.RealVector;
22 import org.apache.commons.math4.legacy.optim.OptimizationData;
23
24 /**
25 * An objective function for a linear optimization problem.
26 * <p>
27 * A linear objective function has one the form:
28 * <div style="white-space: pre"><code>
29 * c<sub>1</sub>x<sub>1</sub> + ... c<sub>n</sub>x<sub>n</sub> + d
30 * </code></div>
31 * The c<sub>i</sub> and d are the coefficients of the equation,
32 * the x<sub>i</sub> are the coordinates of the current point.
33 *
34 * @since 2.0
35 */
36 public class LinearObjectiveFunction
37 implements MultivariateFunction,
38 OptimizationData {
39 /** Coefficients of the linear equation (c<sub>i</sub>). */
40 private final RealVector coefficients;
41 /** Constant term of the linear equation. */
42 private final double constantTerm;
43
44 /**
45 * @param coefficients Coefficients for the linear equation being optimized.
46 * @param constantTerm Constant term of the linear equation.
47 */
48 public LinearObjectiveFunction(double[] coefficients, double constantTerm) {
49 this(new ArrayRealVector(coefficients), constantTerm);
50 }
51
52 /**
53 * @param coefficients Coefficients for the linear equation being optimized.
54 * @param constantTerm Constant term of the linear equation.
55 */
56 public LinearObjectiveFunction(RealVector coefficients, double constantTerm) {
57 this.coefficients = coefficients;
58 this.constantTerm = constantTerm;
59 }
60
61 /**
62 * Gets the coefficients of the linear equation being optimized.
63 *
64 * @return coefficients of the linear equation being optimized.
65 */
66 public RealVector getCoefficients() {
67 return coefficients;
68 }
69
70 /**
71 * Gets the constant of the linear equation being optimized.
72 *
73 * @return constant of the linear equation being optimized.
74 */
75 public double getConstantTerm() {
76 return constantTerm;
77 }
78
79 /**
80 * Computes the value of the linear equation at the current point.
81 *
82 * @param point Point at which linear equation must be evaluated.
83 * @return the value of the linear equation at the current point.
84 */
85 @Override
86 public double value(final double[] point) {
87 return value(new ArrayRealVector(point, false));
88 }
89
90 /**
91 * Computes the value of the linear equation at the current point.
92 *
93 * @param point Point at which linear equation must be evaluated.
94 * @return the value of the linear equation at the current point.
95 */
96 public double value(final RealVector point) {
97 return coefficients.dotProduct(point) + constantTerm;
98 }
99
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 }