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  package org.apache.commons.math4.legacy.optim.linear;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  
22  import org.apache.commons.math4.legacy.exception.TooManyIterationsException;
23  import org.apache.commons.math4.legacy.optim.OptimizationData;
24  import org.apache.commons.math4.legacy.optim.PointValuePair;
25  import org.apache.commons.math4.legacy.optim.nonlinear.scalar.MultivariateOptimizer;
26  
27  /**
28   * Base class for implementing linear optimizers.
29   *
30   * @since 3.1
31   */
32  public abstract class LinearOptimizer
33      extends MultivariateOptimizer {
34      /**
35       * Linear objective function.
36       */
37      private LinearObjectiveFunction function;
38      /**
39       * Linear constraints.
40       */
41      private Collection<LinearConstraint> linearConstraints;
42      /**
43       * Whether to restrict the variables to non-negative values.
44       */
45      private boolean nonNegative;
46  
47      /**
48       * Simple constructor with default settings.
49       *
50       */
51      protected LinearOptimizer() {
52          super(null); // No convergence checker.
53      }
54  
55      /**
56       * @return {@code true} if the variables are restricted to non-negative values.
57       */
58      protected boolean isRestrictedToNonNegative() {
59          return nonNegative;
60      }
61  
62      /**
63       * @return the optimization type.
64       */
65      protected LinearObjectiveFunction getFunction() {
66          return function;
67      }
68  
69      /**
70       * @return the optimization type.
71       */
72      protected Collection<LinearConstraint> getConstraints() {
73          return Collections.unmodifiableCollection(linearConstraints);
74      }
75  
76      /**
77       * {@inheritDoc}
78       *
79       * @param optData Optimization data. In addition to those documented in
80       * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
81       * MultivariateOptimizer}, this method will register the following data:
82       * <ul>
83       *  <li>{@link LinearObjectiveFunction}</li>
84       *  <li>{@link LinearConstraintSet}</li>
85       *  <li>{@link NonNegativeConstraint}</li>
86       * </ul>
87       * @return {@inheritDoc}
88       * @throws TooManyIterationsException if the maximal number of
89       * iterations is exceeded.
90       */
91      @Override
92      public PointValuePair optimize(OptimizationData... optData)
93          throws TooManyIterationsException {
94          // Set up base class and perform computation.
95          return super.optimize(optData);
96      }
97  
98      /**
99       * Scans the list of (required and optional) optimization data that
100      * characterize the problem.
101      *
102      * @param optData Optimization data.
103      * The following data will be looked for:
104      * <ul>
105      *  <li>{@link LinearObjectiveFunction}</li>
106      *  <li>{@link LinearConstraintSet}</li>
107      *  <li>{@link NonNegativeConstraint}</li>
108      * </ul>
109      */
110     @Override
111     protected void parseOptimizationData(OptimizationData... optData) {
112         // Allow base class to register its own data.
113         super.parseOptimizationData(optData);
114 
115         // The existing values (as set by the previous call) are reused if
116         // not provided in the argument list.
117         for (OptimizationData data : optData) {
118             if (data instanceof LinearObjectiveFunction) {
119                 function = (LinearObjectiveFunction) data;
120                 continue;
121             }
122             if (data instanceof LinearConstraintSet) {
123                 linearConstraints = ((LinearConstraintSet) data).getConstraints();
124                 continue;
125             }
126             if  (data instanceof NonNegativeConstraint) {
127                 nonNegative = ((NonNegativeConstraint) data).isRestrictedToNonNegative();
128                 continue;
129             }
130         }
131     }
132 }