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
18 package org.apache.commons.math3.optimization.linear;
19
20 import java.util.Collection;
21 import java.util.Collections;
22
23 import org.apache.commons.math3.exception.MathIllegalStateException;
24 import org.apache.commons.math3.exception.MaxCountExceededException;
25 import org.apache.commons.math3.optimization.GoalType;
26 import org.apache.commons.math3.optimization.PointValuePair;
27
28 /**
29 * Base class for implementing linear optimizers.
30 * <p>
31 * This base class handles the boilerplate methods associated to thresholds
32 * settings and iterations counters.
33 *
34 * @version $Id: AbstractLinearOptimizer.java 1422230 2012-12-15 12:11:13Z erans $
35 * @deprecated As of 3.1 (to be removed in 4.0).
36 * @since 2.0
37 */
38 @Deprecated
39 public abstract class AbstractLinearOptimizer implements LinearOptimizer {
40
41 /** Default maximal number of iterations allowed. */
42 public static final int DEFAULT_MAX_ITERATIONS = 100;
43
44 /**
45 * Linear objective function.
46 * @since 2.1
47 */
48 private LinearObjectiveFunction function;
49
50 /**
51 * Linear constraints.
52 * @since 2.1
53 */
54 private Collection<LinearConstraint> linearConstraints;
55
56 /**
57 * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
58 * @since 2.1
59 */
60 private GoalType goal;
61
62 /**
63 * Whether to restrict the variables to non-negative values.
64 * @since 2.1
65 */
66 private boolean nonNegative;
67
68 /** Maximal number of iterations allowed. */
69 private int maxIterations;
70
71 /** Number of iterations already performed. */
72 private int iterations;
73
74 /**
75 * Simple constructor with default settings.
76 * <p>The maximal number of evaluation is set to its default value.</p>
77 */
78 protected AbstractLinearOptimizer() {
79 setMaxIterations(DEFAULT_MAX_ITERATIONS);
80 }
81
82 /**
83 * @return {@code true} if the variables are restricted to non-negative values.
84 */
85 protected boolean restrictToNonNegative() {
86 return nonNegative;
87 }
88
89 /**
90 * @return the optimization type.
91 */
92 protected GoalType getGoalType() {
93 return goal;
94 }
95
96 /**
97 * @return the optimization type.
98 */
99 protected LinearObjectiveFunction getFunction() {
100 return function;
101 }
102
103 /**
104 * @return the optimization type.
105 */
106 protected Collection<LinearConstraint> getConstraints() {
107 return Collections.unmodifiableCollection(linearConstraints);
108 }
109
110 /** {@inheritDoc} */
111 public void setMaxIterations(int maxIterations) {
112 this.maxIterations = maxIterations;
113 }
114
115 /** {@inheritDoc} */
116 public int getMaxIterations() {
117 return maxIterations;
118 }
119
120 /** {@inheritDoc} */
121 public int getIterations() {
122 return iterations;
123 }
124
125 /**
126 * Increment the iterations counter by 1.
127 * @exception MaxCountExceededException if the maximal number of iterations is exceeded
128 */
129 protected void incrementIterationsCounter()
130 throws MaxCountExceededException {
131 if (++iterations > maxIterations) {
132 throw new MaxCountExceededException(maxIterations);
133 }
134 }
135
136 /** {@inheritDoc} */
137 public PointValuePair optimize(final LinearObjectiveFunction f,
138 final Collection<LinearConstraint> constraints,
139 final GoalType goalType, final boolean restrictToNonNegative)
140 throws MathIllegalStateException {
141
142 // store linear problem characteristics
143 this.function = f;
144 this.linearConstraints = constraints;
145 this.goal = goalType;
146 this.nonNegative = restrictToNonNegative;
147
148 iterations = 0;
149
150 // solve the problem
151 return doOptimize();
152
153 }
154
155 /**
156 * Perform the bulk of optimization algorithm.
157 * @return the point/value pair giving the optimal value for objective function
158 * @exception MathIllegalStateException if no solution fulfilling the constraints
159 * can be found in the allowed number of iterations
160 */
161 protected abstract PointValuePair doOptimize() throws MathIllegalStateException;
162
163 }