Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- class Queue:
- def __init__(self):
- raise NotImplementedError
- def extend(self, items):
- for item in items:
- self.append(item)
- class FIFOQueue(Queue):
- def __init__(self):
- self.A = []
- self.start = 0
- def append(self, item):
- self.A.append(item)
- def __len__(self):
- return len(self.A) - self.start
- def extend(self, items):
- self.A.extend(items)
- def pop(self):
- e = self.A[self.start]
- self.start += 1
- if self.start > 5 and self.start > len(self.A) / 2:
- self.A = self.A[self.start:]
- self.start = 0
- return e
- def __contains__(self, item):
- return item in self.A[self.start:]
- def graph_search(problem, fringe):
- closedStates = []
- fringe.append(Node(problem.initial))
- while fringe:
- node = fringe.pop()
- if problem.goal_test(node.state):
- return node
- inside = False
- for state in closedStates:
- if state == node.state:
- inside = True
- break
- if not inside:
- fringe.extend(node.expand(problem))
- closedStates.append(node.state)
- return None
- def breadth_first_graph_search(problem):
- return graph_search(problem, FIFOQueue())
- class DisksProblem:
- def __init__(self, initial, goal=None, size=4):
- self.initial = initial
- self.goal = goal
- self.size = size
- def actions(self,state):
- actions = []
- for idx, element in enumerate(state):
- if element is None:
- continue
- if idx > 0:
- if state[idx - 1] is None:
- actions.append('L1: ' + element)
- if idx > 1:
- if state[idx - 2] is None and state[idx - 1] is not None:
- actions.append('L2: ' + element)
- if idx < len(state) - 1:
- if state[idx + 1] is None:
- actions.append('D1: ' + element)
- if idx < len(state) - 2:
- if state[idx + 2] is None and state[idx + 1] is not None:
- actions.append('D2: ' + element)
- return actions
- def result(self, state, action):
- split_action = action.split(': ')
- operation = split_action[0]
- element_to_move = split_action[1]
- new_state = copy.deepcopy(state)
- for idx, element in enumerate(state):
- if element == element_to_move:
- new_state[idx] = None
- if operation == "D1": new_state[idx + 1] = element
- if operation == "D2": new_state[idx + 2] = element
- if operation == "L1": new_state[idx - 1] = element
- if operation == "L2": new_state[idx - 2] = element
- return new_state
- def goal_test(self, state):
- return state == self.goal
- def path_cost(self, cost, state1, action, state2):
- return cost + 1
- 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
- if parent:
- self.depth = parent.depth + 1
- def __repr__(self):
- return "<Node %s>" % (self.state,)
- def __lt__(self, other):
- return self.state < other.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 = problem.result(self.state, action)
- return Node(next, self, action, problem.path_cost(self.path_cost, self.state, action, next))
- 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 = self
- result = []
- while x:
- result.append(x)
- x = x.parent
- return list(reversed(result))
- def __eq__(self, other):
- return isinstance(other, Node) and self.state == other.state
- n = input("")
- l = input("")
- initial_state = [ None for y in range(l) ]
- goal_state = [ None for y in range(l) ]
- for idx in range(0, n):
- element = 'Disk ' + str(idx + 1)
- initial_state[idx] = element
- goal_state[l - 1 - idx] = element
- print initial_state
- print goal_state
- disks_problem = DisksProblem(initial=initial_state, goal=goal_state)
- # print(disks_problem.actions(initial_state))
- answer = breadth_first_graph_search(disks_problem)
- path = answer.solve()
- for item in path:
- print (item)
- print (answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement