{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Some settings to show a JS animation\n", "import matplotlib.pyplot as plt\n", "plt.rcParams[\"animation.html\"] = \"jshtml\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import seagull as sg\n", "import seagull.lifeforms as lf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simple Pulsars\n", "\n", "In this example, we'll create four (4) `Pulsars` in a 40x40 board. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Initialize board\n", "board = sg.Board(size=(40,40))\n", "\n", "# Add three Pulsar lifeforms in various locations\n", "board.add(lf.Pulsar(), loc=(1,1))\n", "board.add(lf.Pulsar(), loc=(1,22))\n", "board.add(lf.Pulsar(), loc=(20,1))\n", "board.add(lf.Pulsar(), loc=(20,22))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The view command allows us to see the current state of the `Board`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "board.view()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running the simulation returns a set of statistics that characterizes your run:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Simulate board\n", "sim = sg.Simulator(board)\n", "sim.run(sg.rules.conway_classic, iters=100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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](http://louistiao.me/posts/notebooks/save-matplotlib-animations-as-gifs/)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "anim = sim.animate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "anim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Small ecosystem\n", "\n", "In this example, we'll demonstrate the diversity of our `Lifeform`s and see how they interact with one another!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "board = sg.Board(size=(30,30))\n", "board.add(lf.Glider(), loc=(4,4))\n", "board.add(lf.Glider(), loc=(10,4))\n", "board.add(lf.Glider(), loc=(15,4))\n", "board.add(lf.Pulsar(), loc=(5,12))\n", "board.add(lf.Blinker(length=3), loc=(22,4))\n", "board.add(lf.Blinker(length=3), loc=(22,8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "# Simulate board\n", "sim = sg.Simulator(board)\n", "stats = sim.run(sg.rules.conway_classic, iters=100)\n", "anim = sim.animate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "anim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Custom lifeform\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "board = sg.Board(size=(30,30))\n", "\n", "# Our custom lifeform\n", "custom_lf = lf.Custom([[0, 0, 1, 1, 0],\n", " [1, 1, 0, 1, 1],\n", " [1, 1, 1, 1, 0],\n", " [0, 1, 1, 0, 0]])\n", " \n", "board.add(custom_lf, loc=(1,1))\n", "board.add(custom_lf, loc=(10,1))\n", "board.add(custom_lf, loc=(20,1))\n", "board.add(custom_lf, loc=(5, 10))\n", "board.add(custom_lf, loc=(15, 10))\n", "board.add(custom_lf, loc=(10, 20))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "sim = sg.Simulator(board)\n", "stats = sim.run(sg.rules.conway_classic, iters=100)\n", "anim = sim.animate()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "anim" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }