Ejemplo n.º 1
0
    def reset(self):
        self._destroy()
        self.reward = 0.0
        self.prev_reward = 0.0
        self.t = 0.0

        random_index = random.randint(0, 23)
        #random_index = 5
        init_pos_0 = self.__pos_safe[random_index]
        #init_pos_1 = self.__pos_safe[9]
        init_pos_1 = self.__pos_safe[random.choice(
            self.__id_pos_linked[random_index])]
        print(init_pos_0, init_pos_1)

        self.__R1 = Robot(self.__world, 0, init_pos_0, ID_R1)
        self.__B1 = Robot(self.__world, 0, init_pos_1, ID_B1)
        self.__robots = [self.__R1, self.__B1]

        self.__obstacle = ICRALayout(self.__world)
        self.__projectile = Projectile(self.__world)
        self.__area_buff = AreaBuff()
        self.__area_supply = AreaSupply()

        self.state = [RobotState(init_pos_0), RobotState(init_pos_1)]
        self.actions = [Action(), Action()]

        self.reward = 0

        return init_pos_1
Ejemplo n.º 2
0
    def reset(self):
        self._destroy()
        self.reward = [0.0]*self.red_agent_num
        self.prev_reward = [0.0]*self.red_agent_num
        self.t = 0.0

        
        random_index = random.sample(range(len(self.__pos_safe)-1),self.red_agent_num + self.blue_agent_num)

        init_pos_0 = self.__pos_safe[random_index[0]]
        init_pos_1 = self.__pos_safe[random_index[1]]
        init_pos_2 = self.__pos_safe[random_index[2]]

        self.__R1 = Robot(self.__world, 0, init_pos_0, ID_R1)
        self.__R2 = Robot(self.__world, 0, init_pos_1, ID_R2)
        self.__B1 = Robot(self.__world, 0, init_pos_2, ID_B1)

        self.__robots = [self.__R1, self.__R2, self.__B1]
        self.__obstacle = ICRALayout(self.__world)
        self.__projectile = Projectile(self.__world)
        '''
        self.__area_buff = AreaBuff()
        self.__area_supply = AreaSupply()
        '''
        self.red_agent[0].pos = init_pos_0
        self.red_agent[1].pos = init_pos_1
        self.blue_agent[0].pos = init_pos_2
        #self.state.append([self.red_agent, self.blue_agent])
        self.state = []
        self.state = self.__concat_state()
        #self.actions = [Action(), Action()]

        #self.reward.append(0.0)

        return self.state
Ejemplo n.º 3
0
    def reset(self):
        self._destroy()
        self.reward = 0.0
        self.prev_reward = 0.0
        self.t = 0.0

        random_index = random.randint(0, 23)
        #random_index = 5
        init_pos_0 = self.__pos_safe[random_index]
        #init_pos_1 = self.__pos_safe[9]
        init_pos_1 = self.__pos_safe[random.choice(
            self.__id_pos_linked[random_index])]
        #print(init_pos_0, init_pos_1)

        self.__R1 = Robot(self.__world, 0, init_pos_0, ID_R1)
        self.__B1 = Robot(self.__world, 0, init_pos_1, ID_B1)
        self.__robots = [self.__R1, self.__B1]

        self.__obstacle = ICRALayout(self.__world)
        self.__projectile = Projectile(self.__world)
        '''
        self.__area_buff = AreaBuff()
        self.__area_supply = AreaSupply()
        '''
        self.red_agent.pos = init_pos_0
        self.blue_agent.pos =init_pos_1
        self.state = [self.red_agent, self.blue_agent]
        self.state = self.__concat_state()
        #self.actions = [Action(), Action()]

        self.reward = 0

        return self.state[0:self.state_size],self.state[self.state_size:2*self.state_size]
    def _autoaim(self, robot: Robot, state: RobotState):
        #detected = {}
        if robot.robot_id == ID_R1:
            self.o_dis = 8.0
        scan_distance, scan_type = [], []
        state.detect = False
        type_ = None
        for i in range(-135, 135, 2):
            angle, pos = robot.get_angle_pos()
            angle += i / 180 * math.pi
            p1 = (pos[0] + 0.374 * math.cos(angle),
                  pos[1] + 0.374 * math.sin(angle))
            p2 = (pos[0] + SCAN_RANGE * math.cos(angle),
                  pos[1] + SCAN_RANGE * math.sin(angle))
            self.__world.RayCast(self.__callback_autoaim, p1, p2)
            scan_distance.append(self.__callback_autoaim.fraction)
            #print(f"ang {i}\tfraction:{self.__callback_autoaim.fraction}\t")
            u = self.__callback_autoaim.userData
            # print(f"u type {u.type}")
            # if abs(i) == 45 and robot.robot_id == ID_R1:
            #     print(f"angle:{i}, frac:{self.__callback_autoaim.fraction}")
            # FIXME 射线扫描距离检测
            if robot.robot_id == ID_R1 and u is not None:
                if u.type == 'wall':
                    if self.o_dis > self.__callback_autoaim.fraction * (
                            SCAN_RANGE - 0.374):
                        self.o_dis = self.__callback_autoaim.fraction * (
                            SCAN_RANGE - 0.374)
                        self.o_ang = i
                        #print("wall", self.o_dis)
                if u.type == 'robot':
                    d = self.__callback_autoaim.fraction * (SCAN_RANGE -
                                                            0.374) - 0.10
                    if self.o_dis > d:
                        self.o_dis = d
                        #print("robot", self.o_dis)
                        self.o_ang = i

            if u is not None and u.type == "robot":
                if u.id % 2 == robot.robot_id % 2:
                    scan_type.append(0)
                    continue
                else:
                    scan_type.append(1)
                if -45 <= i <= 45:
                    #if u.id not in [ID_R1, ID_R2]:
                    if u.id % 2 != robot.robot_id % 2:
                        if not state.detect:
                            robot.set_gimbal(angle)
                            state.detect = True
            else:
                scan_type.append(0)
        # FIXME 距离障碍物太近的时候惩罚, 但是由于扫面半径起点不是刚好是车的外表, 可能这个不加更好
        # if min(scan_distance) * (SCAN_RANGE-0.374) <= 0.005:
        #     self.conflict = True

        state.scan = [scan_distance, scan_type]
    def reset(self):
        self._destroy()
        self.reward = 0.0
        self.prev_reward = 0.0
        self.t = 0.0

        random_index = random.randint(0, 23)
        #random_index = 5
        init_pos_0 = self.__pos_safe[random_index]
        #init_pos_1 = self.__pos_safe[9]
        init_pos_1 = self.__pos_safe[random.choice(
            self.__id_pos_linked[random_index])]
        while (init_pos_1[0] < 1.0):
            init_pos_1 = self.__pos_safe[random.randint(0, 23)]

        # 新添加的位置
        init_pos_2 = self.__pos_safe[random.randint(0, 23)]
        init_pos_3 = self.__pos_safe[random.randint(0, 23)]
        while (init_pos_3[0] < 1.0):
            init_pos_3 = self.__pos_safe[random.randint(0, 23)]
        # print(init_pos_0, init_pos_1)

        self.__R1 = Robot(self.__world,
                          0, [0.5, 4.0],
                          ID_R1,
                          color_=(0.6, 0.2, 0.6))
        self.__B1 = Robot(self.__world, 0, init_pos_1, ID_B1)
        # 增加两个车
        self.__R2 = Robot(self.__world, 0, [0.5, 0.5], ID_R2)
        self.__B2 = Robot(self.__world, 0, init_pos_3, ID_B2)
        print([0.5, 4.5], init_pos_1, [0.5, 0.5], init_pos_3)
        self.__robots = [self.__R1, self.__B1, self.__R2, self.__B2]

        self.__obstacle = ICRALayout(self.__world)
        self.__projectile = Projectile(self.__world)
        self.__area_buff = AreaBuff()
        #self.__area_supply = AreaSupply()

        self.state = [
            RobotState([0.5, 4.4]),
            RobotState(init_pos_1),
            RobotState([0.5, 0.5]),
            RobotState(init_pos_3)
        ]
        self.actions = [Action(), Action(), Action(), Action()]

        self.reward = 0

        return init_pos_1, init_pos_3
Ejemplo n.º 6
0
 def detect(self, robot: Robot, t_now):
     if (robot.buff_left_time > 0):
         robot.buff_left_time -= t_now - self.__t_last
     robot.buff_left_time = max(0, robot.buff_left_time)
     for buff in self.__buff_area:
         buff.detect(robot, t_now)
     self.__t_last = t_now
Ejemplo n.º 7
0
 def detect(self, robot: Robot, t_now):
     if (robot.buff_left_time > 0):
         robot.buff_left_time -= t_now - self.__t_last
     robot.buff_left_time = max(0, robot.buff_left_time)
     # if robot.buff_left_time == 0:
     #     print("punish pass once")
     for buff in self.__buff_area:
         buff.detect(robot, t_now)
     self.__t_last = t_now
Ejemplo n.º 8
0
    def _blue_step_action(self, robot: Robot, blue_agent: blueAgent):
        # gas, rotate, transverse, rotate cloud terrance, shoot
        robot.move_ahead_back(blue_agent.v_t)
        robot.move_left_right(blue_agent.v_n)
        robot.turn_left_right(blue_agent.angular)

        '''
        if int(self.t * FPS) % (60 * FPS) == 0:
            robot.refresh_supply_oppotunity()
        if action.supply > 0.99:
            action.supply = 0.0
            if robot.if_supply_available():
                robot.use_supply_oppotunity()
                if self.__area_supply.if_in_area(robot):
                    robot.supply()
        '''
        if blue_agent.shoot > 0.99 and int(self.t*FPS) % (FPS/5) == 1:
            if(robot.if_left_projectile()):
                angle, pos = robot.get_gun_angle_pos()
                robot.shoot()
                blue_agent.shoot = 0.0
                self.__projectile.shoot(robot,angle, pos)
Ejemplo n.º 9
0
    def if_in_area(self, robot: Robot):
        if robot.group == "red":
            supply_area = self.supply_area_red
        elif robot.group == "blue":
            supply_area = self.supply_area_blue
        else:
            print("Wrong Input Object in supply area!")
            return False

        pos = robot.get_pos()
        x, y = pos.x, pos.y
        bx, by, w, h = supply_area
        return (bx <= x <= bx + w) and (by <= y <= by + h)
Ejemplo n.º 10
0
    def detect(self, robot: Robot, t_now):
        # 0, 1, 2min refresh
        if (int(t_now) % 60 == 0):
            self.__activated = False
            self.__t_stay = 0

        if self.__activated:
            return

        if robot.group != self.__group:
            return

        if self._if_in_area(robot.get_pos(), self.__box):
            self.__t_stay += t_now - self.__t_last
            if self.__t_stay >= TIME_BUFF_TRIGGER:
                self.__activated = True
                robot.buff_left_time = TIME_BUFF_INIT
                self.__t_stay = 0
        else:
            self.__t_stay = 0

        self.__t_last = t_now
Ejemplo n.º 11
0
 def _autoaim(self, robot: Robot, state: RobotState):
     #detected = {}
     scan_distance, scan_type = [], []
     state.detect = False
     for i in range(-135, 135, 2):
         angle, pos = robot.get_angle_pos()
         angle += i / 180 * math.pi
         p1 = (pos[0] + 0.3 * math.cos(angle),
               pos[1] + 0.3 * math.sin(angle))
         p2 = (pos[0] + SCAN_RANGE * math.cos(angle),
               pos[1] + SCAN_RANGE * math.sin(angle))
         self.__world.RayCast(self.__callback_autoaim, p1, p2)
         scan_distance.append(self.__callback_autoaim.fraction)
         u = self.__callback_autoaim.userData
         if u is not None and u.type == "robot":
             scan_type.append(1)
             if -45 <= i <= 45:
                 if not state.detect:
                     robot.set_gimbal(angle)
                     state.detect = True
         else:
             scan_type.append(0)
     state.scan = [scan_distance, scan_type]
Ejemplo n.º 12
0
 def _red_autoaim(self, robot: Robot,red_agent : redAgent):
     #detected = {}
     scan_distance, scan_type = [], []
     red_agent.detect = False
     for i in range(-30, 30, 1):
         angle, pos = robot.get_angle_pos()
         angle += i/180*math.pi
         p1 = (pos[0] + 0.2*math.cos(angle), pos[1] + 0.2*math.sin(angle))
         p2 = (pos[0] + SCAN_RANGE*math.cos(angle),
               pos[1] + SCAN_RANGE*math.sin(angle))
         self.__world.RayCast(self.__callback_autoaim, p1, p2)
         scan_distance.append(self.__callback_autoaim.fraction)
         u = self.__callback_autoaim.userData
         if u is not None and u.type == "robot":
             scan_type.append(1)
             if not red_agent.detect:
                 #robot.set_gimbal(angle)
                 distance = self.distance_detection()
                 if(distance <4):
                     red_agent.detect = True
         else:
             scan_type.append(0)
     red_agent.scan = scan_distance+scan_type
Ejemplo n.º 13
0
    def detect(self, robot: Robot, t_now):
        # 0, 1, 2min refresh
        bool_t = (t_now > 3.00 - 0.05
                  and t_now < 3.00 + 0.05) and (t_now > 120.00 - 0.05
                                                and t_now < 120.00 + 0.05)
        if (int(t_now) % 60 == 0):
            # if t_now == 5.00 or t_now == 60.00:
            # if bool_t:
            self.__activated = False
            self.__t_stay = 0
            self.__type = choice(TYPE)
            # print(f'time {t_now}')
            # print(f"buff type - 2 {self.__type}")

        if self.__activated:
            return

        if robot.group != self.__group and self.__type < 1:
            return

        if self._if_in_area(robot.get_pos(), self.__box):
            self.__t_stay += t_now - self.__t_last
            if self.__t_stay >= TIME_BUFF_TRIGGER:
                self.__activated = True
                if self.__type == 0:
                    robot.lose_health(-1000)
                    return
                if self.__type == 1:
                    robot.supply()
                    return
                robot.buff_left_time = PUNISH_TIME
                robot.buff_type = self.__type
                self.__t_stay = 0
        else:
            self.__t_stay = 0

        self.__t_last = t_now
Ejemplo n.º 14
0
 def _update_blue_robot_state(self, robot: Robot, blue_agent: blueAgent):
     blue_agent.pos = robot.get_pos()
     blue_agent.health = robot.get_health()
     blue_agent.angle = robot.get_angle()
Ejemplo n.º 15
0
 def _update_red_robot_state(self, robot: Robot, red_agent: redAgent):
     red_agent.pos = robot.get_pos()
     red_agent.health = robot.get_health()
     red_agent.angle = robot.get_angle()
Ejemplo n.º 16
0
 def _update_robot_state(self, robot: Robot, state: RobotState):
     state.pos = robot.get_pos()
     state.health = robot.get_health()
     state.angle = robot.get_angle()
     state.velocity = robot.get_velocity()
     state.angular = robot.get_angular()
 def _step_action(self, robot: Robot, action: Action):
     # gas, rotate, transverse, rotate cloud terrance, shoot
     robot.move_ahead_back(action.v_t)
     robot.turn_left_right(action.angular)
     robot.move_left_right(action.v_n)
     # if int(self.t * FPS) % (60 * FPS) == 0:
     #     robot.refresh_supply_oppotunity()
     # if action.supply > 0.99:
     #     action.supply = 0.0
     #     if robot.if_supply_available():
     #         robot.use_supply_oppotunity()
     #         if self.__area_supply.if_in_area(robot):
     #             robot.supply()
     if action.shoot > 0.99 and int(self.t * FPS) % (FPS / 5) == 1:
         if (robot.if_left_projectile() > 0) and robot.get_health() > 0:
             # print("shoot")
             # FIXME 下面这行条件只允许红方发射子弹
             if robot.robot_id % 2 == ID_R1:
                 angle, pos = robot.get_gun_angle_pos()
                 robot.shoot()
                 self.__projectile.shoot(angle, pos, robot.robot_id)