View Javadoc
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  
19  import org.apache.commons.math4.legacy.optim.OptimizationData;
20  import org.apache.commons.math4.legacy.optim.PointValuePair;
21  
22  /**
23   * A callback object that can be provided to a linear optimizer to keep track
24   * of the best solution found.
25   *
26   * @since 3.3
27   */
28  public class SolutionCallback implements OptimizationData {
29      /** The SimplexTableau used by the SimplexSolver. */
30      private SimplexTableau tableau;
31  
32      /**
33       * Set the simplex tableau used during the optimization once a feasible
34       * solution has been found.
35       *
36       * @param tableau the simplex tableau containing a feasible solution
37       */
38      void setTableau(final SimplexTableau tableau) {
39          this.tableau = tableau;
40      }
41  
42      /**
43       * Retrieve the best solution found so far.
44       * <p>
45       * <b>Note:</b> the returned solution may not be optimal, e.g. in case
46       * the optimizer did reach the iteration limits.
47       *
48       * @return the best solution found so far by the optimizer, or {@code null} if
49       * no feasible solution could be found
50       */
51      public PointValuePair getSolution() {
52          return tableau != null ? tableau.getSolution() : null;
53      }
54  
55      /**
56       * Returns if the found solution is optimal.
57       * @return {@code true} if the solution is optimal, {@code false} otherwise
58       */
59      public boolean isSolutionOptimal() {
60          return tableau != null && tableau.isOptimal();
61      }
62  }