Пример #1
0
    def clear_path_through_passages_and_cave(self):

        wanted_passages, wanted_cave = self.find_wanted_passages_and_cave()
        #log("wanted_passages: {}, wanted_cave: {}".format(wanted_passages, wanted_cave), "TEST", False)

        can_move = True
        for elm, entrance in wanted_passages + wanted_cave:
            if not self.is_free(elm):
                can_move = False
                break

        if can_move:
            for elm, entrance in wanted_passages + wanted_cave:
                BLACKBOARD.claim(self.id_, elm)
                #TODO: remove claims somewhere!
            return True

        all_locations = []
        #TODO: Maybe the agent should consider taking another route instead of waiting
        for elm, entrance in wanted_passages + wanted_cave:
            all_locations += self.find_area(elm)

        request = Request(self.id_, all_locations)
        box, location = self.unpack_intentions_to_box_and_location()
        if location is not None and location in LEVEL.goals_by_pos:
            request.goal = LEVEL.goals_by_pos[location]
            request.box = box
        BLACKBOARD.add(request, self.id_)

        return False
 def request_get(self, task_id):
     request = Request(req='GET',
                       headers={
                           'Task-id': task_id,
                           'Session': self.session_id
                       })
     return self.__parse(request)
 def request_change_theme(self, path):
     request = Request(req='THEME',
                       headers={
                           "Path": path,
                           'Session': self.session_id
                       })
     return self.__parse(request)
 def request_move(self, task_id, from_index, to_index):
     request = Request(req='MOVE',
                       headers={
                           'Task-id': task_id,
                           'From': from_index,
                           'To': to_index,
                           'Session': self.session_id
                       })
     return self.__parse(request)
 def request_login(self, user, password):
     assert self.logined is False
     verify = utils.get_md5(password)
     request = Request(req='LOGIN',
                       headers={
                           'User': user,
                           'Verify': verify
                       })
     return self.__parse(request)
 def request_update(self, task_json):
     request = Request(req='UPDATE',
                       content=json.dumps(task_json),
                       headers={'Session': self.session_id})
     return self.__parse(request)
 def request_add(self, task_json):
     request = Request(req='ADD',
                       content=json.dumps(task_json),
                       headers={'Session': self.session_id})
     return self.__parse(request)
 def request_list(self):
     request = Request(req='LIST', headers={'Session': self.session_id})
     return self.__parse(request)
 def request_archive(self):
     request = Request(req='ARCHIVE', headers={'Session': self.session_id})
     return self.__parse(request)
Пример #10
0
    def analyse_situation(self):
        log("Starting to analyse situation for agent {}".format(self.id_),
            "ANALYSE", False)
        action = self.current_plan[0]
        if action.action.action_type == ActionType.NoOp:
            return "", None

        # Can/should we do a retreat move?
        rf_loc = action.required_free
        if rf_loc == (self.row, self.col):
            log("self.id_: {}, rf_loc: {}, (self.row, self.col): {}, action: {}"
                .format(self.id_, rf_loc, (self.row, self.col), action))
            self.current_plan = []
            self.remove_intentions_from_blackboard()
            self.wait(1)
        state = self.beliefs

        #Space is occupied
        if not (state.is_free(*rf_loc)):
            #If agent at the position
            #TODO: a more efficient way to find other_agent
            if rf_loc in state.agents:
                log(
                    "Agent {} found agent at desired location {}".format(
                        self.id_, rf_loc), "ANALYSE", False)
                for agent in self.all_agents:
                    if (agent.row, agent.col) == rf_loc:
                        other_agent = agent
                if self.about_to_crash(agent=other_agent):
                    log(
                        "Agent {} thinks it will crash with agent {} and is looking for retreat move"
                        .format(self.id_, other_agent.id_), "ANALYSE", False)
                    return "try retreat", other_agent
                else:
                    if len(
                            other_agent.current_plan
                    ) == 0 or other_agent.current_plan[
                            0].action.action_type == ActionType.NoOp or other_agent.is_next_action_impossible(
                            ):
                        #TODO: it is not supposed to update the plan here! fix!
                        #Try to go around:
                        box, location = self.unpack_intentions_to_box_and_location(
                        )
                        if box is not None and location is not None:
                            simple_plan = self.search_for_simple_plan(
                                self.heuristic, (box, location),
                                ignore_all_other_agents=False)
                            if simple_plan is not None and len(
                                    simple_plan) > 0:
                                self.current_plan = simple_plan
                                if self.sound() and len(self.current_plan) > 1:
                                    log(
                                        "Agent {} thinks agent {} is standing still but found a path around. Path: {}"
                                        .format(self.id_, other_agent.id_,
                                                self.current_plan), "ANALYSE",
                                        False)
                                else:
                                    log(
                                        "Agent {} thinks agent {} is standing still but path found is not sound. Path: {}"
                                        .format(self.id_, other_agent.id_,
                                                self.current_plan), "ANALYSE",
                                        False)
                                    self.wait(1)
                                return "going around agent", other_agent

                        #TODO
                        area_required = [
                            action.required_free
                            for action in self.current_plan[:5]
                            if action.action.action_type != ActionType.NoOp
                        ]
                        request = Request(self.id_, area_required)
                        #TODO: check if request is there!
                        if not BLACKBOARD.request_is_there(self.id_, request):
                            BLACKBOARD.add(request, self.id_)
                            log(
                                "Agent {} added request ({}) to make agent {} move"
                                .format(self.id_, request,
                                        other_agent.id_), "ANALYSE", False)
                        log(
                            "Agent {} is waiting for agent {} to move".format(
                                self.id_, other_agent.id_), "ANALYSE", False)
                        return "wait", None
                    else:
                        log(
                            "Agent {} thinks it can wait for agent {} to pass".
                            format(self.id_,
                                   other_agent.id_), "ANALYSE", False)
                        return "wait", None
            #If box at the location
            if rf_loc in state.boxes:
                log(
                    "Agent {} found box at desired location {}".format(
                        self.id_, rf_loc), "ANALYSE", False)
                box = state.boxes[rf_loc]  #find the box
                moving, other_agent = self.box_on_the_move(box)
                if moving:
                    log(
                        "Agent {} thinks agent {} is moving the box {}".format(
                            self.id_, other_agent, box), "ANALYSE", False)
                    if self.about_to_crash(box=box, agent=other_agent):
                        log(
                            "Agent {} thinks it will crash with agent {} currently moving the box {}"
                            .format(self.id_, other_agent,
                                    box), "ANALYSE", False)
                        return "try retreat", other_agent
                    else:
                        return "wait", None
                else:
                    #Try to go around, if not possible make request
                    #TODO: Try to move around

                    box, location = self.unpack_intentions_to_box_and_location(
                    )
                    if box is not None and location is not None:
                        simple_plan = self.search_for_simple_plan(
                            self.heuristic, (box, location),
                            ignore_all_other_agents=False)
                        if simple_plan is not None and len(simple_plan) > 0:
                            self.current_plan = simple_plan
                            if self.sound() and len(self.current_plan) > 1:
                                log(
                                    "Agent {} thinks box is static but found a path around. Path: {}"
                                    .format(self.id_, self.current_plan),
                                    "ANALYSE", False)
                            else:
                                self.wait(1)
                            return "going around box", other_agent
                    area_required = [
                        action.required_free
                        for action in self.current_plan[:5]
                        if action.action.action_type != ActionType.NoOp
                    ]
                    request = Request(self.id_, area_required)
                    if not BLACKBOARD.request_is_there(self.id_, request):
                        BLACKBOARD.add(request, self.id_)
                        log(
                            "Agent {} added request ({}) to make box {} move".
                            format(self.id_, request, box), "ANALYSE", False)
                    log(
                        "Agent {} is waiting for box {} to move".format(
                            self.id_, box), "ANALYSE", False)
                    return "wait", None
        #When do we get here?
        return "need to replan", None