示例#1
0
    def test_neighborhood_check(self):
        """
        Test for is_neighbor function in utils.py
        :return:
        """
        #positive examples
        points = []
        for x in range(0, 10):
            for y in range(0, 10):
                points.append(global_defs.Point2D(x, y))

        neighbors = []
        for point in points:
            curr_neighbors = []
            for move in global_defs.MOVES:
                curr_neighbors.append(point + move)
            neighbors.append(curr_neighbors)

        not_neighbors = []
        for point in points:
            curr_not_neighbors = []
            for move in global_defs.MOVES:
                curr_not_neighbors.append(point + 2 * (move) + 2)
            not_neighbors.append(curr_not_neighbors)

        for neighbor_set, point in zip(neighbors, points):
            for neighbor in neighbor_set:
                self.assertTrue(utils.is_neighbor(neighbor, point))
                self.assertTrue(utils.is_neighbor(point, neighbor))

        for neighbor_set, point in zip(not_neighbors, points):
            for neighbor in neighbor_set:
                #pdb.set_trace()
                self.assertFalse(utils.is_neighbor(neighbor, point))
                self.assertFalse(utils.is_neighbor(point, neighbor))
示例#2
0
    def test_immediate_neighbor(self):
        for i in range(100):
            with self.subTest(i=i):
                start_position = np.array(
                    [np.random.randint(1, 7),
                     np.random.randint(1, 7)])
                p = np.random.randint(0, 1)
                goal_position = np.power(-1, np.random.randint(
                    1, 2)) * np.array([p, 1 - p]) + start_position
                #print(goal_position,start_position)
                obstacles = []
                start_position = global_defs.Point2D(start_position[0],
                                                     start_position[1])
                goal_position = global_defs.Point2D(goal_position[0],
                                                    goal_position[1])
                # begin_time = time.time()
                a = astar(start_position, goal_position, obstacles, 10, False)
                # time_array.append(time.time() - begin_time)
                res = a.find_minimumpath()
                a.print_path(res)

                # pdb.set_trace()
                self.assertTrue(
                    utils.is_neighbor(start_position, goal_position))
                self.assertEqual(res[0], 1)
示例#3
0
    def test_immediate_neighbor(self):
        """
        Testing ASTAR when the start and end are immediate neighbors of each other. Edge Case.
        :return:
        """
        for i in range(100):
            with self.subTest(i=i):
                start_position = np.array(
                    [np.random.randint(1, 7),
                     np.random.randint(1, 7)])
                p = np.random.randint(0, 1)
                goal_position = np.power(-1, np.random.randint(
                    1, 2)) * np.array([p, 1 - p]) + start_position
                #print(goal_position,start_position)
                obstacles = []
                start_position = (start_position[0], start_position[1])
                goal_position = (goal_position[0], goal_position[1])
                # begin_time = time.time()
                a = astar(start_position, goal_position, obstacles, 10, False)
                # time_array.append(time.time() - begin_time)
                res = a.find_minimumpath()

                # pdb.set_trace()
                start_position = global_defs.Point2D(start_position[0],
                                                     start_position[1])
                goal_position = global_defs.Point2D(goal_position[0],
                                                    goal_position[1])
                self.assertTrue(
                    utils.is_neighbor(start_position, goal_position))
                self.assertEqual(res[0], 1)
                self.assertTrue(len(res[1]) == 2)
    def test_agent_respond_sanity(self):
        """
        Testing that agent probabilites always sum to one and that the agent always takes actions within bounds.
        :return:
        """

        for i in range(1000):
            with self.subTest(i=i):
                random_pos_array = np.random.randint(
                    0, 10, (20, 2))  #Generating 20 random locations
                random_pos_list = [
                    global_defs.Point2D(ele[0], ele[1])
                    for ele in random_pos_array
                ]
                a = agent_random.agent_random(
                    global_defs.Point2D(random_pos_list[0][0],
                                        random_pos_list[0][1]))

                allPos = random_pos_list
                myInd = 0
                loadIndices = range(4, 8)
                random_observation = global_defs.obs(allPos, myInd,
                                                     loadIndices)

                (action_probs, action_idx) = a.respond(random_observation)

                self.assertTrue(len(action_probs) == 6)
                np.testing.assert_approx_equal(np.sum(action_probs), 1)
                self.assertTrue(action_idx < 6)
                print(action_probs, action_idx)
                if action_idx == global_defs.Actions.LOAD:
                    is_neighbor = False
                    for loadidx in (loadIndices):
                        loadPos = allPos[loadidx]
                        is_neighbor = is_neighbor or utils.is_neighbor(
                            loadPos, a.pos)
                    self.assertTrue(is_neighbor)

                is_neighbor = False
                for loadidx in (loadIndices):
                    loadPos = allPos[loadidx]
                    is_neighbor = is_neighbor or utils.is_neighbor(
                        loadPos, a.pos)
                if is_neighbor:
                    msg_str = "Is neighbor {} {}".format(
                        a.pos, [allPos[i] for i in range(4, 8)])
                    self.assertTrue(action_probs[-1] > 0, msg=msg_str)
示例#5
0
    def _proposal_check(self, agent_idx: int, proposal: tuple) -> bool:
        """
        Intended to check if this particular agent's proposal is acceptable or not.
        :param agent_idx: The agent_idx that suggested this proposal for itself
        :param proposal: The proposal  - (action_probs,action_idx)
        :return Yes/No: bool a response indicating yes/no.
        """

        action_probs, action_idx = proposal
        if debug:
            if action_idx > 5 or action_idx < 0:
                pdb.set_trace()

        if action_idx == global_defs.Actions.NOOP:
            #Approve NOOP always.
            decision = True
            return True

        elif action_idx == global_defs.Actions.LOAD:
            #Approve a load decision if it is near an station.

            for sttn_pos in self.sttn_pos:
                if utils.is_neighbor(self.agents[agent_idx].pos, sttn_pos):
                    #If it neighbors any of the stations, then return True
                    decision = True
                    return decision

            #It means it isn't a neighbor to any station.
            decision = False
            return False

        else:
            #Look what can be approved.
            action_result = self.agents[agent_idx].pos + (
                global_defs.ACTIONS_TO_MOVES[action_idx])

            #Check it isn't moving into a invalid location. Part 1: Stations
            for sttn_pos in self.sttn_pos:
                if sttn_pos == action_result:
                    decision = False
                    return decision

            #Check it isn't moving into a invalid location. Part 2: Agents
            for agent in self.agents:
                if agent.pos == action_result:
                    decision = False
                    return decision

            #Allclear, send a yes.
            decision = True
            return decision
示例#6
0
    def respond(self,obs):
        """
        #First retrieve which station is next.
          -Go to knowledge and ask which one we are working on right now and what's the source. If we have it from QA, then skip inference.
          -If we have it from QA, perform inference, and get the latest estimate.
        #Then navigate.
          - If we have the tool, simply go forward. 
          - Else, go to get the tool first. 
        """
        if obs.timestep == 0:
            #If it's the first timestep, we have no clue. Since we don't even know if we are going to ask questions in the
            #future, we go ahead and init the inference engine for future use.
            self.p_obs = copy.deepcopy(obs)
            self.tracking_stations = self.get_remaining_stations(obs)
            self.inference_engine = inference_engine(self.tracking_agent,self.tracking_stations)
            #And set the knowledge source to inference so the next step we know where to look for in the upcoming step.
            self.knowledge.source[0] = ORIGIN.Inference

            #And pick a target station at random since we have to move forward.
            target_station = np.random.choice(self.tracking_stations) #pick a station at random.

        else:
            curr_k_id = self.knowledge.get_current_job_station_id()

            #Checking what knowledge we have.
            if (self.knowledge.source[curr_k_id]==ORIGIN.Answer):
                #Then we simply work on the station because we have an answer telling us that that's the station to work on.
                target_station = self.knowledge.station_order[curr_k_id]

            elif (self.knowledge.source[curr_k_id] == None):
                #which means we just finished a station in the last time-step. This calls for re-initalizing the inference_engine
                self.tracking_stations = self.get_remaining_stations(obs)
                self.inference_engine = inference_engine(self.tracking_agent,self.tracking_stations)
                target_station = np.random.choice(self.tracking_stations)

            elif (self.knowledge.source[curr_k_id]==ORIGIN.Inference):
                #Which means we have been working on a inference for a station.
                target_station = self.inference_engine.inference_step(self.p_obs,obs)
                self.knowledge.update_knowledge_from_inference(target_station)
                warnings.WarningMessage("Provision resetting inference_engine when a station is finished")

            else:
                #it should never come to this.
                raise Exception("Some mistake around")

        """
        Okay, now that we know which station we should be headed to, we need to ensure the nitty-gritty details.
        Do we have a tool?
             If yes,
                if it matches our target station:
                     destination: station
                else:
                     destination: base
        else:
             destination: base
       
        Are we near our destination?
             Yes:
                Is it the base?
                    Pick up the tool.
                else:
                    execute work action.
             No:
                keep moving. 
        """  

        if self.tool is not None:
            if self.tool == target_station:
                destination = obs.allPos[obs.stationIndices[target_station]]
            else:
                destination = global_defs.TOOL_BASE
        else:
            destination = global_defs.TOOL_BASE

        if utils.is_neighbor(self.pos,destination):
            if destination == global_defs.TOOL_BASE:
                #We are at the base to pick up a tool.
                desired_action = global_defs.Actions.NOOP
                self.tool = target_station
            else:
                #we are the station to work.
                desired_action = global_defs.Actions.WORK
        else:
            #Navigate to destination.
            desired_action = None

        obstacles = copy.deepcopy(obs.allPos).remove(self.pos)
        proposal = utils.generate_proposal(self.pos,destination,obstacles,desired_action)
        return proposal