= Agent Development Framework (ADF) Manual :author: RoboCup Rescue Simulation Team :revnumber: 4.1 :revdate: February 03, 2023 :size: A4 :reproducible: true :encode: UTF-8 :lang: en :sectids!: :sectnums: :sectnumlevels: 3 :toclevels: 3 :outlinelevels: 3 :xrefstyle: short :imagesoutdir: images :imagesdir: images :math: :stem: latexmath :source-highlighter: rouge :bibtex-file: references.bib :bibtex-style: apa :bibtex-order: alphabetical :bibtex-format: asciidoc :title-page: :toc: left <<< [#purpose] == Purpose The manual instructs how to install and execute the RoboCup Rescue Simulation Agent Development Framework (ADF) Sample Agents, and how to implement a new team of agents using the ADF Sample Agents. [#installation] == Installation This manual assumes the agents will run in a Linux machine even though it is possible to run them in Microsoft Windows or Apple macOS. We recommend to use Linux because it is open-source and most of the distributions have a good support from the users' community. If you have never used Linux before and intend to, we recommend starting with a user-friendly distribution, such as https://www.ubuntu.com/[Ubuntu] or https://getfedora.org[Fedora]. [#requirements] === Software Requirements * Git * OpenJDK Java 17 * Gradle * Utilities like `wget`, `bash`, `xterm`, `tar`, `gzip`, etc. + **NOTE:** If you are using Ubuntu, all of these utilities are present in the default software repositories. [#download] === Download You can download the sample agents with ADF by cloning the `https://github.com/roborescue/adf-sample-agent-java` repository. Clone this repository using the command [source,shell] ---- git clone https://github.com/roborescue/adf-sample-agent-java.git ---- [#directories] === Directories The `adf-sample-agent-java` contains multiple directories. The important directories are: * `config/`: ADF and Agent Modules' configuration files * `src/`: Sample agents' source codes * `precomp_data`: results of a precomputation for each type of agents [#compiling] === Compiling Execute the steps below to compile the ADF Sample Agent. [source,shell] ---- $ cd adf-sample-agent-java $ ./gradlew clean build ---- [#running] == Running There are two modes of execution of the simulation server and ADF Sample Agent: *Precomputation* and *Normal*. [#precomputation_mode] === Precomputation Mode In the precomputation mode, the simulator connects one agent of each type and allows them to write the computation results persistently. The sequence of commands to run the simulation server in precomputation mode are: [source,shell] ---- $ cd rcrs-server $ cd scripts $ ./start-precompute.sh -m ../maps/test/maps -c ../maps/test/config ---- See https://roborescue.github.io/rcrs-server/rcrs-server/index.html[RoboCup Rescue Simulator Manual] for further information on how to compile and run the RoboCup Rescue Simulator server. After running the simulation server for the precomputation, move to the ADF Sample Agent directory on another terminal window and run the agents executing the commands: [source,shell] ---- $ bash launch.sh -t 1,0,1,0,1,0 -h localhost -pre 1 & APID=$! ; sleep 120 ; kill $APID [START] Connect to server (host:localhost, port:27931) [INFO] Connected - adf.agent.platoon.PlatoonFire@756ec19c (PRECOMPUTATION) [INFO] Connected - adf.agent.platoon.PlatoonPolice@366bbbe (PRECOMPUTATION) [INFO] Connected - adf.agent.platoon.PlatoonAmbulance@2a453513 (PRECOMPUTATION) ******************** [FINISH] Connect PoliceForce (success:1) [FINISH] Connect AmbulanceTeam (success:1) [FINISH] Connect FireBrigade (success:1) [FINISH] Done connecting to server (3 agents) ---- Once the precomputation is completed, press _Control-C_ and type `bash kill.sh` to stop the simulation server of running. [source,shell] ---- Control-C $ bash kill.sh ---- [#normal_mode] === Normal Mode In the normal mode, the simulator connects all agents defined in the scenario and allows them to use the precomputation output (see <>). The sequence of commands to run the simulation server in normal mode are: [source,shell] ---- $ cd rcrs-server $ cd scripts $ bash start-comprun.sh ---- See https://roborescue.github.io/rcrs-server/rcrs-server/index.html[RoboCup Rescue Simulator Manual] for further information on how to compile and run the RoboCup Rescue Simulator server. After running the simulation server, move to the ADF Sample Agent directory on another terminal window and run the agents using the commands: [source,shell] ---- $ bash launch.sh -all [FINISH] Done connecting to server (3 agents) ---- [#develop_agent] == Develop your own agents using ADF This section explain how to implement your agents using the ADF Sample Agent as the starting point. [#workflow] === Workflow for coding your agents The steps necessary to code your own agents are: * Implement the customized modules * Change the `config/module.cfg` to point to the customized modules [#files] === Customize modules ADF is a modular framework whose modules were define in the `adf-core-java` (https://github.com/roborescue/adf-core-java) repository together with a set of default implementations. To implement your own team of agents, you have to implement the modules' Java interfaces correspondent to the behavior you want to customize. The default implementations of the modules' Java interfaces is available under the package `impl` in the `adf-core-java` repository. There you find default implementations for: * `adf.impl.centralized`: source code of the _central agents_. This is the type of agents whose only interaction with the world is through radio communication. There are three types of central agents: *Ambulance Centers*, *Fire Stations* and *Police Office*, and they are represented as buildings in the simulation server. * `adf.impl.extraction`: source code of the possible actions available to agents. * `adf.impl.module`: source code of the algorithms, e.g., path planning, clustering, target detection, etc. representing the agents' behavior. The modules are split into + -- * `adf.impl.module.algorithm` * `adf.impl.module.comm` * `adf.impl.module.complex` -- To customize any of these modules, you can copy modules' file you want to customize to you team agents' repository and make changes to the implementation. Then you need to change the references to your modules by modifying `config/module.cfg` file (see below). [#module_configuration] === Modules' configuration file The modules configuration file `config/module.cfg` indicates which class will be used as agents' module. <> shows part of the modules configuration file. The left-hand side of the colon indicates the module name, the right-hand side is the class name. In most cases, modules of which targets' problems are the same should refer to an identical class for all agent types. The example in <> is in `DefaultTacticsAmbulanceTeam.Search` and `DefaultTacticsFireBrigade.Search` indicates that both modules refer to `sample_team.module.complex.SampleSearch`. An usage example is shown in <>. [#lst:module_configuration] [source,text] .*Listing 1*. Part of a module configuration file ---- ## DefaultTacticsAmbulanceTeam DefaultTacticsAmbulanceTeam.HumanDetector : sample_team.module.complex.SampleHumanDetector DefaultTacticsAmbulanceTeam.Search : sample_team.module.complex.SampleSearch DefaultTacticsAmbulanceTeam.ExtActionTransport : adf.impl.extaction.DefaultExtActionTransport DefaultTacticsAmbulanceTeam.ExtActionMove : adf.impl.extaction.DefaultExtActionMove DefaultTacticsAmbulanceTeam.CommandExecutorAmbulance : adf.impl.centralized.DefaultCommandExecutorAmbulance DefaultTacticsAmbulanceTeam.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ## DefaultTacticsFireBrigade DefaultTacticsFireBrigade.HumanDetector : sample_team.module.complex.SampleHumanDetector DefaultTacticsFireBrigade.Search : sample_team.module.complex.SampleSearch DefaultTacticsFireBrigade.ExtActionFireRescue : adf.impl.extaction.DefaultExtActionFireRescue DefaultTacticsFireBrigade.ExtActionMove : adf.impl.extaction.DefaultExtActionMove DefaultTacticsFireBrigade.CommandExecutorFire : adf.impl.centralized.DefaultCommandExecutorFire DefaultTacticsFireBrigade.CommandExecutorScout : adf.impl.centralized.DefaultCommandExecutorScout ---- [#astar_example] === Example of implementing A* algorithm for Path Planning algorithm In this example, you will learn how to implement the A* Path Planning algorithm in a module and how to setup the ADF Sample Agent to use it instead of the Dijkstra Path Planning. Here we assume that you will apply the changes to the `adf-sample-agent-java` repository. [#copy_sample] ==== Copy the Dijkstra Path Planning file First, you should copy the Dijkstra path planning (`src/main/java/adf/impl/module/algorithm/DijkstraPathPlanning.java`) from the `adf-core-java` repository to the `adf-sample-agent-java` repository (`src/main/java/sample_team/module/algorithm`). [source,shell] ---- $ cd adf-sample-agent-java $ mkdir -p src/main/java/sample_team/module/algorithm $ cp ../adf-core-java/src/main/java/adf/impl/module/algorithm/DijkstraPathPlanning.java src/main/java/sample_team/module/algorithm/AStarPathPlanning.java ---- [#edit_sample] ==== Edit the Dijkstra code <> is the code of `DijkstraPathPlanning.java`, which implements the Dijkstra's algorithm. You should edit line 1 and 23th as well as replace the code in the method `calc()` starting on line 96. Remove the method `isGoal()` that is only used by the Dijkstra `calc()`. <> shows the results of editing these lines. You must implement the method `calc()` to get its calculation result by the method `getResult()`. The type of `getResult()` returning is `List`. <> indicates the contents of the method `calc()`. In addition, you should write the new private class `Node` which is used by the method `calc()`. The code is shown in <>. [#lst:sample_path_planning] [source,java,linenums] .*Listing 2*. `DijkstraPathPlanning.java` file ---- package adf.impl.module.algorithm; // Edit this line import adf.core.agent.communication.MessageManager; import adf.core.agent.develop.DevelopData; import adf.core.agent.info.AgentInfo; import adf.core.agent.info.ScenarioInfo; import adf.core.agent.info.WorldInfo; import adf.core.agent.module.ModuleManager; import adf.core.agent.precompute.PrecomputeData; import adf.core.component.module.algorithm.PathPlanning; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import rescuecore2.misc.collections.LazyMap; import rescuecore2.standard.entities.Area; import rescuecore2.worldmodel.Entity; import rescuecore2.worldmodel.EntityID; public class DijkstraPathPlanning extends PathPlanning { // Edit this line private Map> graph; private EntityID from; private Collection targets; private List result; public DijkstraPathPlanning(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { super(ai, wi, si, moduleManager, developData); this.init(); } private void init() { Map> neighbours = new LazyMap>() { @Override public Set createValue() { return new HashSet<>(); } }; for (Entity next : this.worldInfo) { if (next instanceof Area) { Collection areaNeighbours = ((Area) next).getNeighbours(); neighbours.get(next.getID()).addAll(areaNeighbours); } } this.graph = neighbours; } @Override public List getResult() { return this.result; } @Override public PathPlanning setFrom(EntityID id) { this.from = id; return this; } @Override public PathPlanning setDestination(Collection targets) { this.targets = targets; return this; } @Override public PathPlanning updateInfo(MessageManager messageManager) { super.updateInfo(messageManager); return this; } @Override public PathPlanning precompute(PrecomputeData precomputeData) { super.precompute(precomputeData); return this; } @Override public PathPlanning resume(PrecomputeData precomputeData) { super.resume(precomputeData); return this; } @Override public PathPlanning preparate() { super.preparate(); return this; } @Override public PathPlanning calc() { // Replace the code in this method by the A* Path Planning algorithm List open = new LinkedList<>(); Map ancestors = new HashMap<>(); open.add(this.from); EntityID next; boolean found = false; ancestors.put(this.from, this.from); do { next = open.remove(0); if (isGoal(next, targets)) { found = true; break; } Collection neighbours = graph.get(next); if (neighbours.isEmpty()) { continue; } for (EntityID neighbour : neighbours) { if (isGoal(neighbour, targets)) { ancestors.put(neighbour, next); next = neighbour; found = true; break; } else { if (!ancestors.containsKey(neighbour)) { open.add(neighbour); ancestors.put(neighbour, next); } } } } while (!found && !open.isEmpty()); if (!found) { // No path this.result = null; } // Walk back from goal to this.from EntityID current = next; LinkedList path = new LinkedList<>(); do { path.add(0, current); current = ancestors.get(current); if (current == null) { throw new RuntimeException( "Found a node with no ancestor! Something is broken."); } } while (current != this.from); this.result = path; return this; } private boolean isGoal(EntityID e, Collection test) { return test.contains(e); } } ---- [#lst:astar_planning] [source,java,linenums] .*Listing 3*. `AStartPlanning.java` file ---- package sample_team.module.algorithm; // Position of the file import adf.core.agent.develop.DevelopData; import adf.core.agent.info.AgentInfo; import adf.core.agent.info.ScenarioInfo; import adf.core.agent.info.WorldInfo; import adf.core.agent.module.ModuleManager; import adf.core.agent.precompute.PrecomputeData; import adf.core.component.module.algorithm.PathPlanning; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import rescuecore2.misc.collections.LazyMap; import rescuecore2.standard.entities.Area; import rescuecore2.worldmodel.Entity; import rescuecore2.worldmodel.EntityID; public class AStarPathPlanning extends PathPlanning { private Map> graph; private EntityID from; private Collection targets; private List result; public AStarPathPlanning(AgentInfo ai, WorldInfo wi, ScenarioInfo si, ModuleManager moduleManager, DevelopData developData) { super(ai, wi, si, moduleManager, developData); this.init(); } ... ---- [#lst:astar_planning_calc] [source,java,linenums] .*Listing 4*. `calc()` method ---- @Override public PathPlanning calc() { List open = new LinkedList<>(); List close = new LinkedList<>(); Map nodeMap = new HashMap<>(); open.add(this.from); nodeMap.put(this.from, new Node(null, this.from)); close.clear(); while (true) { if (open.size() < 0) { this.result = null; return this; } Node n = null; for (EntityID id : open) { Node node = nodeMap.get(id); if (n == null) { n = node; } else if (node.estimate() < n.estimate()) { n = node; } } if (targets.contains(n.getID())) { List path = new LinkedList<>(); while (n != null) { path.add(0, n.getID()); n = nodeMap.get(n.getParent()); } this.result = path; return this; } open.remove(n.getID()); close.add(n.getID()); Collection neighbours = this.graph.get(n.getID()); for (EntityID neighbour : neighbours) { Node m = new Node(n, neighbour); if (!open.contains(neighbour) && !close.contains(neighbour)) { open.add(m.getID()); nodeMap.put(neighbour, m); } else if (open.contains(neighbour) && m.estimate() < nodeMap.get(neighbour).estimate()) { nodeMap.put(neighbour, m); } else if (!close.contains(neighbour) && m.estimate() < nodeMap.get(neighbour).estimate()) { nodeMap.put(neighbour, m); } } } } ---- [#lst:astar_node_class] [source,java,linenums] .*Listing 5*. `Node` class ---- private class Node { EntityID id; EntityID parent; double cost; double heuristic; public Node(Node from, EntityID id) { this.id = id; if (from == null) { this.cost = 0; } else { this.parent = from.getID(); this.cost = from.getCost() + worldInfo.getDistance(from.getID(), id); } this.heuristic = worldInfo.getDistance(id, targets.toArray(new EntityID[targets.size()])[0]); } public EntityID getID() { return id; } public double getCost() { return cost; } public double estimate() { return cost + heuristic; } public EntityID getParent() { return this.parent; } } } ---- [#edit_module_configuration] ==== Edit the Modules' configuration file After created the module code, you must edit the module configuration file `config/module.cfg` and replace the modules you would like to use your implementation. <> and <> show the part of the default `module.cfg` and the part of the edited `config/module.cfg` where the lines related to a path planning are changed. In this case, all `adf.impl.module.algorithm.DijkstraPathPlanning` are replaced with `sample_team.module.algorithm.AStarPathPlanning`. [#lst:default_module_cfg] [source,text] .*Listing 6*. Default `module.cfg` ---- ## SampleSearch SampleSearch.PathPlanning.Ambulance : adf.impl.module.algorithm.DijkstraPathPlanning SampleSearch.Clustering.Ambulance : adf.impl.module.algorithm.KMeansClustering SampleSearch.PathPlanning.Fire : adf.impl.module.algorithm.DijkstraPathPlanning SampleSearch.Clustering.Fire : adf.impl.module.algorithm.KMeansClustering SampleSearch.PathPlanning.Police : adf.impl.module.algorithm.DijkstraPathPlanning SampleSearch.Clustering.Police : adf.impl.module.algorithm.KMeansClustering ---- [#lst:edited_module_cfg] [source,text] .*Listing 7*. Edited `module.cfg` ---- ## SampleSearch SampleSearch.PathPlanning.Ambulance : sample_team.module.algorithm.AStarPathPlanning SampleSearch.Clustering.Ambulance : adf.impl.module.algorithm.KMeansClustering SampleSearch.PathPlanning.Fire : adf.impl.module.algorithm.AStarPathPlanning SampleSearch.Clustering.Fire : adf.impl.module.algorithm.KMeansClustering SampleSearch.PathPlanning.Police : adf.impl.module.algorithm.AStarPathPlanning SampleSearch.Clustering.Police : adf.impl.module.algorithm.KMeansClustering ----