Class: Micro::World

Inherits:
Object
  • Object
show all
Defined in:
lib/micro_agent.rb

Overview

The world populates

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#agentsObject

Returns the value of attribute agents.



9
10
11
# File 'lib/micro_agent.rb', line 9

def agents
  @agents
end

#begin_procObject

Returns the value of attribute begin_proc.



9
10
11
# File 'lib/micro_agent.rb', line 9

def begin_proc
  @begin_proc
end

#end_procObject

Returns the value of attribute end_proc.



9
10
11
# File 'lib/micro_agent.rb', line 9

def end_proc
  @end_proc
end

#step_procObject

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_agentsObject



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

#startObject



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_agentsObject



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

#stopObject



35
36
37
# File 'lib/micro_agent.rb', line 35

def stop
  EM.stop
end