Agents

There are two different agent classes than can be used the agents.lunatic_agent.LunaticAgent that is fully customizable, and the simpler but less flexible agents.leaderboard_agent.LunaticChallenger.

Agent Classes

LunaticAgent Class

The agents.lunatic_agent.LunaticAgent can be initialized in different ways and supports all features from the Configuration section:

from agents.lunatic_agent import LunaticAgent
from agents.tools.config_creation import LaunchConfig, LunaticAgentSettings
from classes.worldmodel import GameFramework

# ... setup carla and choose a blueprint
ego_bp.set_attribute('role_name', 'hero')

# Load or setup the configuration
config = LaunchConfig()
config.agent = LunaticAgentSettings()

# The game framework allows to handle the loop and manages the cooldowns of all rules
game_framework = GameFramework(config)
ego = game_framework.spawn_actor(ego_bp, start, must_spawn=True)

# This creates the agent as well as other instances, from a custom configuration
behavior = LunaticAgentSettings(config.agent)
agent = LunaticAgent(behavior, vehicle=ego)

# Certain internal events allow to stop the loop that is managed by the game_framework
while game_framework.continue_loop:
    with game_framework:
        agent.run_step()

The agent can be initialized quicker and directly with the GameFramework, which also provides other useful instances at the same time.

agent, world_model, global_planner, keyboard_controller = game_framework.init_agent_and_interface(ego, agent_class=LunaticAgent, config=behavior)

LunaticChallenger Class

The LunaticChallenger is wrapped around the LunaticAgent. It can be more easily combined with the ScenarioRunner and be used with the Leaderboard 2.0 for which this class is especially designed.

In the simplest way an agent can be used like in the snipplet below. However we recommend that you follow the documentation from the installation section and Leaderboard 2.0, afterwards you can test this agent by executing the run_leaderboard_agent.sh file.

# examples/minimal_leaderboard.py
from agents.leaderboard_agent import LunaticChallenger
from classes.worldmodel import GameFramework

# Sets up Carla and Pygame, and Hydra in a minimal way
game_framework = GameFramework.quickstart()
ego = GameFramework.request_new_actor("car", rolename="hero", random_location=True)

# Create a lunatic agent
agent = LunaticChallenger("localhost", carla_port=2000)
agent.setup(game_framework.launch_config) # Do not forget this step!
try:
    while game_framework.continue_loop:
        with game_framework(agent):
            agent() # The LunaticChallenger should be called
            agent.apply_control()
except:
    game_framework.cleanup()
    raise

Attention

The LunaticChallenger is easier to setup, however its usage and customization are, limited as it complies with the AutonomousAgent interface from leaderboard-2.0. So far, it only supports a limited amount of features mentioned in the Configuration section. For example, changing settings over the command line is not supported as the configuration is loaded in agent.setup rather than a @hydra.main wrapped entry point.

Adding Rules

Once an agent is created, it can be further customized by adding rules. Rules are the building blocks of the agent, they are the components that define the behavior of the agent. The rules can be added and removed or disabled at any time.

# Adding mutliple rules
from agent.rules import create_default_rules
agent.add_rules(create_default_rules())

# Add a new rule
agent.add_rule(MyRule()) # Be sure that you instantiate your rules.

Configuring the Agent

The agent can be configured by passing it any attribute-supporting Mapping structure, for example nested (data)classes, attrs, or omegaconf’s DictConfig which extends the aforementioned.

See also

For more info read the config files documentation.

Workflow

The agent lifecycle involves several stages from initialization to continuous operation. The following diagram illustrates the lifecycle and interactions within the Lunatic AI Driver, providing its operational workflow in detail.

AgentLifecycleDiagram.drawio (click image for fullscreen)