コード例 #1
0
ファイル: cow_agent.py プロジェクト: NujjA/Cow-Herding
    def move(self):
        possible_steps = movement_control.find_empty_location(self.pos, self.model)
        
        #select new position based on cow behavior algorithm from Multi Agent Programming Contest
        new_position = self.cow_behavior(possible_steps)

        if (new_position is not None):
            self.model.grid.move_agent(self, new_position)
コード例 #2
0
    def move(self):
        prev_pos = self.pos
        #possible_steps = movement_control.find_empty_location(self.pos, self.model)
        #new_position = random.choice(possible_steps)
        #self.model.grid.move_agent(self, new_position)

        if (self.current_plan_step == self.LOOKFORCOW):
            print("finding cow to follow")
            self.cow_to_follow = self.find_free_cow_in_radius()
            if (self.cow_to_follow is not None):
                print("found cow")
                self.current_plan_step = self.HERDCOW
            else:
                # Move randomly to find a cow
                print("no cow here, keep looking")
                possible_steps = movement_control.find_empty_location(
                    self.pos, self.model)
                new_position = random.choice(possible_steps)
                self.model.grid.move_agent(self, new_position)
        elif (self.current_plan_step == self.HERDCOW):

            if not self.cow_to_follow:  # if no cow to follow, go back to looking
                self.current_plan_step = self.LOOKFORCOW
            elif self.cow_to_follow.pos in self.model.goalState:  # cow is already in goal
                print("cow is in the goal - resetting")
                self.current_plan_step = self.LOOKFORCOW
                self.cow_to_follow = None
            else:
                # herd the cow!
                print("herding cow")
                self.move_to_herding_location()

        if prev_pos == self.pos:  # if hasn't moved, might be stuck, move randomly
            possible_steps = movement_control.find_empty_location(
                self.pos, self.model)
            new_position = random.choice(possible_steps)
            self.model.grid.move_agent(self, new_position)
コード例 #3
0
 def move(self):
     possible_steps = movement_control.find_empty_location(
         self.pos, self.model)
     new_position = random.choice(possible_steps)
     #print(self.unique_id, " moving from ", self.pos, " to ", new_position)
     self.model.grid.move_agent(self, new_position)
コード例 #4
0
    def move(self):
        # if was following a cow and it entered the goal, stop following it
        if(self.cow_to_follow):
            if self.cow_to_follow.pos in self.model.goalState: # cow is already in goal
                print("followed cow is in the goal - resetting")
                self.cow_to_follow = None
                self.need_new_action = True
            
        if(self.need_new_action): 
            print("Picking a new action")
            possible_actions = []
            # decide possible actions
            # if not at the goal space
            #if(not((self.pos[0] == self.model.goalTarget[0]) and (self.pos[1] == self.model.goalTarget[1]))):
            #    possible_actions.append(self.TOWARDSGOAL)

            # if have a target cow and more than one cow in the radius
            neighbors = self.model.grid.get_neighbors(self.pos, moore = True, include_center=False, radius= self.vision_radius)
            cows_in_radius = cow_methods.find_neighbor_cows(neighbors)
            if(self.cow_to_follow and (len(cows_in_radius) > 0)):
                possible_actions.append(self.HERDTOGETHER)
                
            # if have a target cow, can head towards it, flank it, or herd it towards the goal
            if(self.cow_to_follow):
                # possible_actions.extend([self.TOWARDSCOW, self.FLANK, self.HERDCOW, self.BLOCK])
                possible_actions.extend([self.TOWARDSCOW, self.FLANK, self.HERDCOW]) # Remove block if cows can't escape
            else: #no cow to follow, find one
                possible_actions.append(self.LOOKFORCOW)
        
            # Choose an action
            print("possible actions are ", possible_actions)
            self.current_plan_step = random.choice(possible_actions)
        
        if (self.current_plan_step == self.LOOKFORCOW): # Find a cow
            print("finding cow to follow")
            self.cow_to_follow = self.find_free_cow_in_radius()
            if (self.cow_to_follow is not None):
                print("found cow")
                #self.current_plan_step = self.HERDCOW
                self.need_new_action = True
            else:
                # Move randomly to find a cow
                print("no cow here, keep looking")
                possible_steps = movement_control.find_empty_location(self.pos, self.model)
                new_position = random.choice(possible_steps)
                self.model.grid.move_agent(self, new_position)
        elif (self.current_plan_step == self.HERDCOW): # herd a cow towards the goal
            print("herding cow to goal")
            if not self.cow_to_follow: # if no cow to follow
                print("no cow to follow")
                #self.current_plan_step = self.LOOKFORCOW
                self.need_new_action = True
            elif self.cow_to_follow.pos in self.model.goalState: # cow is already in goal
                print("cow is in the goal - resetting")
                #self.current_plan_step = self.LOOKFORCOW
                self.cow_to_follow = None
                self.need_new_action = True
            else:
                # herd the cow
                print("herding cow")
                self.move_to_herding_location()
        elif (self.current_plan_step == self.FLANK):
            print("flanking cow")
            if not self.cow_to_follow: # if no cow to follow
                print("no cow to flank")
                self.need_new_action = True
            if (random.randint(0, 4) == 0): # don't want to flank forever
                print("not flanking anymore")
                self.need_new_action = True
            else:
                self.flank_cow()

        elif (self.current_plan_step == self.HERDTOGETHER):
            print("herding cows together")
            if not self.cow_to_follow: # if no cow to follow
                print("no cow to herd")
                self.need_new_action = True
            elif not self.cow_to_herd_towards:
                print("CHOOSE SECONDARY COW")
                self.choose_secondary_cow()
                if not self.cow_to_herd_towards:
                    self.need_new_action = True
                self.herd_cows_together()
            else:
                self.herd_cows_together()
        elif (self.current_plan_step == self.TOWARDSCOW): #take a step towards a cow
            print("one step towards cow")
            if not self.cow_to_follow: # if no cow to follow
                self.need_new_action = True
            else:
                prev_pos = self.pos
                movement_control.move_towards(self, self.cow_to_follow.pos)
                if(self.pos == prev_pos): # tried to move towards the cow but it didn't work
                    print("Can't move towards cow, moving randomly")
                    possible_steps = movement_control.find_empty_location(self.pos, self.model)
                    new_position = random.choice(possible_steps)
                self.need_new_action = True

        elif (self.current_plan_step == self.BLOCK):
            # don't want to block forever, chance to break out of block
            if (random.randint(0, 4) == 0):
                print("not blocking anymore")
                self.need_new_action = True
            else:
                print("block")
                self.block()
                
        elif (self.current_plan_step == self.TOWARDSGOAL):
            # if not at the goal space
            if((self.pos[0] == self.model.goalTarget[0]) and (self.pos[1] == self.model.goalTarget[1])):
               print("already at goal")
               self.need_new_action = True
            else:
                print("moving towards goal")
                movement_control.move_towards(self, self.model.goalTarget)
        else:
            print("No more available actions, choose again")
            self.need_new_action = True