Skip to content

Commit 98918a0

Browse files
author
yolo h8cker 93
committed
Fix viewer for new multiprocessing matrix building
1 parent 24bf0da commit 98918a0

4 files changed

Lines changed: 32 additions & 38 deletions

File tree

.cursor/rules/viewer.mdc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,21 @@ If a file `matrix.json` exists, load it. It looks like this:
405405
"matrices": {
406406
"sonnet-4_vs_sonnet-4": {
407407
"0": {
408-
"0": {
408+
"1": {
409409
"round_num": "e2513f264ac04a019c25982d2b03b3bb",
410410
"winner": "sonnet-4_2",
411411
"scores": {
412-
"sonnet-4_2": 1,
413-
"sonnet-4_1": 0.0
412+
"sonnet-4_r0": 1,
413+
"sonnet-4_r1": 0.0
414414
},
415415
"player_stats": {
416-
"sonnet-4_2": {
416+
"sonnet-4_r0": {
417417
"name": "sonnet-4_2",
418418
"invalid_reason": "",
419419
"score": 1,
420420
"valid_submit": true
421421
},
422-
"sonnet-4_1": {
422+
"sonnet-4_r1": {
423423
"name": "sonnet-4_1",
424424
"invalid_reason": "",
425425
"score": 0.0,

codeclash/analysis/matrix.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ def _load_existing_progress(self):
9595
if "matrices" in existing_data:
9696
self._metadata["matrices"] = existing_data["matrices"]
9797

98-
def _save_progress(self):
99-
"""Save current progress to matrix.json in a thread-safe manner."""
100-
with self._save_lock:
101-
self.output_file.write_text(json.dumps(self._metadata, indent=2))
102-
self.logger.debug("Progress saved to matrix.json")
103-
10498
def _initialize_game_pool(self):
10599
"""Initialize a pool of game objects for parallel execution."""
106100
self.game_pool = []
@@ -214,9 +208,16 @@ def _evaluate_matrix_cell_parallel(
214208

215209
round_id = str(uuid.uuid4().hex)
216210
stats = game_worker.run_round([agent1, agent2], round_id)
217-
self.logger.debug(f"Result: {stats.to_dict()}")
211+
result = stats.to_dict()
212+
self.logger.debug(f"Result: {result}")
218213

219-
return (i, j, stats.to_dict())
214+
# Save the result immediately after computation
215+
with self._save_lock:
216+
self.matrices[matrix_id][str(i)][str(j)] = result
217+
self.output_file.write_text(json.dumps(self._metadata, indent=2))
218+
self.logger.debug(f"Saved result for {player1_name} round {i} vs {player2_name} round {j}")
219+
220+
return (i, j, result)
220221

221222
def _evaluate_matrix(self, player1_name: str, player2_name: str):
222223
"""Evaluate a matrix between two players using parallel execution."""
@@ -234,7 +235,7 @@ def _evaluate_matrix(self, player1_name: str, player2_name: str):
234235
game_worker_index = 0
235236

236237
for i in range(self.rounds + 1):
237-
j_range = range(i + 1) if symmetric else range(self.rounds + 1)
238+
j_range = range(i) if symmetric else range(self.rounds + 1)
238239
for j in j_range:
239240
# Skip if already completed
240241
try:
@@ -258,19 +259,11 @@ def _evaluate_matrix(self, player1_name: str, player2_name: str):
258259
# Execute tasks in parallel
259260
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
260261
# Submit all tasks
261-
future_to_task = {executor.submit(self._evaluate_matrix_cell_parallel, *task): task for task in tasks}
262-
263-
# Process completed tasks as they finish
264-
for future in as_completed(future_to_task):
265-
i, j, result = future.result()
266-
if result is not None:
267-
with self._save_lock:
268-
self.matrices[matrix_id][str(i)][str(j)] = result
269-
self._save_progress()
270-
271-
# Log progress
272-
completed = len([f for f in future_to_task if f.done()])
273-
self.logger.info(f"Progress: {completed}/{len(tasks)} matrix cells completed for {matrix_id}")
262+
futures = [executor.submit(self._evaluate_matrix_cell_parallel, *task) for task in tasks]
263+
264+
# Wait for all tasks to complete
265+
for future in as_completed(futures):
266+
future.result() # This will raise any exceptions that occurred
274267

275268
self.logger.info(f"Completed matrix evaluation for {matrix_id}")
276269

codeclash/viewer/app.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,10 @@ def load_matrix_analysis(self) -> dict[str, Any] | None:
628628
for matrix_name, matrix in matrices.items():
629629
processed_matrix = {"name": matrix_name, "data": {}, "max_rounds": 0}
630630

631-
# Find player 1 name (the one with _1 suffix in self-play matrices)
632-
player1_name = None
631+
# Extract base player name from matrix name
632+
base_player_name = None
633633
if "_vs_" in matrix_name:
634-
base_name = matrix_name.split("_vs_")[0]
635-
player1_name = f"{base_name}_1"
634+
base_player_name = matrix_name.split("_vs_")[0]
636635

637636
# Determine matrix dimensions
638637
max_i = max_j = 0
@@ -655,19 +654,21 @@ def load_matrix_analysis(self) -> dict[str, Any] | None:
655654
cell_data = matrix[i_str][j_str]
656655
scores = cell_data.get("scores", {})
657656

658-
# Calculate win percentage from player 1 perspective
659-
if player1_name and player1_name in scores:
660-
player1_score = scores.get(player1_name, 0)
657+
# Calculate win percentage from row player perspective (player using round i code)
658+
row_player_name = f"{base_player_name}_r{i}" if base_player_name else None
659+
660+
if row_player_name and row_player_name in scores:
661+
row_player_score = scores.get(row_player_name, 0)
661662
total_games = sum(scores.values())
662663

663664
if total_games > 0:
664665
# Handle ties if present
665666
ties = scores.get("Tie", 0)
666-
win_percentage = ((player1_score + 0.5 * ties) / total_games) * 100
667+
win_percentage = ((row_player_score + 0.5 * ties) / total_games) * 100
667668
else:
668669
win_percentage = 0
669670
else:
670-
# If we can't identify player 1, show raw scores
671+
# If we can't identify row player, show 0
671672
win_percentage = 0
672673

673674
processed_matrix["data"][i][j] = {

codeclash/viewer/templates/includes/analysis.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ <h2><i class="bi bi-graph-up"></i> Analysis</h2>
5353
<div class="matrix-table-container" id="matrix-{{ matrix_name }}" style="display: none;">
5454
<h4>{{ matrix_name }}</h4>
5555
<p class="matrix-description">
56-
Win percentages from player 1 perspective ({{ matrix_name.split('_vs_')[0] }}_1).
57-
Each cell shows the win rate when player 1 uses round i code vs round j code.
56+
Win percentages from row player perspective ({{ matrix_name.split('_vs_')[0] }}_r{i}).
57+
Each cell shows the win rate when the row player uses round i code vs round j code.
5858
</p>
5959

6060
<div class="matrix-table-wrapper">

0 commit comments

Comments
 (0)