side1_step_reward = 0 side2_step_reward = 0 for y in range(side1_detector_num): side1_step_reward += o_detector_reward[y] for y in range(side1_fighter_num): side1_step_reward += o_fighter_reward[y] for y in range(side2_detector_num): side2_step_reward += e_detector_reward[y] for y in range(side2_fighter_num): side2_step_reward += e_fighter_reward[y] side1_total_reward += side1_step_reward side2_total_reward += side2_step_reward # print('Round %d, Step %d:' % (round_cnt, step_cnt)) # print('Side 1 reward: %d, Side 2 reward: %d' % (side1_step_reward, side2_step_reward)) if env.get_done(): print('Round %d done at step %d!' % (round_cnt, step_cnt)) if o_game_reward > e_game_reward: print('Side 1 WIN!!!') side1_win_times += 1 elif o_game_reward < e_game_reward: print('Side 2 WIN!!!') side2_win_times += 1 else: print('DRAW!!!') draw_times += 1 print( 'Side 1 total step reward: %d, Side 2 total step reward: %d' % (side1_total_reward, side2_total_reward)) print('Side 1 round reward: %d, Side 2 round reward: %d' % (o_game_reward, e_game_reward))
def run(agent1_name, agent2_name, map_name, round_num, max_step, random_pos=False): """ :param agent1_name: 红方名称 :param agent2_name: 蓝方名称 :param map_name: 地图名称 :param round_num: 对战局数 :param max_step: 单局最大step :param random_pos: 随机起始位置 :return: agent1_win_times, agent2_win_times, draw_times, agent1_crash_times, agent2_crash_times, agent1_timeout_times, agent2_timeout_times, agent1_launch_failure_times, agent2_launch_failure_times """ side1_win_times = 0 side2_win_times = 0 draw_times = 0 log_flag = agent1_name + '_vs_' + agent2_name agent1_launch_failed = False agent2_launch_failed = False round_cnt = 0 agent1_crash_list = [] agent2_crash_list = [] agent1_timeout_list = [] agent2_timeout_list = [] # file path constructing map_path = 'maps/' + map_name + '.map' agent1_path = 'agent/' + agent1_name + '/agent.py' agent2_path = 'agent/' + agent2_name + '/agent.py' if not os.path.exists(map_path): print('Error: map file not exist!') exit(-1) if not os.path.exists(agent1_path): print('Error: agent1 file not exist!') exit(-1) if not os.path.exists(agent2_path): print('Error: agent2 file not exist!') exit(-1) # make env env = Environment(map_path, 'raw', 'raw', max_step=max_step, render=True, random_pos=random_pos, log=log_flag) # get map info size_x, size_y = env.get_map_size() side1_detector_num, side1_fighter_num, side2_detector_num, side2_fighter_num = env.get_unit_num( ) # create agent agent1 = AgentCtrl(agent1_name, size_x, size_y, side1_detector_num, side1_fighter_num) agent2 = AgentCtrl(agent2_name, size_x, size_y, side2_detector_num, side2_fighter_num) if not agent1.agent_init(): print('ERROR: Agent1 ' + agent1_name + ' init failed!') agent1.terminate() agent2.terminate() agent1_launch_failed = True if not agent2.agent_init(): print('ERROR: Agent2 ' + agent2_name + ' init failed!') agent1.terminate() agent2.terminate() agent2_launch_failed = True # 若此处一方启动失败,则认为该方全败,启动失败计round_num次,若双方启动失败,则认为双方平局round_num次,其他与前述相同。 if agent1_launch_failed and agent2_launch_failed: return 0, 0, round_num, 0, 0, 0, 0, round_num, round_num elif agent1_launch_failed: return 0, round_num, 0, 0, 0, 0, 0, round_num, 0 elif agent2_launch_failed: return round_num, 0, 0, 0, 0, 0, 0, 0, round_num # execution # input("Press the <ENTER> key to continue...") for x in range(round_num): if x != 0: env.reset() step_cnt = 0 round_cnt += 1 while True: step_cnt += 1 # get obs side1_obs_dict, side2_obs_dict = env.get_obs() # get action agent1_action, agent1_result = agent1.get_action( side1_obs_dict, step_cnt) if agent1_result == 0: side1_detector_action = agent1_action['detector_action'] side1_fighter_action = agent1_action['fighter_action'] elif agent1_result == 1: agent1_crash_list.append(round_cnt) elif agent1_result == 2: agent1_timeout_list.append(round_cnt) agent2_action, agent2_result = agent2.get_action( side2_obs_dict, step_cnt) if agent2_result == 0: side2_detector_action = agent2_action['detector_action'] side2_fighter_action = agent2_action['fighter_action'] elif agent2_result == 1: agent2_crash_list.append(round_cnt) elif agent2_result == 2: agent2_timeout_list.append(round_cnt) # execution if agent1_result == 0 and agent2_result == 0: env.step(side1_detector_action, side1_fighter_action, side2_detector_action, side2_fighter_action) elif agent1_result != 0 and agent2_result != 0: env.set_surrender(2) elif agent1_result != 0: env.set_surrender(0) else: env.set_surrender(1) # get done if env.get_done(): # reward o_detector_reward, o_fighter_reward, o_game_reward, e_detector_reward, e_fighter_reward, e_game_reward = env.get_reward( ) if o_game_reward > e_game_reward: side1_win_times += 1 elif o_game_reward < e_game_reward: side2_win_times += 1 else: draw_times += 1 break agent1.terminate() agent2.terminate() return side1_win_times, side2_win_times, draw_times, len( agent1_crash_list), len(agent2_crash_list), len( agent1_timeout_list), len(agent2_timeout_list), 0, 0
# step and get next obs env.step(red_detector_action, red_fighter_action, blue_detector_action, blue_fighter_action) next_red_obs_dict, next_blue_obs_dict = env.get_obs() next_fighter_tmp_obs = next_red_obs_dict['fighter'] next_enemy_tmp_obs = next_red_obs_dict['enemy'] next_obs_list.append(next_fighter_tmp_obs) next_en_obs_list.append(next_enemy_tmp_obs) # calculate reward red_detector_reward, red_fighter_reward, red_game_reward, blue_detector_reward, blue_fighter_reward, blue_game_reward = env.get_reward() detector_reward = red_detector_reward fighter_reward = red_fighter_reward reward_list.append(sum(fighter_reward)) done = env.get_done() step_cnt += 1 print('step: ', step_cnt) if done: t = {} # 暂时没想好什么处理 break elif len(obs_list) >= BATCH_SIZE: # print('start train') fighter_model.learn(obs_list, en_obs_list, next_obs_list, next_en_obs_list, memory_mask1_list, memory_mask2_list, action_mask_list, reward_list, action_head0_list, action_head1_list, action_head2_list, probs_head0_list, logits_head1_list, probs_head2_list, behavior_value_list) train_epoch += 1 time_to_save = True # summ = fighter_model.output_summ() # fighter_model.train_writer.add_summary(summ, step_cnt)