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.math.optimization.linear; 19 20 import java.util.Collection; 21 22 import org.apache.commons.math.MaxIterationsExceededException; 23 import org.apache.commons.math.optimization.GoalType; 24 import org.apache.commons.math.optimization.OptimizationException; 25 import org.apache.commons.math.optimization.RealPointValuePair; 26 27 /** 28 * Base class for implementing linear optimizers. 29 * <p>This base class handles the boilerplate methods associated to thresholds 30 * settings and iterations counters.</p> 31 * @version $Revision: 786466 $ $Date: 2009-06-19 08:03:14 -0400 (Fri, 19 Jun 2009) $ 32 * @since 2.0 33 * 34 */ 35 public abstract class AbstractLinearOptimizer implements LinearOptimizer { 36 37 /** Default maximal number of iterations allowed. */ 38 public static final int DEFAULT_MAX_ITERATIONS = 100; 39 40 /** Maximal number of iterations allowed. */ 41 private int maxIterations; 42 43 /** Number of iterations already performed. */ 44 private int iterations; 45 46 /** Linear objective function. */ 47 protected LinearObjectiveFunction f; 48 49 /** Linear constraints. */ 50 protected Collection<LinearConstraint> constraints; 51 52 /** Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}. */ 53 protected GoalType goalType; 54 55 /** Whether to restrict the variables to non-negative values. */ 56 protected boolean restrictToNonNegative; 57 58 /** Simple constructor with default settings. 59 * <p>The maximal number of evaluation is set to its default value.</p> 60 */ 61 protected AbstractLinearOptimizer() { 62 setMaxIterations(DEFAULT_MAX_ITERATIONS); 63 } 64 65 /** {@inheritDoc} */ 66 public void setMaxIterations(int maxIterations) { 67 this.maxIterations = maxIterations; 68 } 69 70 /** {@inheritDoc} */ 71 public int getMaxIterations() { 72 return maxIterations; 73 } 74 75 /** {@inheritDoc} */ 76 public int getIterations() { 77 return iterations; 78 } 79 80 /** Increment the iterations counter by 1. 81 * @exception OptimizationException if the maximal number 82 * of iterations is exceeded 83 */ 84 protected void incrementIterationsCounter() 85 throws OptimizationException { 86 if (++iterations > maxIterations) { 87 throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); 88 } 89 } 90 91 /** {@inheritDoc} */ 92 public RealPointValuePair optimize(final LinearObjectiveFunction f, 93 final Collection<LinearConstraint> constraints, 94 final GoalType goalType, final boolean restrictToNonNegative) 95 throws OptimizationException { 96 97 // store linear problem characteristics 98 this.f = f; 99 this.constraints = constraints; 100 this.goalType = goalType; 101 this.restrictToNonNegative = restrictToNonNegative; 102 103 iterations = 0; 104 105 // solve the problem 106 return doOptimize(); 107 108 } 109 110 /** Perform the bulk of optimization algorithm. 111 * @return the point/value pair giving the optimal value for objective function 112 * @exception OptimizationException if no solution fulfilling the constraints 113 * can be found in the allowed number of iterations 114 */ 115 abstract protected RealPointValuePair doOptimize() 116 throws OptimizationException; 117 118 }