Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Kaigi 2015: Prepare yourself against Zombie epidemic

Ruby Kaigi 2015: Prepare yourself against Zombie epidemic

The news is everywhere: some weird disease makes the dead walking. We do not know yet if it is highly contagious. What should we do? What we do everyday: writing code.

This couldn't be a better moment to use an agent based model — a technique that simulates interactions between agents in a environment to understand their effects as a whole. For such, we'll visit its minimal Ruby implementation, address some common design, simulate the Zombie epidemic, visualize it, and test different survival strategies to hopefully find the best one.

We can code, we'll be prepared ... or not.

The code: https://github.com/toch/zombie_epidemic

Christophe Philemotte

December 14, 2015
Tweet

More Decks by Christophe Philemotte

Other Decks in Programming

Transcript

  1. Benjamin Lehman, ©2011

    View full-size slide

  2. Benjamin Lehman, ©2011
    Prepare yourself
    against
    Zombie ゾンビ
    epidemic

    View full-size slide

  3. Agent
    Based
    Model

    View full-size slide

  4. Agent
    Based
    Model

    View full-size slide

  5. Map
    National Geographics, ©2011

    View full-size slide

  6. map = Map.new(4, 4)

    View full-size slide

  7. point = Point.new
    point_north = Point.new
    point.neighborhood[:north] = point_north
    point_north.neighborhood[:south] = point
    # ...

    View full-size slide

  8. John DellAngelo, ©2013
    Agent

    View full-size slide

  9. agent = Agent.new(age, start_point, stm)

    View full-size slide

  10. agent.perceive
    # => {}

    View full-size slide

  11. agent.act
    # => :walk
    agent.walk(:east)

    View full-size slide

  12. agent.age
    # => 1

    View full-size slide

  13. agent.age
    agent.commit
    # => ## @name=:susceptible, ...

    View full-size slide

  14. state = State.new(:susceptible)
    state.define_possible_actions(
    ->(agent) {
    [:walk, :stay, :fight].sample
    }
    )

    View full-size slide

  15. state.decide_action_for(agent)
    # => :walk

    View full-size slide

  16. infected = State.new(:susceptible)
    state.add_transition(
    infected,
    ->(state, agent) {
    true
    }
    )

    View full-size slide

  17. state.trigger_transition(agent)
    # => ## @name=:infected, ...

    View full-size slide

  18. State
    Transition
    Machine

    View full-size slide

  19. stm = StateTransitionMachine.new
    # create states:
    # - susceptible
    # - infected
    # - zombie
    # - dead
    # create transitions
    # set default initial state

    View full-size slide

  20. Simulation
    Loop

    View full-size slide

  21. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  22. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  23. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  24. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  25. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  26. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  27. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  28. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  29. steps = 100
    agents = create_the_agents
    steps.times do
    agents.each do |agent|
    agent.act; agent.age
    end
    agents.each { |agent| agent.commit }
    remove_the_deads(agents)
    end

    View full-size slide

  30. Validation
    &
    Calibration

    View full-size slide

  31. # validation
    # ==
    # functional test

    View full-size slide

  32. # Calibration
    # f(x, y, z, ?) = a

    View full-size slide

  33. Simulate
    &
    Prepare yourself

    View full-size slide

  34. ?
    toch
    _toch
    toch/zombie_epidemic

    View full-size slide