Introducing agent simulation: an actor redefinition example
authorBlackMinou <romain.chanoir@viacesi.fr>
Mon, 8 May 2017 17:59:28 +0000 (13:59 -0400)
committerBlackMinou <romain.chanoir@viacesi.fr>
Wed, 31 May 2017 02:39:27 +0000 (22:39 -0400)
Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

lib/actors/examples/agent_simulation/agent_simulation.nit [new file with mode: 0644]
lib/actors/examples/agent_simulation/simple_simulation.nit [new file with mode: 0644]

diff --git a/lib/actors/examples/agent_simulation/agent_simulation.nit b/lib/actors/examples/agent_simulation/agent_simulation.nit
new file mode 100644 (file)
index 0000000..35fe57c
--- /dev/null
@@ -0,0 +1,60 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# a "Framework" to make Multi-Agent Simulations in Nit
+module agent_simulation is no_warning("missing-doc")
+
+import actors
+
+# Master of the simulation, it initiates the steps and waits for them to terminate
+class ClockAgent
+       actor
+
+       # Number of steps to do in the simulation
+       var nb_steps: Int
+
+       # List of Agents in the simulation
+       var agents: Array[Agent]
+
+       # Number of agents that finished their step
+       var nb_finished = 0
+
+       fun do_step do
+               for a in agents do a.async.do_step
+               nb_steps -= 1
+       end
+
+       fun finished_step do
+               nb_finished += 1
+               if nb_finished == agents.length then
+                       nb_finished = 0
+                       if nb_steps != 0 then async.do_step
+               end
+       end
+end
+
+class Agent
+       actor
+
+       # Doing a step in the simulation
+       fun do_step do
+       end
+
+       fun end_step do clock_agent.async.finished_step
+
+end
+
+redef class Sys
+       var clock_agent: ClockAgent is noautoinit,writable
+end
diff --git a/lib/actors/examples/agent_simulation/simple_simulation.nit b/lib/actors/examples/agent_simulation/simple_simulation.nit
new file mode 100644 (file)
index 0000000..d8be6c6
--- /dev/null
@@ -0,0 +1,57 @@
+# This file is part of NIT (http://www.nitlanguage.org).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# 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
+
+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