Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bisect
- def tree_search(problem, fringe):
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- print(node.state)
- if problem.goal_test(node.state):
- return node
- fringe.extend(node.expand(problem))
- return None
- def breadth_first_tree_search(problem):
- return tree_search(problem, FIFOQueue())
- def depth_first_tree_search(problem):
- return tree_search(problem, Stack())
- def graph_search(problem, fringe):
- closed = set()
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- if problem.goal_test(node.state):
- return node
- if node.state not in closed:
- closed.add(node.state)
- fringe.extend(node.expand(problem))
- return None
- def breadth_first_graph_search(problem):
- return graph_search(problem, FIFOQueue())
- def depth_first_graph_search(problem):
- return graph_search(problem, Stack())
- def depth_limited_search(problem, limit=50):
- def recursive_dls(node, problem, limit):
- cutoff_occurred = False
- if problem.goal_test(node.state):
- return node
- elif node.depth == limit:
- return 'cutoff'
- else:
- for successor in node.expand(problem):
- result = recursive_dls(successor, problem, limit)
- if result == 'cutoff':
- cutoff_occurred = True
- elif result is not None:
- return result
- if cutoff_occurred:
- return 'cutoff'
- return None
- return recursive_dls(Node(problem.initial), problem, limit)
- def iterative_deepening_search(problem):
- for depth in range(sys.maxsize):
- result = depth_limited_search(problem, depth)
- if result is not 'cutoff':
- return result
- def uniform_cost_search(problem):
- return graph_search(problem, PriorityQueue(min, lambda a: a.path_cost))
- class Problem:
- def __init__(self, initial, goal=None):
- self.initial = initial
- self.goal = goal
- def successor(self, state):
- raise NotImplementedError
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- possible = self.successor(state)
- return possible[action]
- def goal_test(self, state):
- return state == self.goal
- def path_cost(self, c, state1, action, state2):
- return c + 1
- def value(self):
- raise NotImplementedError
- class Node:
- def __init__(self, state, parent=None, action=None, path_cost=0):
- self.state = state
- self.parent = parent
- self.action = action
- self.path_cost = path_cost
- self.depth = 0 # search depth
- if parent:
- self.depth = parent.depth + 1
- def __repr__(self):
- return "<Node %s>" % (self.state,)
- def __lt__(self, node):
- return self.state < node.state
- def expand(self, problem):
- return [self.child_node(problem, action)
- for action in problem.actions(self.state)]
- def child_node(self, problem, action):
- next_state = problem.result(self.state, action)
- return Node(next_state, self, action,
- problem.path_cost(self.path_cost, self.state,
- action, next_state))
- def solution(self):
- return [node.action for node in self.path()[1:]]
- def solve(self):
- return [node.state for node in self.path()[0:]]
- def path(self):
- x, result = self, []
- while x:
- result.append(x)
- x = x.parent
- result.reverse()
- return result
- def __eq__(self, other):
- return isinstance(other, Node) and self.state == other.state
- def __hash__(self):
- return hash(self.state)
- class Queue:
- def __init__(self):
- raise NotImplementedError
- def append(self, item):
- raise NotImplementedError
- def extend(self, items):
- raise NotImplementedError
- def pop(self):
- raise NotImplementedError
- def __len__(self):
- raise NotImplementedError
- def __contains__(self, item):
- raise NotImplementedError
- class Stack(Queue):
- """Last-In-First-Out Queue."""
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop()
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class FIFOQueue(Queue):
- """First-In-First-Out Queue."""
- def __init__(self):
- self.data = []
- def append(self, item):
- self.data.append(item)
- def extend(self, items):
- self.data.extend(items)
- def pop(self):
- return self.data.pop(0)
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return item in self.data
- class PriorityQueue(Queue):
- def __init__(self, order=min, f=lambda x: x):
- assert order in [min, max]
- self.data = []
- self.order = order
- self.f = f
- def append(self, item):
- bisect.insort_right(self.data, (self.f(item), item))
- def extend(self, items):
- for item in items:
- bisect.insort_right(self.data, (self.f(item), item))
- def pop(self):
- if self.order == min:
- return self.data.pop(0)[1]
- return self.data.pop()[1]
- def __len__(self):
- return len(self.data)
- def __contains__(self, item):
- return any(item == pair[1] for pair in self.data)
- def __getitem__(self, key):
- for _, item in self.data:
- if item == key:
- return item
- def __delitem__(self, key):
- for i, (value, item) in enumerate(self.data):
- if item == key:
- self.data.pop(i)
- def move_up(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row-1,man_column)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row-1
- y=man_column
- if x>=0 and (man not in [b1,b2,b3]):
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def move_down(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row+1,man_column)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row+1
- y=man_column
- if x<8 and (man not in [b1,b2,b3]):
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def move_left(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row,man_column-1)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row
- y=man_column-1
- if y>=0 and (man not in [b1,b2,b3]):
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def move_right(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row,man_column+1)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row
- y=man_column+1
- if y<10 and (man not in [b1,b2,b3]):
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def moveK_up(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row-1,man_column)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row-1
- y=man_column
- if man==b1 and ((b1_row-1,b1_column) not in[b2,b3]) and x>=0:
- b1_row=b1_row-1
- elif man==b2 and ((b2_row-1,b2_column) not in[b1,b3]) and x>=0:
- b2_row=b2_row-1
- elif man==b3 and ((b3_row-1,b3_column) not in[b2,b1]) and x>=0:
- b3_row=b3_row-1
- else:
- return state_new
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def moveK_down(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row+1,man_column)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row+1
- y=man_column
- if man==b1 and ((b1_row+1,b1_column) not in[b2,b3]) and x<8:
- b1_row=b1_row+1
- elif man==b2 and ((b2_row+1,b2_column) not in[b1,b3]) and x<8:
- b2_row=b2_row+1
- elif man==b3 and ((b3_row+1,b3_column) not in[b2,b1]) and x<8:
- b3_row=b3_row+1
- else:
- return state_new
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def moveK_up(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row-1,man_column)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row-1
- y=man_column
- if man==b1 and ((b1_row-1,b1_column) not in[b2,b3]) and x>=0:
- b1_row=b1_row-1
- elif man==b2 and ((b2_row-1,b2_column) not in[b1,b3]) and x>=0:
- b2_row=b2_row-1
- elif man==b3 and ((b3_row-1,b3_column) not in[b2,b1]) and x>=0:
- b3_row=b3_row-1
- else:
- return state_new
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def moveK_left(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row,man_column-1)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row
- y=man_column-1
- if man==b1 and ((b1_row,b1_column-1) not in[b2,b3]) and y>=0:
- b1_column=b1_column-1
- elif man==b2 and ((b2_row,b2_column-1) not in[b1,b3]) and y>=0:
- b2_column=b2_column-1
- elif man==b3 and ((b3_row,b3_column-1) not in[b2,b1]) and y>=0:
- b3_column=b3_column-1
- else:
- return state_new
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- def moveK_right(state):
- man_row = state[0]
- man_column = state[1]
- b1_row = state[2]
- b1_column = state[3]
- b2_row = state[4]
- b2_column = state[5]
- b3_row = state[6]
- b3_column = state[7]
- man=(man_row,man_column+1)
- b1=(b1_row,b1_column)
- b2=(b2_row,b2_column)
- b3=(b3_row,b3_column)
- state_new=None
- x=man_row
- y=man_column+1
- if man==b1 and ((b1_row,b1_column+1) not in[b2,b3]) and y<10:
- b1_column=b1_column+1
- elif man==b2 and ((b2_row,b2_column+1) not in[b1,b3]) and y<10:
- b2_column=b2_column+1
- elif man==b3 and ((b3_row,b3_column+1) not in[b2,b1]) and y<10:
- b3_column=b3_column+1
- else:
- return state_new
- state_new=(x,y,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column)
- return state_new
- class Kutija(Problem):
- def __init__(self,initial,goal=None):
- self.initial=initial
- self.goal=goal
- def goal_test(self, state):
- b1=(state[2],state[3])
- b2=(state[4],state[5])
- b3=(state[6],state[7])
- return (b1==(2,3) and b2==(2,5) and b3==(2,7)) or\
- (b1==(2,3) and b3==(2,5) and b2==(2,7)) or\
- (b2==(2,3) and b1==(2,5) and b3==(2,7)) or\
- (b2==(2,3) and b3==(2,5) and b1==(2,7)) or\
- (b3==(2,3) and b1==(2,5) and b2==(2,7)) or\
- (b3==(2,3) and b2==(2,5) and b1==(2,7))
- def successor(self, state):
- succs={}
- #GoreCovece
- state_new=move_up(state)
- if state_new!=None: succs['GoreC']=state_new
- #GoreCoveceKutija
- state_new=moveK_up(state)
- if state_new!=None: succs['GoreCK']=state_new
- #DoluCovece
- state_new=move_down(state)
- if state_new!=None: succs['DoluC']=state_new
- #DoluCoveceKutija
- state_new=moveK_down(state)
- if state_new!=None: succs['DoluCK']=state_new
- #LevoCovece
- state_new=move_left(state)
- if state_new!=None: succs['LevoC']=state_new
- #LevoCoveceKutija
- state_new=moveK_left(state)
- if state_new!=None: succs['LevoCK']=state_new
- #DesnoCovece
- state_new=move_right(state)
- if state_new!=None: succs['DesnoC']=state_new
- #DesnoCoveceKutija
- state_new=moveK_right(state)
- if state_new!=None: succs['DesnoCK']=state_new
- return succs
- def actions(self, state):
- return self.successor(state).keys()
- def result(self, state, action):
- return self.successor(state)[action]
- if __name__ == '__main__':
- man_row = int(input())
- man_column = int(input())
- b1_row = int(input())
- b1_column = int(input())
- b2_row = int(input())
- b2_column = int(input())
- b3_row = int(input())
- b3_column = int(input())
- kutii=Kutija((man_row,man_column,b1_row,b1_column,b2_row,b2_column,b3_row,b3_column))
- result=breadth_first_graph_search(kutii)
- print(result.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement