40
How to Evolve Life in a Computer using Python Bert Chan Big Data Consultant @ ASL PyCon HK / Code Conf 2018

How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

How to Evolve Lifein a Computer

using Python

Bert ChanBig Data Consultant @ ASL

PyCon HK / Code Conf 2018

Page 2: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Programming in the 1990’s

• PC: 80286 (8MHz, 8MB RAM)

• OS: MS-DOS (Win3.1 too slow!)

• Pascal• Simulate life

• Simulate gravity, fractals

• Hack & decode games

• Assembly• Main loop – very fast!

• Direct write to video cache

Page 3: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

If you did coding and hacked stuffs in the 90’s, you’re a…

Page 4: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Simulate Life

• Conway’s Game of Life• John Conway 1970

• Cellular Automata – array of cells (0 or 1)

• Neighborhood (8 cells, sum)

• Simple if-then-else rulefor cell in cells:

if cell==1 and sum in [2, 3]: cell = 1 #survive

elif cell==0 and sum in [3]: cell = 1 #born

else: cell = 0 #die

cell: 0 or 1

neighborhood

0 1 1

0 0 0

1 0 0

Page 5: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Conway’s Game of Life

• The moving glider

• Green cells survive, blue cells are born, red cells die

Page 6: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Conway’s Game of Life

• Spaceships, glider gun…

• Logic gate, clock, computer…

• Hackers love it!

• Good way to learn programming!

Page 7: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Play with the rules

• What if we…

cell: 0 or 1

0 1 0

0 0 0

0 0 0

sum = σ𝑛cell = (if sum … then … else …)

neighborhood (n)

Page 8: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Play with the rules

• What if we…• Use floating point?

0.95 0.36 0.17

0.46 0.53 0.22

0.12 0.02 0.00

sum = σ𝑛cell = (if sum … then … else …)

real number

neighborhood (n)

Page 9: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

0.36

Play with the rules

• What if we…• Use floating point?

• Bigger neighborhood? Circular?

neighborhood (n)

sum = σ𝑛cell = (if sum … then … else …)

real number

Page 10: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Play with the rules

• What if we…• Use floating point?

• Bigger neighborhood? Circular?

• Weighted sum?

neighborhood (n)

cell = (if sum … then … else …)

weights (k)

*

real number

sum = σ𝑛𝑘

Page 11: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Play with the rules

• What if we…• Use floating point?

• Bigger neighborhood? Circular?

• Weighted sum?

• Smooth update?

neighborhood (n) weights (k)

*

sum = σ𝑛𝑘

real number

cell = cell + 0.1 * f(sum)

Page 12: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Play with the rules

• What if we…• Use floating point?

• Bigger neighborhood? Circular?

• Weighted sum?

• Smooth update?

• Spooky things happened…

OMG WHAT IS THIS??

Page 13: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Lenia

• New kind of Artificial Life• Microorganism-like creatures

• Discovered 400+ species

• Study their anatomy, behavior, physiology…

• Good programming exercise• JavaScript, C#, MATLAB, Python

Page 14: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Video

• Python → showcase video

• https://vimeo.com/277328815

Page 15: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Kyoto

• Won GECCO Virtual Creatures Contest, Kyoto

• Honorable Mention in ALIFE Art Award, Tokyo

• Meet my AI hero – @hardmaru• David Ha (Google Brain Tokyo)

Page 16: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Using Pythonfor PyCon HK

Page 17: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Why Python?

• Good performance

• Fast coding

• Nice syntax (indent, list comprehension, etc)

• Lots of useful libraries

• Vibrant community (PyCon, GitHub…)

Page 18: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Python Libraries

Array calculations

NumPy

GPU acceleration

PyOpenCL / PyCUDA

Image processing

PIL/Pillow

Interactive UI

Tkinter

Record video

subprocess + ffmpeg

ReiknaSciPy /

OpenCVMatplotlib ffmpeg-python

• “Rule 34” of Python• “If there is a need, there is a Python library for it.”

Page 19: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

NumPy

• Fast array calculations✓Machine learning, deep learning

✓Basis of image processing, time-series

✓Cellular automata (weighted sum using FFT)

• Main loop of Lenia in 3 linespotential_fft = np.fft.fft2(cells) * kernel_fft

potential = np.fft.fftshift(np.real(np.fft.ifft2(potential_fft))

cells_new = np.clip(cells + dt * g(potential, m, s), 0, 1)

Page 20: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

PyOpenCL/PyCUDA + Reikna

• GPU acceleration• (NVIDIA) CUDA → PyCUDA

• (Apple) OpenCL → PyOpenCL

Page 21: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

PyOpenCL/PyCUDA + Reikna

• Reikna• PyOpenCL/PyCUDA wrapper

• Compiles the GPU code for you

• GPU accelerated FFT in a few linesgpu_thr = reikna.cluda.any_api().Thread.create()

gpu_fft = reikna.fft.FFT(cells.astype(np.complex64)).compile(gpu_thr)

op_dev = gpu_thr.to_device(cells.astype(np.complex64))

gpu_fft(op_dev, op_dev, **kwargs)

cells = op_dev.get()

Page 22: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

PIL/Pillow, SciPy, OpenCV

• Image handling• PIL (Python Image Lib) → pillow• Create image, draw lines/texts, save GIF…

img = PIL.Image.frombuffer('P', buffer.shape, buffer, …)draw = PIL.ImageDraw.Draw(img)img[0].save(path, format='GIF', append_images=self.gif[1:], loop=0 …)

• Image processing• SciPy

scipy.ndimage.rotate(A, reshape=False, order=0, mode='wrap’)

• OpenCV-Python

Page 23: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Tkinter vs Matplotlib

• Interactive UI• Real-time 2D image display

• Menu, keyboard binding, clipboard

• Matplotlib• For data visualization

• Powerful but slow…

• Tkinter (Toolkit interface)• Basic and fast

• Others: wxPython, PyQt, PyGTK…

Page 24: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Tkinter vs Matplotlib

• Interactive UIwin = tk.Tk()

tk.Canvas()

tk.Menu()

win.bind('<Key>', key_press_event)

win.clipboard_get()

• Python 3import tkinter as tk

Page 25: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

subprocess + ffmpeg

• Pythonic FFmpeg wrappers• ffmpeg-python, ffmpy, etc.

• Pipe video to ffmpegcmd = ['/usr/local/bin/ffmpeg’, '-f','rawvideo', …]

video = subprocess.Popen(cmd, stdin=subprocess.PIPE)

for img in imgs:

video.stdin.write(img.convert('RGB').tobytes())

video.stdin.close()

Page 26: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About ALife and AIfor HK Code Conf

Page 27: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Lenia

• Not just funny creatures

• Using AI to create ALife

Page 28: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About ALife

• Artificial Life• Simulate biological life or create new lifeforms

• → Create a body

Wet ALife = biochemistry, synthetic lifeArtificial cell, expanded genetic code…

Hard ALife = hardware, robots & machinesHumanoids, Strandbeest…

Soft ALife = software, simulationsCellular automata, virtual creatures…

Page 29: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About ALife

Wet ALife = biochemistry, synthetic lifeArtificial cell, expanded genetic code…

Hard ALife = hardware, robots & machinesHumanoids, Strandbeest…

Soft ALife = software, simulationsCellular automata, virtual creatures…

Synthetic cell (JCVI, 2010)

Expanded DNA (TSRI, 2014)

Page 30: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About ALife

Wet ALife = biochemistry, synthetic lifeArtificial cell, expanded genetic code…

Hard ALife = hardware, robots & machinesHumanoids, Strandbeest…

Soft ALife = software, simulationsCellular automata, virtual creatures…

Strandbeest (Theo Jansen, 1990)

Atlas (Boston Dynamics, 2017)

Page 31: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About ALife

Wet ALife = biochemistry, synthetic lifeArtificial cell, expanded genetic code…

Hard ALife = hardware, robots & machinesHumanoids, Strandbeest…

Soft ALife = software, simulationsCellular automata, virtual creatures…

Virtual creatures (Karl Sims, 1994)

Soft robots (Nick Cheney, 2014)

Page 32: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

ML = Machine LearningSupervised, unsupervised, reinforced

About AI

GOFAI = Good old-fashioned AISymbolic, expert systems

DL = Deep LearningDeep neural nets + big data + many GPUEA = Evolutionary Algorithms

Neuro-evolution, novelty, etc

• Artificial Intelligence• Machines do: learning, planning, vision, language, emotion, art

• → Create a mind

Page 33: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About AI

ML = Machine LearningSupervised, unsupervised, reinforced

GOFAI = Good old-fashioned AISymbolic, expert systems

DL = Deep LearningDeep neural nets + big data + many GPU

EA = Evolutionary AlgorithmsNeuro-evolution, novelty, etc

Deep Blue vs. Kasparov (IBM, 1997)

Watson in Jeopardy! (IBM, 2011)

Page 34: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About AI

ML = Machine LearningSupervised, unsupervised, reinforced

GOFAI = Good old-fashioned AISymbolic, expert systems

DL = Deep LearningDeep neural nets + big data + many GPU

EA = Evolutionary AlgorithmsNeuro-evolution, novelty, etc

AlphaGo vs. Lee Sedol (DeepMind, 2016)

BigGAN (Andrew Brock, 2018)

Autopilot (Tesla, 2014)

Page 35: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

About AI

ML = Machine LearningSupervised, unsupervised, reinforced

GOFAI = Good old-fashioned AISymbolic, expert systems

DL = Deep LearningDeep neural nets + big data + many GPU

EA = Evolutionary AlgorithmsNeuro-evolution, novelty, etc

PicBreeder (EPlex, 2007)

Evolutionary AutoML (Google Brain, 2017)

Page 36: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

DL = Deep Learning(Gradient Descent)

EA = Evolutionary Algorithm(Natural Selection)

Page 37: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Towards AGI

Artificial Life

Deep Learning

EvolutionAlgo

• Artificial General Intelligence• Sapience, sentience, consciousness

• When? How? Should we?

Page 38: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Towards AGI

Learning, Planning(Reinforcement Learning?)

Senses, Communication(Deep Learning?)

Curiosity, Creativity(Evolutionary Algorithms?)

Body, Actions(Artificial Life)

Safety, Ethics(AI Safety)

Knowledge, Reasoning(Symbolic AI?)

Emotions, Empathy(Artificial Empathy)

Consciousness?Mind? Soul?

Lenia

Page 39: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Use Lenia to…

• Understand evolution by writing programs?

• Teach AI to be curious and creative?

• Teach AI to understand life?

Page 40: How to Evolve Life using Python - hongkong.codeconf.io to Evolve Life.pdf · Cellular automata, virtual creatures… Virtual creatures (Karl Sims, 1994) Soft robots (Nick Cheney,

Thank You!

Bert Chanchakazul.github.io@BertChakovsky