Class BestFirst<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,A,V>,N,A,V extends java.lang.Comparable<V>>
- java.lang.Object
-
- ai.libs.jaicore.basic.algorithm.AAlgorithm<I,O>
-
- ai.libs.jaicore.basic.algorithm.ASolutionCandidateIterator<I,O>
-
- ai.libs.jaicore.basic.algorithm.AOptimizer<I,EvaluatedSearchGraphPath<N,A,V>,V>
-
- ai.libs.jaicore.search.core.interfaces.AOptimalPathInORGraphSearch<I,N,A,V>
-
- ai.libs.jaicore.search.algorithms.standard.bestfirst.BestFirst<I,N,A,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>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classBestFirst.ParentDiscarding
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.atomic.AtomicIntegeractiveJobsprotected intadditionalThreadsForNodeAttachmentprotected java.util.Map<N,BackPointerPath<N,A,V>>ext2intprotected org.api4.java.datastructure.graph.implicit.IGraphGenerator<N,A>graphGeneratorprotected org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,A,V>nodeEvaluatorprotected java.util.Queue<BackPointerPath<N,A,V>>openprotected org.api4.java.ai.graphsearch.problem.implicit.graphgenerator.IPathGoalTester<N,A>pathGoalTesterprotected java.util.Queue<EvaluatedSearchSolutionCandidateFoundEvent<N,A,V>>pendingSolutionFoundEventsprotected org.api4.java.datastructure.graph.implicit.IRootGenerator<N>rootGeneratorprotected java.util.Queue<EvaluatedSearchGraphPath<N,A,V>>solutionsprotected org.api4.java.datastructure.graph.implicit.ISuccessorGenerator<N,A>successorGenerator
-
Constructor Summary
Constructors Constructor Description BestFirst(IBestFirstConfig config, I problem)BestFirst(IBestFirstConfig config, I problem, org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,A,V> lowerBoundEvaluator)BestFirst(I problem)BestFirst(I problem, org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,A,V> lowerBoundEvaluator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafterExpansion(BackPointerPath<N,A,V> node)protected voidafterInitialization()BLOCK C: Hooksprotected voidafterSelection(BackPointerPath<N,A,V> node)protected voidbeforeExpansion(BackPointerPath<N,A,V> node)protected booleanbeforeSelection()voidbootstrap(java.util.Collection<BackPointerPath<N,A,V>> initialNodes)This method can be used to create an initial graph different from just root nodes.protected org.api4.java.algorithm.events.IAlgorithmEventexpandNextNode()This method conducts the expansion of the next node.intgetAdditionalThreadsForExpansion()IBestFirstConfiggetConfig()intgetCreatedCounter()java.util.List<N>getCurrentPathToNode(N node)BLOCK D: Getters and SettersintgetExpandedCounter()Check how many times a node was expanded.VgetFValue(BackPointerPath<N,A,V> node)VgetFValue(N node)BackPointerPath<N,A,V>getInternalRepresentationOf(N node)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).java.lang.StringgetLoggerName()java.lang.ObjectgetNodeAnnotation(N node, java.lang.String annotation)java.util.Map<java.lang.String,java.lang.Object>getNodeAnnotations(N node)org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,A,V>getNodeEvaluator()java.util.List<BackPointerPath<N,A,V>>getOpen()java.util.Queue<EvaluatedSearchGraphPath<N,A,V>>getSolutionQueue()intgetTimeoutForComputationOfF()protected voidinitGraph()This method setups the graph by inserting the root nodes.protected voidinsertNodeIntoLocalGraph(BackPointerPath<N,A,V> node)protected voidlabelNode(BackPointerPath<N,A,V> node)protected BackPointerPath<N,A,V>newNode(BackPointerPath<N,A,V> parent, N t2, A arc)protected BackPointerPath<N,A,V>newNode(BackPointerPath<N,A,V> parent, N t2, A arc, V evaluation)NodeExpansionJobSubmittedEvent<N,A,V>nextNodeExpansion()EvaluatedSearchGraphPath<N,A,V>nextSolutionThatDominatesOpen()org.api4.java.algorithm.events.IAlgorithmEventnextWithException()voidonFValueReceivedEvent(FValueEvent<V> event)voidreceiveNodeAnnotationEvent(NodeAnnotationEvent<N> event)voidreceiveRolloutEvent(RolloutEvent<N,V> event)voidreceiveSolutionCandidateAnnotationEvent(SolutionAnnotationEvent<N,A,V> event)voidreceiveSolutionCandidateEvent(EvaluatedSearchSolutionCandidateFoundEvent<N,A,V> solutionEvent)protected EvaluatedSearchSolutionCandidateFoundEvent<N,A,V>registerSolution(EvaluatedSearchGraphPath<N,A,V> solutionPath)protected voidselectNodeForNextExpansion(BackPointerPath<N,A,V> node)voidselectNodeForNextExpansion(N node)voidsetLoggerName(java.lang.String name)voidsetOpen(java.util.Queue<BackPointerPath<N,A,V>> collection)voidsetTimeoutForComputationOfF(int timeoutInMS, org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator<N,A,V> timeoutEvaluator)protected voidshutdown()java.lang.StringtoDetailedString()java.lang.StringtoString()-
Methods inherited from class ai.libs.jaicore.search.core.interfaces.AOptimalPathInORGraphSearch
getGoalTester, getGraphGenerator, nextSolutionCandidateEvent
-
Methods inherited from class ai.libs.jaicore.basic.algorithm.AOptimizer
call, getBestScoreKnownToExist, getBestSeenSolution, nextSolutionCandidate, setBestSeenSolution, tellAboutBestScoreKnownToExist, updateBestSeenSolution
-
Methods inherited from class ai.libs.jaicore.basic.algorithm.ASolutionCandidateIterator
collectAllSolutions
-
Methods inherited from class ai.libs.jaicore.basic.algorithm.AAlgorithm
activate, announceTimeoutDetected, avoidReinterruptionOnShutdownOnCurrentThread, cancel, checkAndConductTermination, checkTermination, computeTimeoutAware, getActivationTime, getId, getInput, getListeners, getNumCPUs, getRemainingTimeToDeadline, getState, getTimeout, getTimeoutPrecautionOffset, hasNext, hasThreadBeenInterruptedDuringShutdown, interruptThreadAsPartOfShutdown, isCanceled, isShutdownInitialized, isStopCriterionSatisfied, isTimeouted, iterator, next, post, registerActiveThread, registerListener, resolveShutdownInterruptOnCurrentThread, setConfig, setDeadline, setMaxNumThreads, setNumCPUs, setState, setTimeout, setTimeout, setTimeoutPrecautionOffset, terminate, unregisterActiveThread, unregisterThreadAndShutdown
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.api4.java.algorithm.IAlgorithm
call, getId, getInput, getNumCPUs, getTimeout, registerListener, setMaxNumThreads, setNumCPUs, setTimeout, setTimeout
-
-
-
-
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
-
solutions
protected final java.util.Queue<EvaluatedSearchGraphPath<N,A,V extends java.lang.Comparable<V>>> solutions
-
pendingSolutionFoundEvents
protected final java.util.Queue<EvaluatedSearchSolutionCandidateFoundEvent<N,A,V extends java.lang.Comparable<V>>> pendingSolutionFoundEvents
-
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)
-
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.AlgorithmTimeoutedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionjava.lang.InterruptedExceptionorg.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.AlgorithmExceptionThis method setups the graph by inserting the root nodes.- Throws:
java.lang.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionjava.util.concurrent.TimeoutExceptionorg.api4.java.algorithm.exceptions.AlgorithmExceptionorg.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.AlgorithmExceptionThis 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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionjava.util.concurrent.TimeoutExceptionorg.api4.java.algorithm.exceptions.AlgorithmExceptionorg.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
-
registerSolution
protected EvaluatedSearchSolutionCandidateFoundEvent<N,A,V> registerSolution(EvaluatedSearchGraphPath<N,A,V> solutionPath)
- Overrides:
registerSolutionin classAOptimalPathInORGraphSearch<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,A,V>,N,A,V extends java.lang.Comparable<V>>
-
shutdown
protected void shutdown()
-
receiveSolutionCandidateEvent
public void receiveSolutionCandidateEvent(EvaluatedSearchSolutionCandidateFoundEvent<N,A,V> solutionEvent)
-
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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionorg.api4.java.algorithm.exceptions.AlgorithmTimeoutedExceptionorg.api4.java.algorithm.exceptions.AlgorithmException
-
selectNodeForNextExpansion
public void selectNodeForNextExpansion(N node) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
nextNodeExpansion
public NodeExpansionJobSubmittedEvent<N,A,V> nextNodeExpansion()
-
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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionjava.util.concurrent.TimeoutExceptionorg.api4.java.algorithm.exceptions.AlgorithmException
-
afterInitialization
protected void afterInitialization()
BLOCK C: Hooks
-
beforeSelection
protected boolean beforeSelection()
-
afterSelection
protected void afterSelection(BackPointerPath<N,A,V> node)
-
beforeExpansion
protected void beforeExpansion(BackPointerPath<N,A,V> node)
-
afterExpansion
protected void afterExpansion(BackPointerPath<N,A,V> node)
-
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:
getLoggerNamein interfaceorg.api4.java.common.control.ILoggingCustomizable- Overrides:
getLoggerNamein classAOptimalPathInORGraphSearch<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:
setLoggerNamein interfaceorg.api4.java.common.control.ILoggingCustomizable- Overrides:
setLoggerNamein classAOptimalPathInORGraphSearch<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,A,V>,N,A,V extends java.lang.Comparable<V>>
-
getSolutionQueue
public java.util.Queue<EvaluatedSearchGraphPath<N,A,V>> getSolutionQueue()
-
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(BackPointerPath<N,A,V> 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:
getConfigin interfaceorg.api4.java.algorithm.IAlgorithm<I extends org.api4.java.ai.graphsearch.problem.IPathSearchWithPathEvaluationsInput<N,A,V>,N>- Overrides:
getConfigin classai.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:
toStringin classjava.lang.Object
-
-