Spaces:
Running
Running
File size: 1,487 Bytes
cf1ebab ba4c3a1 6fe0d0c ba4c3a1 8750468 ba4c3a1 8750468 ba4c3a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# portfolio/npc_social_network/manager/simulation_manager.py
import pickle
import os
from npc_social_network.npc.npc_manager import NPCManager
SAVE_DIR = "npc_social_network/data/saves"
if not os.path.exists(SAVE_DIR):
os.makedirs(SAVE_DIR)
def save_simulation(npc_manager: NPCManager, filename: str = "simulation_state.pkl"):
"""
ํ์ฌ NPC ๋งค๋์ ์ ์ํ๋ฅผ ํ์ผ์ ์ ์ฅํฉ๋๋ค.
"""
filepath = os.path.join(SAVE_DIR, filename)
try:
with open(filepath, "wb") as f:
pickle.dump(npc_manager, f)
print(f"โ
์๋ฎฌ๋ ์ด์
์ํ๊ฐ '{filepath}'์ ์ฑ๊ณต์ ์ผ๋ก ์ ์ฅ๋์์ต๋๋ค.")
except Exception as e:
print(f"โ ์๋ฎฌ๋ ์ด์
์ ์ฅ์ ์คํจํ์ต๋๋ค: {e}")
def load_simulation(filename: str = "simulation_state.pkl") -> NPCManager | None:
"""
ํ์ผ์์ NPC ๋งค๋์ ์ ์ํ๋ฅผ ๋ถ๋ฌ์ต๋๋ค.
"""
filepath = os.path.join(SAVE_DIR, filename)
if not os.path.exists(filepath):
print("โน๏ธ ์ ์ฅ๋ ์๋ฎฌ๋ ์ด์
ํ์ผ์ด ์์ต๋๋ค. ์๋ก์ด ์๋ฎฌ๋ ์ด์
์ ์์ํฉ๋๋ค.")
return None
try:
with open(filepath, "rb") as f:
npc_manager = pickle.load(f)
print(f"โ
์๋ฎฌ๋ ์ด์
์ํ๋ฅผ '{filepath}'์์ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค.")
return npc_manager
except Exception as e:
print(f"โ ์๋ฎฌ๋ ์ด์
๋ถ๋ฌ์ค๊ธฐ์ ์คํจํ์ต๋๋ค: {e}")
return None |