Question
Write a code that implements these rules: The ground will be represented by a 2D array; initialize the ground to a 20202020 array with values
Write a code that implements these rules:
- The ground will be represented by a 2D array; initialize the ground to a 20202020 array with values between 00 and 11. Copy any code from above that might be useful!
- Add a wall to the environment by changing the values of the environment to a value of 44 in the form of a square centered in the environment with side length 1010. Copy any code from above that might be helpful!
- Add the skunk at a random place inside the yard that is enclosed by the wall. Again, use previous code!
- Loop for 100100 steps. At each step, choose a random number that selects a direction up, down, left or right. (Do this however you want, but consider using randint from NumPy.)
- Change the location of the skunk according the movement choice just made and update the environment array to put the skunk at the new, updated location.
- But, if the skunk moves into the wall, do not update that movement: do nothing.
- Make a movie using imshow of this random path of the skunk. Please copy any code from above that might be useful. Pick a good color scheme for your ABM. Consider making the ground brown, the skunk black, etc.
As above, some of the code is given here to help you get started and .... ### is used where code has been removed for you to fill in. But, feel free to change any of it, including starting over with your own code from scratch.
Given code:
# create the environment (the ground) in an array def ground_array(): # rows size_1 = 20 # columns size_2 = 20 # fill the2D array with random numbers between 0 and 1 yard = np.random.random((size_1, size_2))
# add a wall to the yard
yard[5:14,5] = 4 yard[5:15,14] = 4 yard[5,5:14] = 4 yard[14,5:15] = 4
# add a skunk inside the yard
skunk_row = np.random.randint(6,14) skunk_col = .... ###
yard[skunk_row, skunk_col] = 2 new_skunk_row = skunk_row new_skunk_col = skunk_col###
# loop over time
num_steps = .... ### for time_step in range(num_steps):
# pick a random direction using four possible integers direction = np.random.randint(0,4)
# compute possible new position given current position and direction if direction == 0: new_skunk_row = skunk_row - 1 elif direction == 1: new_skunk_row = skunk_row + 1 elif direction == 2: new_skunk_col = skunk_col - .... ### elif direction == 3: .... ###
# check to see if the possible new position is the wall if yard[new_skunk_row, new_skunk_col] == 4: print("Skunk bumped into the wall - ouch!") else: # not the wall, skunk moves skunk_row = new_skunk_row skunk_col = new_skunk_col### yard[skunk_row,skunk_col] = 3
# let's see where the skunk is right now fig = plt.figure(figsize=(8,8)) plt.imshow(yard) # play with different colors! # plt.imshow(data_new, cmap='plasma') # plt.imshow(data_new, cmap='magma') # plt.imshow(data_2, cmap = 'jet') plt.show() sleep(0.5) clear_output(wait=True)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started