1010from codeclash .agents .utils import GameContext
1111from codeclash .constants import DIR_WORK
1212from codeclash .games import get_game
13- from codeclash .games .abstract import CodeGame , RoundStats
13+ from codeclash .games .abstract import CodeGame
1414from codeclash .tournaments .abstract import AbstractTournament
1515from codeclash .tournaments .utils .git_utils import filter_git_diff
1616from codeclash .utils .environment import copy_to_container
1717
1818
1919class SinglePlayerTraining (AbstractTournament ):
20- def __init__ (self , config : dict , cleanup : bool = False ):
20+ def __init__ (self , config : dict , * , cleanup : bool = False ):
2121 super ().__init__ (config , name = "SinglePlayerTraining" )
2222 self .cleanup_on_end = cleanup
2323 self .game : CodeGame = get_game (
@@ -29,12 +29,23 @@ def __init__(self, config: dict, cleanup: bool = False):
2929 mirror_agent_config = copy .deepcopy (self .config ["player" ])
3030 mirror_agent_config ["name" ] = "mirror"
3131 self .mirror_agent : Player = self .get_agent (mirror_agent_config , round = 0 )
32- self .scoreboard : list [RoundStats ] = []
32+
33+ @property
34+ def scoreboard (self ) -> list [tuple [int , str ]]:
35+ return self ._metadata .setdefault ("scoreboard" , [])
3336
3437 @property
3538 def rounds (self ) -> int :
3639 return self .config ["tournament" ]["rounds" ]
3740
41+ def get_metadata (self ) -> dict :
42+ return {
43+ ** super ().get_metadata (),
44+ "scoreboard" : self .scoreboard ,
45+ "game" : self .game .get_metadata (),
46+ "agents" : [self .agent .get_metadata (), self .mirror_agent .get_metadata ()],
47+ }
48+
3849 def get_game_context (self , agent_config : dict , * , round : int ) -> GameContext :
3950 """Create a game context for an agent."""
4051 return GameContext (
@@ -71,7 +82,7 @@ def run(self):
7182 if self .config ["tournament" ]["evaluate_matrix" ]:
7283 self .evaluate ()
7384 finally :
74- self .cleanup ()
85+ self .end ()
7586
7687 def run_training_round (self , round_num : int ) -> None :
7788 """Execute a single training round, i.e., run the game, then run the agent."""
@@ -113,11 +124,11 @@ def set_mirror_state_to_round(self, round_num: int):
113124 full_diff = filter_git_diff (full_diff )
114125 self .mirror_agent .reset_and_apply_patch (full_diff )
115126
116- def cleanup (self ):
127+ def end (self ):
117128 """Clean up game resources."""
118129 self .game .end (self .cleanup_on_end )
119130
120- def evaluate (self , n_repetitions : int = 3 ):
131+ def evaluate (self , n_repetitions : int = 3 ) -> None :
121132 """Evaluate the agent's performance by
122133 calculating the matrix of every round against each other.
123134 """
@@ -144,4 +155,4 @@ def evaluate(self, n_repetitions: int = 3):
144155 self .logger .info (f"Round { p1_round } vs { p2_round } repetition { i_repetition } winner: { winner } " )
145156 matrix [p1_round ][p2_round ].append (winner )
146157 self .logger .info (f"Evaluation matrix: { matrix } " )
147- return matrix
158+ self . _metadata . setdefault ( "evaluation" , {})[ "matrix" ] = matrix
0 commit comments