コード例 #1
0
    def run(self, num_episodes, speed):
        """对战调度程序"""
        # 启动仿真环境, 与服务端建立rpyc连接
        self._start_env()
        self.env_client = connect_loop(self.env_manager.get_server_port())

        self.env_client.take_action(
            [EnvCmd.make_simulation("SPEED", "", speed)])

        f = open("state.json", "w")
        battle_results = [0, 0, 0]  # [红方获胜局数, 平局数量, 蓝方获胜局数]
        for i in range(num_episodes):
            num_frames = 0
            self._reset()
            self._run_env()

            while True:
                try:
                    num_frames += 1
                    observation = self._get_observation()  # 获取态势
                    print(i + 1, observation['sim_time'])
                    print_info(observation['red']['units'])
                    print()

                    done = self._get_done(observation)  # 推演结束(分出胜负或达到最大时长)
                    if len(observation['red']['rockets']) > 0:
                        # 写入所得的json会在同一行里, 打开文件后按Ctrl+Alt+L可自动转换成字典格式
                        f.write(json.dumps(observation, ensure_ascii=False))

                    self._run_agents(observation)  # 发送指令

                    if done[0]:  # 对战结束后环境重置
                        # 统计胜负结果
                        if done[1] == 0 or done[2] == 0:
                            battle_results[1] += 1
                        if done[1] == 1:
                            battle_results[0] += 1
                        if done[2] == 1:
                            battle_results[2] += 1
                        # 环境重置
                        self.env_manager.reset()
                        self.env_client = connect_loop(
                            self.env_manager.get_server_port())
                        self.env_client.take_action(
                            [EnvCmd.make_simulation("SPEED", "", speed)])
                        break
                    self._run_env()
                except Exception as e:
                    print(e)
                    print("容器运行出现异常需要重启")
                    self._start_env()
                    self.env_client = connect_loop(
                        self.env_manager.get_server_port())
                    self.env_client.take_action(
                        [EnvCmd.make_simulation("SPEED", "", speed)])
                    break
        # 关闭文件
        f.close()
        return battle_results
コード例 #2
0
 def _run_env(self, request_period=5):
     new_time = self.env_client.get_time()
     self.env_client.take_action([EnvCmd.make_simulation("RESUME", "", "")])
     while new_time - self.last_time < request_period:
         time.sleep(0.01)
         new_time = self.env_client.get_time()
     self.last_time = new_time
     self.env_client.take_action(
         [EnvCmd.make_simulation("PAUSE", "", new_time)])
コード例 #3
0
 def _takeoff_areahunt(num, area_hunt_point):
     print("红方-轰炸机 执行-起飞区域突击-指令!起飞数量为: {0}!".format(str(num)))
     return [
         EnvCmd.make_takeoff_areahunt(RED_AIRPORT_ID, num, 270, 80,
                                      *area_hunt_point,
                                      *[270, 1000, 1000, 160])
     ]
コード例 #4
0
 def _takeoff_areapatrol(num, lx, patrol_point, patrol_params):
     print("红方-执行-起飞区域巡逻-指令!起飞数量为: {0}!, 起飞战机类型为: {1}!".format(
         str(num), str(lx)))
     return [
         EnvCmd.make_takeoff_areapatrol(RED_AIRPORT_ID, num, lx,
                                        *patrol_point, *patrol_params)
     ]
コード例 #5
0
 def _make_awcs_mode(self_id, mode):
     y_mode = ''
     if mode == 0:
         y_mode = "红方预警机-对空-探测"
     elif mode == 1:
         y_mode = "红方预警机-对海-探测"
     elif mode == 2:
         y_mode = "红方预警机-海空交替-探测"
     return [EnvCmd.make_awcs_mode(self_id, mode)]
コード例 #6
0
    def reset(self):
        while True:
            self.env_manager.stop_docker()
            # time.sleep(random.randint(0, 20))
            self.env_manager.start_docker(self.volume_list)
            # time.sleep(10)

            success = self.env_client.connect_server()
            if success == True:
                self.env_client.take_action(
                    [EnvCmd.make_simulation("SPEED", "", self.sim_speed)])
                self.last_time = 0
                break
        self._run_env()
        ob = self.env_client.get_observation()
        return ob
コード例 #7
0
 def _ship_areapatrol(self, self_id, point):
     return [
         EnvCmd.make_ship_areapatrol(self_id, *point, 180, 20000, 2000, 8,
                                     14400, 0)
     ]
コード例 #8
0
 def _takeoff_targethunt(num, target_id, takeoff_targethunt_params):
     return [
         EnvCmd.make_takeoff_targethunt(BLUE_AIRPORT_ID, num, target_id,
                                        *takeoff_targethunt_params)
     ]
コード例 #9
0
 def _awacs_patrol(self_id, AWACS_PATROL_POINT, AWACS_PATROL_PARAMS):
     return [
         EnvCmd.make_awcs_areapatrol(self_id, *AWACS_PATROL_POINT,
                                     *AWACS_PATROL_PARAMS)
     ]
コード例 #10
0
 def _airattack(unit_id, target_id):
     return [EnvCmd.make_airattack(unit_id, target_id, 1)]
コード例 #11
0
 def _takeoff_areapatrol(num, lx, patrol_point, patrol_params):
     return [
         EnvCmd.make_takeoff_areapatrol(BLUE_AIRPORT_ID, num, lx,
                                        *patrol_point, *patrol_params)
     ]
コード例 #12
0
 def _ship_areapatrol(self, self_id, point):
     return [EnvCmd.make_ship_areapatrol(self_id, *point, *SHIP_PATROL_PARAMS_0)]
コード例 #13
0
ファイル: run.py プロジェクト: gong-159/battle-framework1
    def run(self, num_episodes, speed):
        global blue_obs
        global red_obs
        global obs_time
        global actions_blue
        global actions_red
        global dict_actionToUnit
        global need_pause
        """对战调度程序"""
        # 启动仿真环境, 与服务端建立rpyc连接
        self._start_env()
        self.env_client = connect_loop(self.env_manager.get_server_port())

        self.env_client.take_action([EnvCmd.make_simulation("SPEED", "", speed)])

        f = open("state.json", "w")
        battle_results = [0, 0, 0]  # [红方获胜局数, 平局数量, 蓝方获胜局数]
        for i in range(num_episodes):
            num_frames = 0
            sim_time = 0  # 记录容器或者程序崩溃时的仿真时间
            self._reset()
            self._run_env()

            # 开启记录本轮数据的两个线程
            if self.save_replay:
                data_port = self.env_manager.get_data_port()
                folder = self.replay_dir
                net_todocker_init('127.0.0.1', data_port, self.agents[0].name, self.agents[1].name, i, folder)
            # red_last=[]
            # rockets_lists={}
            # f = True
            while True:
                try:
                    num_frames += 1
                    observation = self._get_observation()  # 获取态势
                    sim_time = observation['sim_time']
                    # print(i + 1, sim_time)
                    # 获取各种推测态势信息
                    obs_time = sim_time
                    # f = True
                    # if sim_time>=300 and sim_time<=310 and f:
                    #     f = False
                    #     print(sim_time)
                    #     # self.env_manager.stop()
                    #     self.env_client.take_action([EnvCmd.make_simulation('PAUSE','', 0)])
                    #     time.sleep(10)
                    #     print('10秒后')
                    #     self.env_client.take_action([EnvCmd.make_simulation('PAUSE', '', -1)])
                    if need_pause:
                        self.env_client.take_action([EnvCmd.make_simulation('PAUSE', '', 0)])
                    red_obs = observation['red']
                    blue_obs = observation['blue']
                    actions_red = self._get_actions_red()
                    actions_blue = self._get_actions_blue()
                    caculate_TaskObject(red_obs) #更新任务-对象字典
                    # rockets_lists = rockets_hitRate(red_obs, red_last, rockets_lists)
                    print_info(observation['red']['units'])

                    done = self._get_done(observation)  # 推演结束(分出胜负或达到最大时长)
                    if len(observation['red']['rockets']) > 0:
                        # 写入所得的json会在同一行里, 打开文件后按Ctrl+Alt+L可自动转换成字典格式
                        f.write(json.dumps(observation, ensure_ascii=False))
                    self._run_agents(observation)  # 发送指令

                    if done[0]:  # 对战结束后环境重置
                        # 统计胜负结果
                        if done[1] == 0 or done[2] == 0:
                            battle_results[1] += 1
                        if done[1] == 1:
                            battle_results[0] += 1
                        if done[2] == 1:
                            battle_results[2] += 1
                        # 环境重置
                        self.env_manager.reset()
                        self.env_client = connect_loop(self.env_manager.get_server_port())
                        self.env_client.take_action([EnvCmd.make_simulation("SPEED", "", speed)])
                        break
                    self._run_env()
                except Exception as e:
                    print(e)
                    print("容器运行出现异常需要重启")
                    # 记录错误信息
                    self.logger.debug("第{}局程序运行{}秒后出错, 错误信息为: {}\n".format(i + 1, sim_time, e))
                    self._start_env()
                    self.env_client = connect_loop(self.env_manager.get_server_port())
                    self.env_client.take_action([EnvCmd.make_simulation("SPEED", "", speed)])
                    break
        # 关闭文件
        f.close()
        return battle_results
コード例 #14
0
 def _ship_areapatrol(self_id, point):
     print("红方-护卫舰-区域巡逻防空!")
     return [
         EnvCmd.make_ship_areapatrol(self_id, *point, *SHIP_PATROL_PARAMS_0)
     ]
コード例 #15
0
 def _disturb_stop(self_id):
     return [EnvCmd.make_disturb_stop(self_id)]
コード例 #16
0
 def _targethunt(self_id, target_id):
     return [EnvCmd.make_targethunt(self_id, target_id, 270, 80)]
コード例 #17
0
 def _areahunt(self_id, point):
     return [EnvCmd.make_areahunt(self_id, 270, 80, *point, *AREA_HUNT_PARAMS)]
コード例 #18
0
 def _uav_areapatrol(uav_id, uav_point, uav_params):
     return [EnvCmd.make_uav_areapatrol(uav_id, *uav_point, *uav_params)]
コード例 #19
0
 def _ship_movedeploy(self, self_id, point):
     return [EnvCmd.make_ship_movedeploy(self_id, *point, 90, 1)]
コード例 #20
0
 def _awacs_escort(self, awacs_team_id):
     return [EnvCmd.make_takeoff_protect(RED_AIRPORT_ID, 8, awacs_team_id, 0, 100, 250)]
コード例 #21
0
 def _areapatrol(unit_id, patrol_point, patrol_params):
     return [EnvCmd.make_areapatrol(unit_id, *patrol_point, *patrol_params)]
コード例 #22
0
 def _disturb_patrol(self, disturb_team_id, patrol_point, patrol_params):
     return [EnvCmd.make_disturb_areapatrol(disturb_team_id, *patrol_point, *patrol_params)]
コード例 #23
0
 def _returntobase(unit_id):
     return [EnvCmd.make_returntobase(unit_id, 20001)]
コード例 #24
0
 def _disturb_linepatrol(self, self_id, point_list):
     return [EnvCmd.make_disturb_linepatrol(self_id, 160, 0, 'line', point_list)]
コード例 #25
0
 def _takeoff_areahunt(num, area_hunt_point, area_hunt_area):
     return [
         EnvCmd.make_takeoff_areahunt(20001, num, 90, 100, *area_hunt_point,
                                      *area_hunt_area)
     ]
コード例 #26
0
 def _takeoff_areahunt(num, area_hunt_point):
     return [EnvCmd.make_takeoff_areahunt(RED_AIRPORT_ID, num, 270, 80, *area_hunt_point, *[270, 1000, 1000, 160])]
コード例 #27
0
 def _A2G_escort(self, a2g_team_id):
     return [
         EnvCmd.make_takeoff_protect(BLUE_AIRPORT_ID, 2, a2g_team_id, 1,
                                     100, 250)
     ]
コード例 #28
0
 def _disturb_escort(self, disturb_team_id):
     return [EnvCmd.make_takeoff_protect(RED_AIRPORT_ID, 2, disturb_team_id, 1, 100, 250)]
コード例 #29
0
ファイル: run.py プロジェクト: maiwen/coach
    def run(self, num_episodes, speed):
        """对战调度程序"""
        # 启动仿真环境, 与服务端建立rpyc连接
        self._start_env()
        self.env_client = connect_loop(self.env_manager.get_server_port())

        self.env_client.take_action(
            [EnvCmd.make_simulation("SPEED", "", speed)])

        f = open("state.json", "w")
        battle_results = [0, 0, 0]  # [红方获胜局数, 平局数量, 蓝方获胜局数]
        for i in range(num_episodes):
            num_frames = 0
            sim_time = 0  # 记录容器或者程序崩溃时的仿真时间
            self._reset()
            self._run_env()

            # 开启记录本轮数据的两个线程
            if self.save_replay:
                data_port = self.env_manager.get_data_port()
                folder = self.replay_dir
                net_todocker_init('127.0.0.1', data_port, self.agents[0].name,
                                  self.agents[1].name, i, folder)

            while True:
                try:
                    num_frames += 1
                    observation = self._get_observation()  # 获取态势
                    sim_time = observation['sim_time']
                    print(i + 1, sim_time)
                    print_info(observation['red']['units'])
                    print()

                    done = self._get_done(observation)  # 推演结束(分出胜负或达到最大时长)
                    if len(observation['red']['rockets']) > 0:
                        # 写入所得的json会在同一行里, 打开文件后按Ctrl+Alt+L可自动转换成字典格式
                        f.write(json.dumps(observation, ensure_ascii=False))

                    self._run_agents(observation)  # 发送指令

                    if done[0]:  # 对战结束后环境重置
                        # 统计胜负结果
                        if done[1] == 0 or done[2] == 0:
                            battle_results[1] += 1
                        if done[1] == 1:
                            battle_results[0] += 1
                        if done[2] == 1:
                            battle_results[2] += 1
                        # 环境重置
                        self.env_manager.reset()
                        self.env_client = connect_loop(
                            self.env_manager.get_server_port())
                        self.env_client.take_action(
                            [EnvCmd.make_simulation("SPEED", "", speed)])
                        break
                    self._run_env()
                except Exception as e:
                    print(e)
                    print("容器运行出现异常需要重启")
                    # 记录错误信息
                    self.logger.debug("第{}局程序运行{}秒后出错, 错误信息为: {}\n".format(
                        i + 1, sim_time, e))
                    self._start_env()
                    self.env_client = connect_loop(
                        self.env_manager.get_server_port())
                    self.env_client.take_action(
                        [EnvCmd.make_simulation("SPEED", "", speed)])
                    break
        # 关闭文件
        f.close()
        return battle_results
コード例 #30
0
 def _takeoff_areapatrol(num, lx, patrol_point, patrol_params):
     # patrol_params为5个参数
     return [EnvCmd.make_takeoff_areapatrol(RED_AIRPORT_ID, num, lx, *patrol_point, *patrol_params)]