Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions codeclash/analysis/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import uuid
from pathlib import Path
from queue import Queue
from threading import Lock, Thread
from threading import Event, Lock, Thread

from codeclash.agents.dummy_agent import Dummy
from codeclash.agents.utils import GameContext
Expand Down Expand Up @@ -213,13 +213,13 @@ def _evaluate_matrix_cell_parallel(
self.logger.info(f"Evaluating {player1_name} round {i} vs {player2_name} round {j}")

round_id = str(uuid.uuid4().hex)
stats = game_worker.run_round([agent1, agent2], round_id, copy_logs=False)
stats = game_worker.run_round([agent1, agent2], round_id, copy_logs=True)
result = stats.to_dict()
self.logger.debug(f"Result: {result}")

with self._save_lock:
self.matrices[matrix_id][str(i)][str(j)] = result
self._save()
self._save()

return (i, j, result)

Expand All @@ -238,6 +238,7 @@ def _worker_thread(self, worker_id: int, task_queue: Queue):
try:
self._evaluate_matrix_cell_parallel(game_worker, **task)
except Exception as e:
self._evaluation_failed.set()
self.logger.error(f"Worker {worker_id} failed on task {task}: {e}")
finally:
task_queue.task_done()
Expand Down Expand Up @@ -293,14 +294,19 @@ def _evaluate_matrix(self, player1_name: str, player2_name: str):
for worker in workers:
worker.join()

self.logger.info(f"Completed matrix evaluation for {matrix_id}")
self.logger.info(f"Finished processing matrix {matrix_id}")

def evaluate_all_matrices(self) -> dict:
"""Evaluate vs matrix between the two players."""
self.logger.info("Starting matrix evaluation")
self._evaluation_failed = Event()
self._metadata.pop("evaluation_completed_timestamp", None)
self._evaluate_matrix(self.players[0], self.players[0])
self._evaluate_matrix(self.players[1], self.players[1])
self._metadata["evaluation_completed_timestamp"] = int(time.time())
if self._evaluation_failed.is_set():
self.logger.error("Matrix evaluation incomplete: one or more cells failed")
else:
self._metadata["evaluation_completed_timestamp"] = int(time.time())
self.end()
return self.matrices

Expand Down