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.

    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.
        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.
        if at least one new solution has been found.
      • searchLoop

        public boolean searchLoop()
        Executes the search loop
        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()
        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.
        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()
        the model of this resolver
      • getLearner

        public Learn getLearner()
        the current learn.
      • getMove

        public Move getMove()
        the current move.
      • getPropagate

        public Propagate getPropagate()
        the current propagate.
      • getEnvironment

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

        public DecisionPath getDecisionPath()
        the current decision path
      • getSearch

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

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

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

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

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

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

        public int getSearchWorldIndex()
        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.
        this model's measure recorder
      • getEventObserver

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

        public PropagationEngine getEngine()
        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.

        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.

        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()
        how many worlds should be rolled back when backtracking (usually 1)
      • isLearnOff

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

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

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

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

        public void setObjectiveManager​(IObjectiveManager om)
        Declares an objective manager to use.
        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.

        strategies - the search strategies to use.
      • setEventObserver

        public void setEventObserver​(AbstractEventObserver explainer)
        Overrides the explanation engine.
        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.
        propagationEngine - a propagation strategy
        SolverException - is already initialized.
      • makeCompleteStrategy

        public void makeCompleteStrategy​(boolean isComplete)
        Completes (or not) the declared search strategy with one over all variables
        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.
        With a built-in counter, stop after 20 seconds:
                 SMF.limitTime(solver, "20s");
        With lambda, stop when 10 nodes are visited:
             () -> solver.getNodeCount() >= 10
        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.
        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()
        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.
        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.
        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
        jto - how many worlds to rollback when backtracking
      • ref

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        public Number getBestSolutionValue()
        Specified by:
        getBestSolutionValue in interface IMeasures
        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
        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
        printStream - a print stream