Beispiel #1
0
        rowlist.append(0)
    densitymap.append(rowlist)
print(height,width) # Checks that the Density Map has been made.

# Pub Location within the Environment.   
for y, row in enumerate (environment):
    for x, value in enumerate (row):
        if value==1:
            xpub=x
            ypub=y
print('Pub Located')

# Initialise Agents (Drunks) Loop. 
for i in range(num_of_agents):
    house_number = (i+1)*10
    agents.append(agentframework2.Agent(environment, agents, house_number, xpub, 
                                            ypub))
print('Drunks successful made.')

# Movement of Agents (Drunks):
for i in range (num_of_agents): # Each agent is numbered, corresponding with their house.
    while agents[i].is_home==False: # Not reached home yet carry on moving, adding 1 to the Density Map.
        agents[i].move()
        densitymap[agents[i].y][agents[i].x]+=1
        if agents[i].house_number==agents[i].environment[agents[i].y][agents[i].x]:
            agents[i].is_home=True # No further movement required, Drunks stop when reach home.

# Produces the Density Map.      
matplotlib.pyplot.imshow(densitymap)
matplotlib.pyplot.show()
 
   
Beispiel #2
0
        rowlist.append(0)
    Stored_moves.append(rowlist)

#locate the pub using environment data which as been assigned a value of 1
for y, row in enumerate(environment):
    for x, value in enumerate(row):
        if value == 1:
            startx = x
            starty = y

# Assign each agent a house number
#append the agents starting point to the pubs location
for j in range(num_of_agents):
    number = (j + 1) * 10
    agents.append(
        agentframework2.Agent(environment, agents, number, startx, starty))

#plot of the agents start location - to make sure they start at the pub
#the agents will appear as one dot as all agents are on top of each other
matplotlib.pyplot.xlim = len(environment)
matplotlib.pyplot.ylim = len(environment)
matplotlib.pyplot.imshow(environment)
for i in range(num_of_agents):
    matplotlib.pyplot.scatter(agents[i]._x, agents[i]._y)
matplotlib.pyplot.show()

#get the agents home and store each of the moves
for i in range(num_of_agents):
    while agents[i].arrived == False:
        agents[i].random_move()
        Stored_moves[agents[i]._y][agents[i]._x] += 1
Beispiel #3
0
zhi = 0
for row in environment:
    for value in row:
        if value > 0:
            x = row.index(value)
            y = zhi

    zhi += 1

# after locating the resource of the weapon, mark it as a red star
matplotlib.pyplot.scatter(x, y, marker='*', color='red')

##### step 2: where bacteria will end up #####
# link the model with the agentframework2
for i in range(temp):
    agents.append(agentframework2.Agent(x, y, z))

for i in range(temp):
    while agents[i].z != 0:
        agents[i].Hori()
        agents[i].Vert()

##### step 3: Draws a density map of where all the bacteria end up as an image and displays it on the screen ####
# after bateria falling on the environment, the value of the certain area plus 10, which makes it more visable
# this is the core of the code to make the density map
for i in range(temp):
    environment[agents[i].y][agents[i].x] += 10
    #matplotlib.pyplot.scatter(agents[i].x,agents[i].y,marker='.')

# draw a map with the 300*300 size
matplotlib.pyplot.ylim(0, 300)
Beispiel #4
0
#0.3 Define functions
def distance_between(agents_row_a, agents_row_b):
    return (((agents_row_a.x - agents_row_b.x)**2) +
            ((agents_row_a.y - agents_row_b.y)**2))**0.5


#1. SET UP THE AGENTS
#1.1 Create an empty list to contain all the agents
agents = []
#set number of agents to 10
num_of_agents = 10
#set number of agent moves TO 100
num_of_iterations = 100
#1.2 START LOCATION & ENVIRONMENT- for each agent in 'num_of_agents' assigned coordinates, and attach environment using agentframework module and Agent class
for i in range(num_of_agents):
    agents.append(agentframework.Agent(environment))

#2. MOVE AGENTS
#2.1 RANDOM WALK 'NUM_OF ITERATIONS' STEPS using move function
for j in range(num_of_iterations):
    for i in range(num_of_agents):
        #move agent
        agents[i].move()
        #agent eat
        agents[i].eat()

#3. GRAPHICS
#3.1 SCATTER PLOT
matplotlib.pyplot.ylim(0, 99)
matplotlib.pyplot.xlim(0, 99)
matplotlib.pyplot.imshow(environment)
Beispiel #5
0
    for value in row:
        rowlist.append(value)

    environment.append(rowlist)

#Close the reader
f.close()

matplotlib.pyplot.imshow(environment)
matplotlib.pyplot.show()

#############################################

for i in range(num_of_agents):
    #agents.append([random.randint(0,100),random.randint(0,100)])
    agents.append(agentframework2.Agent(i, environment))
    #print all agents coordinates
    print(agents[i])

print(len(agents))

for j in range(num_of_iterations):
    for i in range(num_of_agents):
        agents[i].move()
        agents[i].eat()


#define what distance_between is i.e. pythagoras theorum
def distance_between(agents_row_a, agents_row_b):
    #return (((agents_row_a[0] - agents_row_b[0])**2) +
    #((agents_row_a[1] - agents_row_b[1])**2))**0.5
Beispiel #6
0
def setup_agents():
    """
    Function:   Sets up Police in the environment by choosing a number from the GUI slider.
                Initialises Drunks and Police loops derived from agentframework2.py. Within the Drunk loop
                house number and agent location can be determined. 
                
    Parameters: num_of_police - Global variable defines number of Police as selected using the slider. 
                num_of_agents - Variable defines number of Drunks.
                environment - 300 x 300 raster grid containing the town plan.
                agents - List of Drunks.
                police - List of Police.
                x - Randomly generated coordinate. 
                y - Randomly generated coordinate.
                
    Returns:    Prints number of Drunks and Police that are going to be in the model.
                Specific Agent (Drunks) number and Boolean values, 'True' or 'False' is printed in the iPython console to show if an 
                agent is home or not. 

    """
    global num_of_police
    num_of_police = policeslider.get()
    print('Total Drunks:', num_of_agents
          )  # Prints total number of Drunks. This should always be 25.
    print('Total Police:',
          num_of_police)  # Prints total number of Police as selected.

    # Initialise Agents (Drunks) Loop.
    for i in range(num_of_agents):
        house_number = (i + 1) * 10
        #print("House Number:", house_number)
        agents.append(
            agentframework2.Agent(environment, agents, police, house_number))
        agent_location = agents[i]  #  xy location of each agent is retained.
        #print("Drunks Location:",agents[i]) # Prints to ensure the Drunks xy coordinates are being generated.

    # Initialise Police Loop.
    for i in range(num_of_police):
        police.append(agentframework2.Police(environment, agents, police, x,
                                             y))
        #print("Police:",police[i]) # Prints to ensure Police xy coordinates are being generated.

    # Movement of Agents (Drunks).
    for i in range(
            num_of_agents
    ):  # Each agent is numbered, corresponding with their house.
        if agent_location.house_number == agents[i].environment[agents[i].y][
                agents[i].x]:
            #print("House Number:",house_number)
            agent_location.at_home == True
            reached_home = reached_home + 1  # When Drunks get home they are added to the count. Ensures all get home.
            agents[i].stop(
            )  # Once Drunk reaches home, they should stop moving about.
            #print("Agent should stop at home.")
            #print("Testing Environment-Agent value:",environment[agents[i].y][agents[i].x])
        else:
            agent_location.at_home == False
            agents[i].move(
            )  # If Agents that have not reached home, keep moving.
            agents[i].density(
            )  # Agents continuing to move, continue adding 1 to the Density Map with each iteration.
            #print("House_Number:",house_number) # Testing to see if House Number stays the same.
            #print("Agent moves:", agents[i]) # Testing to see if the Drunk continues to move as it has not reached home yet.

        # Produces the Density Map.
        matplotlib.pyplot.imshow(densitymap)

    for i in range(len(agents)):
        print(
            'agent {0}: {1}'.format(i, agents[i].at_home)
        )  # If an agent gets home, the agent number plus 'True' is printed.