1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| class warmodel(mesa.Model):
geojson_regions = "\german.geojson" unique_id = "regionnum"
def __init__(self,german_army_size=60,france_army_size=60): super().__init__() self.german_army_size=german_army_size self.france_army_size=france_army_size
self.schedule = mesa.time.RandomActivationByType(self) self.space = mg.GeoSpace(warn_crs_conversion=False)
self.counts = None self.reset_counts()
self.datacollector = mesa.DataCollector( { "in_war": get_in_war_count, "set_a_place":get_set_a_place_count, "prepared": get_prepared_count, "dead": get_dead_count, "region in war":get_region_in_war_count, "not in war":get_region_not_in_war
} )
ac = mg.AgentCreator(regionAgent, model=self)
region_agents=ac.from_file( self.geojson_regions, unique_id=self.unique_id ) self.space.add_agents(region_agents) for agent in region_agents: self.schedule.add(agent)
for i in range(german_army_size): attack_value=0.6 + self.random.random() * 0.5 attack_speed= self.random.randint(8,20) soldier_number=self.random.randint(3000,6000) enermy_distance=90000
unique_german_army = mg.AgentCreator( german_army, model=self, crs=self.space.crs, agent_kwargs={"attack_value": attack_value, "attack_speed":attack_speed, "soldier_number":soldier_number, "enermy_distance": enermy_distance } )
x_home, y_home = self.find_home(region_agents,"german")
german_army_agent = unique_german_army.create_agent( Point(x_home, y_home), "G" + str(i), ) self.space.add_agents(german_army_agent) self.schedule.add(german_army_agent)
for i in range(france_army_size):
attack_speed= self.random.randint(6,10) soldier_number=self.random.randint(4000,8000)
unique_german_army = mg.AgentCreator( france_army, model=self, crs=self.space.crs, agent_kwargs={ "attack_speed":attack_speed, "soldier_number":soldier_number } )
x_home, y_home = self.find_home(region_agents,"france") france_army_agent = unique_german_army.create_agent( Point(x_home, y_home), "F" + str(i), ) self.space.add_agents(france_army_agent) self.schedule.add(france_army_agent)
def find_home(self,region_agents,country): self.country=country select_in_germanregions=[] select_in_franceregions=[] for agent in region_agents: if "g" in agent.unique_id: select_in_germanregions.append(agent) elif "f" in agent.unique_id: select_in_franceregions.append(agent) if self.country=="german": this_region = self.random.randint( 0, len(select_in_germanregions) - 1 ) center_x, center_y = select_in_germanregions[ this_region ].geometry.centroid.coords.xy this_bounds = select_in_germanregions[this_region].geometry.bounds spread_x = int( this_bounds[2] - this_bounds[0] ) spread_y = int(this_bounds[3] - this_bounds[1]) this_x = center_x[0] + self.random.randint(0, spread_x) - spread_x / 2 this_y = center_y[0] + self.random.randint(0, spread_y) - spread_y / 2 return this_x,this_y elif self.country=="france": this_region = self.random.randint( 0, len(select_in_franceregions) - 1 ) center_x, center_y = select_in_franceregions[ this_region ].geometry.centroid.coords.xy this_bounds = select_in_franceregions[this_region].geometry.bounds spread_x = int( this_bounds[2] - this_bounds[0] ) spread_y = int(this_bounds[3] - this_bounds[1]) this_x = center_x[0] + self.random.randint(0, spread_x) - spread_x / 2 this_y = center_y[0] + self.random.randint(0, spread_y) - spread_y / 2 return this_x,this_y def reset_counts(self): self.counts = { "in_war": 0, "set_a_place": 0, "prepared": 0, "dead": 0, "region in war":0, "not in war":0 }
def step(self):
self.reset_counts() self.schedule.step() self.datacollector.collect(self)
|