def create_agents(): global sheep_slider global wolf_slider global num_sheep global num_wolves # get agent parameters from sliders num_sheep = sheep_slider.get() num_wolves = wolf_slider.get() # download y and x data from web # extract for use in positioning agents mydata = requests.get( 'https://www.geog.leeds.ac.uk/courses/computing/practicals/python/agent-framework/part9/data.html' ) content = mydata.text soup = bs4.BeautifulSoup(content, 'html.parser') mydata_y = soup.find_all(attrs={"class": "y"}) mydata_x = soup.find_all(attrs={"class": "x"}) # print(mydata_y) # Test download of y # print(mydata_x) # Test download of x # Create sheep within environment, using y and x classes for i in range(num_sheep): y = int(mydata_y[i].text) x = int(mydata_x[i].text) sheep.append(agentframework.Sheep(current_environment, sheep, y, x)) # print(y, x) # Test print sheep coordinates # print() # create wolves within environment, using y and x classes for i in range(num_wolves): y = int(mydata_y[num_sheep + i].text) x = int(mydata_x[num_sheep + i].text) wolves.append(agentframework.Wolves(sheep, y, x))
content = mydata.text soup = bs4.BeautifulSoup(content, 'html.parser') mydata_y = soup.find_all(attrs={"class": "y"}) mydata_x = soup.find_all(attrs={"class": "x"}) # print(mydata_y) # Test download of y # print(mydata_x) # Test download of x # create an environment for the agents # NB update filename to use a different csv dataset current_environment = Environment.create_environment('in.txt') # Create sheep within environment, using y and x classes for i in range(num_sheep): y = int(mydata_y[i].text) x = int(mydata_x[i].text) sheep.append(agentframework.Sheep(current_environment, sheep, y, x)) # print(y, x) # Test print sheep coordinates # print() # create wolves within environment, using y and x classes for i in range(num_wolves): y = int(mydata_y[num_sheep + i].text) x = int(mydata_x[num_sheep + i].text) wolves.append(agentframework.Wolves(sheep, y, x)) # print(y, x) # Test print wolf coordinates carry_on = True # print descriptive text for user print("Sheep are white, wolves are red") print()
#print(td_xs) r.close # create fig and ax the animation function based on fig = matplotlib.pyplot.figure(figsize=(7, 7)) ax = fig.add_axes([0, 0, 1, 1]) carry_on = True # Make the agents fro object Sheep. for i in range( num_of_agents): #use the for-loop to create agents in each iterations y = int(td_ys[i].text) #assign x,y coordinates values to agents x = int(td_xs[i].text) agents.append( agentframework.Sheep(x, y, environment, agents, neighbourhood)) #Make the wolf agents. for j in range(num_of_wolves): wolves.append(w.wolf(x, y, wolves)) #the function animation based on def update(frame_number): fig.clear() global carry_on for j in range(num_of_wolves): wolves[j].random_move()
def update(frame_number): """Update the model""" fig.clear() """Clear figure before rebuilding it.""" for j in range(num_of_iterations): try: for j in range(len(wolves)): """Loop through the list of wolves.""" wolves[j].wolves = rnd.sample(wolves[j].wolves, k=len(wolves[j].wolves)) wolves[j].hunt_sheep() """Wolf moves to hunt sheep.""" wolves[j].starve() """If wolf is not able to catch sheep, it will starve""" if wolves[j].alive == False: """ remove dead wolves """ wolves.pop(j) for i in range(len(herd)): """Loop through the sheep herd.""" herd[i].escape_wolves(wolves) """Sheep tries to escape wolf.""" if herd[i].alive == False: """Remove dead sheep.""" herd.pop(i) else: herd[i].herd = rnd.sample(herd[i].herd, k=len(herd[i].herd)) """reorder sheep randomly.""" herd[i].move() """Sheep moves.""" herd[i].eat() """Sheep eats and sickens up with overeaten.""" herd[i].share_with_neighbours(neighbourhood) if herd[i].mate(): """If female sheep try to mate.""" herd.append(af.Sheep(environment,herd)) # control sheep population if ctrl.new_wolves(): wolves.append(af.Wolf(wolves)) """There is a 1% chance a new wolf is introduced.""" ctrl.disease_outbreak(herd) """ If sheep herd exceeds 100, a disease outbreak occurs. Sheep have a 20% chance of surviving the disease.""" except IndexError: pass #: Map environment, sheep and wolves pos = plt.imshow(environment,cmap='summer_r') """Plot environment raster.""" plt.colorbar(pos, orientation="horizontal") """Add color bar to the figure. """ for i in range(len(herd)): """Loop through the herd and plot sheep.""" plt.scatter(herd[i].x, herd[i].y,color ='white') for j in range(len(wolves)): """Loop through the wolves and plot wolves.""" plt.scatter(wolves[j].x, wolves[j].y, color ='red') #: Style plot plt.scatter(102, 102,color ='white',label="Sheep #("+ str(len(herd)) +')') plt.scatter(102, 102,color ='red',label="Wolf #("+ str(len(wolves)) +')') """Create two fakes points outside the plot area. These points will be used for the legend.""" plt.legend(loc=9, bbox_to_anchor=(0.5, -0.1),ncol=2,frameon=False) """ Add plot legend.""" plt.xlim(0, 99) plt.ylim(0, 99) """ restrict plot area to 100px """ plt.suptitle('Sheep and Wolves', fontsize=20,color='#0d6d13') plt.title('(Agent Based Model)', fontsize=12,loc='center',color='#4b4f4c') """ Add plot title and subtitle """
"""Add raster to the environment list.""" file.close() for i in range(num_of_sheep): """Generate sheep herd. Loop through the number of sheep to be generated and add each sheep to the herd list. Use the Sheep class from the agentframework to create the x/y coordinates for each sheep. The __str__ class has been overridden to print the original location of each sheep. """ herd.append(af.Sheep(environment,herd)) herd[i].__str__() """Print original location of each sheep.""" #: Make the wolves for j in range(num_of_wolves): """Generate wolves. Loop through the number of wolves to be generated and add each wolf to the list of wolves. Use the Wolf class from the agentframework to create the x/y coordinates for each wolf. """ wolves.append(af.Wolf(wolves))