    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     */
    017
    018    package org.apache.commons.math3.optimization.linear;
    019
    020    import java.util.Collection;
    021    import java.util.Collections;
    022
    023    import org.apache.commons.math3.exception.MathIllegalStateException;
    024    import org.apache.commons.math3.exception.MaxCountExceededException;
    025    import org.apache.commons.math3.optimization.GoalType;
    026    import org.apache.commons.math3.optimization.PointValuePair;
    027
    028    /**
    029     * Base class for implementing linear optimizers.
    030     * <p>
    031     * This base class handles the boilerplate methods associated to thresholds
    032     * settings and iterations counters.
    033     *
    034     * @version $Id: AbstractLinearOptimizer.java 1422230 2012-12-15 12:11:13Z erans $
    035     * @deprecated As of 3.1 (to be removed in 4.0).
    036     * @since 2.0
    037     */
    038    @Deprecated
    039    public abstract class AbstractLinearOptimizer implements LinearOptimizer {
    040
    041        /** Default maximal number of iterations allowed. */
    042        public static final int DEFAULT_MAX_ITERATIONS = 100;
    043
    044        /**
    045         * Linear objective function.
    046         * @since 2.1
    047         */
    048        private LinearObjectiveFunction function;
    049
    050        /**
    051         * Linear constraints.
    052         * @since 2.1
    053         */
    054        private Collection<LinearConstraint> linearConstraints;
    055
    056        /**
    057         * Type of optimization goal: either {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
    058         * @since 2.1
    059         */
    060        private GoalType goal;
    061
    062        /**
    063         * Whether to restrict the variables to non-negative values.
    064         * @since 2.1
    065         */
    066        private boolean nonNegative;
    067
    068        /** Maximal number of iterations allowed. */
    069        private int maxIterations;
    070
    071        /** Number of iterations already performed. */
    072        private int iterations;
    073
    074        /**
    075         * Simple constructor with default settings.
    076         * <p>The maximal number of evaluation is set to its default value.</p>
    077         */
    078        protected AbstractLinearOptimizer() {
    079            setMaxIterations(DEFAULT_MAX_ITERATIONS);
    080        }
    081
    082        /**
    083         * @return {@code true} if the variables are restricted to non-negative values.
    084         */
    085        protected boolean restrictToNonNegative() {
    086            return nonNegative;
    087        }
    088
    089        /**
    090         * @return the optimization type.
    091         */
    092        protected GoalType getGoalType() {
    093            return goal;
    094        }
    095
    096        /**
    097         * @return the optimization type.
    098         */
    099        protected LinearObjectiveFunction getFunction() {
    100            return function;
    101        }
    102
    103        /**
    104         * @return the optimization type.
    105         */
    106        protected Collection<LinearConstraint> getConstraints() {
    107            return Collections.unmodifiableCollection(linearConstraints);
    108        }
    109
    110        /** {@inheritDoc} */
    111        public void setMaxIterations(int maxIterations) {
    112            this.maxIterations = maxIterations;
    113        }
    114
    115        /** {@inheritDoc} */
    116        public int getMaxIterations() {
    117            return maxIterations;
    118        }
    119
    120        /** {@inheritDoc} */
    121        public int getIterations() {
    122            return iterations;
    123        }
    124
    125        /**
    126         * Increment the iterations counter by 1.
    127         * @exception MaxCountExceededException if the maximal number of iterations is exceeded
    128         */
    129        protected void incrementIterationsCounter()
    130            throws MaxCountExceededException {
    131            if (++iterations > maxIterations) {
    132                throw new MaxCountExceededException(maxIterations);
    133            }
    134        }
    135
    136        /** {@inheritDoc} */
    137        public PointValuePair optimize(final LinearObjectiveFunction f,
    138                                       final Collection<LinearConstraint> constraints,
    139                                       final GoalType goalType, final boolean restrictToNonNegative)
    140            throws MathIllegalStateException {
    141
    142            // store linear problem characteristics
    143            this.function          = f;
    144            this.linearConstraints = constraints;
    145            this.goal              = goalType;
    146            this.nonNegative       = restrictToNonNegative;
    147
    148            iterations  = 0;
    149
    150            // solve the problem
    151            return doOptimize();
    152
    153        }
    154
    155        /**
    156         * Perform the bulk of optimization algorithm.
    157         * @return the point/value pair giving the optimal value for objective function
    158         * @exception MathIllegalStateException if no solution fulfilling the constraints
    159         * can be found in the allowed number of iterations
    160         */
    161        protected abstract PointValuePair doOptimize() throws MathIllegalStateException;
    162
    163    }