Class KSolver

java.lang.Object
com.artelys.kalis.KSolver

public class KSolver extends Object
KSolver is the main class for solving problems defined in a KProblem instance.

Once the problem has been fully built, we can begin to look for solutions.
For this, the main class to be used is KSolver, which allows us to :

  • look for one solution
  • look for all solutions
  • look for another solution when we already know some of them
  • look for the optimal solution according to the problem objective

A KSolver object must be associated to a specific problem. Here is how we can
declare and create a KSolver which will be associated to our problem :

KSolver mySolver(myProblem);


When performing its solving functionalities, our object mySolver will store
all solutions in the myProblem object. Retrieving these solutions and working
on them is the subject of the next section.

In order to find only one solution to our problem, we would write:

mySolver.solve();


The solve() method looks for any valid solution and stores it in the
associated KProblem object.

In order to fine all solutions to the problem, we would write :

mySolver.findAllSolutions();


The findAllSolutions() method searches for all solutions of the problem and
stores them in the associated KProblem object.

When the problem is too large, it can be very time consuming to search for
all solutions. If one needs to obtain more than one unique solution, then he
should use the KSolver findNextSolution() method. For example :

for (int solutionIndex = 0; solutionIndex < 5; ++solutionIndex)
    mySolver.findNextSolution();
mySolver.endLookingForSolution();


The findNextSolution() method searches for the next solution and stop in a
restartable state.
To go back to the state before search, it is necessary to call the
endLookingForSolution() method after the successive calls to findNextSolution().

In order to find the optimal solution to the problem, we would write:

mySolver.optimize();


The optimize() method searches for the optimal solutions according to the
problem objective and stores it in the associated KProblem object.

In order to fine tune the search, one may set integer or double control
parameters using the setIntControl() and setDblControl() methods.

Statistics on the search can be obtained using the getIntAttrib() and
getDblAttrib() methods.

Multi-threaded search is automatically activated provided that the KProblem
object holds multiple problem instances and that the KSolver::NumberOfThreads
control parameter is greater than 1.

See Also:
KProblem

  • Field Details

  • Constructor Details

    • KSolver

      protected KSolver(long cPtr, boolean cMemoryOwn)
    • KSolver

      public KSolver()
      Default constructor
    • KSolver

      public KSolver(KProblem problem)
      Constructor

      Parameters:
      problem - the problem to solve
    • KSolver

      public KSolver(KProblem problem, KBranchingSchemeArray branchingSchemeArray)
      Constructor

      Parameters:
      problem - the problem to solve
      branchingSchemeArray - the resolution strategy used during branch and bound to solve the problem
    • KSolver

      public KSolver(KSolver toCopy)
      CopyConstructor
  • Method Details

    • getCPtr

      protected static long getCPtr(KSolver obj)
    • finalize

      protected void finalize()
      Overrides:
      finalize in class Object
    • delete

      public void delete()
    • getProblem

      public KProblem getProblem()
      Get the KProblem instance
    • solve

      public int solve()
      Search for a solution to the problem

      Returns:
      0 if no solution was found, 1 otherwise

      See Also:
      DblControl
    • findAllSolutions

      public int findAllSolutions()
      Search for all solutions to the problem

      Returns:
      number of solutions found

      See Also:
      DblControls
    • findNextSolution

      public int findNextSolution()
      Start looking for a solution to the problem or look for a new one

      Returns:
      0 if no solution was found

      See Also:
      DblControls
    • endLookingForSolution

      public int endLookingForSolution()
      Stop looking for solutions and restore the state before search
    • optimize

      public int optimize(boolean optimizeWithRestart, boolean dichotomicSearch)
      Search for an optimal solution to the problem.

      Parameters:
      optimizeWithRestart - boolean indicating if the search has to be restarted after finding a solution (@see NumberOfSolutionBetweenRestarts)
      dichotomicSearch - boolean indicating the type of search (linear or dichotomic) to optimize the objective variable (@see OptimizationAlgorithm)
      Returns:
      0 if no solution was found

      See Also:
      DblControls
    • optimize

      public int optimize(boolean optimizeWithRestart)
      Search for an optimal solution to the problem.

      Parameters:
      optimizeWithRestart - boolean indicating if the search has to be restarted after finding a solution (@see NumberOfSolutionBetweenRestarts)
      Returns:
      0 if no solution was found

      See Also:
      DblControls
    • optimize

      public int optimize()
      Search for an optimal solution to the problem.

      Returns:
      0 if no solution was found

      See Also:
      DblControls
    • getIntAttrib

      public int getIntAttrib(int attrib)
      Return a integer attribute of the solver.

      Parameters:
      attrib - the integer attribute to retrieve

      See Also:
      KSolver.IntAttrib
    • getDblAttrib

      public double getDblAttrib(int attrib)
      Return a double attribute of the solver.

      Parameters:
      attrib - the double attribute to retrieve

      See Also:
      KSolver.DblAttrib
    • getIntControl

      public int getIntControl(int control)
      Return the value of an int control

      Parameters:
      control - integer control to retrieve

      See Also:
      KSolver.IntControl
    • getDblControl

      public double getDblControl(int control)
      Return the value of a double control

      Parameters:
      control - double control to retrieve

      See Also:
      KSolver.DblControl
    • setIntControl

      public void setIntControl(int control, int value)
      Set the value of an int control

      Parameters:
      control - the int control to set
      value - the value of the control

      See Also:
      KSolver.IntControl
    • setDblControl

      public void setDblControl(int control, double value)
      Set the value of a double control

      Parameters:
      control - tjhe double control to set
      value - value of the control

      See Also:
      KSolver.DblControl
    • setNodeFunctionPtr

      public void setNodeFunctionPtr(com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr, com.artelys.kalis.SWIGTYPE_p_void param)
      Deprecated.

      Parameters:
      ptr - function pointer
      param - user parameter passed to the function when called
      See Also:
      Set the node explored function ptr
    • setBranchFunctionPtr

      public void setBranchFunctionPtr(com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr1, com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr2, com.artelys.kalis.SWIGTYPE_p_void param)
      Deprecated.

      Parameters:
      param - user parameter passed to the function when called
      See Also:
      Set the branch explored function ptr (called each time a branch is explored)
    • setBranchingSchemeFunctionPtr

      public void setBranchingSchemeFunctionPtr(com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr1, com.artelys.kalis.SWIGTYPE_p_void param)
      Deprecated.

      Parameters:
      param - user parameter passed to the function when called
      See Also:
      Set the Branching scheme switch function ptr
    • setAsyncStopFunctionPtr

      public void setAsyncStopFunctionPtr(com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr, com.artelys.kalis.SWIGTYPE_p_void param)
    • setSolutionFunctionPtr

      public void setSolutionFunctionPtr(com.artelys.kalis.SWIGTYPE_p_f_p_void__int ptr, com.artelys.kalis.SWIGTYPE_p_void param)
      Deprecated.

      Parameters:
      ptr - function pointer
      param - user parameter passed to the function when called
      See Also:
      Set the solution function ptr (called each time a solution is found)
    • setSolverEventListener

      public void setSolverEventListener(KSolverEventListener listener)
      Set the solver event listener for tracking and controlling the search

      See Also:
      KSolverEventListener
    • useShaving

      public void useShaving(boolean use)
      Set shaving activation flag
    • getUseShaving

      public boolean getUseShaving()
      Return the shaving activation flag
    • getCurrentBranchingScheme

      public KBranchingScheme getCurrentBranchingScheme()
      Return the current branching scheme
    • getCurrentVariableSelector

      public KVariableSelector getCurrentVariableSelector()
      Return the current variable selector
    • getCurrentValueSelector

      public KValueSelector getCurrentValueSelector()
      Return the current value selector
    • getCurrentBranchingObject

      public com.artelys.kalis.SWIGTYPE_p_void getCurrentBranchingObject()
      Return a pointer to the current branching object
    • printStats

      public void printStats(com.artelys.kalis.SWIGTYPE_p_std__ostream fout)
      Pretty printing of resolution statistics
    • printStats

      public void printStats()
    • printStats

      public void printStats(com.artelys.kalis.SWIGTYPE_p_void ctx, com.artelys.kalis.SWIGTYPE_p_f_p_void_p_q_const__char__int pfp)
    • printStatsFile

      public void printStatsFile(String filename)
    • setBranchingSchemeArray

      public void setBranchingSchemeArray(KBranchingSchemeArray branchingSchemeArray, int solverInstance)
      Sets the branching scheme array
    • setBranchingSchemeArray

      public void setBranchingSchemeArray(KBranchingSchemeArray branchingSchemeArray)
      Sets the branching scheme array
    • getDefaultBranchingSchemeArray

      public KBranchingSchemeArray getDefaultBranchingSchemeArray()
      Return the default branching scheme array
    • addRelaxationSolver

      public void addRelaxationSolver(KLinearRelaxationSolver solver, boolean initDefaultBranchingScheme)
      Add a relaxation solver
    • addRelaxationSolver

      public void addRelaxationSolver(KLinearRelaxationSolver solver)
      Add a relaxation solver
    • setUseReducedCostFixing

      public void setUseReducedCostFixing(boolean flag)
      Use reducing cost fixing
    • localOptimization

      public boolean localOptimization()
      Do a local optimization