Skip to content

Simulation

Interface for running simulations.

APISimulation

Bases: APISimulationInternal

Exposes functions for running simulations on server and adding simulation checkpoints.

Source code in src/rmlab/api/operations/simulation.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class APISimulation(APISimulationInternal):
    """Exposes functions for running simulations on server and adding simulation checkpoints."""

    async def upload_checkpoints(
        self, scen_id: int, checkpoints: List[datetime]
    ) -> None:
        """Upload date checkpoints at which simulation is paused.

        Args:
            scen_id (int): Scenario ID in which the simulation is running
            checkpoints (List[datetime]): List of checkpoints
        """

        await self._submit_call(
            "api-operation-simulation-checkpoint",
            scen_id=scen_id,
            checkpoints=[datetime.strftime(chp, DateFormat) for chp in checkpoints],
        )

    async def trigger_simulation(
        self, scen_id: int, next: Optional[datetime] = None
    ) -> Tuple[ScenarioDates, ItemsCount, SchedulesCount, FlightsCount]:
        """Trigger a simulation run on a given scenario.

        Args:
            scen_id (int): Scenario ID
            next (Optional[datetime], optional): Checkpoint at which the simulation is stopped. Defaults to None.

        Raises:
            ValueError: If the type of `next` is invalid
            RuntimeError: If simulation failed for any reason

        Returns:
            Summarized information of the scenario after the simulation finishes
        """

        if next is not None:
            if not isinstance(next, datetime):
                raise ValueError(
                    f"Expected `datetime` type in `next` {next}, got `{type(next)}`"
                )

            await self._submit_call(
                "api-operation-simulation-checkpoint",
                scen_id=scen_id,
                checkpoints=[datetime.strftime(next, DateFormat)],
            )

        trigger_task = asyncio.create_task(
            self._submit_call(
                "api-operation-simulation-trigger", scen_id=scen_id, operation="auto"
            )
        )

        stop_event = asyncio.Event()

        progress_task = asyncio.create_task(
            self._periodic_trigger_progress(scen_id, stop_event)
        )

        await asyncio.wait(
            [trigger_task, progress_task],
            timeout=None,
            return_when=asyncio.FIRST_COMPLETED,
        )

        if not stop_event.is_set() and not progress_task.done():
            stop_event.set()
            await progress_task

        exc = progress_task.exception()
        if exc is not None:
            raise RuntimeError(f"Error {type(exc)} while tracking progress: {exc}")

        summary = trigger_task.result()

        return self._process_summary(scen_id, summary)

trigger_simulation(scen_id, next=None) async

Trigger a simulation run on a given scenario.

Parameters:

NameTypeDescriptionDefault
scen_idint

Scenario ID

required
nextOptional[datetime]

Checkpoint at which the simulation is stopped. Defaults to None.

None

Raises:

TypeDescription
ValueError

If the type of next is invalid

RuntimeError

If simulation failed for any reason

Returns:

TypeDescription
Tuple[ScenarioDates, ItemsCount, SchedulesCount, FlightsCount]

Summarized information of the scenario after the simulation finishes

Source code in src/rmlab/api/operations/simulation.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
async def trigger_simulation(
    self, scen_id: int, next: Optional[datetime] = None
) -> Tuple[ScenarioDates, ItemsCount, SchedulesCount, FlightsCount]:
    """Trigger a simulation run on a given scenario.

    Args:
        scen_id (int): Scenario ID
        next (Optional[datetime], optional): Checkpoint at which the simulation is stopped. Defaults to None.

    Raises:
        ValueError: If the type of `next` is invalid
        RuntimeError: If simulation failed for any reason

    Returns:
        Summarized information of the scenario after the simulation finishes
    """

    if next is not None:
        if not isinstance(next, datetime):
            raise ValueError(
                f"Expected `datetime` type in `next` {next}, got `{type(next)}`"
            )

        await self._submit_call(
            "api-operation-simulation-checkpoint",
            scen_id=scen_id,
            checkpoints=[datetime.strftime(next, DateFormat)],
        )

    trigger_task = asyncio.create_task(
        self._submit_call(
            "api-operation-simulation-trigger", scen_id=scen_id, operation="auto"
        )
    )

    stop_event = asyncio.Event()

    progress_task = asyncio.create_task(
        self._periodic_trigger_progress(scen_id, stop_event)
    )

    await asyncio.wait(
        [trigger_task, progress_task],
        timeout=None,
        return_when=asyncio.FIRST_COMPLETED,
    )

    if not stop_event.is_set() and not progress_task.done():
        stop_event.set()
        await progress_task

    exc = progress_task.exception()
    if exc is not None:
        raise RuntimeError(f"Error {type(exc)} while tracking progress: {exc}")

    summary = trigger_task.result()

    return self._process_summary(scen_id, summary)

upload_checkpoints(scen_id, checkpoints) async

Upload date checkpoints at which simulation is paused.

Parameters:

NameTypeDescriptionDefault
scen_idint

Scenario ID in which the simulation is running

required
checkpointsList[datetime]

List of checkpoints

required
Source code in src/rmlab/api/operations/simulation.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
async def upload_checkpoints(
    self, scen_id: int, checkpoints: List[datetime]
) -> None:
    """Upload date checkpoints at which simulation is paused.

    Args:
        scen_id (int): Scenario ID in which the simulation is running
        checkpoints (List[datetime]): List of checkpoints
    """

    await self._submit_call(
        "api-operation-simulation-checkpoint",
        scen_id=scen_id,
        checkpoints=[datetime.strftime(chp, DateFormat) for chp in checkpoints],
    )