Home | History | Annotate | Download | only in linear
      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: 925812 $ $Date: 2010-03-21 16:49:31 +0100 (dim. 21 mars 2010) $
     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     /**
     41      * Linear objective function.
     42      * @since 2.1
     43      */
     44     protected LinearObjectiveFunction function;
     45 
     46     /**
     47      * Linear constraints.
     48      * @since 2.1
     49      */
     50     protected Collection<LinearConstraint> linearConstraints;
     51 
     52     /**
     53      * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
     54      * @since 2.1
     55      */
     56     protected GoalType goal;
     57 
     58     /**
     59      * Whether to restrict the variables to non-negative values.
     60      * @since 2.1
     61      */
     62     protected boolean nonNegative;
     63 
     64     /** Maximal number of iterations allowed. */
     65     private int maxIterations;
     66 
     67     /** Number of iterations already performed. */
     68     private int iterations;
     69 
     70     /** Simple constructor with default settings.
     71      * <p>The maximal number of evaluation is set to its default value.</p>
     72      */
     73     protected AbstractLinearOptimizer() {
     74         setMaxIterations(DEFAULT_MAX_ITERATIONS);
     75     }
     76 
     77     /** {@inheritDoc} */
     78     public void setMaxIterations(int maxIterations) {
     79         this.maxIterations = maxIterations;
     80     }
     81 
     82     /** {@inheritDoc} */
     83     public int getMaxIterations() {
     84         return maxIterations;
     85     }
     86 
     87     /** {@inheritDoc} */
     88     public int getIterations() {
     89         return iterations;
     90     }
     91 
     92     /** Increment the iterations counter by 1.
     93      * @exception OptimizationException if the maximal number
     94      * of iterations is exceeded
     95      */
     96     protected void incrementIterationsCounter()
     97         throws OptimizationException {
     98         if (++iterations > maxIterations) {
     99             throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
    100         }
    101     }
    102 
    103     /** {@inheritDoc} */
    104     public RealPointValuePair optimize(final LinearObjectiveFunction f,
    105                                        final Collection<LinearConstraint> constraints,
    106                                        final GoalType goalType, final boolean restrictToNonNegative)
    107          throws OptimizationException {
    108 
    109         // store linear problem characteristics
    110         this.function          = f;
    111         this.linearConstraints = constraints;
    112         this.goal              = goalType;
    113         this.nonNegative       = restrictToNonNegative;
    114 
    115         iterations  = 0;
    116 
    117         // solve the problem
    118         return doOptimize();
    119 
    120     }
    121 
    122     /** Perform the bulk of optimization algorithm.
    123      * @return the point/value pair giving the optimal value for objective function
    124      * @exception OptimizationException if no solution fulfilling the constraints
    125      * can be found in the allowed number of iterations
    126      */
    127     protected abstract RealPointValuePair doOptimize()
    128         throws OptimizationException;
    129 
    130 }
    131