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 * https://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 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.optim.linear; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 import org.hipparchus.exception.MathIllegalStateException; 28 import org.hipparchus.optim.LocalizedOptimFormats; 29 import org.hipparchus.optim.OptimizationData; 30 import org.hipparchus.optim.PointValuePair; 31 import org.hipparchus.util.FastMath; 32 import org.hipparchus.util.Precision; 33 34 /** 35 * Solves a linear problem using the "Two-Phase Simplex" method. 36 * <p> 37 * The {@link SimplexSolver} supports the following {@link OptimizationData} data provided 38 * as arguments to {@link #optimize(OptimizationData...)}: 39 * <ul> 40 * <li>objective function: {@link LinearObjectiveFunction} - mandatory</li> 41 * <li>linear constraints {@link LinearConstraintSet} - mandatory</li> 42 * <li>type of optimization: {@link org.hipparchus.optim.nonlinear.scalar.GoalType GoalType} 43 * - optional, default: {@link org.hipparchus.optim.nonlinear.scalar.GoalType#MINIMIZE MINIMIZE}</li> 44 * <li>whether to allow negative values as solution: {@link NonNegativeConstraint} - optional, default: true</li> 45 * <li>pivot selection rule: {@link PivotSelectionRule} - optional, default {@link PivotSelectionRule#DANTZIG}</li> 46 * <li>callback for the best solution: {@link SolutionCallback} - optional</li> 47 * <li>maximum number of iterations: {@link org.hipparchus.optim.MaxIter} - optional, default: {@link Integer#MAX_VALUE}</li> 48 * </ul> 49 * <p> 50 * <b>Note:</b> Depending on the problem definition, the default convergence criteria 51 * may be too strict, resulting in {@link MathIllegalStateException} or 52 * {@link MathIllegalStateException}. In such a case it is advised to adjust these 53 * criteria with more appropriate values, e.g. relaxing the epsilon value. 54 * <p> 55 * Default convergence criteria: 56 * <ul> 57 * <li>Algorithm convergence: 1e-6</li> 58 * <li>Floating-point comparisons: 10 ulp</li> 59 * <li>Cut-Off value: 1e-10</li> 60 * </ul> 61 * <p> 62 * The cut-off value has been introduced to handle the case of very small pivot elements 63 * in the Simplex tableau, as these may lead to numerical instabilities and degeneracy. 64 * Potential pivot elements smaller than this value will be treated as if they were zero 65 * and are thus not considered by the pivot selection mechanism. The default value is safe 66 * for many problems, but may need to be adjusted in case of very small coefficients 67 * used in either the {@link LinearConstraint} or {@link LinearObjectiveFunction}. 68 * 69 */ 70 public class SimplexSolver extends LinearOptimizer { 71 /** Default amount of error to accept in floating point comparisons (as ulps). */ 72 static final int DEFAULT_ULPS = 10; 73 74 /** Default cut-off value. */ 75 static final double DEFAULT_CUT_OFF = 1e-10; 76 77 /** Default amount of error to accept for algorithm convergence. */ 78 private static final double DEFAULT_EPSILON = 1.0e-6; 79 80 /** Amount of error to accept for algorithm convergence. */ 81 private final double epsilon; 82 83 /** Amount of error to accept in floating point comparisons (as ulps). */ 84 private final int maxUlps; 85 86 /** 87 * Cut-off value for entries in the tableau: values smaller than the cut-off 88 * are treated as zero to improve numerical stability. 89 */ 90 private final double cutOff; 91 92 /** The pivot selection method to use. */ 93 private PivotSelectionRule pivotSelection; 94 95 /** 96 * The solution callback to access the best solution found so far in case 97 * the optimizer fails to find an optimal solution within the iteration limits. 98 */ 99 private SolutionCallback solutionCallback; 100 101 /** 102 * Builds a simplex solver with default settings. 103 */ 104 public SimplexSolver() { 105 this(DEFAULT_EPSILON, DEFAULT_ULPS, DEFAULT_CUT_OFF); 106 } 107 108 /** 109 * Builds a simplex solver with a specified accepted amount of error. 110 * 111 * @param epsilon Amount of error to accept for algorithm convergence. 112 */ 113 public SimplexSolver(final double epsilon) { 114 this(epsilon, DEFAULT_ULPS, DEFAULT_CUT_OFF); 115 } 116 117 /** 118 * Builds a simplex solver with a specified accepted amount of error. 119 * 120 * @param epsilon Amount of error to accept for algorithm convergence. 121 * @param maxUlps Amount of error to accept in floating point comparisons. 122 */ 123 public SimplexSolver(final double epsilon, final int maxUlps) { 124 this(epsilon, maxUlps, DEFAULT_CUT_OFF); 125 } 126 127 /** 128 * Builds a simplex solver with a specified accepted amount of error. 129 * 130 * @param epsilon Amount of error to accept for algorithm convergence. 131 * @param maxUlps Amount of error to accept in floating point comparisons. 132 * @param cutOff Values smaller than the cutOff are treated as zero. 133 */ 134 public SimplexSolver(final double epsilon, final int maxUlps, final double cutOff) { 135 this.epsilon = epsilon; 136 this.maxUlps = maxUlps; 137 this.cutOff = cutOff; 138 this.pivotSelection = PivotSelectionRule.DANTZIG; 139 } 140 141 /** 142 * {@inheritDoc} 143 * 144 * @param optData Optimization data. In addition to those documented in 145 * {@link LinearOptimizer#optimize(OptimizationData...) 146 * LinearOptimizer}, this method will register the following data: 147 * <ul> 148 * <li>{@link SolutionCallback}</li> 149 * <li>{@link PivotSelectionRule}</li> 150 * </ul> 151 * 152 * @return {@inheritDoc} 153 * @throws MathIllegalStateException if the maximal number of iterations is exceeded. 154 * @throws org.hipparchus.exception.MathIllegalArgumentException if the dimension 155 * of the constraints does not match the dimension of the objective function 156 */ 157 @Override 158 public PointValuePair optimize(OptimizationData... optData) 159 throws MathIllegalStateException { 160 // Set up base class and perform computation. 161 return super.optimize(optData); 162 } 163 164 /** 165 * {@inheritDoc} 166 * 167 * @param optData Optimization data. 168 * In addition to those documented in 169 * {@link LinearOptimizer#parseOptimizationData(OptimizationData[]) 170 * LinearOptimizer}, this method will register the following data: 171 * <ul> 172 * <li>{@link SolutionCallback}</li> 173 * <li>{@link PivotSelectionRule}</li> 174 * </ul> 175 */ 176 @Override 177 protected void parseOptimizationData(OptimizationData... optData) { 178 // Allow base class to register its own data. 179 super.parseOptimizationData(optData); 180 181 // reset the callback before parsing 182 solutionCallback = null; 183 184 for (OptimizationData data : optData) { 185 if (data instanceof SolutionCallback) { 186 solutionCallback = (SolutionCallback) data; 187 continue; 188 } 189 if (data instanceof PivotSelectionRule) { 190 pivotSelection = (PivotSelectionRule) data; 191 continue; 192 } 193 } 194 } 195 196 /** 197 * Returns the column with the most negative coefficient in the objective function row. 198 * 199 * @param tableau Simple tableau for the problem. 200 * @return the column with the most negative coefficient. 201 */ 202 private Integer getPivotColumn(SimplexTableau tableau) { 203 double minValue = 0; 204 Integer minPos = null; 205 for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) { 206 final double entry = tableau.getEntry(0, i); 207 // check if the entry is strictly smaller than the current minimum 208 // do not use a ulp/epsilon check 209 if (entry < minValue) { 210 minValue = entry; 211 minPos = i; 212 213 // Bland's rule: chose the entering column with the lowest index 214 if (pivotSelection == PivotSelectionRule.BLAND && isValidPivotColumn(tableau, i)) { 215 break; 216 } 217 } 218 } 219 return minPos; 220 } 221 222 /** 223 * Checks whether the given column is valid pivot column, i.e. will result 224 * in a valid pivot row. 225 * <p> 226 * When applying Bland's rule to select the pivot column, it may happen that 227 * there is no corresponding pivot row. This method will check if the selected 228 * pivot column will return a valid pivot row. 229 * 230 * @param tableau simplex tableau for the problem 231 * @param col the column to test 232 * @return {@code true} if the pivot column is valid, {@code false} otherwise 233 */ 234 private boolean isValidPivotColumn(SimplexTableau tableau, int col) { 235 for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { 236 final double entry = tableau.getEntry(i, col); 237 238 // do the same check as in getPivotRow 239 if (Precision.compareTo(entry, 0d, cutOff) > 0) { 240 return true; 241 } 242 } 243 return false; 244 } 245 246 /** 247 * Returns the row with the minimum ratio as given by the minimum ratio test (MRT). 248 * 249 * @param tableau Simplex tableau for the problem. 250 * @param col Column to test the ratio of (see {@link #getPivotColumn(SimplexTableau)}). 251 * @return the row with the minimum ratio. 252 */ 253 private Integer getPivotRow(SimplexTableau tableau, final int col) { 254 // create a list of all the rows that tie for the lowest score in the minimum ratio test 255 List<Integer> minRatioPositions = new ArrayList<>(); 256 double minRatio = Double.MAX_VALUE; 257 for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) { 258 final double rhs = tableau.getEntry(i, tableau.getWidth() - 1); 259 final double entry = tableau.getEntry(i, col); 260 261 // only consider pivot elements larger than the cutOff threshold 262 // selecting others may lead to degeneracy or numerical instabilities 263 if (Precision.compareTo(entry, 0d, cutOff) > 0) { 264 final double ratio = FastMath.abs(rhs / entry); 265 // check if the entry is strictly equal to the current min ratio 266 // do not use a ulp/epsilon check 267 final int cmp = Double.compare(ratio, minRatio); 268 if (cmp == 0) { 269 minRatioPositions.add(i); 270 } else if (cmp < 0) { 271 minRatio = ratio; 272 minRatioPositions.clear(); 273 minRatioPositions.add(i); 274 } 275 } 276 } 277 278 if (minRatioPositions.isEmpty()) { 279 return null; 280 } else if (minRatioPositions.size() > 1) { 281 // there's a degeneracy as indicated by a tie in the minimum ratio test 282 283 // 1. check if there's an artificial variable that can be forced out of the basis 284 if (tableau.getNumArtificialVariables() > 0) { 285 for (Integer row : minRatioPositions) { 286 for (int i = 0; i < tableau.getNumArtificialVariables(); i++) { 287 int column = i + tableau.getArtificialVariableOffset(); 288 final double entry = tableau.getEntry(row, column); 289 if (Precision.equals(entry, 1d, maxUlps) && row.equals(tableau.getBasicRow(column))) { 290 return row; 291 } 292 } 293 } 294 } 295 296 // 2. apply Bland's rule to prevent cycling: 297 // take the row for which the corresponding basic variable has the smallest index 298 // 299 // see http://www.stanford.edu/class/msande310/blandrule.pdf 300 // see http://en.wikipedia.org/wiki/Bland%27s_rule (not equivalent to the above paper) 301 302 Integer minRow = null; 303 int minIndex = tableau.getWidth(); 304 for (Integer row : minRatioPositions) { 305 final int basicVar = tableau.getBasicVariable(row); 306 if (basicVar < minIndex) { 307 minIndex = basicVar; 308 minRow = row; 309 } 310 } 311 return minRow; 312 } 313 return minRatioPositions.get(0); 314 } 315 316 /** 317 * Runs one iteration of the Simplex method on the given model. 318 * 319 * @param tableau Simple tableau for the problem. 320 * @throws MathIllegalStateException if the allowed number of iterations has been exhausted. 321 * @throws MathIllegalStateException if the model is found not to have a bounded solution. 322 */ 323 protected void doIteration(final SimplexTableau tableau) 324 throws MathIllegalStateException { 325 326 incrementIterationCount(); 327 328 Integer pivotCol = getPivotColumn(tableau); 329 Integer pivotRow = getPivotRow(tableau, pivotCol); 330 if (pivotRow == null) { 331 throw new MathIllegalStateException(LocalizedOptimFormats.UNBOUNDED_SOLUTION); 332 } 333 334 tableau.performRowOperations(pivotCol, pivotRow); 335 } 336 337 /** 338 * Solves Phase 1 of the Simplex method. 339 * 340 * @param tableau Simple tableau for the problem. 341 * @throws MathIllegalStateException if the allowed number of iterations has been exhausted, 342 * or if the model is found not to have a bounded solution, or if there is no feasible solution 343 */ 344 protected void solvePhase1(final SimplexTableau tableau) 345 throws MathIllegalStateException { 346 347 // make sure we're in Phase 1 348 if (tableau.getNumArtificialVariables() == 0) { 349 return; 350 } 351 352 while (!tableau.isOptimal()) { 353 doIteration(tableau); 354 } 355 356 // if W is not zero then we have no feasible solution 357 if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) { 358 throw new MathIllegalStateException(LocalizedOptimFormats.NO_FEASIBLE_SOLUTION); 359 } 360 } 361 362 /** {@inheritDoc} */ 363 @Override 364 public PointValuePair doOptimize() 365 throws MathIllegalStateException { 366 367 // reset the tableau to indicate a non-feasible solution in case 368 // we do not pass phase 1 successfully 369 if (solutionCallback != null) { 370 solutionCallback.setTableau(null); 371 } 372 373 final SimplexTableau tableau = 374 new SimplexTableau(getFunction(), 375 getConstraints(), 376 getGoalType(), 377 isRestrictedToNonNegative(), 378 epsilon, 379 maxUlps); 380 381 solvePhase1(tableau); 382 tableau.dropPhase1Objective(); 383 384 // after phase 1, we are sure to have a feasible solution 385 if (solutionCallback != null) { 386 solutionCallback.setTableau(tableau); 387 } 388 389 while (!tableau.isOptimal()) { 390 doIteration(tableau); 391 } 392 393 // check that the solution respects the nonNegative restriction in case 394 // the epsilon/cutOff values are too large for the actual linear problem 395 // (e.g. with very small constraint coefficients), the solver might actually 396 // find a non-valid solution (with negative coefficients). 397 final PointValuePair solution = tableau.getSolution(); 398 if (isRestrictedToNonNegative()) { 399 final double[] coeff = solution.getPoint(); 400 for (int i = 0; i < coeff.length; i++) { 401 if (Precision.compareTo(coeff[i], 0, epsilon) < 0) { 402 throw new MathIllegalStateException(LocalizedOptimFormats.NO_FEASIBLE_SOLUTION); 403 } 404 } 405 } 406 return solution; 407 } 408 }