Using PYTHON GIVEN code: import random import matplotlib.pyplot as plt import numpy as np RMAX = 20 CMAX = 30 POP =

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Using PYTHON GIVEN code: import random import matplotlib.pyplot as plt import numpy as np RMAX = 20 CMAX = 30 POP =

Post by answerhappygod »

Using PYTHON
Using Python Given Code Import Random Import Matplotlib Pyplot As Plt Import Numpy As Np Rmax 20 Cmax 30 Pop 1
Using Python Given Code Import Random Import Matplotlib Pyplot As Plt Import Numpy As Np Rmax 20 Cmax 30 Pop 1 (229.38 KiB) Viewed 65 times
GIVEN code:
import random
import matplotlib.pyplot as plt
import numpy as np
RMAX = 20
CMAX = 30
POP = 20
STEPS = 10

def main():
print("\n ### 2D Growth Simulation ###\n")

# Initialise population
popgrid = np.zeros((RMAX, CMAX), dtype=int)
nextgrid = np.zeros((RMAX, CMAX), dtype=int)

for i in range(POP):
randR = random.randint(0,RMAX-1)
randC = random.randint(0,CMAX-1)
popgrid[randR,randC] += 1
print("Life at : [", randR, randC,
"]")
print("\n ### INITIAL POPULATION ###")

plt.imshow(popgrid) # Note plt origin is top
left
plt.show()
# Simulation

for i in range(STEPS):
print("\n ### TIMESTEP ",i, "###")
# Move population - by one step (randomly) in x
and y

nextgrid = np.zeros((RMAX, CMAX),
dtype=int)
for r in range(RMAX):
for c in
range(CMAX):
for i in
range(popgrid[r,c]):

rmoved = r + random.choice([-1,0,1])

cmoved = c + random.choice([-1,0,1])

if rmoved == -1:

rmoved = 0

if cmoved == -1:

cmoved = 0

if rmoved == RMAX:

rmoved = RMAX - 1

if cmoved == CMAX:

cmoved = CMAX -1


nextgrid[rmoved,cmoved] += 1
# Reproduction - 10% chance of reproducing +1 to
pop in cell

for r in range(RMAX):
for c in
range(CMAX):
for i in
range(nextgrid[r,c]):

if random.random() <= 0.1:

nextgrid[r,c] += 1

# Update grid

popgrid = nextgrid
# Plot current population as array (could also use
scatter plot)

plt.imshow(popgrid) # Note plt
origin is top left
plt.show()

if __name__ == "__main__":
main()
Plz make sure the code runs:
2 The Problem We will be simulating the movement of classes of beings in an environment - each with independent behaviour and interaction rules that are impacted by a change in "mode" for the simulation. Your simulation should allow for a varying set of strategies for movement, based on the mode. Examples would be the game of What's the time, Mr Woolf?", PacMan games and (a serious scenario) evacuation responses. You will be given some sample code, showing a range of approaches to similar problems. For the assignment, you will develop code to model the beings using objects, and to add features to the simulation (e.g. more functionality, statistics, graphics). Your task is to extend the code and then showcase your simulation, varying the input parameters, to show how they impact the overall simulation. The required extensions are: 1. Movement: Movement should change based on the mode of the simulation and use a combination of reasoning and randomness for each move. You should provide the option of Moore or Von Neumann neighbourhoods. Prompts: How do they move towards or away from targets/threats? Are they sometimes faster or slower? Are they always active, or does time have an impact? 2. Boundaries and Terrain: You should be able to read in the layout of the area from a file, including boundaries to stop the beings going beyond the grid, and limiting movement within the grid (walls). This may include some representation of the height of the terrain. Prompts: How can beings move between cells taking barriers into account? How do you stop them moving to invalid spaces? How might the beings negotiate height? 3. Interaction: Your code should recognise when beings are in neighbouring cells and respond accordingly. It could be an interaction when one block away, or require 1 beings to be in the same cell - depending on the scenarios. Prompts: How do you recognise a target/threat is near? Add rules as to what happens when different combinations of beings meet. 4. Non-moving targets: You should model special cells/zones and their affect on the beings. Beyond terrain, you will need a way to indicate that a cell has a "landmark" for them to interact with e.g. an emergency exit. Prompts: How do you recognise the location of the non-moving targets/threats? Can your beings "see", or do you do a scan of the area and base movement on that? 5. Visualisation/Results: Enhance the display for the simulation to vary the representation of beings and the area they move in. You should display a log of events and/or statistics for the simulation and also be able to save the simulation state as a plot image or a csv file. Prompts: How will you differentiate the beings? Are they the same throughout the simulation? You may choose an “image" representation, to improve on using coloured dots. Note: Your program should allow command line arguments to control the parameters of the experiment/simulation.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply