Question
Please Write in on Python. I know it is very easy but I cannot figure it out. Now that we have a working evolve function,
Please Write in on Python. I know it is very easy but I cannot figure it out.
Now that we have a working evolve function, we can carry out longer-term simulations of the Game of Life.
import numpy as np def evolve( array ): nx,ny = array.shape # the shape of the input array
neighbor_array = np.zeros((nx, ny), dtype= np.int) # should be zeros the same size as the input array evolved_array = np.zeros((nx, ny), dtype= np.int64) # should be zeros the same size as the input array, dtype of np.int64 for ix in range( 0,nx ): # loop over all x-values for iy in range( 0,ny ): # loop over all y-values lx,rx = ix-1,ix+1 # define the left and right neighbor indices uy,dy = iy-1,iy+1 # define the top and bottom neighbor indices rx %= nx # normalize the indices to fall in [0,nx) lx = (lx + nx) % nx # do this for the other three neighbor indices dy %= nx uy = (uy + ny) % ny # include all eight neighboring cells in an array and sum it up neighbors = [ array[ lx,iy ],array[ rx,iy ],array[ ix,uy ], array[ix, dy], array[lx, uy], array[lx, dy], array[rx, uy], array[rx, dy]] neighbor_array[ ix,iy ] = sum( neighbors ) # handle the case where the current cell is alive and has two neighbors if array[ ix,iy ] > 0 and neighbor_array[ ix,iy ] == 2: evolved_array[ ix,iy ] = 1 # handle the case where the current cell has three neighbors elif neighbor_array[ ix,iy ] == 3: evolved_array[ ix,iy ] = 1 # otherwise the cell is dead else: evolved_array[ ix,iy ] = 0 return evolved_array
Consider a wrapper loop that will simulate a glider traveling for nt time steps:
import numpy as np import matplotlib.pyplot as plt glider = np.zeros( ( 24,24 ) ) glider[ 10,10 ] = 1 glider[ 11,10 ] = 1 glider[ 12,10 ] = 1 glider[ 12,11 ] = 1 glider[ 11,12 ] = 1 nt = 10 for t in range( nt ): cells = evolve( cells ) plt.imshow( cells ) plt.show()
Write a program which implements a loop evolving pentadecathlon for thirty time steps. You should implement a list of ndarrays named pd_list, each containing a single generation of pentadecathlon's evolution.
import numpy as np import matplotlib.pyplot as plt pentadecathlon = np.zeros( ( 24,24 ) ) pentadecathlon[ 10:20,10 ] = 1 pentadecathlon[ 12, 9 ] = 1 pentadecathlon[ 12,10 ] = 0 pentadecathlon[ 12,11 ] = 1 pentadecathlon[ 17, 9 ] = 1 pentadecathlon[ 17,10 ] = 0 pentadecathlon[ 17,11 ] = 1 pd_list = ??? nt = 30 for t in ???: ??? # evolve the simulation and append it to `pd_list` plt.imshow( pd_list[ -1 ] ) plt.show()
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