View Javadoc
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.math3.optimization.linear;
19  
20  import java.util.Collection;
21  import java.util.Collections;
22  
23  import org.apache.commons.math3.exception.MathIllegalStateException;
24  import org.apache.commons.math3.exception.MaxCountExceededException;
25  import org.apache.commons.math3.optimization.GoalType;
26  import org.apache.commons.math3.optimization.PointValuePair;
27  
28  /**
29   * Base class for implementing linear optimizers.
30   * <p>
31   * This base class handles the boilerplate methods associated to thresholds
32   * settings and iterations counters.
33   *
34   * @deprecated As of 3.1 (to be removed in 4.0).
35   * @since 2.0
36   */
37  @Deprecated
38  public abstract class AbstractLinearOptimizer implements LinearOptimizer {
39  
40      /** Default maximal number of iterations allowed. */
41      public static final int DEFAULT_MAX_ITERATIONS = 100;
42  
43      /**
44       * Linear objective function.
45       * @since 2.1
46       */
47      private LinearObjectiveFunction function;
48  
49      /**
50       * Linear constraints.
51       * @since 2.1
52       */
53      private Collection<LinearConstraint> linearConstraints;
54  
55      /**
56       * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
57       * @since 2.1
58       */
59      private GoalType goal;
60  
61      /**
62       * Whether to restrict the variables to non-negative values.
63       * @since 2.1
64       */
65      private boolean nonNegative;
66  
67      /** Maximal number of iterations allowed. */
68      private int maxIterations;
69  
70      /** Number of iterations already performed. */
71      private int iterations;
72  
73      /**
74       * Simple constructor with default settings.
75       * <p>The maximal number of evaluation is set to its default value.</p>
76       */
77      protected AbstractLinearOptimizer() {
78          setMaxIterations(DEFAULT_MAX_ITERATIONS);
79      }
80  
81      /**
82       * @return {@code true} if the variables are restricted to non-negative values.
83       */
84      protected boolean restrictToNonNegative() {
85          return nonNegative;
86      }
87  
88      /**
89       * @return the optimization type.
90       */
91      protected GoalType getGoalType() {
92          return goal;
93      }
94  
95      /**
96       * @return the optimization type.
97       */
98      protected LinearObjectiveFunction getFunction() {
99          return function;
100     }
101 
102     /**
103      * @return the optimization type.
104      */
105     protected Collection<LinearConstraint> getConstraints() {
106         return Collections.unmodifiableCollection(linearConstraints);
107     }
108 
109     /** {@inheritDoc} */
110     public void setMaxIterations(int maxIterations) {
111         this.maxIterations = maxIterations;
112     }
113 
114     /** {@inheritDoc} */
115     public int getMaxIterations() {
116         return maxIterations;
117     }
118 
119     /** {@inheritDoc} */
120     public int getIterations() {
121         return iterations;
122     }
123 
124     /**
125      * Increment the iterations counter by 1.
126      * @exception MaxCountExceededException if the maximal number of iterations is exceeded
127      */
128     protected void incrementIterationsCounter()
129         throws MaxCountExceededException {
130         if (++iterations > maxIterations) {
131             throw new MaxCountExceededException(maxIterations);
132         }
133     }
134 
135     /** {@inheritDoc} */
136     public PointValuePair optimize(final LinearObjectiveFunction f,
137                                    final Collection<LinearConstraint> constraints,
138                                    final GoalType goalType, final boolean restrictToNonNegative)
139         throws MathIllegalStateException {
140 
141         // store linear problem characteristics
142         this.function          = f;
143         this.linearConstraints = constraints;
144         this.goal              = goalType;
145         this.nonNegative       = restrictToNonNegative;
146 
147         iterations  = 0;
148 
149         // solve the problem
150         return doOptimize();
151 
152     }
153 
154     /**
155      * Perform the bulk of optimization algorithm.
156      * @return the point/value pair giving the optimal value for objective function
157      * @exception MathIllegalStateException if no solution fulfilling the constraints
158      * can be found in the allowed number of iterations
159      */
160     protected abstract PointValuePair doOptimize() throws MathIllegalStateException;
161 
162 }