diff --git a/src/primaite/__init__.py b/src/primaite/__init__.py index 28245d33..1e5fe925 100644 --- a/src/primaite/__init__.py +++ b/src/primaite/__init__.py @@ -38,6 +38,7 @@ class _PrimaitePaths: self.app_config_file_path = self.generate_app_config_file_path() self.app_log_dir_path = self.generate_app_log_dir_path() self.app_log_file_path = self.generate_app_log_file_path() + self.episode_steps_log_file_path = self.generate_episode_step_log_file_path() def _get_dirs_properties(self) -> List[str]: class_items = self.__class__.__dict__.items() @@ -105,6 +106,10 @@ class _PrimaitePaths: """The PrimAITE app log file path.""" return self.app_log_dir_path / "primaite.log" + def generate_episode_step_log_file_path(self) -> Path: + """The PrimAITE app episode step log file path.""" + return self.app_log_dir_path / "epi_step.json" + def __repr__(self) -> str: properties_str = ", ".join([f"{p}='{getattr(self, p)}'" for p in self._get_dirs_properties()]) return f"{self.__class__.__name__}({properties_str})" diff --git a/src/primaite/game/game.py b/src/primaite/game/game.py index 4e896987..3409100e 100644 --- a/src/primaite/game/game.py +++ b/src/primaite/game/game.py @@ -1,18 +1,17 @@ """PrimAITE game - Encapsulates the simulation and agents.""" import json -from datetime import datetime +import os from ipaddress import IPv4Address from typing import Dict, List from pydantic import BaseModel, ConfigDict -from primaite import getLogger +from primaite import getLogger, PRIMAITE_PATHS from primaite.game.agent.actions import ActionManager from primaite.game.agent.data_manipulation_bot import DataManipulationAgent from primaite.game.agent.interface import AbstractAgent, AgentSettings, ProxyAgent, RandomAgent from primaite.game.agent.observations import ObservationManager from primaite.game.agent.rewards import RewardFunction -from primaite.session.io import generate_session_path from primaite.simulator.network.hardware.base import NIC, NodeOperatingState from primaite.simulator.network.hardware.nodes.computer import Computer from primaite.simulator.network.hardware.nodes.router import ACLAction, Router @@ -112,10 +111,10 @@ class PrimaiteGame: # Create state suitable for dumping to JSON file. dump_state = {self.episode_counter: {self.step_counter: sim_state}} - json_path = generate_session_path(datetime.now()) / "describe_state.json" # Dump to file - with open(json_path, "a") as f: - json.dump(dump_state, f) + if os.path.isfile(PRIMAITE_PATHS.episode_steps_log_file_path): + with open(PRIMAITE_PATHS.episode_steps_log_file_path, "a") as f: + json.dump(dump_state, f) # Update agents' observations and rewards based on the current state self.update_agents(sim_state)