adf-manual.adoc 19.3 KB
Newer Older
k20066's avatar
k20066 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
= 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 <<precomputation_mode>>).

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. <<lst:module_configuration,Listing 1>> 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 <<lst:module_configuration,Listing 1>> is in `DefaultTacticsAmbulanceTeam.Search` and `DefaultTacticsFireBrigade.Search` indicates that both modules refer to `sample_team.module.complex.SampleSearch`. An usage example is shown in <<edit_module_configuration>>.

[#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
<<lst:sample_path_planning,Listing 2>> 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()`. <<lst:astar_planning,Listing 3>> 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<EntityID>`.

<<lst:astar_planning_calc,Listing 4>> 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:astar_node_class,Listing 5>>.

[#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<EntityID, Set<EntityID>> graph;

  private EntityID from;
  private Collection<EntityID> targets;
  private List<EntityID> 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<EntityID,
        Set<EntityID>> neighbours = new LazyMap<EntityID, Set<EntityID>>() {

          @Override
          public Set<EntityID> createValue() {
            return new HashSet<>();
          }
        };
    for (Entity next : this.worldInfo) {
      if (next instanceof Area) {
        Collection<EntityID> areaNeighbours = ((Area) next).getNeighbours();
        neighbours.get(next.getID()).addAll(areaNeighbours);
      }
    }
    this.graph = neighbours;
  }

  @Override
  public List<EntityID> getResult() {
    return this.result;
  }

  @Override
  public PathPlanning setFrom(EntityID id) {
    this.from = id;
    return this;
  }

  @Override
  public PathPlanning setDestination(Collection<EntityID> 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<EntityID> open = new LinkedList<>();
    Map<EntityID, EntityID> 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<EntityID> 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<EntityID> 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<EntityID> 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<EntityID, Set<EntityID>> graph;

  private EntityID from;
  private Collection<EntityID> targets;
  private List<EntityID> 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<EntityID> open = new LinkedList<>();
    List<EntityID> close = new LinkedList<>();
    Map<EntityID, Node> 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<EntityID> 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<EntityID> 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. <<lst:default_module_cfg,Listing 6>> and <<lst:edited_module_cfg,Listing 7>> 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
----