Basic Usage¶
This notebook illustrates some of the basic features of Seagull
. Most of these are just animated outputs found in the Documentation. If you wish to add more examples, please create a Jupyter notebook and open up a Pull Request!
[1]:
# Some settings to show a JS animation
import matplotlib.pyplot as plt
plt.rcParams["animation.html"] = "jshtml"
[2]:
import seagull as sg
import seagull.lifeforms as lf
Simple Pulsars¶
In this example, we’ll create four (4) Pulsars
in a 40x40 board.
[3]:
# Initialize board
board = sg.Board(size=(40,40))
# Add three Pulsar lifeforms in various locations
board.add(lf.Pulsar(), loc=(1,1))
board.add(lf.Pulsar(), loc=(1,22))
board.add(lf.Pulsar(), loc=(20,1))
board.add(lf.Pulsar(), loc=(20,22))
The view command allows us to see the current state of the Board
[4]:
board.view()
[4]:
(<Figure size 360x360 with 1 Axes>,
<matplotlib.image.AxesImage at 0x7fec0720b7d0>)
Running the simulation returns a set of statistics that characterizes your run:
[5]:
# Simulate board
sim = sg.Simulator(board)
sim.run(sg.rules.conway_classic, iters=100)
2020-11-08 02:21:21.266 | INFO | seagull.simulator:compute_statistics:128 - Computing simulation statistics...
[5]:
{'peak_cell_coverage': 0.18,
'avg_cell_coverage': 0.1463366336633663,
'avg_shannon_entropy': 3.0217796046186556,
'peak_shannon_entropy': 3.2433182601909962}
The animation command returns a matplotlib.FuncAnimation
that you can use to show or save your animation. Note that sometimes, saving to GIF or MP4 might require other dependencies such as ffmpeg
or ImageMagick
. For more information, please check this blog post.
[6]:
%%capture
anim = sim.animate()
2020-11-08 02:21:21.289 | INFO | seagull.simulator:animate:183 - Rendering animation...
[7]:
anim
[7]:
Small ecosystem¶
In this example, we’ll demonstrate the diversity of our Lifeform
s and see how they interact with one another!
[8]:
board = sg.Board(size=(30,30))
board.add(lf.Glider(), loc=(4,4))
board.add(lf.Glider(), loc=(10,4))
board.add(lf.Glider(), loc=(15,4))
board.add(lf.Pulsar(), loc=(5,12))
board.add(lf.Blinker(length=3), loc=(22,4))
board.add(lf.Blinker(length=3), loc=(22,8))
[9]:
%%capture
# Simulate board
sim = sg.Simulator(board)
stats = sim.run(sg.rules.conway_classic, iters=100)
anim = sim.animate()
2020-11-08 02:21:24.578 | INFO | seagull.simulator:compute_statistics:128 - Computing simulation statistics...
2020-11-08 02:21:24.588 | INFO | seagull.simulator:animate:183 - Rendering animation...
[10]:
anim
[10]:
Custom lifeform¶
Lastly, we’ll create our very first custom lifeform! Defining it is just the same as instantiating any other pre-made lifeform. However in this example, we’ll save the instance in a variable custom_lf
so that we can place it easily without writing the whole matrix again and again.
[11]:
board = sg.Board(size=(30,30))
# Our custom lifeform
custom_lf = lf.Custom([[0, 0, 1, 1, 0],
[1, 1, 0, 1, 1],
[1, 1, 1, 1, 0],
[0, 1, 1, 0, 0]])
board.add(custom_lf, loc=(1,1))
board.add(custom_lf, loc=(10,1))
board.add(custom_lf, loc=(20,1))
board.add(custom_lf, loc=(5, 10))
board.add(custom_lf, loc=(15, 10))
board.add(custom_lf, loc=(10, 20))
[12]:
%%capture
sim = sg.Simulator(board)
stats = sim.run(sg.rules.conway_classic, iters=100)
anim = sim.animate()
2020-11-08 02:21:27.798 | INFO | seagull.simulator:compute_statistics:128 - Computing simulation statistics...
2020-11-08 02:21:27.807 | INFO | seagull.simulator:animate:183 - Rendering animation...
[13]:
anim
[13]: