Using graphics

The file examples/visualization.py showcases the use of graphical visualization elements:
  • creating an image from a maze string

  • plotting an agent’s trajectory in a maze

  • accessing the main entry point programmatically

1from amaze import (
2    Maze,
3    Robot,
4    MazeWidget,
5    Simulation,
6    qt_application,
7    load,
8    amaze_main,
9)

As with the simulation-side, all major components are available in a single import statement.

11# Global variables
12FOLDER = "tmp/demos/visualization"
13WIDTH = 512  # Of generated images
14
15# Define a seed-less maze and get the resolved name
16maze_str = "10x10_U"
17maze = Maze.from_string(maze_str)
18maze_str = maze.to_string()

Next we define some helpful variables including the maze string. Notice that, in this case, we do not provide a seed to the maze thereby implying that we want a random one. Thus, in the highlighted line, we “resolve” the maze string so that the seed (determined on the line above) is known.

21app = qt_application()
22maze_img = f"{FOLDER}/{maze_str}.png"
23if MazeWidget.static_render_to_file(
24    maze=Maze.from_string(maze_str),
25    path=maze_img,
26    size=WIDTH,
27    colorblind=True,
28    robot=False,
29    solution=True,
30    dark=True,
31):
32    print(f"Saved {maze_str} to {maze_img}")

The graphical elements are introduced, first, by creating a QtApplication object which is essential for PyQt (the underlying widgets library) to work properly. This is done transparently through the qt_application() function. Then, rendering a maze to a given file is trivially done by static_render_to_file(). The various rendering options which can be provided to tweak the appearance are detailed in default_config().

21app = qt_application()
22maze_img = f"{FOLDER}/{maze_str}.png"
23if MazeWidget.static_render_to_file(
24    maze=Maze.from_string(maze_str),
25    path=maze_img,
26    size=WIDTH,
27    colorblind=True,
28    robot=False,
29    solution=True,

Then, as in the previous example, we load an agent and have it roaming the maze until completion. Note, however, the save_trajectory flag provided to the simulation so that we can later plot it.

34# Have an agent move around in the maze ...
35agent_path = "examples/agents/unicursive_tabular.zip"
36controller = load(agent_path)
37simulation = Simulation(maze, Robot.BuildData.from_string("DD"), save_trajectory=True)
38simulation.run(controller)

This is actually done similarly the rendering of an empty maze, although we now provide the whole simulation and use plot_trajectory().

43MazeWidget.plot_trajectory(
44    simulation=simulation,
45    size=WIDTH,
46    path=trajectory_img,
47)

Finally, we also could call the main script from python and provide it with arguments. This way one can easily instrumentalize the library, e.g. when computing generalization performance. The list of arguments is detailed in amaze.bin.main.Options.

As before, the full listing of the example is provided below.

Full listing for examples/visualization.py
 1from amaze import (
 2    Maze,
 3    Robot,
 4    MazeWidget,
 5    Simulation,
 6    qt_application,
 7    load,
 8    amaze_main,
 9)
10
11# Global variables
12FOLDER = "tmp/demos/visualization"
13WIDTH = 512  # Of generated images
14
15# Define a seed-less maze and get the resolved name
16maze_str = "10x10_U"
17maze = Maze.from_string(maze_str)
18maze_str = maze.to_string()
19
20# Draw the maze
21app = qt_application()
22maze_img = f"{FOLDER}/{maze_str}.png"
23if MazeWidget.static_render_to_file(
24    maze=Maze.from_string(maze_str),
25    path=maze_img,
26    size=WIDTH,
27    colorblind=True,
28    robot=False,
29    solution=True,
30    dark=True,
31):
32    print(f"Saved {maze_str} to {maze_img}")
33
34# Have an agent move around in the maze ...
35agent_path = "examples/agents/unicursive_tabular.zip"
36controller = load(agent_path)
37simulation = Simulation(maze, Robot.BuildData.from_string("DD"), save_trajectory=True)
38simulation.run(controller)
39
40# ... and print its trajectory
41agent_name = agent_path.split("/")[-1].split(".")[0]
42trajectory_img = f"{FOLDER}/{agent_name}_{maze_str}.png"
43MazeWidget.plot_trajectory(
44    simulation=simulation,
45    size=WIDTH,
46    path=trajectory_img,
47)
48print(
49    f"Plotted {agent_path}" f" in {simulation.maze.to_string()}" f" to {trajectory_img}"
50)
51
52# Invoke the main from python (with arguments)
53amaze_main(
54    [
55        "--maze",
56        maze_str,
57        "--robot",
58        "D",
59        "--controller",
60        "cheater",
61        "--dt",
62        "0.1",
63        "--auto-quit",
64        "--no-restore-config",
65        "--width",
66        "500",
67    ]
68)