SolutionCallback.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 org.apache.commons.math4.legacy.optim.OptimizationData;
  19. import org.apache.commons.math4.legacy.optim.PointValuePair;

  20. /**
  21.  * A callback object that can be provided to a linear optimizer to keep track
  22.  * of the best solution found.
  23.  *
  24.  * @since 3.3
  25.  */
  26. public class SolutionCallback implements OptimizationData {
  27.     /** The SimplexTableau used by the SimplexSolver. */
  28.     private SimplexTableau tableau;

  29.     /**
  30.      * Set the simplex tableau used during the optimization once a feasible
  31.      * solution has been found.
  32.      *
  33.      * @param tableau the simplex tableau containing a feasible solution
  34.      */
  35.     void setTableau(final SimplexTableau tableau) {
  36.         this.tableau = tableau;
  37.     }

  38.     /**
  39.      * Retrieve the best solution found so far.
  40.      * <p>
  41.      * <b>Note:</b> the returned solution may not be optimal, e.g. in case
  42.      * the optimizer did reach the iteration limits.
  43.      *
  44.      * @return the best solution found so far by the optimizer, or {@code null} if
  45.      * no feasible solution could be found
  46.      */
  47.     public PointValuePair getSolution() {
  48.         return tableau != null ? tableau.getSolution() : null;
  49.     }

  50.     /**
  51.      * Returns if the found solution is optimal.
  52.      * @return {@code true} if the solution is optimal, {@code false} otherwise
  53.      */
  54.     public boolean isSolutionOptimal() {
  55.         return tableau != null && tableau.isOptimal();
  56.     }
  57. }