Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. 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!
  2. 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!
  3. Add the skunk at a random place inside the yard that is enclosed by the wall. Again, use previous code!
  4. 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.)
  5. 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.
  6. But, if the skunk moves into the wall, do not update that movement: do nothing.
  7. 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.

In [0]:

1

# create the environment (the ground) in an array

2

 

3

# rows

4

size_1 = 20

5

# columns

6

size_2 = 20

7

# fill the2D array with random numbers between 0 and 1

8

yard = np.random.random((size_1, size_2))

9

 

10

 

11

# add a wall to the yard

12

 

13

yard[5:14,5] = 4

14

yard[5:15,14] = 4

15

yard[5,5:14] = 4

16

yard[14,5:15] = 4

17

 

18

 

19

# add a skunk inside the yard

20

 

21

skunk_row = np.random.randint(6,14)

22

skunk_col = .... ###

23

 

24

yard[skunk_row, skunk_col] = 2

25

new_skunk_row = skunk_row

26

new_skunk_col = .... ###

27

 

28

 

29

# loop over time

30

 

31

num_steps = .... ###

32

for time_step in range(num_steps):

33

 

34

 # pick a random direction using four possible integers

35

 direction = np.random.randint(0,4)

36

 

37

 # compute possible new position given current position and direction

38

 if direction == 0:

39

 new_skunk_row = skunk_row - 1

40

 elif direction == 1:

41

 new_skunk_row = skunk_row + 1

42

 elif direction == 2:

43

 new_skunk_col = skunk_col - .... ###

44

 elif direction == 3:

45

 .... ###

46

 

47

 # check to see if the possible new position is the wall

48

 if yard[new_skunk_row, new_skunk_col] == 4:

49

 print("Skunk bumped into the wall - ouch!")

50

 else:

51

 # not the wall, skunk moves

52

 skunk_row = new_skunk_row

53

 skunk_col = .... ###

54

 yard[skunk_row,skunk_col] = 3

55

 

56

 # let's see where the skunk is right now

57

 fig = plt.figure(figsize=(8,8))

58

 plt.imshow(yard)

59

 # play with different colors! 

60

 # plt.imshow(data_new, cmap='plasma')

61

 # plt.imshow(data_new, cmap='magma')

62

 # plt.imshow(data_2, cmap = 'jet')

63

 plt.show()

64

 sleep(0.5)

65

 clear_output(wait=True)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions