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.  *      https://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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.linear;

  22. import java.util.Collection;
  23. import java.util.Collections;

  24. import org.hipparchus.exception.MathIllegalStateException;
  25. import org.hipparchus.optim.OptimizationData;
  26. import org.hipparchus.optim.PointValuePair;
  27. import org.hipparchus.optim.nonlinear.scalar.MultivariateOptimizer;

  28. /**
  29.  * Base class for implementing linear optimizers.
  30.  *
  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.      * Simple constructor with default settings.
  48.      *
  49.      */
  50.     protected LinearOptimizer() {
  51.         super(null); // No convergence checker.
  52.     }

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

  59.     /** Get optimization type.
  60.      * @return the optimization type
  61.      */
  62.     protected LinearObjectiveFunction getFunction() {
  63.         return function;
  64.     }

  65.     /** Get constraints.
  66.      * @return the constraints
  67.      */
  68.     protected Collection<LinearConstraint> getConstraints() {
  69.         return Collections.unmodifiableCollection(linearConstraints);
  70.     }

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

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

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