59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from diceplayer.config.player_config import PlayerConfig
|
|
from diceplayer.dice.dice_handler import DiceHandler
|
|
from diceplayer.logger import logger
|
|
from diceplayer.state.state_handler import StateHandler
|
|
from diceplayer.state.state_model import StateModel
|
|
|
|
from typing_extensions import TypedDict, Unpack
|
|
|
|
from diceplayer.utils.potential import read_system_from_phb
|
|
|
|
|
|
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 = 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 = None
|
|
|
|
if state is None:
|
|
system = read_system_from_phb(self.config)
|
|
state = StateModel(
|
|
config=self.config, system=system
|
|
)
|
|
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(step_directory).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.")
|