This project is a port to Scala 2.11 of the 2009 Ant colony simulation in Clojure from a talk by Rich Hickey : original clojure source here or here. (According to this thread, the clojure code is rather dated so a version updated to clojure 1.4 is suggested here).
Features of the clojure ants simulation:
- The clojure demo demonstrates the use of Clojure "agents" with one agent per ant accessing a shared world state via Clojure refs using the clojure STM.
- The world is made up of Cells where each cell is a clojure Ref.
- The ants attempt to operate on the world of cells, relying on the clojure STM to rollback or commit conflicts with other ants.
- There is a clojure "animator" agent that takes a consistent snapshot of the world in order to display it.
- There is a clojure "evaporator" agent that evaporates the pheromones left by the foraging ants.
- The code is implicitly multi threaded and in principle can use all available processors to access shared state using clojure's controlled STM semantics.
Features of this scala version:
- It uses the Scala STM for Refs and and Akka actors instead of clojure agents.
- It follows the clojure implementation very closely.
- It uses the Scala STM "Ref" class for managing shared state - this is a direct analog of Clojure "Ref" type.
- Scala does not have a direct equivalent of Clojure agents, so Akka "Actors" are used in a similar way to the clojure agents. (This is not an idiomatic usage of Akka actors. Conceptually "Actors" are intended never to share state and only communicate using message passing.)
- The code is implicitly multi threaded and uses all available processors. (This can be seen if you set the ant sleep time to zero on startup).
- On startup, the application allows you to select the number of ants to use, the ant sleep time, and whether the animator should "stop-the-world" to get a consistent world view.
- For reasons I haven't tried to understand, Akka's default fork-join-executor actor dispatcher does not allow the "animator" actor any processing time, when more than 16 ants are used. So to make it work for any number of ants, the default dispatcher has bee set to use a "thread-pool-executor" and everything works beautifully.
Rich Hickey (Credit also to to Peter Vlugter for the original port in 2009 to Scala 2.8)
To build and run you need sbt Simple Build Tool
Fetch this project with
git clone https://github.com/azzoti/Scala-STM-Ants.git
cd Scala-STM-Ants
To run Ants use "sbt run":
sbt run
Project based on the Clojure ants simulation by Rich Hickey.
Copyright (c) Rich Hickey. All rights reserved. The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl1.0.php) which can be found in the file cpl.txt at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.