Class Solver

  • All Implemented Interfaces:
    Serializable, ISelf<Solver>, ISolver, IResolutionHelper, ILearnFactory, ISearchMonitorFactory, IMoveFactory, IMeasures, IOutputFactory

    public class Solver
    extends Object
    implements ISolver, IMeasures, IOutputFactory
    This class is inspired from : Inspired from "Unifying search algorithms for CSP" N. Jussien and O. Lhomme, Technical report 02-3-INFO, EMN

    It declares a search loop made of three components:

    • Propagate: it aims at propagating information throughout the constraint network when a decision is made,
    • Learn: it aims at ensuring that the search mechanism will avoid (as much as possible) to get back to states that have been explored and proved to be solution-less,
    • Move: aims at, unlike other ones, not pruning the search space but rather exploring it.

    Created by cprudhom on 01/09/15. Project: choco.

    Since:
    01/09/15.
    Author:
    Charles Prud'homme
    See Also:
    Serialized Form
    • Field Detail

      • P

        protected Propagate P
        The propagate component of this search loop
      • L

        protected Learn L
        The learning component of this search loop
      • M

        protected Move M
        The moving component of this search loop
      • mModel

        protected Model mModel
        The declaring model
      • objectivemanager

        protected IObjectiveManager objectivemanager
        The objective manager declare
      • action

        protected Solver.Action action
        The next action to execute in the search loop
      • mMeasures

        protected MeasuresRecorder mMeasures
        The measure recorder to keep up to date
      • criteria

        protected List<Criterion> criteria
        List of stopping criteria. When at least one is satisfied, the search loop ends.
      • searchMonitors

        protected SearchMonitorList searchMonitors
        List of search monitors attached to this search loop
      • exception

        protected final ContradictionException exception
        Internal unique contradiction exception, used on propagation failures
      • feasible

        protected ESat feasible
        Problem feasbility: - UNDEFINED if unknown, - TRUE if satisfiable, - FALSE if unsatisfiable
      • stop

        protected boolean stop
        Set to true to stop the search loop
    • Constructor Detail

      • Solver

        protected Solver​(Model aModel)
        Create a resolver based for the model aModel.
        Parameters:
        aModel - the target model
    • Method Detail

      • solve

        public boolean solve()
        Executes the resolver as it is configured. Default configuration: - SATISFACTION : Computes a feasible solution. Use while(solve()) to enumerate all solutions. - OPTIMISATION : Computes a feasible solution, wrt to the objective defined. Use while(solve()) to find the optimal solution. Indeed, each new solution improves the objective. If no new solution is found (and no stop criterion encountered), the last one is guaranteed to be the optimal one.
        Returns:
        if at least one new solution has been found.
      • searchLoop

        public boolean searchLoop()
        Executes the search loop
        Returns:
        true if ends on a solution, false otherwise
      • initialize

        protected boolean initialize()
        Preparation of the search: - start time recording, - store root world - push a back up world, - run the initial propagation, - initialize the Move and the search strategy
      • propagate

        protected void propagate​(boolean left)
        Search loop propagation phase. This needs to be distinguished from propagate()
        Parameters:
        left - true if we are branching on the left false otherwise
      • extend

        protected void extend()
        Search loop extend phase
      • repair

        protected void repair()
        Search loop repair phase
      • propagate

        public void propagate()
                       throws ContradictionException
        Propagates constraints and related events through the constraint network until a fix point is find, or a contradiction is detected.
        Throws:
        ContradictionException - inconsistency is detected, the problem has no solution with the current set of domains and constraints.
      • restart

        public void restart()
        Sets the following action in the search to be a restart instruction. Note that the restart may not be immediate
      • getModel

        public Model getModel()
        Returns:
        the model of this resolver
      • getLearner

        public Learn getLearner()
        Returns:
        the current learn.
      • getMove

        public Move getMove()
        Returns:
        the current move.
      • getPropagate

        public Propagate getPropagate()
        Returns:
        the current propagate.
      • getEnvironment

        public IEnvironment getEnvironment()
        Returns:
        the backtracking environment used for this solver
      • getDecisionPath

        public DecisionPath getDecisionPath()
        Returns:
        the current decision path
      • getSearch

        public <V extends VariableAbstractStrategy<V> getSearch()
        Type Parameters:
        V - kind of variables the search strategy deals with
        Returns:
        the current search strategy in use
      • getObjectiveManager

        public <V extends VariableIObjectiveManager<V> getObjectiveManager()
        Type Parameters:
        V - type of the objective variable
        Returns:
        the currently used objective manager
      • isDefaultSearchUsed

        public boolean isDefaultSearchUsed()
        Indicates if the default search strategy is used
        Returns:
        false if a specific search strategy is used
      • isSearchCompleted

        public boolean isSearchCompleted()
        Indicates if the search strategy is completed with one over all variables
        Returns:
        false if no strategy over all variables complete the declared one
      • hasEndedUnexpectedly

        public boolean hasEndedUnexpectedly()
        Returns:
        true if the search loops ends unexpectedly (externally killed, for instance).
      • isStopCriterionMet

        public boolean isStopCriterionMet()
        Returns:
        true if the search loops encountered at least one of the stop criteria declared.
      • getSearchWorldIndex

        public int getSearchWorldIndex()
        Returns:
        the index of the world where the search starts, after initialization.
      • getMeasures

        public MeasuresRecorder getMeasures()
        Returns a reference to the measures recorder. This enables to get, for instance, the number of solutions found, time count, etc.
        Returns:
        this model's measure recorder
      • getEventObserver

        public AbstractEventObserver getEventObserver()
        Return the events observer plugged into this.
        Returns:
        this events observer
      • getEngine

        public PropagationEngine getEngine()
        Returns:
        the propagation engine used in this.
      • isFeasible

        public ESat isFeasible()
        Returns information on the feasibility of the current problem defined by the solver.

        Possible back values are:
        - ESat.TRUE: a solution has been found,
        - ESat.FALSE: the CSP has been proven to have no solution,
        - ESat.UNDEFINED: no solution has been found so far (within given limits) without proving the unfeasibility, though.

        Returns:
        an ESat.
      • isSatisfied

        public ESat isSatisfied()
        Return the current state of the CSP.

        Given the current domains, it can return a value among:
        - ESat.TRUE: all constraints of the CSP are satisfied for sure,
        - ESat.FALSE: at least one constraint of the CSP is not satisfied.
        - ESat.UNDEFINED: neither satisfiability nor unsatisfiability could be proven so far.

        Presumably, not all variables are instantiated.

        Returns:
        ESat.TRUE if all constraints of the problem are satisfied, ESat.FLASE if at least one constraint is not satisfied, ESat.UNDEFINED neither satisfiability nor unsatisfiability could be proven so far.
      • getJumpTo

        public int getJumpTo()
        Returns:
        how many worlds should be rolled back when backtracking (usually 1)
      • isLearnOff

        public boolean isLearnOff()
        Returns:
        true when learning algorithm is not plugged in
      • setLearner

        public void setLearner​(Learn l)
        Replaces the current learn with l
        Parameters:
        l - the new learn to apply
      • setMove

        public void setMove​(Move... m)
        Replaces the current move with m
        Parameters:
        m - the new move to apply
      • setPropagate

        public void setPropagate​(Propagate p)
        Overrides the Propagate object
        Parameters:
        p - the new Propagate to use
      • setObjectiveManager

        public void setObjectiveManager​(IObjectiveManager om)
        Declares an objective manager to use.
        Parameters:
        om - the objective manager to use instead of the declared one (if any).
      • setSearch

        public void setSearch​(AbstractStrategy... strategies)
        Override the default search strategies to use in this. In case many strategies are given, they will be called in sequence: The first strategy in parameter is first called to compute a decision, if possible. If it cannot provide a new decision, the second strategy is called ... and so on, until the last strategy.

        Parameters:
        strategies - the search strategies to use.
      • setEventObserver

        public void setEventObserver​(AbstractEventObserver explainer)
        Overrides the explanation engine.
        Parameters:
        explainer - the explanation to use
      • setEngine

        public void setEngine​(PropagationEngine propagationEngine)
        Attaches a propagation engine this. It overrides the previously defined one, only if no propagation was done yet. Indeed, some incremental propagators may have set up their internal structure, which cannot be set up twice safely. If propagation was done calling solve(), calling reset() enables to set the propagation engine anew. If propagation was done "manually" (calling propagate(), then nothing can be done.
        Parameters:
        propagationEngine - a propagation strategy
        Throws:
        SolverException - is already initialized.
      • makeCompleteStrategy

        public void makeCompleteStrategy​(boolean isComplete)
        Completes (or not) the declared search strategy with one over all variables
        Parameters:
        isComplete - set to true to complete the current search strategy
      • addStopCriterion

        public void addStopCriterion​(Criterion... criterion)
        Adds a stop criterion, which, when met, stops the search loop. There can be multiple stop criteria, a logical OR is then applied. The stop criteria are declared to the search loop just before launching the search, the previously defined ones are erased. There is no check if there are any duplicates.
        Examples:
        With a built-in counter, stop after 20 seconds:
                 SMF.limitTime(solver, "20s");
         
        With lambda, stop when 10 nodes are visited:
             () -> solver.getNodeCount() >= 10
         
        Parameters:
        criterion - one or many stop criterion to add.
        See Also:
        removeStopCriterion(Criterion...), removeAllStopCriteria()
      • removeStopCriterion

        public void removeStopCriterion​(Criterion... criterion)
        Removes one or many stop criterion from the one to declare to the search loop.
        Parameters:
        criterion - criterion to remove
      • removeAllStopCriteria

        public void removeAllStopCriteria()
        Empties the list of stop criteria declared. This is not automatically called on reset().
      • getSearchMonitors

        public SearchMonitorList getSearchMonitors()
        Returns:
        the list of search monitors plugged in this resolver
      • plugMonitor

        public void plugMonitor​(ISearchMonitor sm)
        Put a search monitor to react on search events (solutions, decisions, fails, ...). Any search monitor is actually plugged just before the search starts. There is no check if there are any duplicates. A search monitor added during while the resolution has started will not be taken into account.
        Parameters:
        sm - a search monitor to be plugged in the solver
      • unplugMonitor

        public void unplugMonitor​(ISearchMonitor sm)
        Removes a search monitors from the ones to plug when the search will start.
        Parameters:
        sm - a search monitor to be unplugged in the solver
      • unplugAllSearchMonitors

        public void unplugAllSearchMonitors()
        Empties the list of search monitors.
      • setJumpTo

        public void setJumpTo​(int jto)
        Sets how many worlds to rollback when backtracking
        Parameters:
        jto - how many worlds to rollback when backtracking
      • ref

        public Solver ref()
        Specified by:
        ref in interface ISelf<Solver>
        Returns:
        the encapsulated type
      • getModelName

        public String getModelName()
        Specified by:
        getModelName in interface IMeasures
        Returns:
        name of the model/solver observed
      • getTimestamp

        public long getTimestamp()
        Specified by:
        getTimestamp in interface IMeasures
        Returns:
        the current world unique id
      • getTimeCount

        public float getTimeCount()
        Specified by:
        getTimeCount in interface IMeasures
        Returns:
        the time count (in seconds), including initial propagation time count
      • getTimeCountInNanoSeconds

        public long getTimeCountInNanoSeconds()
        Specified by:
        getTimeCountInNanoSeconds in interface IMeasures
        Returns:
        the time count (in nano seconds), including initial propagation time count
      • getReadingTimeCountInNanoSeconds

        public long getReadingTimeCountInNanoSeconds()
        Specified by:
        getReadingTimeCountInNanoSeconds in interface IMeasures
        Returns:
        the reading time count (in nano seconds).
      • getReadingTimeCount

        public float getReadingTimeCount()
        Specified by:
        getReadingTimeCount in interface IMeasures
        Returns:
        the reading time count (in sec)
      • getNodeCount

        public long getNodeCount()
        Specified by:
        getNodeCount in interface IMeasures
        Returns:
        the node count
      • getBackTrackCount

        public long getBackTrackCount()
        Specified by:
        getBackTrackCount in interface IMeasures
        Returns:
        the backtrack count
      • getBackjumpCount

        public long getBackjumpCount()
        Specified by:
        getBackjumpCount in interface IMeasures
        Returns:
        the non chronological backtracks count
      • getFailCount

        public long getFailCount()
        Specified by:
        getFailCount in interface IMeasures
        Returns:
        the fail count
      • getFixpointCount

        public long getFixpointCount()
        Specified by:
        getFixpointCount in interface IMeasures
        Returns:
        the fixpoint count
      • getRestartCount

        public long getRestartCount()
        Specified by:
        getRestartCount in interface IMeasures
        Returns:
        the restart count
      • getSolutionCount

        public long getSolutionCount()
        Specified by:
        getSolutionCount in interface IMeasures
        Returns:
        the solution count of the measure
      • getDecisionCount

        public long getDecisionCount()
        Specified by:
        getDecisionCount in interface IMeasures
        Returns:
        the decision count
      • getMaxDepth

        public long getMaxDepth()
        Specified by:
        getMaxDepth in interface IMeasures
        Returns:
        the maximum depth of the search tree
      • getCurrentDepth

        public long getCurrentDepth()
        Specified by:
        getCurrentDepth in interface IMeasures
        Returns:
        the current depth in the search tree
      • hasObjective

        public boolean hasObjective()
        Specified by:
        hasObjective in interface IMeasures
        Returns:
        true iff the problem has an objective variable (i.e. optimization problem)
      • isObjectiveOptimal

        public boolean isObjectiveOptimal()
        Specified by:
        isObjectiveOptimal in interface IMeasures
        Returns:
        true iff the optimum has been found and proved
      • getBestSolutionValue

        public Number getBestSolutionValue()
        Specified by:
        getBestSolutionValue in interface IMeasures
        Returns:
        the objective value of the best solution found (can be Integer or Double)
      • setOut

        public void setOut​(PrintStream printStream)
        Description copied from interface: IOutputFactory
        Set the current output stream (default is System.out)
        Specified by:
        setOut in interface IOutputFactory
        Parameters:
        printStream - a print stream
      • setErr

        public void setErr​(PrintStream printStream)
        Description copied from interface: IOutputFactory
        Set the current error stream (default is System.err)
        Specified by:
        setErr in interface IOutputFactory
        Parameters:
        printStream - a print stream