def process_state(self, t, dt): StateAgent.process_state(self, t, dt) # Calculate the lateral distance to the threat aircraft entity = self.beliefs.entity_state threat = self.beliefs.threat_state self.is_param_one = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_ONE) self.is_param_two = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_TWO) self.is_param_three = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_THREE) self.is_param_four = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_FOUR) self.is_param_five = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_FIVE) self.is_param_six = helpers.transition_condition(entity.tactical_3d(), threat.tactical_3d(), self.PARAMETER_SET_SIX) if self.is_param_one or self.is_param_two or self.is_param_three or self.is_param_four or self.is_param_five or self.is_param_six: self.transition_request = transition(self.is_param_one, self.is_param_two, self.is_param_three, self.is_param_four, self.is_param_five, self.is_param_six, LeftDown, LeftUp, PullUp, LevelFlight, PushDown, RightDown)
def execute(self, t, dt): StateAgent.execute(self, t, dt) # Check threat is detected if not self.beliefs.threat_state: return # Determine the bearing to the threat aircraft entity = self.beliefs.entity_state threat = self.beliefs.threat_state threat_bearing = ut.relative_bearing(entity.x, entity.y, threat.x, threat.y) # Issue command to turn toward threat if not ut.is_close(threat_bearing, entity.desired_heading): self.commands.append(SetHeadingGLoadCmd(psi_c=threat_bearing, gload_c=10))
def execute(self, t, dt): StateAgent.execute(self, t, dt) # Check threat is detected if not self.beliefs.threat_state: return # Determine heading parallel to flight path of threat entity = self.beliefs.entity_state threat = self.beliefs.threat_state parallel_heading = ut.reciprocal_heading(threat.heading) # Issue the change heading command if not ut.is_close(parallel_heading, entity.desired_heading, abs_tol=1.0): # self.commands.append(SetHeadingCmd(parallel_heading)) # TODO: extract GLoad, calculate based on desired displacement self.commands.append(SetHeadingGLoadCmd(psi_c=parallel_heading, gload_c=5))
def __init__(self, params_filename=None): """ Creates an agent that performs a stern conversion against a target aircraft. A new StateAgent is initialised with the states in this module. The tactical parameters for each state may be set using a json file. Args: params_filename (string): optional json file containing tactical parameters to overwrite the default state parameters Returns: (StateAgent): stern conversion state agent """ states = [PureIntercept(), MatchAltitude(), MatchSpeed()] StateAgent.__init__(self, states, initial=[PureIntercept, MatchAltitude, MatchSpeed])
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) # print 'State: ', self.__class__.__name__, ' at time step: ', t if self.beliefs.threat_state: entity = self.beliefs.entity_state threat = self.beliefs.threat_state self.is_param_one = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.PP2FR) self.is_param_two = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.PP2FO) self.is_param_three = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.PP2C) if self.is_param_one or self.is_param_two or self.is_param_three: self.transition_request = transition( self.is_param_one, self.is_param_two, self.is_param_three, EcuFlyRelativeBearing, EcuFlyingOffset, EcuConverting)
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) if self.beliefs.threat_state: # Calculate the lateral distance to the threat aircraft entity = self.beliefs.entity_state threat = self.beliefs.threat_state distance = ut.distance(entity.pos_2d(), threat.pos_2d()) displacement = ut.lateral_displacement(entity.x, entity.y, threat.x, threat.y, threat.heading, distance) displacement = ut.metres_to_feet(displacement) # Check whether the desired lateral separation has been reached if displacement >= self.DESIRED_DISPLACEMENT: self.transition_request = FlyingOffset else: # No threats detected so return to default state self.transition_request = PureIntercept
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) # print 'State: ', self.__class__.__name__, ' at time step: ', t if self.beliefs.threat_state: # Calculate the lateral distance to the threat aircraft entity = self.beliefs.entity_state threat = self.beliefs.threat_state self.is_param_one = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.C2PP) self.is_param_two = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.C2FR) self.is_param_three = helpers.transition_condition( entity.tactical_3d(), threat.tactical_3d(), self.C2FO) if self.is_param_one or self.is_param_two or self.is_param_three: self.transition_request = transition( self.is_param_one, self.is_param_two, self.is_param_three, EcuPureIntercept, EcuFlyRelativeBearing, EcuFlyingOffset)
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) if self.beliefs.threat_state: entity = self.beliefs.entity_state threat = self.beliefs.threat_state # Check whether threat is aligned with aircraft taa = ut.target_aspect_angle(entity.x, entity.y, threat.x, threat.y, threat.heading) bearing_to_entity = ut.relative_bearing(threat.x, threat.y, entity.x, entity.y) bearing_offset = abs(threat.heading - bearing_to_entity) if bearing_offset > self.MAX_ALIGNMENT_DIFF: # Threat not aligned so don't proceed to next state return # Check whether at turn range from threat distance = ut.distance(entity.pos_2d(), threat.pos_2d()) if ut.metres_to_nautical_miles(distance) <= self.TURN_RANGE: self.transition_request = FlyRelativeBearing
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) if self.beliefs.threat_state: entity = self.beliefs.entity_state threat = self.beliefs.threat_state distance = ut.distance(entity.pos_2d(), threat.pos_2d()) # Check whether the conversion point has been reached if ut.metres_to_nautical_miles(distance) <= self.CONVERSION_RANGE: self.transition_request = Converting # Check if still closing with threat if self._last_range: if distance > self._last_range: # Not closing with threat so return to default state self.transition_request = PureIntercept self._last_range = distance else: # No threats detected so return to default state self.transition_request = PureIntercept
def __init__(self, params_filename=None): """ Creates an agent that performs a stern conversion against a target aircraft. A new StateAgent is initialised with the states in this module. The tactical parameters for each state may be set using a json file. Args: params_filename (string): optional json file containing tactical parameters to overwrite the default state parameters Returns: (StateAgent): stern conversion state agent """ states = [ LeftDown(), LeftUp(), PullUp(), LevelFlight(), PushDown(), RightUp(), RightDown() ] # Pre-fill tactical parameters if json file is defined if params_filename: with open(params_filename) as data_file: data = json.load(data_file) for state in states: state_classname = state.__class__.__name__ if state_classname in data: # Set parameters to the values defined in the json file for param, value in data[state_classname].items(): setattr(state, param, value) StateAgent.__init__(self, states, initial=[LeftDown])
def __init__(self, params_filename=None): """ Creates an agent that performs a stern conversion against a target aircraft. A new StateAgent is initialised with the states in this module. The tactical parameters for each state may be set using a json file. Args: params_filename (string): optional json file containing tactical parameters to overwrite the default state parameters Returns: (StateAgent): stern conversion state agent """ states = [ PureIntercept(), FlyRelativeBearing(), FlyingOffset(), Converting(), MatchAltitude() ] # Pre-fill tactical parameters if json file is defined # if params_filename: # with open(params_filename) as data_file: # data = json.load(data_file) # for state in states: # state_classname = state.__class__.__name__ # if state_classname in data: # # Set parameters to the values defined in the json file # for param, value in data[state_classname].items(): # setattr(state, param, value) StateAgent.__init__(self, states, initial=[PureIntercept, MatchAltitude])
def __init__(self, params_filename=None): """ Creates an agent that performs basic air flight maneuvers based on the Park agent paper. A new StateAgent is initialised with the states in this module. The transition parameters are set from a specified JSON file. Args: params_filename (string): optional json file containing tactical parameters to overwrite the default transition parameters Returns: (StateAgent): basic state agent """ states = [ LeftDown(), LeftUp(), PullUp(), LevelFlight(), PushDown(), RightUp(), RightDown() ] # Pre-fill tactical parameters if json file is defined # if params_filename: # with open(params_filename) as data_file: # data = json.load(data_file) # for state in states: # state_classname = state.__class__.__name__ # if state_classname in data: # # Set parameters to the values defined in the json file # for param, value in data[state_classname].items(): # setattr(state, param, value) StateAgent.__init__(self, states, initial=[LeftUp])
def execute(self, t, dt): StateAgent.execute(self, t, dt) # Check threat is detected if not self.beliefs.threat_state: return entity = self.beliefs.entity_state threat = self.beliefs.threat_state # Check if the threat heading is exactly reciprocal if ut.is_reciprocal(entity.heading, threat.heading): # Issue command to turn toward the threat aircraft threat_bearing = ut.relative_bearing(entity.x, entity.y, threat.x, threat.y) if not ut.is_close( entity.desired_heading, threat_bearing, abs_tol=1.0): # self.commands.append(SetHeadingCmd(threat_bearing)) # TODO: extract GLoad, calculate based on desired displacement self.commands.append( SetHeadingGLoadCmd(psi_c=threat_bearing, gload_c=5)) elif not ut.is_close(entity.desired_heading, threat.heading): # Issue command to turn to match the threat aircraft heading # self.commands.append(SetHeadingCmd(threat.heading)) # TODO: extract GLoad, calculate based on desired displacement self.commands.append( SetHeadingGLoadCmd(psi_c=threat.heading, gload_c=5)) # Adjust the entity speed to avoid overshooting if (entity.desired_v > threat.v and not ut.is_close(entity.desired_v, threat.v)): distance = ut.distance(entity.pos_2d(), threat.pos_2d()) if ut.metres_to_nautical_miles(distance) <= \ self.NO_CLOSER_RANGE: # Issue command to match threat speed self.commands.append(SetSpeedCmd(threat.v))
def main(args): dir_name = ROLLOUT_DIR + "/rollout_" + args.env_name + "/" episodes = args.N action_dim = action_space_dimension(args.env_name) epochs = args.epochs time_steps = args.time_steps [next_states, correct_state] = import_data(episodes,action_dim,dir_name, time_steps) try: agent = StateAgent(action_dim,args.env_name) except: print('NO DATA FOUND') raise agent.train(next_states,correct_state,epochs) agent.save_weights()
def execute(self, t, dt): StateAgent.execute(self, t, dt) self.commands.append(SetPitchAngleCmd(theta_c=self.THETA_C))
def execute(self, t, dt): StateAgent.execute(self, t, dt) self.commands.append(SetFlyLevelCmd())
def test_against_environment(env_name, num_runs, agent_name): env = gym.make(env_name) # env.seed(0) try: predictor = load_predictive_model(env_name, env.action_space.n) if agent_name == 'Next_agent': agent = StateAgent(env.action_space.n, env_name) agent.set_weights() elif agent_name == 'DQN': agent = Agent(gamma=0.99, epsilon=0.00, alpha=0.0001, input_dims=(104, 80, 4), n_actions=env.action_space.n, mem_size=25000, eps_min=0.00, batch_size=32, replace=1000, eps_dec=1e-5, env_name=env_name) agent.load_models() except: print( "Error loading model, check environment name and action space dimensions" ) rewards = [] start = time.time() total_steps = 0.0 for i in range(num_runs): frame_queue = deque(maxlen=4) observation = env.reset() done = False if agent_name == 'DQN': init_queue(frame_queue, observation, True) else: init_queue(frame_queue, observation) total_reward = 0.0 frame_count = 0 while not done: observation_states = np.concatenate(frame_queue, axis=2) # Human start of breakout since the next state agent just keeps moving to the left if agent_name == 'Next_agent': if env_name == 'BreakoutDeterministic-v4' and not frame_count: agent_action = 1 else: next_states = predictor.generate_output_states( np.expand_dims(observation_states, axis=0)) agent_action = agent.choose_action_from_next_states( np.expand_dims(next_states, axis=0)) elif agent_name == 'DQN': agent_action = agent.choose_action(observation_states) else: agent_action = env.action_space.sample() observation, reward, done, _ = env.step(agent_action) total_reward += reward frame_count += 1 total_steps += 1 frame_queue.pop() if agent_name == 'DQN': frame_queue.appendleft(preprocess_frame_dqn(observation)) else: frame_queue.appendleft(preprocess_frame(observation)) print("Completed episode {} with reward {}".format( i + 1, total_reward)) rewards.append(total_reward) end = time.time() time_taken = (end - start) / total_steps print("Test complete - Average score: {} Max score: {}".format( np.average(rewards), np.max(rewards))) return (rewards, time_taken)
def process_state(self, t, dt): StateAgent.process_state(self, t, dt) if not self.beliefs.threat_state: # No threats detected so return to the default state self.transition_request = PureIntercept
def execute(self, t, dt): StateAgent.execute(self, t, dt) self.commands.append(SetPitchAngleCmd(theta_c=self.THETA_C)) self.commands.append(SetHeadingGLoadCmd(psi_c=self.PSI_C + self.beliefs.entity_state.psi, gload_c=5))