AbstractLinearOptimizer.java

  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.optimization.linear;

  18. import java.util.Collection;
  19. import java.util.Collections;

  20. import org.apache.commons.math3.exception.MathIllegalStateException;
  21. import org.apache.commons.math3.exception.MaxCountExceededException;
  22. import org.apache.commons.math3.optimization.GoalType;
  23. import org.apache.commons.math3.optimization.PointValuePair;

  24. /**
  25.  * Base class for implementing linear optimizers.
  26.  * <p>
  27.  * This base class handles the boilerplate methods associated to thresholds
  28.  * settings and iterations counters.
  29.  *
  30.  * @deprecated As of 3.1 (to be removed in 4.0).
  31.  * @since 2.0
  32.  */
  33. @Deprecated
  34. public abstract class AbstractLinearOptimizer implements LinearOptimizer {

  35.     /** Default maximal number of iterations allowed. */
  36.     public static final int DEFAULT_MAX_ITERATIONS = 100;

  37.     /**
  38.      * Linear objective function.
  39.      * @since 2.1
  40.      */
  41.     private LinearObjectiveFunction function;

  42.     /**
  43.      * Linear constraints.
  44.      * @since 2.1
  45.      */
  46.     private Collection<LinearConstraint> linearConstraints;

  47.     /**
  48.      * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
  49.      * @since 2.1
  50.      */
  51.     private GoalType goal;

  52.     /**
  53.      * Whether to restrict the variables to non-negative values.
  54.      * @since 2.1
  55.      */
  56.     private boolean nonNegative;

  57.     /** Maximal number of iterations allowed. */
  58.     private int maxIterations;

  59.     /** Number of iterations already performed. */
  60.     private int iterations;

  61.     /**
  62.      * Simple constructor with default settings.
  63.      * <p>The maximal number of evaluation is set to its default value.</p>
  64.      */
  65.     protected AbstractLinearOptimizer() {
  66.         setMaxIterations(DEFAULT_MAX_ITERATIONS);
  67.     }

  68.     /**
  69.      * @return {@code true} if the variables are restricted to non-negative values.
  70.      */
  71.     protected boolean restrictToNonNegative() {
  72.         return nonNegative;
  73.     }

  74.     /**
  75.      * @return the optimization type.
  76.      */
  77.     protected GoalType getGoalType() {
  78.         return goal;
  79.     }

  80.     /**
  81.      * @return the optimization type.
  82.      */
  83.     protected LinearObjectiveFunction getFunction() {
  84.         return function;
  85.     }

  86.     /**
  87.      * @return the optimization type.
  88.      */
  89.     protected Collection<LinearConstraint> getConstraints() {
  90.         return Collections.unmodifiableCollection(linearConstraints);
  91.     }

  92.     /** {@inheritDoc} */
  93.     public void setMaxIterations(int maxIterations) {
  94.         this.maxIterations = maxIterations;
  95.     }

  96.     /** {@inheritDoc} */
  97.     public int getMaxIterations() {
  98.         return maxIterations;
  99.     }

  100.     /** {@inheritDoc} */
  101.     public int getIterations() {
  102.         return iterations;
  103.     }

  104.     /**
  105.      * Increment the iterations counter by 1.
  106.      * @exception MaxCountExceededException if the maximal number of iterations is exceeded
  107.      */
  108.     protected void incrementIterationsCounter()
  109.         throws MaxCountExceededException {
  110.         if (++iterations > maxIterations) {
  111.             throw new MaxCountExceededException(maxIterations);
  112.         }
  113.     }

  114.     /** {@inheritDoc} */
  115.     public PointValuePair optimize(final LinearObjectiveFunction f,
  116.                                    final Collection<LinearConstraint> constraints,
  117.                                    final GoalType goalType, final boolean restrictToNonNegative)
  118.         throws MathIllegalStateException {

  119.         // store linear problem characteristics
  120.         this.function          = f;
  121.         this.linearConstraints = constraints;
  122.         this.goal              = goalType;
  123.         this.nonNegative       = restrictToNonNegative;

  124.         iterations  = 0;

  125.         // solve the problem
  126.         return doOptimize();

  127.     }

  128.     /**
  129.      * Perform the bulk of optimization algorithm.
  130.      * @return the point/value pair giving the optimal value for objective function
  131.      * @exception MathIllegalStateException if no solution fulfilling the constraints
  132.      * can be found in the allowed number of iterations
  133.      */
  134.     protected abstract PointValuePair doOptimize() throws MathIllegalStateException;

  135. }