def plan_action(self):
		planner = Planner()

		if self.current_state == RobotState.searching:
			planner.graph = self.graph
			path = planner.solve(self.actual_position, [self.target])

		elif self.current_state == RobotState.returning:
			planner.graph = self.original_graph
			path,keys_used = self.best_plan(self.original_graph, self.keys_available)

		self.current_plan = path

		if len(path) <= 1:
			return Action.nothing
		else:
			next_state = path[1]
			turn = next_state[2] - self.actual_position[2]

			if( not (next_state in self.graph.edges[self.actual_position]) and
					( abs(next_state[0] - self.actual_position[0]) +
						abs(next_state[1] - self.actual_position[1]) == 1 ) and turn == 0):

				if self.waiting_for_door == False:
					print "Waiting For Door!"
					self.waiting_for_door = True
					return Action.open_door
				else:
					self.keys_available -= 1
					print 'USING KEY ! left: ',self.keys_available
					self.waiting_for_door = False

			if turn == 0:
				return Action.move
			elif turn==-1:
				return Action.turn_right
			elif turn==1:
				return Action.turn_left
			elif turn == 3:
				return Action.turn_right
			elif turn == -3:
				return Action.turn_left
	def get_best_plan_with_keys_recursive(self,graph, keys):

		if keys == 0:
			planner = Planner()
			planner.graph = graph
			path = planner.solve(self.actual_position, [self.target])
			return path, self.keys_available - keys

		best_plan = None
		best_plan_dist = 999999
		best_keys_num = 9999999
		removed_walls = []

		for node, walls in self.walls.items():
			aux = None
			for i,w in enumerate(walls):

				if walls[Orientation.up] == 1 and i == Orientation.up:
					aux = (node[0]+1,node[1], Orientation.up)
				elif walls[Orientation.left] == 1 and i == Orientation.left:
					aux = (node[0],node[1]-1, Orientation.left)
				elif walls[Orientation.down] == 1 and i == Orientation.down:
					aux = (node[0]-1,node[1], Orientation.down)
				elif walls[Orientation.right] == 1 and i == Orientation.right:
					aux = (node[0],node[1]+1, Orientation.right)

				if aux in graph.nodes:
					graph.add_edge( (node[0],node[1],i),aux)

					path, keys_used = self.get_best_plan_with_keys_recursive(graph,keys - 1)

					graph.disconect( (node[0],node[1],i),aux)

					if(len(path) < best_plan_dist ):
						best_plan_dist = len(path)
						best_keys_num = keys_used
						best_plan = path
					if( len(path) == best_plan_dist and keys_used < best_keys_num ):
						best_plan_dist = len(path)
						best_keys_num = keys_used
						best_plan = path

		return best_plan, best_keys_num
Exemple #3
0
	def plan_action(self):
		planner = Planner()
		planner.graph = self.graph
		path = planner.solve(self.location, self.goal)
		self.current_plan = path

		if len(path) <= 1:
			return False
		else:
			next_state = path[1]
			turn = next_state[2] - self.location[2]
			if turn == 0:
				return Action.move
			elif turn==-1:  
				return Action.turn_right
			elif turn==1:
				return Action.turn_left
			elif turn == 3:
				return Action.turn_right
			elif turn == -3:
				return Action.turn_left