def wolf_action(agent): global wolves_created prey = get_prey(agent, get_group(SHEEP_GROUP)) if prey is not None: eat(agent, prey) agent["time_to_repr"] -= 1 reproduce(agent, create_wolf, wolves_created, get_group(WOLF_GROUP)) return False
def buyer_action(agent): # how to write this testcase """ This functions lets buyer to decides whether wants to buy a car or not """ print("_" * 20) print("Agent: " + agent.name) agent["age"] += 1 if not agent["has_car"]: my_dealer = get_env().get_neighbor_of_groupX(agent, get_group(DEALER_GRP), hood_size=4) if my_dealer is None: print("No dealers nearby.") elif is_credible(my_dealer, agent): buy_from_dealer(agent, my_dealer) else: print("I found a rotten dealer: ", str(my_dealer)) else: # return False means to move print("I have a car!") agent["car_life"] -= 1 if agent["car_life"] <= 0: agent["has_car"] = False # call strategy function to update the data agent["strategy"]["func"]() print("Agent strategy =", agent["strategy"]) return False
def test_get_group(self): """ Don't worry about using a "real" group: just make sure we get what was added! """ add_group("Group name", "Test") grp = get_group("Group name") self.assertEqual(grp, "Test")
def distribute_coupons(agent): """ Distribute coupons from central bank randomly to each babysitter. Coupons are gaussian distributed based on extra_coupons and extra_dev. """ co_op_members = get_group(CO_OP_MEMBERS) for bbsit in co_op_members: co_op_members[bbsit]['coupons'] += int( gaussian(agent["extra_coupons"], agent["extra_dev"]))
def coop_action(coop_env): global last_period_exchanges global last_period_unemployed sitters = get_sitters(get_group(CO_OP_MEMBERS)) going_out = get_going_out(get_group(CO_OP_MEMBERS)) exchanges = min(len(sitters), len(going_out)) sitter_agents = [agent for agent in sitters] going_out_agents = [agent for agent in going_out] for i in range(exchanges): sitter, outer = sitter_agents[i], going_out_agents[i] sitters[sitter]['coupons'] += 1 going_out[outer]['coupons'] -= 1 last_period_exchanges = exchanges last_period_unemployed = max(len(sitters), len(going_out)) - exchanges return True
def bacterium_action(agent, **kwargs): """ Algorithm: 1) sense env (toxin_level = calc_toxin(toxins, agent)) 2) see if it is worse or better than previous env 3) if worse, change direction (agent["angle"] = new_angle) 4) move (done automatically by returning False) """ if DEBUG: print("I'm " + agent.name + " and I'm hungry.") toxin_level = calc_toxin(get_group(TOXINS), agent) nutrient_level = calc_nutrient( get_group(NUTRIENTS), agent) if agent["prev_toxicity"] is not None: toxin_change = toxin_level - agent["prev_toxicity"] else: toxin_change = sys.maxsize * (-1) if agent["prev_nutricity"] is not None: nutrient_change = nutrient_level - agent["prev_nutricity"] else: nutrient_change = sys.maxsize * (-1) threshold = DEF_THRESHOLD agent["prev_toxicity"] = toxin_level agent["prev_nutricity"] = nutrient_level if (toxin_change > nutrient_change) or (threshold >= toxin_level): if agent["angle"] is None: new_angle = randint(0, 360) else: angle_shift = randint(45, 315) new_angle = agent["angle"] + angle_shift if (new_angle > 360): new_angle = new_angle % 360 agent["angle"] = new_angle # return False means to move return False
def drinker_action(agent): global attendance global attendance_record global agents_decided drinkers = get_group(DRINKERS) non_drinkers = get_group(NON_DRINKERS) changed = True decision = get_decision(agent) agents_decided += 1 if agents_decided == population: attendance_record.append(attendance) if attendance > optimal_occupancy: extras = attendance - optimal_occupancy discourage(extras) agents_decided = 0 attendance = 0 print("Avg attendance so far: ", get_average_attendance(attendance_record)) if decision: attendance += 1 if agent.primary_group() == non_drinkers: changed = False get_env().add_switch(agent, non_drinkers, drinkers) else: if agent.primary_group() == drinkers: changed = False get_env().add_switch(agent, drinkers, non_drinkers) # return False means to move return changed
def central_bank_action(agent): """ If exchanges are down "enough", distribute coupons! Enough is a parameter. """ global num_of_rounds global CB_intervention_points num_of_rounds += 1 co_op_members = get_group(CO_OP_MEMBERS) unemployment_rates = last_period_unemployed / len(co_op_members) * 100 unemployment_threshold = agent["percent_change"] if unemployment_rates >= unemployment_threshold: user_tell("Unemployment has risen to " + str(unemployment_rates) + " more than default value " + str(unemployment_threshold) + " CB Intervened") CB_intervention_points.append([num_of_rounds, last_period_exchanges]) user_tell(CB_intervention_points) distribute_coupons(agent)
def discourage(unwanted): """ Discourages extra drinkers from going to the bar by decreasing motivation. Chooses drinkers randomly from the drinkers that went to the bar. """ discouraged = 0 drinkers = get_group(DRINKERS) while unwanted: if DEBUG: print("The members are: ", drinkers.members) random_drunk = random.choice(list(drinkers.members)) if DEBUG: print("drinker ", random_drunk, " = ", repr(drinkers[random_drunk])) drinkers[random_drunk]["motivation"] -= 0.05 discouraged += 1 unwanted -= 1 return discouraged
def sheep_action(agent): global sheep_created agent["time_to_repr"] -= 1 reproduce(agent, create_sheep, sheep_created, get_group(SHEEP_GROUP)) return False
def coop_report(coop_env): num_babysitter = len(get_sitters(get_group(CO_OP_MEMBERS))) return 'Number of babysitters is: ' + str(num_babysitter) + '\n' pass
def is_dealer(agent): # testcase need changes return get_group(DEALER_GRP).ismember(agent)