001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.optim.linear;
018
019import java.util.Collection;
020import java.util.Collections;
021import org.apache.commons.math3.exception.TooManyIterationsException;
022import org.apache.commons.math3.optim.OptimizationData;
023import org.apache.commons.math3.optim.PointValuePair;
024import org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer;
025
026/**
027 * Base class for implementing linear optimizers.
028 *
029 * @since 3.1
030 */
031public abstract class LinearOptimizer
032    extends MultivariateOptimizer {
033    /**
034     * Linear objective function.
035     */
036    private LinearObjectiveFunction function;
037    /**
038     * Linear constraints.
039     */
040    private Collection<LinearConstraint> linearConstraints;
041    /**
042     * Whether to restrict the variables to non-negative values.
043     */
044    private boolean nonNegative;
045
046    /**
047     * Simple constructor with default settings.
048     *
049     */
050    protected LinearOptimizer() {
051        super(null); // No convergence checker.
052    }
053
054    /**
055     * @return {@code true} if the variables are restricted to non-negative values.
056     */
057    protected boolean isRestrictedToNonNegative() {
058        return nonNegative;
059    }
060
061    /**
062     * @return the optimization type.
063     */
064    protected LinearObjectiveFunction getFunction() {
065        return function;
066    }
067
068    /**
069     * @return the optimization type.
070     */
071    protected Collection<LinearConstraint> getConstraints() {
072        return Collections.unmodifiableCollection(linearConstraints);
073    }
074
075    /**
076     * {@inheritDoc}
077     *
078     * @param optData Optimization data. In addition to those documented in
079     * {@link MultivariateOptimizer#parseOptimizationData(OptimizationData[])
080     * MultivariateOptimizer}, this method will register the following data:
081     * <ul>
082     *  <li>{@link LinearObjectiveFunction}</li>
083     *  <li>{@link LinearConstraintSet}</li>
084     *  <li>{@link NonNegativeConstraint}</li>
085     * </ul>
086     * @return {@inheritDoc}
087     * @throws TooManyIterationsException if the maximal number of
088     * iterations is exceeded.
089     */
090    @Override
091    public PointValuePair optimize(OptimizationData... optData)
092        throws TooManyIterationsException {
093        // Set up base class and perform computation.
094        return super.optimize(optData);
095    }
096
097    /**
098     * Scans the list of (required and optional) optimization data that
099     * characterize the problem.
100     *
101     * @param optData Optimization data.
102     * The following data will be looked for:
103     * <ul>
104     *  <li>{@link LinearObjectiveFunction}</li>
105     *  <li>{@link LinearConstraintSet}</li>
106     *  <li>{@link NonNegativeConstraint}</li>
107     * </ul>
108     */
109    @Override
110    protected void parseOptimizationData(OptimizationData... optData) {
111        // Allow base class to register its own data.
112        super.parseOptimizationData(optData);
113
114        // The existing values (as set by the previous call) are reused if
115        // not provided in the argument list.
116        for (OptimizationData data : optData) {
117            if (data instanceof LinearObjectiveFunction) {
118                function = (LinearObjectiveFunction) data;
119                continue;
120            }
121            if (data instanceof LinearConstraintSet) {
122                linearConstraints = ((LinearConstraintSet) data).getConstraints();
123                continue;
124            }
125            if  (data instanceof NonNegativeConstraint) {
126                nonNegative = ((NonNegativeConstraint) data).isRestrictedToNonNegative();
127                continue;
128            }
129        }
130    }
131}