public void MutateSelf( Random randomness_provider, PD_Game initial_gameState, PD_AI_PathFinder pathFinder, PD_AI_Macro_Agent_Base defaultPolicyAgent, PD_AI_Macro_Agent_Base mutatorAgent ) { PD_Game mutation_GameState = initial_gameState.Request_Randomized_Copy(randomness_provider); int numberOfMacros = MacroActions.Count; int mutationIndex = randomness_provider.Next(numberOfMacros); List <PD_MacroAction> mutated_MacroActions = new List <PD_MacroAction>(); // first apply the non - mutated actions int counter = 0; while (Is_GameState_CorrectTurn_And_Ongoing(mutation_GameState)) { if (counter < mutationIndex) { // counter < mutation index => simply copy the existing macros into the mutated macros list. PD_MacroAction currentMacro = MacroActions[counter]; RH_State_Type stateType = Calculate_State_Type(mutation_GameState); RH_Macro_Type macroType = Calculate_Macro_Type(currentMacro); RH_Macro_Apply_Type response = Calculate_Macro_Application_Type( stateType, macroType ); switch (response) { case RH_Macro_Apply_Type.Normal: // store the macro and then apply it mutated_MacroActions.Add( MacroActions[counter] ); ApplyMacroOnGameState( randomness_provider, mutation_GameState, MacroActions[counter] ); counter++; break; case RH_Macro_Apply_Type.Insert: PD_MacroAction actionToInsert = defaultPolicyAgent.GetNextMacroAction( randomness_provider, mutation_GameState, pathFinder ); // first correct the macro actions, themselves... MacroActions.Insert( counter, actionToInsert ); mutated_MacroActions.Add( actionToInsert ); ApplyMacroOnGameState( randomness_provider, mutation_GameState, actionToInsert ); counter++; mutationIndex++; break; case RH_Macro_Apply_Type.Skip: // keep the skipped action, but do not apply it! mutated_MacroActions.Add( MacroActions[counter] ); counter++; break; case RH_Macro_Apply_Type.Error: throw new System.Exception("error here!"); } } else if (counter == mutationIndex) { // counter == mutation index => ask the mutator to provide a new action var replacement_MacroAction = mutatorAgent.GetNextMacroAction( randomness_provider, mutation_GameState, pathFinder ); mutated_MacroActions.Add(replacement_MacroAction); ApplyMacroOnGameState( randomness_provider, mutation_GameState, replacement_MacroAction ); counter++; } else { // counter > mutation index => ask the default policy agent to provide a new action var nextMacro = defaultPolicyAgent.GetNextMacroAction( randomness_provider, mutation_GameState, pathFinder ); if (nextMacro == null) { throw new System.Exception("problem here"); } ApplyMacroOnGameState( randomness_provider, mutation_GameState, nextMacro ); mutated_MacroActions.Add(nextMacro); counter++; } } MacroActions = mutated_MacroActions; }
public void ApplySelfOnGameState( Random randomness_provider, PD_Game gameStateToApplySelfOn, PD_AI_PathFinder pathFinder, PD_AI_Macro_Agent_Base defaultPolicyAgent ) { int currentMacroIndex = 0; while (Is_GameState_CorrectTurn_And_Ongoing(gameStateToApplySelfOn)) { PD_MacroAction currentMacro; if (currentMacroIndex < MacroActions.Count) { currentMacro = MacroActions[currentMacroIndex]; } else { currentMacro = null; } RH_State_Type state_Type = Calculate_State_Type(gameStateToApplySelfOn); RH_Macro_Type macro_Type = Calculate_Macro_Type(currentMacro); int currentGameState_TurnIndex = gameStateToApplySelfOn.game_state_counter.turn_index; RH_Macro_Apply_Type macroApplicationType = Calculate_Macro_Application_Type( state_Type, macro_Type ); // ACT ACCORDINGLY! switch (macroApplicationType) { case RH_Macro_Apply_Type.Normal: ApplyMacroOnGameState( randomness_provider, gameStateToApplySelfOn, currentMacro ); currentMacroIndex++; break; case RH_Macro_Apply_Type.Skip: currentMacroIndex++; break; case RH_Macro_Apply_Type.Insert: var missingMacro = defaultPolicyAgent.GetNextMacroAction( randomness_provider, gameStateToApplySelfOn, pathFinder ); RH_Macro_Type missingMacroType = Calculate_Macro_Type(missingMacro); MacroActions.Insert( currentMacroIndex, missingMacro ); ApplyMacroOnGameState( randomness_provider, gameStateToApplySelfOn, missingMacro ); currentMacroIndex++; break; case RH_Macro_Apply_Type.Error: throw new System.Exception("something wrong here!"); default: throw new System.Exception("something wrong here!"); } } }