def setup(): #this is done to allow for connection with scale bar global num_of_agents #global allows to be seen outside of def function, agents=sheep num_of_agents = scale_sheep.get() #number of agents, connecting with scale bar global num_of_wolves #number of wolves num_of_wolves = scale_wolves.get() #set number of wolves global wolves #make wolves global wolves = [] #makes wolves list global agents #makes agents global agents = [] # Make the agents list for i in range(num_of_agents): #setting x y for sheep y = int(td_ys[i].text) #grabing text from html x = int(td_xs[i].text) #grabing text from html agents.append(agentframework.Agent(environment, agents, wolves, x, y)) #connects agents to the environment, and grabs from framework #print(agents[i]) for i in range(num_of_wolves): #seeting up num of wolves x = random.randint(150, 199) #seting wolves random starting x coordinate y = random.randint(0, 50) #setting agents random y starting points wolves.append(agentframework.Wolf(environment, wolves, agents, x, y))
carry_on = True # boolean for stopping functions fig = plt.figure(figsize=(7, 7)) ax = fig.add_axes([0, 0, 1, 1]) env_total = sum(sum(x) for x in environment) # sum of all values in environment counter = 0 # counter for num_of_iterations agents_pop = [] infants_pop = [] # make the agents for i in range(num_of_agents): agents.append(agentframework.Agent(environment, neighbourhood)) # make the wolves for i in range(num_of_wolves): wolves.append(agentframework.Wolf(environment, neighbourhood)) # update function def update(misc): """ Update function to be called at each frame, actions include: Agents and wolves will be made to interact with the environment and themselves Stopping conditions will be assessed Environment, wolves and agents will be plotted """ fig.clear() global carry_on global counter
""" 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)) #: Generate plot layout (figure) fig =plt.figure(figsize=(7, 7)) """Figure: Define figure size.""" ax = fig.add_axes([0, 0, 1, 1]) """Axes: Define figure axes.""" fig.patch.set_facecolor('#dedede') """Set figure background color to grey.""" def update(frame_number): """Update the model""" fig.clear() """Clear figure before rebuilding it."""
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 """
num_of_wolves = 60 num_of_iterations = 100 neighbourhood = 20 zone = 10 # Setting Plots fig = matplotlib.pyplot.figure(figsize=(7, 7)) ax = fig.add_axes([0, 0, 1, 1]) # Creating sheeps and wolves for i in range(num_of_agents): agents.append(agentframework.Agent(environment, agents, neighbourhood)) for i in range(num_of_wolves): wolves.append(agentframework.Wolf(agents, zone)) #Moving, eating and sharing carry_on = True def update(frame_number): fig.clear() global carry_on if len(agents) == 0: carry_on=False print("stopping condition") for j in range(len(agents)):