@@ -59,7 +59,7 @@ protected void TrainAi(ScreenData screen, Action<byte[]> saveBrainBytes)
5959 }
6060
6161 System . Console . WriteLine ( "Starting training..." ) ;
62- var brain = CreateGame ( ) . Brain ;
62+ var brain = CreateBrain ( ) ;
6363 System . Console . WriteLine ( $ "Brain layers: { brain . InputSize } : { brain . HiddenLayers . ToCsv ( ' ' ) } : { brain . OutputSize } ") ;
6464
6565 m_stopTraining = false ;
@@ -85,7 +85,7 @@ public override void StopScreensaver()
8585
8686 private void TrainAiImpl ( Action < byte [ ] > saveBrainBytes )
8787 {
88- m_nextGenBrains ??= Enumerable . Range ( 0 , InitialPopSize ) . Select ( _ => CreateGame ( ) . Brain ) . ToList ( ) ;
88+ m_nextGenBrains ??= Enumerable . Range ( 0 , InitialPopSize ) . Select ( _ => CreateBrain ( ) ) . ToList ( ) ;
8989
9090 var games = new ( double AverageRating , AiGameBase Game , AiBrainBase Brain ) [ m_nextGenBrains . Count ] ;
9191 Parallel . For ( 0 , games . Length , i =>
@@ -102,13 +102,10 @@ private void TrainAiImpl(Action<byte[]> saveBrainBytes)
102102 var gameCount = 1 ;
103103 for ( var trial = 0 ; trial < 4 && ! m_stopTraining ; trial ++ , gameCount ++ )
104104 {
105- var game = CreateGameWithSeed ( Random . Shared . Next ( ) ) ;
106- game . Brain = baseGame . Brain ;
105+ var game = CreateGameWithSeed ( Random . Shared . Next ( ) , baseGame . Brain ) ;
107106 while ( ! game . IsGameOver )
108107 game . Tick ( ) ;
109108 totalRating += game . Rating ;
110- if ( game . Rating <= 0.0001 )
111- break ; // No score, no point in continuing.
112109 }
113110
114111 totalRating /= gameCount ;
@@ -170,7 +167,7 @@ private void TrainAiImpl(Action<byte[]> saveBrainBytes)
170167 nextBrains . AddRange ( m_goatBrains . Select ( o => o . Brain . Clone ( ) ) ) ;
171168
172169 // Spawn 5% pure randoms.
173- nextBrains . AddRange ( Enumerable . Range ( 0 , ( int ) ( m_currentPopSize * 0.05 ) ) . Select ( _ => CreateGameWithSeed ( 0 ) . Brain . Clone ( ) . Randomize ( ) ) ) ;
170+ nextBrains . AddRange ( Enumerable . Range ( 0 , ( int ) ( m_currentPopSize * 0.05 ) ) . Select ( _ => CreateBrain ( ) ) ) ;
174171
175172 // Elite get to be parents.
176173 var breeders = orderedGames . Select ( o => ( o . AverageRating , o . Brain ) ) . ToList ( ) ;
@@ -187,13 +184,14 @@ private void TrainAiImpl(Action<byte[]> saveBrainBytes)
187184 m_nextGenBrains = nextBrains ;
188185 }
189186
190- private AiGameBase CreateGameWithSeed ( int seed )
187+ private AiGameBase CreateGameWithSeed ( int seed , AiBrainBase brain = null )
191188 {
192- var game = CreateGame ( ) ;
189+ var game = CreateGame ( brain ?? CreateBrain ( ) ) ;
193190 game . GameRand = new Random ( seed ) ;
194191 game . ResetGame ( ) ;
195192 return game ;
196193 }
197194
198- protected abstract AiGameBase CreateGame ( ) ;
195+ protected abstract AiGameBase CreateGame ( AiBrainBase brain ) ;
196+ protected abstract AiBrainBase CreateBrain ( ) ;
199197}
0 commit comments