Example #1
0
def l4_planning(env, agent):
	# 1. Choosing a target
	if agent.target is None or env.state[agent.target[0],agent.target[1]] == -1:
		# - choosing a target
		target_position = l4_choose_target(env.state)
		agent.target = target_position
	else:
		target_position = agent.target

	# - planning the action/route to the target
	# if it exists
	if target_position is not None:
		next_action = a_star_planning(env.state, env.state.shape[0], env.state.shape[1],
									 	env.action_space, agent.position, target_position)
	# else, take a random action
	else:
		next_action = env.action_space.sample()

	# 2. Verifying if the agent can complete a task
	if agent.direction == np.pi/2:
		pos = (agent.position[0],agent.position[1]+1)
	elif agent.direction == 3*np.pi/2:
		pos = (agent.position[0],agent.position[1]-1)
	elif agent.direction == 0:
		pos = (agent.position[0]+1,agent.position[1])
	elif agent.direction == np.pi:
		pos = (agent.position[0]-1,agent.position[1])

	if pos == target_position:
		#target_position = None
		agent.target = target_position
		return 4, target_position

	return next_action,target_position
Example #2
0
def c1_planning(env, agent, mode='spatial'):
    action, target_pos = None, None

    # checking if the agent already chosen a prey
    if (agent.target):
        for t in env.components['tasks']:
            if (t.index == agent.target):
                # if it is completed, pursue a new one
                if (t.completed):
                    agent.target = choose_target(env, agent, mode)
                    target_pos = agent.target_position
                # else, keep going
                else:
                    target_pos = t.position

        # defining the path to the prey
        if (target_pos):
            agent.target_position = target_pos
            action = a_star_planning(env.state, env.state.shape[0],
                                     env.state.shape[1], env.action_space,
                                     agent.position, agent.target_position)
            return action, agent.target
        else:
            action = a_star_planning(env.state, env.state.shape[0],
                                     env.state.shape[1], env.action_space,
                                     agent.position, agent.target_position)
            return action, agent.target

    # else, choose one
    else:
        agent.target = choose_target(env, agent, mode)

        # if did not find a valid target, move randomly
        if (not agent.target):
            action = env.action_space.sample()
            return action, None
        # else pursue it
        else:
            action = a_star_planning(env.state, env.state.shape[0],
                                     env.state.shape[1], env.action_space,
                                     agent.position, agent.target_position)

            return action, agent.target