Basic usage¶
- The file examples/basics.py showcases the major simulation-side components of AMaze:
creating a maze, robot and simulation
loading from an existing pre-trained agent
having the agent explore said maze
print various statistics about the maze and the agent’s performance
3from amaze.simu import Maze, Robot, Simulation, load
We start by importing modules: everything related to the simulation is conveniently exposed by the module
amaze.simu.
Note
pprint is imported to generate pretty outputs and is not mandatory.
6maze = Maze.from_string("M16_10x10_U")
7robot = Robot.BuildData.from_string("DD")
8simulation = Simulation(maze, robot)
With every relevant class in local scope we can create the basic components: the
Maze, Robot and
Simulation.
The first can be quickly generated from a string
(see Maze.from_string()).
For convenience, the inputs and outputs spaces can also be provided as a string
(Robot.BuildData.from_string()).
The last component is the simulation object, created using both the maze and robot description.
10print("Maze stats:", pprint.pformat(maze.stats()))
For the curious, line 10 will provide detailed stats
about the maze including its size, intersections, etc.
13pprint.pprint(simulation.compute_metrics(maze, robot.inputs, robot.vision))
For the complexity metrics, which are theoretically dependant on the agent, one may use the
simulation’s dedicated method to
compute_metrics.
Note that, contrary to the statistics, this implies a fair amount of computation (O(n*m),
for a maze of n rows and m columns)
16controller = load("examples/agents/unicursive_tabular.zip")
17print(f"Agent infos: {pprint.pformat(controller.infos)}")
Next we retrieve one of the demonstration agents provided with the library and print some of the information stored alongside it.
19simulation.run(controller)
After which we let the simulation run freely until completion.
This means either success() (the agent has reached the target) or
failure() (the agent has exceeded the deadline).
22print("Target reached:", simulation.success())
23print("Raw reward:", simulation.cumulative_reward())
24print("Normalized reward:", simulation.normalized_reward())
25print(f"Details: {pprint.pformat(simulation.infos())}")
To conclude, we extract select pieces of information from the simulation (success, rewards) as well
as the more exhaustive infos().
The full listing for the example is shown below
1import pprint
2
3from amaze.simu import Maze, Robot, Simulation, load
4
5print("=" * 80)
6maze = Maze.from_string("M16_10x10_U")
7robot = Robot.BuildData.from_string("DD")
8simulation = Simulation(maze, robot)
9
10print("Maze stats:", pprint.pformat(maze.stats()))
11print()
12print("Maze complexity:")
13pprint.pprint(simulation.compute_metrics(maze, robot.inputs, robot.vision))
14
15print("=" * 80)
16controller = load("examples/agents/unicursive_tabular.zip")
17print(f"Agent infos: {pprint.pformat(controller.infos)}")
18
19simulation.run(controller)
20
21print("=" * 80)
22print("Target reached:", simulation.success())
23print("Raw reward:", simulation.cumulative_reward())
24print("Normalized reward:", simulation.normalized_reward())
25print(f"Details: {pprint.pformat(simulation.infos())}")