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.util.ArrayList;
20 import java.util.List;
21 import org.apache.commons.math3.exception.TooManyIterationsException;
22 import org.apache.commons.math3.optim.PointValuePair;
23 import org.apache.commons.math3.util.Precision;
24
25 /**
26 * Solves a linear problem using the "Two-Phase Simplex" method.
27 * <p>
28 * <b>Note:</b> Depending on the problem definition, the default convergence criteria
29 * may be too strict, resulting in {@link NoFeasibleSolutionException} or
30 * {@link TooManyIterationsException}. In such a case it is advised to adjust these
31 * criteria with more appropriate values, e.g. relaxing the epsilon value.
32 * <p>
33 * Default convergence criteria:
34 * <ul>
35 * <li>Algorithm convergence: 1e-6</li>
36 * <li>Floating-point comparisons: 10 ulp</li>
37 * <li>Cut-Off value: 1e-12</li>
38 * </ul>
39 * <p>
40 * The cut-off value has been introduced to zero out very small numbers in the Simplex tableau,
41 * as these may lead to numerical instabilities due to the nature of the Simplex algorithm
42 * (the pivot element is used as a denominator). If the problem definition is very tight, the
43 * default cut-off value may be too small, thus it is advised to increase it to a larger value,
44 * in accordance with the chosen epsilon.
45 * <p>
46 * It may also be counter-productive to provide a too large value for {@link
47 * org.apache.commons.math3.optim.MaxIter MaxIter} as parameter in the call of {@link
48 * #optimize(org.apache.commons.math3.optim.OptimizationData...) optimize(OptimizationData...)},
49 * as the {@link SimplexSolver} will use different strategies depending on the current iteration
50 * count. After half of the allowed max iterations has already been reached, the strategy to select
51 * pivot rows will change in order to break possible cycles due to degenerate problems.
52 *
53 * @version $Id: SimplexSolver.java 1462503 2013-03-29 15:48:27Z luc $
54 * @since 2.0
55 */
56 public class SimplexSolver extends LinearOptimizer {
57 /** Default amount of error to accept in floating point comparisons (as ulps). */
58 static final int DEFAULT_ULPS = 10;
59
60 /** Default cut-off value. */
61 static final double DEFAULT_CUT_OFF = 1e-12;
62
63 /** Default amount of error to accept for algorithm convergence. */
64 private static final double DEFAULT_EPSILON = 1.0e-6;
65
66 /** Amount of error to accept for algorithm convergence. */
67 private final double epsilon;
68
69 /** Amount of error to accept in floating point comparisons (as ulps). */
70 private final int maxUlps;
71
72 /**
73 * Cut-off value for entries in the tableau: values smaller than the cut-off
74 * are treated as zero to improve numerical stability.
75 */
76 private final double cutOff;
77
78 /**
79 * Builds a simplex solver with default settings.
80 */
81 public SimplexSolver() {
82 this(DEFAULT_EPSILON, DEFAULT_ULPS, DEFAULT_CUT_OFF);
83 }
84
85 /**
86 * Builds a simplex solver with a specified accepted amount of error.
87 *
88 * @param epsilon Amount of error to accept for algorithm convergence.
89 */
90 public SimplexSolver(final double epsilon) {
91 this(epsilon, DEFAULT_ULPS, DEFAULT_CUT_OFF);
92 }
93
94 /**
95 * Builds a simplex solver with a specified accepted amount of error.
96 *
97 * @param epsilon Amount of error to accept for algorithm convergence.
98 * @param maxUlps Amount of error to accept in floating point comparisons.
99 */
100 public SimplexSolver(final double epsilon, final int maxUlps) {
101 this(epsilon, maxUlps, DEFAULT_CUT_OFF);
102 }
103
104 /**
105 * Builds a simplex solver with a specified accepted amount of error.
106 *
107 * @param epsilon Amount of error to accept for algorithm convergence.
108 * @param maxUlps Amount of error to accept in floating point comparisons.
109 * @param cutOff Values smaller than the cutOff are treated as zero.
110 */
111 public SimplexSolver(final double epsilon, final int maxUlps, final double cutOff) {
112 this.epsilon = epsilon;
113 this.maxUlps = maxUlps;
114 this.cutOff = cutOff;
115 }
116
117 /**
118 * Returns the column with the most negative coefficient in the objective function row.
119 *
120 * @param tableau Simple tableau for the problem.
121 * @return the column with the most negative coefficient.
122 */
123 private Integer getPivotColumn(SimplexTableau tableau) {
124 double minValue = 0;
125 Integer minPos = null;
126 for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) {
127 final double entry = tableau.getEntry(0, i);
128 // check if the entry is strictly smaller than the current minimum
129 // do not use a ulp/epsilon check
130 if (entry < minValue) {
131 minValue = entry;
132 minPos = i;
133 }
134 }
135 return minPos;
136 }
137
138 /**
139 * Returns the row with the minimum ratio as given by the minimum ratio test (MRT).
140 *
141 * @param tableau Simple tableau for the problem.
142 * @param col Column to test the ratio of (see {@link #getPivotColumn(SimplexTableau)}).
143 * @return the row with the minimum ratio.
144 */
145 private Integer getPivotRow(SimplexTableau tableau, final int col) {
146 // create a list of all the rows that tie for the lowest score in the minimum ratio test
147 List<Integer> minRatioPositions = new ArrayList<Integer>();
148 double minRatio = Double.MAX_VALUE;
149 for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
150 final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
151 final double entry = tableau.getEntry(i, col);
152
153 if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
154 final double ratio = rhs / entry;
155 // check if the entry is strictly equal to the current min ratio
156 // do not use a ulp/epsilon check
157 final int cmp = Double.compare(ratio, minRatio);
158 if (cmp == 0) {
159 minRatioPositions.add(i);
160 } else if (cmp < 0) {
161 minRatio = ratio;
162 minRatioPositions = new ArrayList<Integer>();
163 minRatioPositions.add(i);
164 }
165 }
166 }
167
168 if (minRatioPositions.size() == 0) {
169 return null;
170 } else if (minRatioPositions.size() > 1) {
171 // there's a degeneracy as indicated by a tie in the minimum ratio test
172
173 // 1. check if there's an artificial variable that can be forced out of the basis
174 if (tableau.getNumArtificialVariables() > 0) {
175 for (Integer row : minRatioPositions) {
176 for (int i = 0; i < tableau.getNumArtificialVariables(); i++) {
177 int column = i + tableau.getArtificialVariableOffset();
178 final double entry = tableau.getEntry(row, column);
179 if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) {
180 return row;
181 }
182 }
183 }
184 }
185
186 // 2. apply Bland's rule to prevent cycling:
187 // take the row for which the corresponding basic variable has the smallest index
188 //
189 // see http://www.stanford.edu/class/msande310/blandrule.pdf
190 // see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper)
191 //
192 // Additional heuristic: if we did not get a solution after half of maxIterations
193 // revert to the simple case of just returning the top-most row
194 // This heuristic is based on empirical data gathered while investigating MATH-828.
195 if (getEvaluations() < getMaxEvaluations() / 2) {
196 Integer minRow = null;
197 int minIndex = tableau.getWidth();
198 final int varStart = tableau.getNumObjectiveFunctions();
199 final int varEnd = tableau.getWidth() - 1;
200 for (Integer row : minRatioPositions) {
201 for (int i = varStart; i < varEnd && !row.equals(minRow); i++) {
202 final Integer basicRow = tableau.getBasicRow(i);
203 if (basicRow != null && basicRow.equals(row) && i < minIndex) {
204 minIndex = i;
205 minRow = row;
206 }
207 }
208 }
209 return minRow;
210 }
211 }
212 return minRatioPositions.get(0);
213 }
214
215 /**
216 * Runs one iteration of the Simplex method on the given model.
217 *
218 * @param tableau Simple tableau for the problem.
219 * @throws TooManyIterationsException if the allowed number of iterations has been exhausted.
220 * @throws UnboundedSolutionException if the model is found not to have a bounded solution.
221 */
222 protected void doIteration(final SimplexTableau tableau)
223 throws TooManyIterationsException,
224 UnboundedSolutionException {
225
226 incrementIterationCount();
227
228 Integer pivotCol = getPivotColumn(tableau);
229 Integer pivotRow = getPivotRow(tableau, pivotCol);
230 if (pivotRow == null) {
231 throw new UnboundedSolutionException();
232 }
233
234 // set the pivot element to 1
235 double pivotVal = tableau.getEntry(pivotRow, pivotCol);
236 tableau.divideRow(pivotRow, pivotVal);
237
238 // set the rest of the pivot column to 0
239 for (int i = 0; i < tableau.getHeight(); i++) {
240 if (i != pivotRow) {
241 final double multiplier = tableau.getEntry(i, pivotCol);
242 tableau.subtractRow(i, pivotRow, multiplier);
243 }
244 }
245 }
246
247 /**
248 * Solves Phase 1 of the Simplex method.
249 *
250 * @param tableau Simple tableau for the problem.
251 * @throws TooManyIterationsException if the allowed number of iterations has been exhausted.
252 * @throws UnboundedSolutionException if the model is found not to have a bounded solution.
253 * @throws NoFeasibleSolutionException if there is no feasible solution?
254 */
255 protected void solvePhase1(final SimplexTableau tableau)
256 throws TooManyIterationsException,
257 UnboundedSolutionException,
258 NoFeasibleSolutionException {
259
260 // make sure we're in Phase 1
261 if (tableau.getNumArtificialVariables() == 0) {
262 return;
263 }
264
265 while (!tableau.isOptimal()) {
266 doIteration(tableau);
267 }
268
269 // if W is not zero then we have no feasible solution
270 if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
271 throw new NoFeasibleSolutionException();
272 }
273 }
274
275 /** {@inheritDoc} */
276 @Override
277 public PointValuePair doOptimize()
278 throws TooManyIterationsException,
279 UnboundedSolutionException,
280 NoFeasibleSolutionException {
281 final SimplexTableau tableau =
282 new SimplexTableau(getFunction(),
283 getConstraints(),
284 getGoalType(),
285 isRestrictedToNonNegative(),
286 epsilon,
287 maxUlps,
288 cutOff);
289
290 solvePhase1(tableau);
291 tableau.dropPhase1Objective();
292
293 while (!tableau.isOptimal()) {
294 doIteration(tableau);
295 }
296 return tableau.getSolution();
297 }
298 }