Module org.maxicp

Class AbstractSearchMethod<T>

java.lang.Object
org.maxicp.search.AbstractSearchMethod<T>
Type Parameters:
T - the type of the branching
All Implemented Interfaces:
SearchMethod
Direct Known Subclasses:
AbstractConcurrentSearchMethod, RunnableSearchMethod, SymbolicSearchMethod

public abstract class AbstractSearchMethod<T> extends Object implements SearchMethod
An abstract search method, implementing all the parts needed, but the search method itself.
  • Field Details

  • Constructor Details

  • Method Details

    • onSolution

      public void onSolution(Consumer<SearchStatistics> listener)
      Adds a listener that is called on each solution.
      Specified by:
      onSolution in interface SearchMethod
      Parameters:
      listener - the closure to be called whenever a solution is found
    • onFailure

      public void onFailure(Consumer<SearchStatistics> listener)
      Adds a listener that is called whenever a failure occurs and the search backtracks. This happens when a InconsistencyException is thrown when executing the closure generated by the branching.
      Specified by:
      onFailure in interface SearchMethod
      Parameters:
      listener - the closure to be called whenever a failure occurs and the search need to backtrack
    • notifySolution

      protected void notifySolution(SearchStatistics stats)
    • notifyFailure

      protected void notifyFailure(SearchStatistics stats)
    • solve

      protected SearchStatistics solve(SearchStatistics statistics, Predicate<SearchStatistics> limit, Runnable onNodeVisit)
    • solve

      public SearchStatistics solve()
      Start the solving process
      Specified by:
      solve in interface SearchMethod
      Returns:
      an object with the statistics on the search
    • solve

      public SearchStatistics solve(DFSListener listener)
      Start the solving process with a given listener of the search process.
      Parameters:
      listener - a listener of the search process
      Returns:
      an object with the statistics on the search
    • solve

      Start the solving process with a given predicate called at each node to stop the search when it becomes true.
      Specified by:
      solve in interface SearchMethod
      Parameters:
      limit - a predicate called at each node that stops the search when it becomes true
      Returns:
      an object with the statistics on the search
    • solveSubjectTo

      public SearchStatistics solveSubjectTo(Predicate<SearchStatistics> limit, Runnable subjectTo)
      Start the solving process with a given predicate called at each node to stop the search when it becomes true. The state manager saves the state before executing the closure and restores it after the search. Any InconsistencyException that may be throw when executing the closure is also catched.
      Specified by:
      solveSubjectTo in interface SearchMethod
      Parameters:
      limit - a predicate called at each node that stops the search when it becomes true
      subjectTo - the closure to execute prior to the search starts
      Returns:
      an object with the statistics on the search
    • allFixed

      public boolean allFixed(CPVar[] vars)
    • replaySubjectTo

      public SearchStatistics replaySubjectTo(DFSLinearizer linearizer, CPVar[] variables, Runnable subjectTo, Objective obj)
    • replay

      public SearchStatistics replay(DFSLinearizer linearizer, CPVar[] variables)
    • replaySubjectTo

      public SearchStatistics replaySubjectTo(DFSLinearizer linearizer, CPVar[] variables, Runnable subjectTo)
    • replaySubjectTo

      public SearchStatistics replaySubjectTo(DFSLinearizer linearizer, CPVar[] variables, Runnable subjectTo, Runnable onNodeVisit)
    • optimize

      public SearchStatistics optimize(Objective obj)
      Start the solving process with a given objective.
      Specified by:
      optimize in interface SearchMethod
      Parameters:
      obj - the objective to optimize that is tightened each time a new solution is found
      Returns:
      an object with the statistics on the search
    • optimize

      public SearchStatistics optimize(Objective obj, Predicate<SearchStatistics> limit)
      Specified by:
      optimize in interface SearchMethod
    • optimize

      public SearchStatistics optimize(Objective obj)
      Specified by:
      optimize in interface SearchMethod
    • optimize

      public SearchStatistics optimize(Objective toTighten, Predicate<SearchStatistics> limit)
      Start the solving process with a given objective and with a given predicate called at each node to stop the search when it becomes true.
      Specified by:
      optimize in interface SearchMethod
      Parameters:
      toTighten - the objective to optimize that is tightened each time a new solution is found
      limit - a predicate called at each node that stops the search when it becomes true
      Returns:
      an object with the statistics on the search
    • optimizeSubjectTo

      public SearchStatistics optimizeSubjectTo(Objective objToTighten, Predicate<SearchStatistics> limit, Runnable subjectTo)
      Executes a closure prior to effectively starting a branch and bound depth-first-search with a given objective to optimize and a given predicate called at each node to stop the search when it becomes true. The state manager saves the state before executing the closure and restores it after the search. Any InconsistencyException that may be thrown when executing the closure is also catched.
      Specified by:
      optimizeSubjectTo in interface SearchMethod
      Parameters:
      objToTighten - the objective to optimize that is tightened each time a new solution is found
      limit - a predicate called at each node that stops the search when it becomes true
      subjectTo - the closure to execute prior to the search starts
      Returns:
      an object with the statistics on the search
    • optimizeSubjectTo

      public SearchStatistics optimizeSubjectTo(Objective objToTighten, Predicate<SearchStatistics> limit, Runnable subjectTo)
      Specified by:
      optimizeSubjectTo in interface SearchMethod
    • startSolve

      protected abstract void startSolve(SearchStatistics statistics, Predicate<SearchStatistics> limit, Runnable onNodeVisit)
      Start the solving process.

      This method must be implemented by subclasses and do the heavy work. It must call notifySolution/notifyFailure when a solution is found or a failure occurs.

      Parameters:
      statistics -
      limit -
      onNodeVisit - a closure to execute at each node visit