Created
April 5, 2013 10:56
-
-
Save ceth-x86/5318426 to your computer and use it in GitHub Desktop.
Exploring Everyday Things : Offices and Restrooms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DURATION = 9 * 60 # minutes | |
class Restroom | |
attr_reader :queue | |
attr_reader :facilities | |
def initialize(facilities_per_restroom = 3) | |
@queue = [] | |
@facilities = [] | |
facilities_per_restroom.times { @facilities << Facility.new } | |
end | |
def enter(person) | |
unoccupied_facility = @facilities.find { |facility| not facility.occupied? } | |
if unoccupied_facility | |
unoccupied_facility.occupy person | |
else | |
@queue << person | |
end | |
end | |
def tick | |
@facilities.each { |f| f.tick } | |
end | |
end | |
class Facility | |
def initialize | |
@occupier = nil | |
@duration = 0 | |
end | |
def occupy(person) | |
unless occupied? | |
@occupier = person | |
@duration = 1 | |
Person.population.delete person | |
true | |
else | |
false | |
end | |
end | |
def occupied? | |
not @occupier.nil? | |
end | |
def vacate | |
Person.population << @occupier | |
@occupier = nil | |
end | |
def tick | |
if occupied? and @duration > @occupier.use_duration | |
vacate | |
@duration = 0 | |
elsif occupied? | |
@duration += 1 | |
end | |
end | |
end | |
class Person | |
@@population = [] | |
attr_reader :use_duration | |
attr_accessor :frequency | |
def initialize(frequency=4,use_duration=1) | |
@frequency = frequency | |
@use_duration = use_duration | |
end | |
def self.population | |
@@population | |
end | |
def need_to_go? | |
rand(DURATION) + 1 <= @frequency | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
require './rest' | |
frequency = 3 | |
facilities_per_restroom = 3 | |
use_duration = 1 | |
population_range = 10..600 | |
data = {} | |
population_range.step(10).each do |population_size| | |
Person.population.clear | |
population_size.times { Person.population << Person.new(frequency, use_duration) } | |
data[population_size] = [] | |
restroom = Restroom.new facilities_per_restroom | |
DURATION.times do |t| | |
data[population_size] << restroom.queue.size | |
queue = restroom.queue.clone | |
restroom.queue.clear | |
unless queue.empty? | |
restroom.enter queue.shift | |
end | |
Person.population.each do |person| | |
if person.need_to_go? | |
restroom.enter person | |
end | |
end | |
restroom.tick | |
end | |
end | |
CSV.open('simulation1.csv', 'w') do |csv| | |
lbl = [] | |
population_range.step(10).each { |population_size| lbl << population_size } | |
csv << lbl | |
DURATION.times do |t| | |
row = [] | |
population_range.step(10).each do |population_size| | |
row << data[population_size][t] | |
end | |
csv << row | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment