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.math4.optim.linear;
018
019import org.apache.commons.math4.optim.OptimizationData;
020import org.apache.commons.math4.optim.PointValuePair;
021
022/**
023 * A callback object that can be provided to a linear optimizer to keep track
024 * of the best solution found.
025 *
026 * @since 3.3
027 */
028public class SolutionCallback implements OptimizationData {
029    /** The SimplexTableau used by the SimplexSolver. */
030    private SimplexTableau tableau;
031
032    /**
033     * Set the simplex tableau used during the optimization once a feasible
034     * solution has been found.
035     *
036     * @param tableau the simplex tableau containing a feasible solution
037     */
038    void setTableau(final SimplexTableau tableau) {
039        this.tableau = tableau;
040    }
041
042    /**
043     * Retrieve the best solution found so far.
044     * <p>
045     * <b>Note:</b> the returned solution may not be optimal, e.g. in case
046     * the optimizer did reach the iteration limits.
047     *
048     * @return the best solution found so far by the optimizer, or {@code null} if
049     * no feasible solution could be found
050     */
051    public PointValuePair getSolution() {
052        return tableau != null ? tableau.getSolution() : null;
053    }
054
055    /**
056     * Returns if the found solution is optimal.
057     * @return {@code true} if the solution is optimal, {@code false} otherwise
058     */
059    public boolean isSolutionOptimal() {
060        return tableau != null ? tableau.isOptimal() : false;
061    }
062}