interface Problem abstract class State { } Set<State> initialStates() Boolean isGoalState(State s) Set<State> nextStates(State s) double moveCost(State from, State to)
interface StateSpaceSearch interface SolutionPath extends Queue<Problem.State> SolutionPath search(Problem problem) interface Comparison boolean isBetterThan( SolutionPath a, SolutionPath b) SolutionPath search( Problem problem, Comparison c)
Problem.State solve(Problem p) states = p.states() while !states.empty() s = states.pick() if p.goalState(s) return s return null
|
|
SolutionPath search(Problem problem) Set<Path> pendingPaths = initialPaths(problem) while !pendingPaths.isEmpty() Path path = pick(pendingPaths) for Problem.State neighbor : problem.moveResults(path.lastState()) Path newPath = path.addStep(neighbor) if problem.isGoalState(neighbor) return newPath pendingPaths.add(newPath) return new Path()
Path path = pick(pendingPaths) for Problem.State neighbor : problem.moveResults(path.lastState())
SolutionPath search(Problem problem, Comparison c) SolutionPath bestSolution = new Path() Set<Path> pendingPaths = initialPaths(problem) while !pendingPaths.isEmpty() Path path = pick(pendingPaths) for Problem.State neighbor : problem.moveResults(path.lastState()) Path newPath = path.addStep(neighbor) if problem.isGoalState(neighbor) && c.isBetterThan(newPath, bestSolution) bestSolution = newPath pendingPaths.add(newPath) return bestSolution
SolutionPath search(Problem problem) Stack<Path> pendingPaths = initialPaths(problem) while !pendingPaths.isEmpty() Path path = pendingPaths.pop() for Problem.State neighbor : problem.moveResults(path.lastState()) Path newPath = path.addStep(neighbor) if problem.isGoalState(neighbor) return newPath pendingPaths.push(newPath) return new Path()
Queue<Problem.State> solve(Problem p) paths = Queue(Queue(s | s ∈ p.startStates())) while !paths.empty() path = paths.get() s = path.tail() if p.goalState(s) return path for n in p.nextStates(s) paths.put(path.copy().add(n)) return Queue()
Queue<Problem.State> solve(Problem p)
paths =
MinQueue(Queue(s | s ∈ p.startStates()))
while !paths.empty()
path = paths.get()
s = path.tail()
if p.goalState(s)
return path
for n in p.nextStates(s)
paths.put(path.copy().add(n))
return Queue()
T(v0, …, vk) = C(v0, …, vk) + h(vk).
This page last modified on 2011 September 15. |