openmobisim.operator#
Operators: policies that steer shared-mobility services during a fleet day.
A policy is called every step_s seconds of the day (the first at the window’s start)
with a read-only view of the services, and returns decisions, which the fleet engine
checks, refuses with a reason when they cannot be carried out (the day goes on), or carries
out. Every decision is recorded (FleetDay.decisions()).
A policy is one of:
a built-in one, run in Rust:
policy_noop,policy_reset,policy_threshold,policy_charge_rule, or their names"noop","reset","threshold","charge_rule";any Python object with
step(view)returning a list of decisions; optionallystep_s(900 by default),describe()(its name and parameters: a dict{"name": ..., "parameters": {...}}or a name;"python:<class name>"without it) andday_start(day)(called once when the day begins, with{"window", "seed", "services"});a name registered with
policy_register.
The view (view.t, view.step, view.rng_seed, view.window,
view.services) holds per service name a dict of arrays, one value per dock or vehicle:
a docked service’s dock_id, node (its road node id), zone, bikes, slots,
incoming (bikes in vans on their way to the dock), lon, lat, and since the last
step pickups, returns, no_bike (found no bike there nor near), redirected_in,
redirected_out (people), and
moving (bikes in vans now) and riding; a taxi fleet’s vehicle_id, state (names:
idle, to_pickup, occupied, to_charger, queued_at_charger, charging,
relocating), node (road node ids: where it is, or where it is driving to), until_s
(the second it gets there when driving, or ends its charge when charging; nan when idle or
queued for a plug), zone, soc (charge as a share of the battery),
nearest_charger (by free-flow time, within the fleet’s charger_reach_s, or None),
the waiting requests (waiting_id, waiting_node, waiting_zone, waiting_since_s)
and the chargers (charger_id, charger_node, charger_plugs, charger_in_use,
charger_queued).
The arrays are copies, and the view refuses to be read after its step: a policy keeps copies
of what it needs. For its own random draws, a policy uses
numpy.random.default_rng(view.rng_seed): the same day and seed then give the same day.
Zones (view.zones): a square grid of grid_m metres (1 000: an assumption, not
calibrated) over the road network’s nodes, from the south-west corner of their extent; a
zone is a cell holding at least one road node (id from 0, by row south to north then column
west to east, so the same network always gives the same zones; lon and lat of its
centre). Each service’s zone gives the zone of each dock or vehicle (-1: none).
Queries, computed in Rust when asked (sources in parallel, the same answer at any number of threads) and cached within the step; like the rest of the view, they refuse after the step:
view.travel_times(service, from_nodes, to_nodes, max_s=inf): seconds from each road node offrom_nodesto each ofto_nodes, leaving at the step’s second on the day’s road times (they change by the time of day; free flow withoutroad_times), a numpy matrix[len(from_nodes), len(to_nodes)],infwhere no road gets there withinmax_sseconds (nor within 4 hours).service(a name, orNone) changes nothing: every service’s vehicles (a docked service’s vans, a taxi fleet’s cars) drive the roads.view.expected_legs(service, bin_s=900): the people expected to ask forserviceper time bin ofbin_sseconds from the window’s start, a numpy matrix[rows, bins]: per dock for a docked service (pickups at the dock each leg planned; rows asdock_id), per zone for a taxi fleet (requests by their origin’s zone; rows asview.zones["id"]). They are counted from the day’s legs before the variation a seed draws in afleet_environment(legs_keep,departure_jitter_s): the expectation over the days the seeds draw, not one day’s own variation. In a day without variation (``fleet_day``, or a ``fleet_environment`` with ``legs_keep=1`` and no jitter) these are the day’s own legs: exactly what the day will ask for, so a policy reading them knows the day’s future.view.nearest(service, kind, node, k): thekdocks (kind="docks", by their road nodes) or vehicles (kind="vehicles", by the node each is at or driving to) ofservicenearest road nodenodeby free-flow time: a list of(id, seconds), nearest first, ties by their order in the view; those no road reaches are left out.
Road nodes are named by their ids, as the view gives them (node, waiting_node).
Decisions name services, docks, vehicles, chargers and road nodes by their ids: the
dataclasses below, or tuples ("move", service, from_dock, to_dock, n), ("target",
service, dock, level), ("relocate", service, vehicle, node), ("charge", service,
vehicle, charger, to_soc). An id that does not exist stops the day with an error; a decision
that cannot be carried out is refused with its reason. A policy’s exception stops the day and
is raised by fleet_day.
Classes#
Send an idle taxi to a charger, to charge to |
|
Move up to |
|
A built-in policy (run in Rust): its name and parameters ( |
|
Send an idle taxi, empty, to a road node, where it waits for a request. |
|
The bikes a dock should hold: the engine's solver moves bikes to reach it. |
Functions#
|
Idle taxis low on charge sent to charge, every step. |
|
The policy that decides nothing: the day without an operator, with steps. |
|
Make a policy usable by name, like the built-in ones. |
|
Every dock back toward its count at the start of the day, at each step from |
|
Docks running empty topped up from docks running full, every step. |
Module Contents#
- class openmobisim.operator.Charge#
Send an idle taxi to a charger, to charge to
to_socof its battery.- charger: str#
- service: str#
- to_soc: float#
- vehicle: str#
- class openmobisim.operator.Move#
Move up to
nbikes of a docked service from one dock to another by van.As many leave as the first dock holds, up to
nand the service’svan_bikes; the van drives the roads’ time between the docks plushandling_sa bike.- from_dock: str#
- n: int#
- service: str#
- to_dock: str#
- class openmobisim.operator.PolicyBuiltin#
A built-in policy (run in Rust): its name and parameters (
policy_thresholdetc.).- name: str#
- parameters: collections.abc.Mapping[str, float]#
- class openmobisim.operator.Relocate#
Send an idle taxi, empty, to a road node, where it waits for a request.
- node: str#
- service: str#
- vehicle: str#
- class openmobisim.operator.Target#
The bikes a dock should hold: the engine’s solver moves bikes to reach it.
At the step’s end, every dock given a target and holding more gives to those holding fewer, the dock furthest below its target first, from the nearest docks as the crow flies, at most the service’s
van_bikesa move. A dock without a target neither gives nor takes.- dock: str#
- level: int#
- service: str#
- openmobisim.operator.policy_charge_rule(charge_below=0.2, charge_to=0.8, *, step_s=900.0)#
Idle taxis low on charge sent to charge, every step.
An idle vehicle charged below
charge_belowof its battery is sent to the charger nearest it by free-flow time (within the fleet’scharger_reach_s), to charge tocharge_to. It acts besides the fleet’s own charging rule (its options of the same names, applied when a vehicle drops a passenger). The defaults are assumptions, not calibrated.- Parameters:
charge_below (float) – Below this share of its battery, an idle vehicle is sent to charge.
charge_to (float) – The share it charges to (
0 <= charge_below < charge_to <= 1, checked byfleet_day).step_s (float) – The seconds between its steps.
- Return type:
- openmobisim.operator.policy_noop(*, step_s=900.0)#
The policy that decides nothing: the day without an operator, with steps.
- Parameters:
step_s (float) – The seconds between its steps.
- Return type:
- openmobisim.operator.policy_register(name, factory)#
Make a policy usable by name, like the built-in ones.
After
policy_register("my_rule", MyRule),fleet_day(..., policy="my_rule", policy_options={...})builds the policy asfactory(policy_options)(a dict, empty when none are given). A name is registered once per session: registering it again replaces the factory.- Parameters:
name (str) – Letters, digits and underscores, not a built-in policy’s name.
factory (collections.abc.Callable[[dict[str, float]], Any]) – Called with the options dict; returns a policy (an object with
step(view), or a built-in one).
- Raises:
ValueError – If the name is a built-in policy’s or not a plain identifier, or the factory is not callable.
- Return type:
None
- openmobisim.operator.policy_reset(from_s=0.0, *, step_s=900.0)#
Every dock back toward its count at the start of the day, at each step from
from_s.At each step at or after
from_s, every dock whose bikes differ from its start-of-day count is given that count as its target (Target); the engine’s solver moves bikes from the docks above their count to those below it, nearest first, counting the bikes already in a van on their way to a dock, so a dock is not served twice when vans take longer thanstep_s.- Parameters:
from_s (float) – The second of the day (from midnight) it starts acting at; 0: from the start.
step_s (float) – The seconds between its steps.
- Return type:
- openmobisim.operator.policy_threshold(low=0.2, high=0.8, *, step_s=900.0)#
Docks running empty topped up from docks running full, every step.
A dock holding fewer bikes than
lowof its slots is given the targetslots × (low + high) / 2rounded down; one holding more thanhighof its slots, that middle rounded up; the engine’s solver moves bikes from the second kind to the first, nearest first. The defaults are assumptions, not calibrated.- Parameters:
low (float) – Below this share of its slots, a dock is topped up.
high (float) – Above this share, a dock gives bikes away (
0 <= low < high <= 1, checked byfleet_day).step_s (float) – The seconds between its steps.
- Return type: