Build an ACI Controller
Goal
This tutorial attaches a Python controller to a normal scenario, observes demand requests, applies strategic decisions, and advances the simulation. No special ACI value is needed in either configuration XML.
1. Start a Normal Scenario
from loft import aci
aci.start(
config_file="example/scenarios/default/config/simulation_config.xml",
gui=False,
)
If the configuration references operation_strategy.xml, its internal models
form the baseline. If that optional input is absent, LOFT-Sim uses no strategic
model, Newell corridor/terminal following,
FCFS controlled-junction sequencing, and no network-level
contingency model.
2. Submit or Observe Requests
Demand-file requests and requests submitted through ACI share one domain. Each step below continues the same session started above.
vertiport_ids = aci.vertiport.getIDList()
origin, destination = vertiport_ids[:2]
result = aci.request.submit(
origin=origin,
destination=destination,
requested_departure_time=aci.simulation.getTime() + 60,
vehicle_type="hybrid",
service_class="passenger",
priority_class="regular",
priority_rank=0,
max_delay=120,
route=None,
)
request_id = result["request_id"]
print(aci.request.getSnapshot(request_id))
print(len(aci.request.getPendingIDList()), "requests are pending")
Submitting a request does not insert a vehicle directly. The request remains observable while the effective strategic decision is resolved.
3. Override a Strategic Decision
should_accept below stands in for your policy. Anything that reads the
request and returns a bool will do; this one meters everything
the internal model was not already going to confirm. getDefaultDecision()
returns None while the internal model has not decided yet, for example under
DCB or for a request submitted after a batch 4D
schedule was built, so the policy must tolerate a missing decision.
def should_accept(request_id, default_decision):
"""Your policy. Replace it; the loop below is what matters."""
return (default_decision or {}).get("action") == "confirm"
for pending_id in aci.request.getPendingIDList():
default = aci.request.getDefaultDecision(pending_id)
if should_accept(pending_id, default):
aci.request.confirm(pending_id)
else:
aci.request.delay(pending_id, delay=30, reason="controller_metering")
delay=30 moves the request 30 s past its current schedule (or past the
current time, if that is later). The absolute form,
scheduled_departure_time=..., must name a time later than the request’s
current schedule. Either way the new departure must fall before the end of
the run; otherwise ACI raises aci.ACIException.
With the bundled default scenario the internal model confirms every
request, so this loop only calls confirm(). To reach the delay() branch,
run it against example/scenarios/default/config/sim_config_preflight.xml, a
batch 4D strategy. The batch schedule has already confirmed every request
from the demand file, but the request you submitted in step 2 has no internal
decision yet (getDefaultDecision() returns None), so the loop delays it by
30 s. That configuration writes its outputs to
example/scenarios/default/outputs/preflight/, so a run of this tutorial
replaces the files from step 4 of Hello World with those of
a shorter run.
confirm is an operator/ACI acceptance. decline is an operator rejection,
while cancel represents demand-owner withdrawal. Request release is an
internal transition after confirmation and feasibility checks; it is not an
ACI method.
Use clearDecisionOverride(requestID) to return a request to the current
internal result.
4. Apply Element or Vehicle Overrides
Nothing is airborne yet at t=0, so first advance the clock until a vehicle is
flying the corridor you want to close (in the
default scenario that takes about 300 simulated seconds):
corridor_id = aci.corridor.getIDList()[0]
while not aci.corridor.getVehicleIDs(corridor_id) and aci.simulation.getTime() < 900:
aci.simulationStep()
aci.corridor.close(corridor_id, duration=60, reason="weather")
for vehicle_id in aci.vehicle.getIDList():
if aci.vehicle.getCurrentCorridorID(vehicle_id) == corridor_id:
print(aci.vehicle.reroute(vehicle_id))
Each reroute returns a result such as
{'ok': True, 'action': 'vehicle_reroute', 'target_id': 'D16', ..., 'route': [...]}.
In a real controller this check belongs inside the step loop of step 5.
An ACI open() command is an explicit open override. To stop
overriding the internal/XML closure state, call clearClosureOverride()
instead. Vehicle commands remain constrained by valid 3D routes,
dynamics, and hard safety logic.
5. Step and Close
try:
for _ in range(500):
# Observe and act here.
aci.simulationStep()
finally:
aci.close()
Read the ACI Reference for the control model and every domain.
Runnable Scripts
example/tutorials/aci/ holds three complete scripts that use these calls
against a scenario with an empty demand file. They use an older form of the
same API: import aci (a module at the repository root that works only in a
source checkout) instead of from loft import aci, and the command-list call
aci.start(["loft", "-c", ..., "--nogui"]) instead of config_file= and
gui=False. Run them from the repository root:
python example/tutorials/aci/01_basic_live_state.py
python example/tutorials/aci/02_request_override_strategy.py
python example/tutorials/aci/03_runtime_control_strategy.py
01 submits a request and reads live state, 02 overrides request decisions
with a priority-first policy, and 03 applies typed vehicle, corridor and
environment overrides. Example Cases lists
them with the configuration-only cases.