Class MILP


  • public final class MILP
    extends Object
    This class allows executing and solving formulations of mixed integer linear programming problems, binary or semicontinuous. The method used for solving such optimization problems is the simplex method combined with the Branch and Bound method.
    Version:
    4.2
    Author:
    Stefano Scarioli
    See Also:
    SSC Software www.sscLab.org
    • Field Detail

      • NaN

        public static double NaN
      • father_pl_original_zero

        protected org.ssclab.pl.milp.PLProblem father_pl_original_zero
    • Constructor Detail

      • MILP

        public MILP​(String pl_text)
             throws Exception
        Constructor of a MILP object for solving problems formulated in inequality format contained in an external file.
        Parameters:
        pl_text - String containing the problem in text format
        Throws:
        Exception - if the problem is not correctly formulated
      • MILP

        public MILP​(ArrayList<String> inequality)
             throws Exception
        Constructor of a MILP object for solving problems formulated in inequality format contained in an ArrayList of Strings.
        Parameters:
        inequality - The ArrayList with the problem
        Throws:
        Exception - if the ArrayList is null or empty
      • MILP

        public MILP​(Path path)
             throws Exception
        Constructor of a MILP object for solving problems formulated in inequality format contained in an external file.
        Parameters:
        path - Path of the file containing the problem in inequality format
        Throws:
        Exception - if the problem is not correctly formulated
      • MILP

        public MILP​(LinearObjectiveFunction fo,
                    ArrayList<Constraint> constraints)
             throws Exception
        Constructor of a MILP object for solving problems expressed in matrix format.
        Parameters:
        fo - A LinearObjectiveFunction object representing the objective function
        constraints - The list of constraints
        Throws:
        Exception - An exception is thrown if the problem is not correctly formulated
      • MILP

        public MILP​(LinearObjectiveFunction fo,
                    ListConstraints constraints)
             throws Exception
        Constructor of a MILP object for solving problems expressed in matrix format.
        Parameters:
        fo - A LinearObjectiveFunction object representing the objective function
        constraints - The list of constraints
        Throws:
        Exception - An exception is thrown if the problem is not correctly formulated
      • MILP

        public MILP​(org.ssclab.ref.Input input)
             throws Exception
        Constructor of a MILP object for solving problems expressed in coefficient format.
        Parameters:
        input - The problem formulated in coefficient format
        Throws:
        Exception - An exception is thrown if the problem is not formulated correctly
      • MILP

        public MILP​(org.ssclab.ref.Input input,
                    org.ssclab.context.Session session)
             throws Exception
        Constructor of a MILP object for solving problems expressed in coefficient format.
        Parameters:
        input - The problem formulated in coefficient format
        session - An SSC working session
        Throws:
        Exception - An exception is thrown if the problem is not formulated correctly
      • MILP

        public MILP​(org.ssclab.ref.Input input,
                    org.ssclab.context.Session session,
                    FormatTypeInput.FormatType format)
             throws Exception
        Constructor of a MILP object for solving problems expressed in either sparse or coefficient format.
        Parameters:
        input - The problem formulated in sparse or coefficient format
        session - An SSC working session
        format - The type of format used (FormatType.SPARSE or FormatType.COEFF)
        Throws:
        Exception - An exception is thrown if the problem is not formulated correctly
      • MILP

        public MILP​(org.ssclab.ref.Input input,
                    FormatTypeInput.FormatType format)
             throws Exception
        Constructor of a MILP object for solving problems expressed in sparse or coefficient format.
        Parameters:
        input - The problem formulated in sparse format
        format - The type of format used (FormatType.SPARSE or FormatType.COEFF)
        Throws:
        Exception - An exception is thrown if the problem is not formulated correctly
      • MILP

        public MILP​(JsonProblem pl_json)
             throws Exception
        Constructor of a MILP object for solving problems formulated in json format.
        Parameters:
        pl_json - JsonProblem object containing the problem in json format
        Throws:
        Exception - if the problem is not correctly formulated
    • Method Detail

      • getNumMaxIterationForSingleSimplex

        public int getNumMaxIterationForSingleSimplex()
        Returns:
        the maximum number of iterations that each simplex can execute
      • setNumMaxIterationForSingleSimplex

        public MILP setNumMaxIterationForSingleSimplex​(int num_max_iteration)
                                                throws org.ssclab.pl.milp.simplex.SimplexException
        Method to set the number of iterations for each individual simplex.
        Parameters:
        num_max_iteration - The maximum number of iterations that each simplex can execute
        Returns:
        the MILP instance (this) on which the method call is being made
        Throws:
        org.ssclab.pl.milp.simplex.SimplexException - If an error occurs during the process
      • getNumMaxSimplexs

        public int getNumMaxSimplexs()
        Returns:
        the maximum number of simplexes executable in the Branch and Bound procedure
      • setNumMaxSimplexs

        public void setNumMaxSimplexs​(int num_max_simplex)
        Method to set the maximum number of simplexes.
        Parameters:
        num_max_simplex - the maximum number of simplexes executable in the Branch and Bound procedure
      • setEpsilon

        public MILP setEpsilon​(EPSILON epsilon)
        This method allows setting the epsilon value relative to the tolerance that intervenes in various aspects of the simplex. It is used in the following cases:
        1) During phase one, both in determining the entering variable and in determining the exiting variable with or without the Bland rule. Also to determine if the base is degenerate. It is also used at the end of phase one: if there is an auxiliary variable in the base, epsilon is used to determine if it is possible to eliminate the rows and columns of these on the extended table.
        2) During phase two, both in determining the entering variable and in determining the exiting variable with or without the Bland rule. Also to determine if the base is degenerate.
        Parameters:
        epsilon - Tolerance used in various phases of the simplex. Default value 1E-10
        Returns:
        the MILP instance (this) on which the method call is being made
      • setCEpsilon

        public MILP setCEpsilon​(EPSILON cepsilon)
        This method allows setting the epsilon value relative to the tolerance for determining if an optimal solution of phase 1 of the simplex is close to or equal to zero and thus gives rise to feasible solutions for the problem.
        Parameters:
        cepsilon - Tolerance of phase 1 solution with respect to zero. Default value 1E-8
        Returns:
        the MILP instance (this) on which the method call is being made
      • setIEpsilon

        public MILP setIEpsilon​(EPSILON iepsilon)
        This method allows setting the epsilon value relative to the tolerance for determining if a number should be considered integer or not. This check occurs when at the end of the simplex the solution found is evaluated to satisfy the integer condition on the variables that must be integers. Let x be a number and Int(x) the nearest integer to x, if | Int(x) - x | < epsilon -> x ∈ Z
        Parameters:
        iepsilon - Tolerance to consider a number as integer. Default value 1E-10
        Returns:
        the MILP instance (this) on which the method call is being made
      • resolve

        public SolutionType resolve()
                             throws Exception
        Executes the Branch and Bound algorithm.
        Returns:
        The type of solution found
        Throws:
        Exception - If the execution process generates an error
      • getRelaxedSolution

        public Solution getRelaxedSolution()
        This method returns the solution of the problem by removing the integer constraints (relaxed solution). If there are binary variables, only the constraint from assuming integer values is removed, but the binary variable still has the constraint of being between zero and one.
        Returns:
        returns the relaxed solution, i.e., the solution of the problem without integer constraints.
      • getSolution

        public Solution getSolution()
                             throws org.ssclab.pl.milp.simplex.SimplexException
        This method returns, if it exists, the optimal integer, mixed-integer, or binary solution.
        Returns:
        the optimal integer, mixed-integer, or binary solution
        Throws:
        org.ssclab.pl.milp.simplex.SimplexException - if the problem has not solution
      • getValuesSolution

        public double[] getValuesSolution()
                                   throws org.ssclab.pl.milp.simplex.SimplexException
        If the problem has an optimal solution, this method returns that optimal solution in the form of an array with the values of the variables.
        Returns:
        The optimal solution of the problem as an array of values
        Throws:
        org.ssclab.pl.milp.simplex.SimplexException - If the optimal solution is not present
      • getThreadNumber

        public MILPThreadsNumber getThreadNumber()
        Returns:
        the number of threads set for solving the problem
      • setThreadsNumber

        public MILP setThreadsNumber​(MILPThreadsNumber lpThreadsNumber)
        This method allows setting the number of threads to use for executing the Branch and Bound.
        Parameters:
        lpThreadsNumber - Enumeration for setting the number of Threads
        Returns:
        the MILP instance (this) on which the method call is being made
      • isJustTakeFeasibleSolution

        public boolean isJustTakeFeasibleSolution()
        Returns:
        true if the mode of returning a feasible solution instead of an optimal one is set
      • setJustTakeFeasibleSolution

        public MILP setJustTakeFeasibleSolution​(boolean isJustTakeFeasibleSolution)
        Setting it to true allows interrupting the Branch and Bound in order to determine not an optimal solution but only a feasible solution to the problem.
        Parameters:
        isJustTakeFeasibleSolution - true to interrupt the Branch and Bound and obtain only a feasible solution.
        Returns:
        the MILP instance (this) on which the method call is being made
      • getEpsilon

        public EPSILON getEpsilon()
      • getIepsilon

        public EPSILON getIepsilon()
      • getCepsilon

        public EPSILON getCepsilon()
      • setTitle

        public MILP setTitle​(String title)
        Allows you to give a title to the current elaboration related to the LP problem to be solved
        Parameters:
        title - The title of the linear programming problem
        Returns:
        This instance of MILP
      • getSolutionAsJson

        public JsonSolution getSolutionAsJson​(SolutionDetail... option)
                                       throws org.ssclab.pl.milp.simplex.SimplexException
        Returns a JsonSolution object that represents the solution of the Linear Programming (LP) or Mixed-Integer Linear Programming (MILP) problem in JSON format. This method constructs the solution JSON, including optional sections based on the SolutionDetail values provided as arguments. The solution can represent either the optimal or feasible solution, depending on the problem's outcome. The `SolutionDetail` enum values can specify additional information to include in the JSON output:
        • INCLUDE_BOUNDS: Includes the variable bounds (upper and lower limits) in the JSON.
        • INCLUDE_RELAXED: Adds the relaxed solution (when applicable) alongside the integer solution.
        • INCLUDE_CONSTRAINT: Includes the Left-Hand Side (LHS) and Right-Hand Side (RHS) values for constraints in the JSON.
        • INCLUDE_META: Inserts metadata information like runtime, threads, iterations, etc.
        • INCLUDE_TYPEVAR: Shows the original type of each variable (e.g., integer, binary, continuous) in the JSON.
        Parameters:
        option - A variable number of SolutionDetail enum values to customize the JSON output.
        Returns:
        A JsonSolution object representing the problem's solution in JSON format.
        Throws:
        org.ssclab.pl.milp.simplex.SimplexException - If there is an error in the Simplex execution or problem-solving process.
      • resolve

        public MILP resolve​(Object nullable)
                     throws Exception
        Solves the linear programming (MILP) problem and returns the current instance of the MILP object. This method is designed to allow method chaining by returning 'this' rather than a result type.

        By passing 'null' to this method, the standard resolve method is bypassed, and this version of the method is invoked. The 'nullable' parameter exists to differentiate between the standard resolve method and this version, which enables method chaining.

        For example, this method can be used as follows:

        
         new MILP(pl_string).resolve(null).getSolutionAsJson().saveToFile("solution.json");
         

        In the above code, the 'resolve()' method solves the MILP problem, and 'saveSolutionToJson()' is chained to save the resulting solution to a JSON file.

        Parameters:
        nullable - an object that is intentionally ignored; passing 'null' to this parameter ensures that this version of the resolve method is invoked.
        Returns:
        the current instance of the MILP object for method chaining.
        Throws:
        Exception - if an error occurs during the resolution process.