55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from diceplayer.config.player_config import PlayerConfig, RoutineType
|
|
from diceplayer.dice.dice_handler import DiceHandler
|
|
from diceplayer.logger import logger
|
|
from diceplayer.optimization.optimization_handler import OptimizationHandler
|
|
from diceplayer.state.state_handler import StateHandler
|
|
from diceplayer.state.state_model import StateModel
|
|
|
|
from typing_extensions import TypedDict, Unpack
|
|
|
|
|
|
class PlayerFlags(TypedDict):
|
|
continuation: bool
|
|
force: bool
|
|
|
|
|
|
class Player:
|
|
def __init__(self, config: PlayerConfig):
|
|
self.config = config
|
|
self._state_handler = StateHandler(config.simulation_dir)
|
|
|
|
def play(self, **flags: Unpack[PlayerFlags]):
|
|
continuation = flags.get("continuation", False)
|
|
force = flags.get("force", False)
|
|
|
|
state: StateModel = self._state_handler.get(self.config, force=force)
|
|
if not continuation and state is not None:
|
|
logger.info(
|
|
"Continuation flag is not set. Starting a new simulation and deleting any existing state."
|
|
)
|
|
self._state_handler.delete()
|
|
state = self._state_handler.get(self.config, force=force)
|
|
|
|
if state is None:
|
|
state = StateModel.from_config(self.config)
|
|
else:
|
|
logger.info("Resuming from existing state.")
|
|
|
|
while state.current_cycle < self.config.max_cyc:
|
|
logger.info(
|
|
f"Starting cycle {state.current_cycle + 1} of {self.config.max_cyc}."
|
|
)
|
|
|
|
step_directory = self.config.simulation_dir / f"{state.current_cycle:02d}"
|
|
if not step_directory.exists():
|
|
step_directory.mkdir(parents=True)
|
|
|
|
state = DiceHandler.run(state, state.current_cycle)
|
|
|
|
state = OptimizationHandler.run(state, state.current_cycle)
|
|
|
|
state.current_cycle += 1
|
|
self._state_handler.save(state)
|
|
|
|
logger.info("Reached maximum number of cycles. Simulation complete.")
|