def __init__(self, maze, position1, position2, size, heuristic="manhattan", tie="bigger", improving=False): self.start = position1 self.maze = maze self.size = size if (tie == "bigger"): self.states = sts.states(size) if (tie == "smaller"): self.states = sts.states(size, heuristic, tie) self.agent = self.states.states[position1[0]][position1[1]] self.target = self.states.states[position2[0]][position2[1]] if (self.maze[self.target.i][self.target.j]) == 1: while True: print("Target position wrong") #print self.agent #print self.target self.number_expaned_cells = 0 self.opn_list = my_heapq.my_heapq() self.counter = 0
def begin(self): whole_path = [] self.states.set_target(self.target) while self.agent != self.target: #print "In this round, the agent start with" #print (self.agent.i,self.agent.j) self.counter = self.counter + 1 self.agent.g = 0 self.target.g = float('inf') self.agent.search = self.counter self.opn_list = my_heapq.my_heapq() self.states.clear() self.opn_list.push(self.agent) self.A_star() if self.opn_list.is_empty() == True: print("cannot reach target") #print_maze() break else: path = [] path.append(self.target) next_node = self.target.node path.append(next_node) #while next_node!=agent: while next_node.node != self.agent: next_node = next_node.node path.append(next_node) path.reverse() # for temp in path: # print (temp.i,temp.j) for node in path: #one cell is detected to be blocked if node.i >= 1: self.states.maze[node.i - 1][node.j] = self.maze[node.i - 1][node.j] if node.j >= 1: self.states.maze[node.i][node.j - 1] = self.maze[node.i][node.j - 1] if node.i < self.size - 1: self.states.maze[node.i + 1][node.j] = self.maze[node.i + 1][node.j] if node.j < self.size - 1: self.states.maze[node.i][node.j + 1] = self.maze[node.i][node.j + 1] if self.maze[node.i][node.j] == 1: self.states.maze[node.i][node.j] = 1 break else: whole_path.append(node) self.agent = node if node.i == self.target.i and node.j == self.target.j: self.show_route(whole_path) print("Reach the target")
def begin(self): #self.print_maze() whole_path = [] self.target.h = self.states.heuristic(self.agent, self.target) self.agent.h = 0 self.states.set_target(self.target) while self.agent.i!=self.target.i or self.agent.j!=self.target.j: if self.states.maze[self.target.i][self.target.j]==1: break #for temp in range(1,100): print "In this round, the agent start with" print (self.agent.i,self.agent.j) self.counter = self.counter+1 self.target.g = 0 self.agent.g= float('inf') self.agent.search = self.counter self.opn_list = my_heapq.my_heapq() self.states.clear() self.opn_list.push(self.target) self.A_star_back() if self.opn_list.is_empty()==True: print("cannot reach target") self.print_maze() break else: path = [] #path.append(self.agent) next_node = self.agent.node path.append(next_node) while next_node!=self.target: next_node = next_node.node path.append(next_node) for node in path: #print("in path") #one cell is detected to be blocked if node.i>=1: self.states.maze[node.i-1][node.j] = self.maze[node.i-1][node.j] if node.j>=1: self.states.maze[node.i][node.j-1] = self.maze[node.i][node.j-1] if node.i<self.size-1: self.states.maze[node.i+1][node.j] = self.maze[node.i+1][node.j] if node.j<self.size-1: self.states.maze[node.i][node.j+1] = self.maze[node.i][node.j+1] if self.maze[node.i][node.j]==1: self.states.maze[node.i][node.j] = 1 break else: whole_path.append(node) self.agent = node if node.i==self.target.i and node.j== self.target.j: self.show_route(whole_path) print("Reach the target")
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Sat Feb 8 21:24:57 2020 @author: apple """ import my_heapq as heapq a = heapq.my_heapq() a.push(1) a.show() a.push(4) a.show() a.push(3) a.show() a.push(2) a.show() a.push(1) a.show() a.push(4) a.show() a.push(3) a.show() a.push(2) a.show() a.pop() a.show() a.pop() a.show()