agent_simulation
by refining the Agent class to makea multi-agent simulation where every agent know each other
The steps consist of each agent greeting each other, and
waiting for every other agent to respond before notifying
to the ClockAgent
that they finished their step.
actors :: simple_simulation $ Agent
actors :: simple_simulation $ Agent
pthreads :: concurrent_collections
Introduces thread-safe concurrent collectionscore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Using `agent_simulation` by refining the Agent class to make
# a multi-agent simulation where every agent know each other
# The steps consist of each agent greeting each other, and
# waiting for every other agent to respond before notifying
# to the `ClockAgent` that they finished their step.
module simple_simulation is example
import agent_simulation
redef class Agent
var others = new Array[Agent]
var count = 0
fun greet(message: String, other: Agent) do other.async.greet_back("Hello back !")
fun greet_back(message: String) do
count -= 1
if count == 0 then end_step
end
redef fun do_step do
for o in others do
o.async.greet("Hello !", self)
count += 1
end
end
end
var nb_steps = 0
var nb_agents = 0
if args.is_empty or args.length != 2 then
nb_steps = 10
nb_agents = 10
else
nb_steps = args[0].to_i
nb_agents = args[1].to_i
end
var agents = new Array[Agent]
for i in [0..nb_agents[ do agents.add(new Agent)
for a in agents do for b in agents do if a != b then a.others.add(b)
clock_agent = new ClockAgent(nb_steps, agents)
clock_agent.async.do_step
lib/actors/examples/agent_simulation/simple_simulation.nit:15,1--57,25