순수 플레이 전환: ACTION_DELAY 축소, 에러 트레이스백 추가, 타임스탬프 로깅
This commit is contained in:
24
main.py
24
main.py
@@ -1,8 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
main.py — 컨텍스트 압축 통합 버전
|
main.py — 순수 AI 플레이 버전
|
||||||
|
|
||||||
초반: StateReader 직접 사용 (상세 정보 필요)
|
치트 없이 실제 게임 메커닉으로 플레이.
|
||||||
중반 이후: ContextCompressor로 자동 전환 (건물 50개 초과 시)
|
걷기/채굴/제작에 실제 시간이 걸리므로 ACTION_DELAY를 줄임.
|
||||||
"""
|
"""
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
@@ -16,7 +16,7 @@ from action_executor import ActionExecutor
|
|||||||
RCON_HOST = os.getenv("FACTORIO_HOST", "127.0.0.1")
|
RCON_HOST = os.getenv("FACTORIO_HOST", "127.0.0.1")
|
||||||
RCON_PORT = int(os.getenv("FACTORIO_PORT", "25575"))
|
RCON_PORT = int(os.getenv("FACTORIO_PORT", "25575"))
|
||||||
RCON_PASSWORD = os.getenv("FACTORIO_PASSWORD", "factorio_ai")
|
RCON_PASSWORD = os.getenv("FACTORIO_PASSWORD", "factorio_ai")
|
||||||
ACTION_DELAY = 0.5
|
ACTION_DELAY = 0.2 # 행동 자체에 시간이 걸리므로 간격 짧게
|
||||||
LOG_FILE = "agent_log.jsonl"
|
LOG_FILE = "agent_log.jsonl"
|
||||||
|
|
||||||
# 건물 N개 이상이면 압축 모드로 전환
|
# 건물 N개 이상이면 압축 모드로 전환
|
||||||
@@ -25,7 +25,8 @@ COMPRESS_THRESHOLD = 50
|
|||||||
|
|
||||||
def run():
|
def run():
|
||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
print(" 팩토리오 완전 자율 AI 에이전트 (컨텍스트 압축 포함)")
|
print(" 팩토리오 순수 AI 에이전트 (치트 없음)")
|
||||||
|
print(" - 실제 걷기 / 실제 채굴 / 실제 제작 / 건설 거리 제한")
|
||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
|
|
||||||
with FactorioRCON(RCON_HOST, RCON_PORT, RCON_PASSWORD) as rcon:
|
with FactorioRCON(RCON_HOST, RCON_PORT, RCON_PASSWORD) as rcon:
|
||||||
@@ -36,7 +37,7 @@ def run():
|
|||||||
|
|
||||||
total_actions = 0
|
total_actions = 0
|
||||||
queue: list[dict] = []
|
queue: list[dict] = []
|
||||||
entity_count = 0 # 마지막으로 파악한 건물 수
|
entity_count = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@@ -46,7 +47,6 @@ def run():
|
|||||||
|
|
||||||
# ── 압축 레벨 자동 선택 ──────────────────────
|
# ── 압축 레벨 자동 선택 ──────────────────────
|
||||||
if entity_count < COMPRESS_THRESHOLD:
|
if entity_count < COMPRESS_THRESHOLD:
|
||||||
# 초반: 상세 상태
|
|
||||||
print("[상태] 초반 모드 — 상세 상태 수집")
|
print("[상태] 초반 모드 — 상세 상태 수집")
|
||||||
state = reader.get_full_state()
|
state = reader.get_full_state()
|
||||||
summary = reader.summarize_for_ai(state)
|
summary = reader.summarize_for_ai(state)
|
||||||
@@ -55,15 +55,12 @@ def run():
|
|||||||
if isinstance(v, int)
|
if isinstance(v, int)
|
||||||
)
|
)
|
||||||
elif entity_count < 200:
|
elif entity_count < 200:
|
||||||
# 중반: 구역별 압축 (level 1)
|
|
||||||
print(f"[상태] 중반 모드 — 구역 압축 (건물 {entity_count}개)")
|
print(f"[상태] 중반 모드 — 구역 압축 (건물 {entity_count}개)")
|
||||||
summary = compressor.get_compressed_state(detail_level=1)
|
summary = compressor.get_compressed_state(detail_level=1)
|
||||||
else:
|
else:
|
||||||
# 후반: 글로벌 압축 (level 0)
|
|
||||||
print(f"[상태] 후반 모드 — 글로벌 압축 (건물 {entity_count}개)")
|
print(f"[상태] 후반 모드 — 글로벌 압축 (건물 {entity_count}개)")
|
||||||
summary = compressor.get_compressed_state(detail_level=0)
|
summary = compressor.get_compressed_state(detail_level=0)
|
||||||
|
|
||||||
# 건물 수 업데이트
|
|
||||||
global_info = compressor._get_global_summary()
|
global_info = compressor._get_global_summary()
|
||||||
entity_count = global_info.get("total_entities", entity_count)
|
entity_count = global_info.get("total_entities", entity_count)
|
||||||
|
|
||||||
@@ -91,7 +88,7 @@ def run():
|
|||||||
planner.record_feedback(action, success, message)
|
planner.record_feedback(action, success, message)
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
print(" → 실패. 재계획")
|
print(" → 실패. 재계획 요청")
|
||||||
queue.clear()
|
queue.clear()
|
||||||
|
|
||||||
_log(total_actions, action, success, message)
|
_log(total_actions, action, success, message)
|
||||||
@@ -102,6 +99,8 @@ def run():
|
|||||||
break
|
break
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"[오류] {e}")
|
print(f"[오류] {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
@@ -109,7 +108,8 @@ def _log(step, action, success, message):
|
|||||||
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||||
f.write(json.dumps({
|
f.write(json.dumps({
|
||||||
"step": step, "action": action,
|
"step": step, "action": action,
|
||||||
"success": success, "message": message
|
"success": success, "message": message,
|
||||||
|
"timestamp": time.time()
|
||||||
}, ensure_ascii=False) + "\n")
|
}, ensure_ascii=False) + "\n")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user