def __init__(self): self.num_agents_sita = 30 self.num_agents_ue = 30 self.num_kabe = 1 self.schedule = RandomActivationByBreed(self) #self.schedule = RandomActivation(self) self.width = 10 self.height = 50 self.space = ContinuousSpace(self.width, self.height, True) self.syudan_hito_sita = np.zeros((self.num_agents_sita, 2)) self.syudan_hito_ue = np.zeros((self.num_agents_ue, 2)) self.syudan_kabe = np.zeros((self.num_kabe, 2)) self.time = 1000 # Create agents for j in range(self.num_agents_sita): a = Hokousya_sita(j, self) self.syudan_hito_sita[j, 0] = a.iti_x self.syudan_hito_sita[j, 1] = a.iti_y self.schedule.add(a) #self.syudan_hito_sita[i,0] = a.iti_x #self.syudan_hito_sita[i,1] = a.iti_y for i in range(self.num_agents_ue): b = Hokousya_ue(i, self) self.syudan_hito_ue[i, 0] = b.iti_x self.syudan_hito_ue[i, 1] = b.iti_y self.schedule.add(b)
class AlbatrossModel(Model): def __init__(self, param): self.seed = np.random.RandomState(param['SEED']) self.param = param self.users = list() self.platforms = list() self.advertisers = list() self.schedule = RandomActivationByBreed(self) for i in range(param['N_USERS']): new_user = User(unique_id=i, model=self) self.users.append(new_user) self.schedule.add(new_user) for j in range(param['N_PLATFORMS']): new_platform = Platform(unique_id=j, model=self) for m in range(param['PAGES_PER_PLATFORM']): new_platform.create_page() self.platforms.append(new_platform) self.schedule.add(new_platform) for k in range(param['N_ADVERTISERS']): new_advertiser = Advertiser(unique_id=k, model=self) self.advertisers.append(new_advertiser) self.schedule.add(new_advertiser) # Random network # for agent in self.users: # if np.random.random() > .5: # pass # TODO. Make the network a typical small world network def step(self): self.schedule.step()
def __init__(self, param): self.seed = np.random.RandomState(param['SEED']) self.param = param self.users = list() self.platforms = list() self.advertisers = list() self.schedule = RandomActivationByBreed(self) for i in range(param['N_USERS']): new_user = User(unique_id=i, model=self) self.users.append(new_user) self.schedule.add(new_user) for j in range(param['N_PLATFORMS']): new_platform = Platform(unique_id=j, model=self) for m in range(param['PAGES_PER_PLATFORM']): new_platform.create_page() self.platforms.append(new_platform) self.schedule.add(new_platform) for k in range(param['N_ADVERTISERS']): new_advertiser = Advertiser(unique_id=k, model=self) self.advertisers.append(new_advertiser) self.schedule.add(new_advertiser)
class Oundahodou(Model): """これはモデル 連続空間に配置する""" def __init__(self): self.num_agents_sita = 2 self.num_agents_ue = 2 self.num_kabe = 1 self.schedule = RandomActivationByBreed(self) #self.schedule = RandomActivation(self) self.width = 10 self.height = 50 self.space = ContinuousSpace(self.width, self.height, True) self.syudan_hito_sita = np.zeros((self.num_agents_sita, 2)) self.syudan_hito_ue = np.zeros((self.num_agents_ue, 2)) self.syudan_kabe = np.zeros((self.num_kabe, 2)) self.time = 1000 # Create agents for j in range(self.num_agents_sita): a = Hokousya_sita(j, self) self.syudan_hito_sita[j,0] = a.iti_x self.syudan_hito_sita[j,1] = a.iti_y self.schedule.add(a) #self.syudan_hito_sita[i,0] = a.iti_x #self.syudan_hito_sita[i,1] = a.iti_y for i in range(self.num_agents_ue): b = Hokousya_ue(i, self) self.syudan_hito_ue[i, 0] = b.iti_x self.syudan_hito_ue[i, 1] = b.iti_y self.schedule.add(b) #self.syudan_hito_ue[i,0] = b.iti_x #self.syudan_hito_ue[i,1] = b.iti_y #壁を作る for i in range(self.num_kabe): c = Kabe(i, self) self.schedule.add(c) self.syudan_kabe[i, 0] = c.iti[0] self.syudan_kabe[i, 1] = c.iti[1] def make_agents(self): """途中からアクティブになるエージェントの作成""" for j in range(self.num_agents_sita): d = Hokousya_sita(j, self) self.syudan_hito_sita[j,0] = d.iti_x self.syudan_hito_sita[j,1] = d.iti_y self.schedule.add(d) #self.syudan_hito_sita[i,0] = a.iti_x #self.syudan_hito_sita[i,1] = a.iti_y for i in range(self.num_agents_ue): e = Hokousya_ue(i, self) self.syudan_hito_ue[i, 0] = e.iti_x self.syudan_hito_ue[i, 1] = e.iti_y self.schedule.add(e) #self.syudan_hito_ue[i,0] = b.iti_x #self.syudan_hito_ue[i,1] = b.iti_y def step(self): '''Advance the model by one step.''' self.schedule.step() self.dasu_syudan_hito() self.fasu_num_agents() self.dasu_kabe() self.dasu_nu_kabe() """モデル描画を目指す""" #def make_im(self): #for i in range(self.num_agents): #im = ax.plot() def ptint_syudan(self): print(self.syudan) def dasu_syudan_hito(self): self.syudan_hito = np.append(self.syudan_hito_sita,self.syudan_hito_ue,axis=0) return self.syudan_hito def fasu_num_agents(self): return self.num_agents_sita + self.num_agents_ue def dasu_kabe(self): return self.syudan_kabe def dasu_nu_kabe(self): return self.num_kabe
def __init__(self, height = 50, width = 50, initial_population =200, \ Moore = False, torus = True, regrow = 1, seed = None): ''' Args: height - y axis of grid_size width - x axis of grid size initial_population - number of agents starting moore - type of neighborhood torus - whether or no world wraps regrow - amout each resource grows bas each step process - Number of additonal proces by agents 0 = Movement/Survive; 1 = +trade, 2 = + Initial Parameters: Multigrid ActivationbyBreed (see schedule) Num_Agents counter to account for each agent number timekeeper - dictionary to keep track of time for each section start_time - create initial time datacollector to collect agent data of model ''' self.step_num = 0 self.height = height self.width = width self.initial_population = initial_population self.num_agents = 0 #Mesa Agent Scheduler #self.schedule = schedule.RandomActivationByBreed(self) self.schedule = RandomActivationByBreed(self) self.grid = MultiGrid(self.height, self.width, torus=True) self.regrow = regrow self.running = True self.price_record = defaultdict(list) ''' Recorders Start datacollector Start time recorder ''' self.start_time = time.time() self.datacollector = DataCollector(\ tables = {"Time":["Time Per Step"]}) ''' Creates the landscape: Fours mounds 2 sugar, 2 spice located- 1 in each quadrant imports landscape module to account for various landscape sizes ''' self.resource_dict = {} landscape = Landscape.create_landscape(height, width) #places resources from landscape on grid for k, v in landscape.items(): resource = R.resource(k, self, v, self.regrow) self.grid.place_agent(resource, (resource.pos[0], resource.pos[1])) #POINT self.schedule.add(resource) #fills in empty grids with null value resource agent #Deviation from GrAS -- in empty cells has random resource from 0 to 4 for a, x, y in self.grid.coord_iter(): if a == set(): resource = R.resource((x,y), self, \ (self.random.randrange(0,2), \ self.random.randrange(0,2)),self.regrow) self.grid.place_agent(resource, (resource.pos[0], resource.pos[1])) #POINT self.schedule.add(resource) ''' Creates the agents: ''' pos_array = list(self.schedule.agents_by_breed['resource'].keys()) self.random.shuffle(pos_array) vision_array = np.random.randint(4, 6, self.initial_population) spice_array = np.random.randint(1, 6, self.initial_population) sugar_array = np.random.randint(1, 6, self.initial_population) for n in range(self.initial_population): #x = 0 #y = 0 #print ("position: ", (n, x,y)) #GrAS p. 108 sugar = self.random.randrange(25, 50) spice = self.random.randrange(25, 50) #GrAS p.108 sug_bolism = sugar_array[n] spice_bolism = spice_array[n] #GrAS p. 108 vision = vision_array[n] neighbors = Moore a = N.NetAgent(n, pos_array[n], self, \ {"sug_bolism": sug_bolism, \ "spice_bolism": spice_bolism}, \ {1 : sugar, 2: spice}, {"vision": vision}, \ neighbors) #POINT self.schedule.add(a) self.grid.place_agent(a, pos_array[n])
def __init__( self, height=25, width=25, initial_sheep=60, initial_wolves=40, sheep_reproduce=0.2, wolf_reproduce=0.1, wolf_gain_from_food=13, grass=True, grass_regrowth_time=20, sheep_gain_from_food=5, ): """ Create a new Wolf-Sheep model with the given parameters. Args: initial_sheep: Number of sheep to start with initial_wolves: Number of wolves to start with sheep_reproduce: Probability of each sheep reproducing each step wolf_reproduce: Probability of each wolf reproducing each step wolf_gain_from_food: Energy a wolf gains from eating a sheep grass: Whether to have the sheep eat grass for energy grass_regrowth_time: How long it takes for a grass patch to regrow once it is eaten sheep_gain_from_food: Energy sheep gain from grass, if enabled. """ super().__init__() # Set parameters self.height = height self.width = width self.initial_sheep = initial_sheep self.initial_wolves = initial_wolves self.sheep_reproduce = sheep_reproduce self.wolf_reproduce = wolf_reproduce self.wolf_gain_from_food = wolf_gain_from_food self.grass = grass self.grass_regrowth_time = grass_regrowth_time self.sheep_gain_from_food = sheep_gain_from_food self.schedule = RandomActivationByBreed(self) self.grid = MultiGrid(self.height, self.width, torus=True) # Create sheep: for i in range(self.initial_sheep): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.sheep_gain_from_food) sheep = Sheep(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(sheep, (x, y)) self.schedule.add(sheep) # Create wolves for i in range(self.initial_wolves): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.wolf_gain_from_food) wolf = Wolf(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(wolf, (x, y)) self.schedule.add(wolf) # Create grass patches if self.grass: for agent, x, y in self.grid.coord_iter(): fully_grown = self.random.choice([True, False]) if fully_grown: countdown = self.grass_regrowth_time else: countdown = self.random.randrange(self.grass_regrowth_time) patch = GrassPatch(self.next_id(), (x, y), self, fully_grown, countdown) self.grid.place_agent(patch, (x, y)) self.schedule.add(patch)
class WolfSheep(Model): """ Wolf-Sheep Predation Model """ height = 25 width = 25 initial_sheep = 60 initial_wolves = 40 sheep_reproduce = 0.2 wolf_reproduce = 0.1 wolf_gain_from_food = 13 grass = True grass_regrowth_time = 20 sheep_gain_from_food = 5 verbose = False # Print-monitoring description = ( "A model for simulating wolf and sheep (predator-prey) ecosystem modelling." ) def __init__( self, height=25, width=25, initial_sheep=60, initial_wolves=40, sheep_reproduce=0.2, wolf_reproduce=0.1, wolf_gain_from_food=13, grass=True, grass_regrowth_time=20, sheep_gain_from_food=5, ): """ Create a new Wolf-Sheep model with the given parameters. Args: initial_sheep: Number of sheep to start with initial_wolves: Number of wolves to start with sheep_reproduce: Probability of each sheep reproducing each step wolf_reproduce: Probability of each wolf reproducing each step wolf_gain_from_food: Energy a wolf gains from eating a sheep grass: Whether to have the sheep eat grass for energy grass_regrowth_time: How long it takes for a grass patch to regrow once it is eaten sheep_gain_from_food: Energy sheep gain from grass, if enabled. """ super().__init__() # Set parameters self.height = height self.width = width self.initial_sheep = initial_sheep self.initial_wolves = initial_wolves self.sheep_reproduce = sheep_reproduce self.wolf_reproduce = wolf_reproduce self.wolf_gain_from_food = wolf_gain_from_food self.grass = grass self.grass_regrowth_time = grass_regrowth_time self.sheep_gain_from_food = sheep_gain_from_food self.schedule = RandomActivationByBreed(self) self.grid = MultiGrid(self.height, self.width, torus=True) # Create sheep: for i in range(self.initial_sheep): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.sheep_gain_from_food) sheep = Sheep(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(sheep, (x, y)) self.schedule.add(sheep) # Create wolves for i in range(self.initial_wolves): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.wolf_gain_from_food) wolf = Wolf(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(wolf, (x, y)) self.schedule.add(wolf) # Create grass patches if self.grass: for agent, x, y in self.grid.coord_iter(): fully_grown = self.random.choice([True, False]) if fully_grown: countdown = self.grass_regrowth_time else: countdown = self.random.randrange(self.grass_regrowth_time) patch = GrassPatch(self.next_id(), (x, y), self, fully_grown, countdown) self.grid.place_agent(patch, (x, y)) self.schedule.add(patch) self.running = True def step(self): self.schedule.step() if self.verbose: print([ self.schedule.time, self.schedule.get_breed_count(Wolf), self.schedule.get_breed_count(Sheep), ]) def run_model(self, step_count=500): if self.verbose: print("Initial number wolves: ", self.schedule.get_breed_count(Wolf)) print("Initial number sheep: ", self.schedule.get_breed_count(Sheep)) for i in range(step_count): self.step() if self.verbose: print("") print("Final number wolves: ", self.schedule.get_breed_count(Wolf)) print("Final number sheep: ", self.schedule.get_breed_count(Sheep))
def __init__(self, height=25, width=25, initial_rabbit=100, initial_bears=50, rabbit_reproduce=0.2, bear_reproduce=0.1, bear_gain_from_food=10, verbose=False, grass=False, grass_regrowth_time=15, rabbit_gain_from_food=5, initial_hunter=10, hunter_gain_from_rabbit=5, hunter_welafre_list=[], hunter_gain_form_bear=10, extinction_punishment=-1000, hunting_season_start=1, hunting_season_end=5): # Note: always 1 =< hunting_season_start =< hunting_season_end <= 10 ''' Create a new bear-rabbit model with the given parameters. Args: initial_rabbit: Number of rabbit to start with initial_bears: Number of bears to start with rabbit_reproduce: Probability of each rabbit reproducing each step bear_reproduce: Probability of each bear reproducing each step bear_gain_from_food: Energy a bear gains from eating a rabbit grass: Whether to have the rabbit eat grass for energy grass_regrowth_time: How long it takes for a grass patch to regrow once it is eaten rabbit_gain_from_food: Energy rabbit gain from grass, if enabled. ''' super().__init__() # Set parameters self.height = height self.width = width self.initial_rabbit = initial_rabbit self.initial_bears = initial_bears self.rabbit_reproduce = rabbit_reproduce self.bear_reproduce = bear_reproduce self.bear_gain_from_food = bear_gain_from_food self.grass = grass self.grass_regrowth_time = grass_regrowth_time self.rabbit_gain_from_food = rabbit_gain_from_food self.hunter_gain_from_rabbit = hunter_gain_from_rabbit self.hunter_gain_from_bear = hunter_gain_form_bear self.initial_hunter = initial_hunter self.hunter_welfare_list = hunter_welafre_list self.extinction_punishment = extinction_punishment self.hunting_season_start = hunting_season_start self.hunting_season_end = hunting_season_end self.verbose = verbose self.schedule = RandomActivationByBreed(self) self.grid = MultiGrid(self.height, self.width, torus=True) self.datacollector = DataCollector({ "bears": lambda m: m.schedule.get_breed_count(Bear), "rabbit": lambda m: m.schedule.get_breed_count(Rabbit), "total_welfare": lambda m: m.sum_welfare(), "average_welfare": lambda m: m.average_welfare() }) # Create hunter: self.hunter_welfare_list = [] for i in range(self.initial_hunter): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = 5 hunter = Hunter(self.next_id(), (x, y), self, True, energy, welfare=0) self.hunter_welfare_list.append(hunter.welfare) self.grid.place_agent(hunter, (x, y)) self.schedule.add(hunter) # Create rabbit: for i in range(self.initial_rabbit): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.rabbit_gain_from_food) rabbit = Rabbit(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(rabbit, (x, y)) self.schedule.add(rabbit) # Create bears for i in range(self.initial_bears): x = self.random.randrange(self.width) y = self.random.randrange(self.height) energy = self.random.randrange(2 * self.bear_gain_from_food) bear = Bear(self.next_id(), (x, y), self, True, energy) self.grid.place_agent(bear, (x, y)) self.schedule.add(bear) # Create grass patches if self.grass: for agent, x, y in self.grid.coord_iter(): fully_grown = self.random.choice([True, False]) if fully_grown: countdown = self.grass_regrowth_time else: countdown = self.random.randrange(self.grass_regrowth_time) patch = GrassPatch(self.next_id(), (x, y), self, fully_grown, countdown) self.grid.place_agent(patch, (x, y)) self.schedule.add(patch) self.running = True self.datacollector.collect(self)