| @bryanl | @thunderboltlabs | Sim(foo) SIMULATION_LENGTH = 100 PEOPLE_COUNT = 100 def create_people(people_count); end def report; end def simulate_to(count) 1.upto(count) {|i| yield i } end people = create_people(PEOPLE_COUNT) simulate_to(SIMULATION_LENGTH) do |iteration| # mish and mash people end report
| @bryanl | @thunderboltlabs | Sim(foo) class Simulation def transmit(iteration) current_pairs(iteration).each{|pair| pair.contact(iteration)} end end class Pair def contact(iteration) @alpha.infect(iteration, @beta) @beta.infect(iteration, @alpha) end end class Person def infect(iteration, person); end def acquire(infection); end end
| @bryanl | @thunderboltlabs | Sim(foo) class Person def progress_infections(iteration) @infections.each do |infection| if rand <= infection.chance_to_acquire_disease unless has_disease? infection.disease @diseases << infection.disease.new(iteration) end end end end end
| @bryanl | @thunderboltlabs | Sim(foo) #!/usr/bin/env ruby require 'benchmark' n,m = [1_000_000, 100] Benchmark.bm do |x| x.report do m.times do a = []; n.times{|i| a[i] = i}; n.times{ j=a[rand(n)]; j*j} end end x.report do m.times do h = {}; n.times{|i| h[i] = i}; n.times{j=h[rand(n)]; j*j} end end end
| @bryanl | @thunderboltlabs | Sim(foo) -> ❸ % ruby discrete.rb 36500 1000 creating 1000 people inoculating 50% of the population circle circle dot dot. now you have a cootie shot starting simulation 3650 7300 10950 14600 18250 21900 25550 29200 32850 36500 runtime: 129.49060773849487 Infected: 479 Noninfected: 521
| @bryanl | @thunderboltlabs | Sim(foo) -> ♢ % ruby discrete.rb 36500 1000 creating 1000 people inoculating 50% of the population circle circle dot dot. now you have a cootie shot starting simulation 3650 7300 10950 14600 18250 21900 25550 29200 32850 36500 runtime: 129.88899993896484 Infected: 526 Noninfected: 474
| @bryanl | @thunderboltlabs | Sim(foo) class PairEvent < Event def initialize(person) @person = person end def run # add person to “available # to pair pool” end end