0% found this document useful (0 votes)
7 views

CSCI427 Wk01 Lab Part3

Uploaded by

That Guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CSCI427 Wk01 Lab Part3

Uploaded by

That Guy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSCI 427 ARTIFICIAL INTELLIGENCE FOUNDATIONS

D E PA R T M E N T O F C O M P U T E R S C I E N C E A N D I N F O R M AT I O N S Y S T E M S

Week 1 Project: Jupyter Lab and the Game of Life

The goal of this assignment is to set up a machine learning environment on your personal
computer and to gain some familiarity using it.

WHAT TO SUBMIT

Game of Life Notebook (yourUsername-wk1.ipynb)

Movie of Life Simulation (yourUsername-wk1.mp4)


Part 1: Install Anaconda with Jupyter Lab and ffmpeg
This course uses Jupyter Lab to write and manage Python scripts and documentation. To
simplify the installation and management of our Jupyter environment, we will use Anaconda.
• Download and run the Anaconda installer for your operating system. The installer can be
downloaded from:
https://www.anaconda.com/download
When the Anaconda Navigator application launches, it is optional to create an account to the
Anaconda cloud services.
• To save videos of plot animations, we need to install the ffmpeg library.
1. Click on Environments on the left navigation pane
2. On the “Installed” drop-down menu, select “Not installed”
3. Enter ffmpeg in the Search Packages search box.
4. Select the ffmpeg item by clicking the checkbox next to it.
5. Click the Apply button at the bottom of the window to install the ffmpeg library.

• The installer checks which packages need to be installed or updated and asks you to Apply
those changes.

We can now launch Jupyter Lab by clicking the Launch button on the Home tab.
Jupyter Lab will launch into your default web browser.
Part 2: Learning to Use Jupyter Lab
Complete the notebook in the archive.
Part 3: The Game of Life
BACKGROUND

The Game of Life ("Conway's Life" or just "Life") is a cellular automation that simulates a self-
replicating system and was devised by John Conway, a British mathematician. It first appeared
in the October 1970 issue of Scientific American. Life grew out of early research into robots
creating robots and self-replicating robots.

The universe Life simulates is a two-dimensional grid of square cells, each of which has one of
two possible states, live or dead. Every cell interacts with its eight neighbors, which are the cells
that are horizontally, vertically, or diagonally adjacent. At each step in time, the following
operations occur:

• Any live cell with less than two live neighbors dies from loneliness.
• Any live cell with two or three live neighbors lives on to the next generation.
• Any live cell with more than three live neighbors dies from overpopulation.
• Any dead cell with exactly three live neighbors becomes a live cell -- a birth.
Another way to state these rules is this:

• Any live cell with two or three live neighbors survives.


• Any dead cell with three live neighbors becomes a live cell.
• All other live cells die in the next generation. Similarly, all other dead cells stay dead.
The initial pattern of live and dead cells is the seed of the system. The first generation is created
by applying the above rules simultaneously to every cell in the seed, live or dead. Births and
deaths occur simultaneously and each generation is created from the preceding one. The rules
continue to be applied repeatedly to create further generations.

Several interesting phenomenon may occur during the simulation.

Stable or Still Life -- sets of cells that all have two or three neighbors and so remain
alive every generation.

Oscillators -- shapes that repeat over a period of generations. The simplest oscillator is
a set of three cells in a line that alternates between horizontal and vertical each
generation.
Spaceships -- shapes that oscillate, but shift position each time they return to their
original shape so they appear to move across the grid. The simplest spaceship is the
glider, which we will create in this lab. We will also create a Gosper Glider Gun that
sends out gliders as it oscillates.

Sadly, it is also possible for all cells to eventually die and the species to go extinct.
(Portions of this background are from Wikipedia.)

STEP A: SET-UP

• Create a new notebook in Jupyter Lab


• Select the Python 3 kernel
• Save and name the notebook yourUsername-wk1.ipynb.
• Change the first cell to be a Markdown cell. In that cell, create a heading "The Game of Life"
and add your name in the byline.

For example, this markdown:


## *Game of Life*
### by Timothy Henry

generates this:

Game of Life
by Timothy Henry

STEP B: IMPORT IMPORTANT PYTHON LIBRARIES

The first code cell should always import necessary libraries. The aliases you see here are
common aliases you’ll see in many code examples and throughout this course.

• Create a new code cell and enter the following:


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as ani
from IPython.display import HTML

STEP C: INITIALIZATION AND CONFIGURATION

Let’s initialize some constants and variables that help determine how the animation will run. We
write them in this notebook cell so that they are easier to find and change if we want to change
the simulation.
• Theses constants are used so it is easier to read how the grid is initialized:
# Game Constants
LIVE = 1
DEAD = 0
STATUS = [LIVE, DEAD]

• We are going to create three different starting configurations. Feel free to create more if you
like! The variable gameSeed determines which configuration is used for this run.

# initial cells that are LIVE


# can be 'glider', 'gosper' or 'random'
gameSeed = 'glider'

• For our random initial configuration we set the probability that a cell is live or dead here.
# for random cell initialization, probability of live or dead cells
probabilityLive = 0.15
probabilityDead = 1.0 - probabilityLive

• Let’s set the gridSize and initialize it with zeros


# create empty grid of zero gridSize X gridSize
gridSize = 100
grid = np.zeros(gridSize * gridSize).reshape(gridSize, gridSize)

• Finally, we have some variables to hold animation settings.


# for animation
updateInterval = 75
animationFrames = 1000

STEP D: CREATE SEED SHAPES

Each of the different seed shapes has its own function. A good practice it to place each of the
functions in its own notebook cell.

• The first function initializes the grid randomly.


# returns a grid of gridSize * gridSize random values
def seedRandom(gridSize):
gridValues = np.random.choice(STATUS,
gridSize * gridSize,
p=[probabilityLive, probabilityDead])
return gridValues.reshape(gridSize, gridSize)

• This function creates a small array with the initial glider spaceship shape. If this is the
selected shape, the small array is use to overwrite the main grid’s cells starting at (i, j).

# adds a glider with top left cell at (i, j)


def seedGlider(i, j, grid):
glider = np.array([ [DEAD, DEAD, LIVE],

You might also like