LinearOptimizer.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.math4.legacy.optim.linear;

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

  20. import org.apache.commons.math4.legacy.exception.TooManyIterationsException;
  21. import org.apache.commons.math4.legacy.optim.OptimizationData;
  22. import org.apache.commons.math4.legacy.optim.PointValuePair;
  23. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.MultivariateOptimizer;

  24. /**
  25.  * Base class for implementing linear optimizers.
  26.  *
  27.  * @since 3.1
  28.  */
  29. public abstract class LinearOptimizer
  30.     extends MultivariateOptimizer {
  31.     /**
  32.      * Linear objective function.
  33.      */
  34.     private LinearObjectiveFunction function;
  35.     /**
  36.      * Linear constraints.
  37.      */
  38.     private Collection<LinearConstraint> linearConstraints;
  39.     /**
  40.      * Whether to restrict the variables to non-negative values.
  41.      */
  42.     private boolean nonNegative;

  43.     /**
  44.      * Simple constructor with default settings.
  45.      *
  46.      */
  47.     protected LinearOptimizer() {
  48.         super(null); // No convergence checker.
  49.     }

  50.     /**
  51.      * @return {@code true} if the variables are restricted to non-negative values.
  52.      */
  53.     protected boolean isRestrictedToNonNegative() {
  54.         return nonNegative;
  55.     }

  56.     /**
  57.      * @return the optimization type.
  58.      */
  59.     protected LinearObjectiveFunction getFunction() {
  60.         return function;
  61.     }

  62.     /**
  63.      * @return the optimization type.
  64.      */
  65.     protected Collection<LinearConstraint> getConstraints() {
  66.         return Collections.unmodifiableCollection(linearConstraints);
  67.     }

  68.     /**
  69.      * {@inheritDoc}
  70.      *
  71.      * @param optData Optimization data. In addition to those documented in
  72.      * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
  73.      * MultivariateOptimizer}, this method will register the following data:
  74.      * <ul>
  75.      *  <li>{@link LinearObjectiveFunction}</li>
  76.      *  <li>{@link LinearConstraintSet}</li>
  77.      *  <li>{@link NonNegativeConstraint}</li>
  78.      * </ul>
  79.      * @return {@inheritDoc}
  80.      * @throws TooManyIterationsException if the maximal number of
  81.      * iterations is exceeded.
  82.      */
  83.     @Override
  84.     public PointValuePair optimize(OptimizationData... optData)
  85.         throws TooManyIterationsException {
  86.         // Set up base class and perform computation.
  87.         return super.optimize(optData);
  88.     }

  89.     /**
  90.      * Scans the list of (required and optional) optimization data that
  91.      * characterize the problem.
  92.      *
  93.      * @param optData Optimization data.
  94.      * The following data will be looked for:
  95.      * <ul>
  96.      *  <li>{@link LinearObjectiveFunction}</li>
  97.      *  <li>{@link LinearConstraintSet}</li>
  98.      *  <li>{@link NonNegativeConstraint}</li>
  99.      * </ul>
  100.      */
  101.     @Override
  102.     protected void parseOptimizationData(OptimizationData... optData) {
  103.         // Allow base class to register its own data.
  104.         super.parseOptimizationData(optData);

  105.         // The existing values (as set by the previous call) are reused if
  106.         // not provided in the argument list.
  107.         for (OptimizationData data : optData) {
  108.             if (data instanceof LinearObjectiveFunction) {
  109.                 function = (LinearObjectiveFunction) data;
  110.                 continue;
  111.             }
  112.             if (data instanceof LinearConstraintSet) {
  113.                 linearConstraints = ((LinearConstraintSet) data).getConstraints();
  114.                 continue;
  115.             }
  116.             if  (data instanceof NonNegativeConstraint) {
  117.                 nonNegative = ((NonNegativeConstraint) data).isRestrictedToNonNegative();
  118.                 continue;
  119.             }
  120.         }
  121.     }
  122. }