Class: Micro::World
- Inherits:
-
Object
- Object
- Micro::World
- Defined in:
- lib/micro_agent.rb
Overview
The world populates
Instance Attribute Summary collapse
-
#agents ⇒ Object
Returns the value of attribute agents.
-
#begin_proc ⇒ Object
Returns the value of attribute begin_proc.
-
#end_proc ⇒ Object
Returns the value of attribute end_proc.
-
#step_proc ⇒ Object
Returns the value of attribute step_proc.
Instance Method Summary collapse
- #create_agents ⇒ Object
-
#initialize(cycle_delay_seconds, number_of_agents, percent_per_cycle = 1.0, step_proc = nil, begin_proc = nil, end_proc = nil, &block) ⇒ World
constructor
A new instance of World.
- #start ⇒ Object
- #step_agents ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(cycle_delay_seconds, number_of_agents, percent_per_cycle = 1.0, step_proc = nil, begin_proc = nil, end_proc = nil, &block) ⇒ World
Returns a new instance of World.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/micro_agent.rb', line 11 def initialize(cycle_delay_seconds, number_of_agents, percent_per_cycle = 1.0, step_proc = nil, begin_proc = nil, end_proc = nil, &block) @cycle_delay_seconds = cycle_delay_seconds @step_proc = step_proc @begin_proc = begin_proc @end_proc = end_proc @number_of_agents = number_of_agents @create_agent_proc = block @percent_per_cycle = percent_per_cycle create_agents end |
Instance Attribute Details
#agents ⇒ Object
Returns the value of attribute agents.
9 10 11 |
# File 'lib/micro_agent.rb', line 9 def agents @agents end |
#begin_proc ⇒ Object
Returns the value of attribute begin_proc.
9 10 11 |
# File 'lib/micro_agent.rb', line 9 def begin_proc @begin_proc end |
#end_proc ⇒ Object
Returns the value of attribute end_proc.
9 10 11 |
# File 'lib/micro_agent.rb', line 9 def end_proc @end_proc end |
#step_proc ⇒ Object
Returns the value of attribute step_proc.
9 10 11 |
# File 'lib/micro_agent.rb', line 9 def step_proc @step_proc end |
Instance Method Details
#create_agents ⇒ Object
22 23 24 25 26 27 |
# File 'lib/micro_agent.rb', line 22 def create_agents @agents = [] @number_of_agents.times do |i| @agents << @create_agent_proc.call(i) end end |
#start ⇒ Object
29 30 31 32 33 |
# File 'lib/micro_agent.rb', line 29 def start EM.run do EventMachine::add_periodic_timer( @cycle_delay_seconds ) { step_agents } end end |
#step_agents ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/micro_agent.rb', line 39 def step_agents @begin_proc.call unless @begin_proc.nil? @agents.each do |agent| next unless rand <= @percent_per_cycle agent.step @step_proc.call(agent) unless @step_proc.nil? end @end_proc.call unless @end_proc.nil? end |
#stop ⇒ Object
35 36 37 |
# File 'lib/micro_agent.rb', line 35 def stop EM.stop end |