2024-06-05 09:11:37 +01:00
|
|
|
# © Crown-owned copyright 2024, Defence Science and Technology Laboratory UK
|
2023-08-17 15:32:12 +01:00
|
|
|
from typing import Dict
|
|
|
|
|
|
2024-03-08 12:42:22 +00:00
|
|
|
from primaite.interface.request import RequestResponse
|
2023-10-09 13:24:08 +01:00
|
|
|
from primaite.simulator.core import RequestManager, RequestType, SimComponent
|
2023-08-16 16:45:52 +01:00
|
|
|
from primaite.simulator.domain.controller import DomainController
|
2023-08-24 12:41:46 +01:00
|
|
|
from primaite.simulator.network.container import Network
|
2023-08-16 16:45:52 +01:00
|
|
|
|
|
|
|
|
|
2023-08-17 15:32:12 +01:00
|
|
|
class Simulation(SimComponent):
|
2023-08-21 10:04:23 +01:00
|
|
|
"""Top-level simulation object which holds a reference to all other parts of the simulation."""
|
2023-08-16 16:45:52 +01:00
|
|
|
|
2023-08-24 12:41:46 +01:00
|
|
|
network: Network
|
2023-11-27 23:01:56 +00:00
|
|
|
# domain: DomainController
|
2023-08-16 16:45:52 +01:00
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-08-21 10:04:23 +01:00
|
|
|
"""Initialise the Simulation."""
|
2023-08-17 15:32:12 +01:00
|
|
|
if not kwargs.get("network"):
|
2023-08-24 12:41:46 +01:00
|
|
|
kwargs["network"] = Network()
|
2023-08-17 15:32:12 +01:00
|
|
|
|
|
|
|
|
if not kwargs.get("domain"):
|
|
|
|
|
kwargs["domain"] = DomainController()
|
|
|
|
|
|
2023-08-16 16:45:52 +01:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
2024-02-23 10:06:48 +00:00
|
|
|
def setup_for_episode(self, episode: int):
|
2023-11-27 23:01:56 +00:00
|
|
|
"""Reset the original state of the SimComponent."""
|
2024-02-25 17:44:41 +00:00
|
|
|
self.network.setup_for_episode(episode=episode)
|
2023-11-27 23:01:56 +00:00
|
|
|
|
2023-10-09 13:24:08 +01:00
|
|
|
def _init_request_manager(self) -> RequestManager:
|
2024-03-11 10:20:47 +00:00
|
|
|
"""
|
|
|
|
|
Initialise the request manager.
|
|
|
|
|
|
|
|
|
|
More information in user guide and docstring for SimComponent._init_request_manager.
|
|
|
|
|
"""
|
2023-10-13 10:41:27 +01:00
|
|
|
rm = super()._init_request_manager()
|
2023-10-09 13:24:08 +01:00
|
|
|
# pass through network requests to the network objects
|
2023-10-13 10:41:27 +01:00
|
|
|
rm.add_request("network", RequestType(func=self.network._request_manager))
|
2023-10-09 13:24:08 +01:00
|
|
|
# pass through domain requests to the domain object
|
2023-10-13 10:41:27 +01:00
|
|
|
rm.add_request("domain", RequestType(func=self.domain._request_manager))
|
2024-03-08 12:42:22 +00:00
|
|
|
# if 'do_nothing' is requested, just return a success
|
|
|
|
|
rm.add_request("do_nothing", RequestType(func=lambda request, context: RequestResponse(status="success")))
|
2023-10-13 10:41:27 +01:00
|
|
|
return rm
|
2023-08-29 14:33:28 +01:00
|
|
|
|
|
|
|
|
def describe_state(self) -> Dict:
|
|
|
|
|
"""
|
|
|
|
|
Produce a dictionary describing the current state of this object.
|
|
|
|
|
|
|
|
|
|
Please see :py:meth:`primaite.simulator.core.SimComponent.describe_state` for a more detailed explanation.
|
|
|
|
|
|
|
|
|
|
:return: Current state of this object and child objects.
|
|
|
|
|
:rtype: Dict
|
|
|
|
|
"""
|
|
|
|
|
state = super().describe_state()
|
|
|
|
|
state.update(
|
|
|
|
|
{
|
|
|
|
|
"network": self.network.describe_state(),
|
|
|
|
|
"domain": self.domain.describe_state(),
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
return state
|
2023-12-21 15:07:41 +00:00
|
|
|
|
|
|
|
|
def apply_timestep(self, timestep: int) -> None:
|
|
|
|
|
"""Apply a timestep to the simulation."""
|
|
|
|
|
super().apply_timestep(timestep)
|
|
|
|
|
self.network.apply_timestep(timestep)
|
2024-04-15 11:50:08 +01:00
|
|
|
|
|
|
|
|
def pre_timestep(self, timestep: int) -> None:
|
|
|
|
|
"""Apply pre-timestep logic."""
|
|
|
|
|
super().pre_timestep(timestep)
|
|
|
|
|
self.network.pre_timestep(timestep)
|