Class BestFirst<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​N,​A,​V extends java.lang.Comparable<V>>

  • Type Parameters:
    I -
    N -
    A -
    V -
    All Implemented Interfaces:
    java.lang.Iterable<org.api4.java.algorithm.events.IAlgorithmEvent>, java.util.concurrent.Callable<EvaluatedSearchGraphPath<N,​A,​V>>, java.util.Iterator<org.api4.java.algorithm.events.IAlgorithmEvent>, org.api4.java.ai.graphsearch.problem.IOptimalPathInORGraphSearch<I,​EvaluatedSearchGraphPath<N,​A,​V>,​N,​A,​V>, org.api4.java.ai.graphsearch.problem.IPathInORGraphSearch<I,​EvaluatedSearchGraphPath<N,​A,​V>,​N,​A>, org.api4.java.ai.graphsearch.problem.IPathSearch<I,​EvaluatedSearchGraphPath<N,​A,​V>,​N,​A>, org.api4.java.algorithm.IAlgorithm<I,​EvaluatedSearchGraphPath<N,​A,​V>>, org.api4.java.algorithm.IOptimizationAlgorithm<I,​EvaluatedSearchGraphPath<N,​A,​V>,​V>, org.api4.java.algorithm.ISolutionCandidateIterator<I,​EvaluatedSearchGraphPath<N,​A,​V>>, org.api4.java.common.control.ICancelable, org.api4.java.common.control.ILoggingCustomizable
    Direct Known Subclasses:
    AStar, StandardBestFirst

    public class BestFirst<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​N,​A,​V extends java.lang.Comparable<V>>
    extends AOptimalPathInORGraphSearch<I,​N,​A,​V>
    • Field Detail

      • graphGenerator

        protected final org.api4.java.datastructure.graph.implicit.IGraphGenerator<N,​A> graphGenerator
      • rootGenerator

        protected final org.api4.java.datastructure.graph.implicit.IRootGenerator<N> rootGenerator
      • successorGenerator

        protected final org.api4.java.datastructure.graph.implicit.ISuccessorGenerator<N,​A> successorGenerator
      • pathGoalTester

        protected final org.api4.java.ai.graphsearch.problem.implicit.graphgenerator.IPathGoalTester<N,​A> pathGoalTester
      • nodeEvaluator

        protected final org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,​A,​V extends java.lang.Comparable<V>> nodeEvaluator
      • ext2int

        protected final java.util.Map<N,​BackPointerPath<N,​A,​V extends java.lang.Comparable<V>>> ext2int
      • open

        protected java.util.Queue<BackPointerPath<N,​A,​V extends java.lang.Comparable<V>>> open
      • additionalThreadsForNodeAttachment

        protected int additionalThreadsForNodeAttachment
      • activeJobs

        protected final java.util.concurrent.atomic.AtomicInteger activeJobs
    • Constructor Detail

      • BestFirst

        public BestFirst​(I problem)
      • BestFirst

        public BestFirst​(I problem,
                         org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,​A,​V> lowerBoundEvaluator)
      • BestFirst

        public BestFirst​(IBestFirstConfig config,
                         I problem,
                         org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,​A,​V> lowerBoundEvaluator)
    • Method Detail

      • newNode

        protected BackPointerPath<N,​A,​V> newNode​(BackPointerPath<N,​A,​V> parent,
                                                             N t2,
                                                             A arc)
                                                      throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • newNode

        protected BackPointerPath<N,​A,​V> newNode​(BackPointerPath<N,​A,​V> parent,
                                                             N t2,
                                                             A arc,
                                                             V evaluation)
                                                      throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • labelNode

        protected void labelNode​(BackPointerPath<N,​A,​V> node)
                          throws org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException,
                                 org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                 java.lang.InterruptedException,
                                 org.api4.java.algorithm.exceptions.AlgorithmException
        Throws:
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmException
      • initGraph

        protected void initGraph()
                          throws org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException,
                                 org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                 java.lang.InterruptedException,
                                 org.api4.java.algorithm.exceptions.AlgorithmException
        This method setups the graph by inserting the root nodes.
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        java.util.concurrent.TimeoutException
        org.api4.java.algorithm.exceptions.AlgorithmException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
      • selectNodeForNextExpansion

        protected void selectNodeForNextExpansion​(BackPointerPath<N,​A,​V> node)
                                           throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • expandNextNode

        protected org.api4.java.algorithm.events.IAlgorithmEvent expandNextNode()
                                                                         throws java.lang.InterruptedException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmException
        This method conducts the expansion of the next node. Unless the next node has been selected from outside, it selects the first node on OPEN (if OPEN is empty but active jobs are running, it waits until those terminate)
        Returns:
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        java.util.concurrent.TimeoutException
        org.api4.java.algorithm.exceptions.AlgorithmException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
      • shutdown

        protected void shutdown()
        Overrides:
        shutdown in class ai.libs.jaicore.basic.algorithm.AAlgorithm<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​EvaluatedSearchGraphPath<N,​A,​V extends java.lang.Comparable<V>>>
      • receiveRolloutEvent

        public void receiveRolloutEvent​(RolloutEvent<N,​V> event)
      • receiveSolutionCandidateAnnotationEvent

        public void receiveSolutionCandidateAnnotationEvent​(SolutionAnnotationEvent<N,​A,​V> event)
      • receiveNodeAnnotationEvent

        public void receiveNodeAnnotationEvent​(NodeAnnotationEvent<N> event)
      • insertNodeIntoLocalGraph

        protected void insertNodeIntoLocalGraph​(BackPointerPath<N,​A,​V> node)
                                         throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • getLocalVersionOfNode

        protected BackPointerPath<N,​A,​V> getLocalVersionOfNode​(BackPointerPath<N,​A,​V> node)
        This is relevant if we work with several copies of a node (usually if we need to copy the search space somewhere).
        Parameters:
        node -
        Returns:
      • bootstrap

        public void bootstrap​(java.util.Collection<BackPointerPath<N,​A,​V>> initialNodes)
                       throws java.lang.InterruptedException
        This method can be used to create an initial graph different from just root nodes. This can be interesting if the search is distributed and we want to search only an excerpt of the original one.
        Parameters:
        initialNodes -
        Throws:
        java.lang.InterruptedException
      • nextWithException

        public org.api4.java.algorithm.events.IAlgorithmEvent nextWithException()
                                                                         throws java.lang.InterruptedException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException,
                                                                                org.api4.java.algorithm.exceptions.AlgorithmException
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
        org.api4.java.algorithm.exceptions.AlgorithmException
      • selectNodeForNextExpansion

        public void selectNodeForNextExpansion​(N node)
                                        throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • nextSolutionThatDominatesOpen

        public EvaluatedSearchGraphPath<N,​A,​V> nextSolutionThatDominatesOpen()
                                                                                  throws java.lang.InterruptedException,
                                                                                         org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                                                                         java.util.concurrent.TimeoutException,
                                                                                         org.api4.java.algorithm.exceptions.AlgorithmException
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        java.util.concurrent.TimeoutException
        org.api4.java.algorithm.exceptions.AlgorithmException
      • afterInitialization

        protected void afterInitialization()
        BLOCK C: Hooks
      • beforeSelection

        protected boolean beforeSelection()
      • getCurrentPathToNode

        public java.util.List<N> getCurrentPathToNode​(N node)
        BLOCK D: Getters and Setters
      • getNodeEvaluator

        public org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,​A,​V> getNodeEvaluator()
      • getAdditionalThreadsForExpansion

        public int getAdditionalThreadsForExpansion()
      • getTimeoutForComputationOfF

        public int getTimeoutForComputationOfF()
      • setTimeoutForComputationOfF

        public void setTimeoutForComputationOfF​(int timeoutInMS,
                                                org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,​A,​V> timeoutEvaluator)
      • getOpen

        public java.util.List<BackPointerPath<N,​A,​V>> getOpen()
        Returns:
        the openCollection
      • getInternalRepresentationOf

        public BackPointerPath<N,​A,​V> getInternalRepresentationOf​(N node)
      • setOpen

        public void setOpen​(java.util.Queue<BackPointerPath<N,​A,​V>> collection)
        Parameters:
        open - the openCollection to set
      • getLoggerName

        public java.lang.String getLoggerName()
        Specified by:
        getLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
        Overrides:
        getLoggerName in class AOptimalPathInORGraphSearch<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​N,​A,​V extends java.lang.Comparable<V>>
      • setLoggerName

        public void setLoggerName​(java.lang.String name)
        Specified by:
        setLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
        Overrides:
        setLoggerName in class AOptimalPathInORGraphSearch<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​N,​A,​V extends java.lang.Comparable<V>>
      • getExpandedCounter

        public int getExpandedCounter()
        Check how many times a node was expanded.
        Returns:
        A counter of how many times a node was expanded.
      • getCreatedCounter

        public int getCreatedCounter()
      • getFValue

        public V getFValue​(N node)
      • getNodeAnnotations

        public java.util.Map<java.lang.String,​java.lang.Object> getNodeAnnotations​(N node)
      • getNodeAnnotation

        public java.lang.Object getNodeAnnotation​(N node,
                                                  java.lang.String annotation)
      • onFValueReceivedEvent

        public void onFValueReceivedEvent​(FValueEvent<V> event)
      • getConfig

        public IBestFirstConfig getConfig()
        Specified by:
        getConfig in interface org.api4.java.algorithm.IAlgorithm<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​N>
        Overrides:
        getConfig in class ai.libs.jaicore.basic.algorithm.AAlgorithm<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,​A,​V>,​EvaluatedSearchGraphPath<N,​A,​V extends java.lang.Comparable<V>>>
      • toDetailedString

        public java.lang.String toDetailedString()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object