001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package org.wpilib.math.optimization.solver;
006
007/** Solver exit status. Negative values indicate failure. */
008public enum ExitStatus {
009  /** Solved the problem to the desired tolerance. */
010  SUCCESS(0),
011  /** The solver returned its solution so far after the user requested a stop. */
012  CALLBACK_REQUESTED_STOP(1),
013  /** The solver determined the problem to be overconstrained and gave up. */
014  TOO_FEW_DOFS(-1),
015  /** The solver determined the problem to be locally infeasible and gave up. */
016  LOCALLY_INFEASIBLE(-2),
017  /** The problem setup frontend determined the problem to have an empty feasible region. */
018  GLOBALLY_INFEASIBLE(-3),
019  /** The linear system factorization failed. */
020  FACTORIZATION_FAILED(-4),
021  /** The backtracking line search failed, and the problem isn't locally infeasible. */
022  LINE_SEARCH_FAILED(-5),
023  /**
024   * The solver failed to reach the desired tolerance, and feasibility restoration failed to
025   * converge.
026   */
027  FEASIBILITY_RESTORATION_FAILED(-6),
028  /** The solver encountered nonfinite initial cost, constraints, or derivatives and gave up. */
029  NONFINITE_INITIAL_GUESS(-7),
030  /** The solver encountered diverging primal iterates xₖ and/or sₖ and gave up. */
031  DIVERGING_ITERATES(-8),
032  /** The solver returned its solution so far after exceeding the maximum number of iterations. */
033  MAX_ITERATIONS_EXCEEDED(-9),
034  /**
035   * The solver returned its solution so far after exceeding the maximum elapsed wall clock time.
036   */
037  TIMEOUT(-10);
038
039  /** ExitStatus value. */
040  public final int value;
041
042  ExitStatus(int value) {
043    this.value = value;
044  }
045
046  /**
047   * Converts integer to its corresponding enum value.
048   *
049   * @param x The integer.
050   * @return The enum value.
051   */
052  public static ExitStatus fromInt(int x) {
053    return switch (x) {
054      case 0 -> ExitStatus.SUCCESS;
055      case 1 -> ExitStatus.CALLBACK_REQUESTED_STOP;
056      case -1 -> ExitStatus.TOO_FEW_DOFS;
057      case -2 -> ExitStatus.LOCALLY_INFEASIBLE;
058      case -3 -> ExitStatus.GLOBALLY_INFEASIBLE;
059      case -4 -> ExitStatus.FACTORIZATION_FAILED;
060      case -5 -> ExitStatus.LINE_SEARCH_FAILED;
061      case -6 -> ExitStatus.FEASIBILITY_RESTORATION_FAILED;
062      case -7 -> ExitStatus.NONFINITE_INITIAL_GUESS;
063      case -8 -> ExitStatus.DIVERGING_ITERATES;
064      case -9 -> ExitStatus.MAX_ITERATIONS_EXCEEDED;
065      case -10 -> ExitStatus.TIMEOUT;
066      default -> null;
067    };
068  }
069}