/// <summary> /// Plays k simulated games using the given Simulation Strategy. /// </summary> /// <param name="state">The game tree root</param> /// <returns> /// 1 case the AI wins, -1 case it loses and 0 if the simulation dos not reach /// a leaf node that finish the game. /// </returns> private int RunSimulation(Node state) { MCTSGame = new Game(CurGame); int result = 0; int playouts = 0; Random rnd = new Random(); List <ACommand> simulatedCmds; List <ACommand> validCmds; while (playouts < MAX_SIMULATION_DEPTH) { validCmds = MCTSGame.GetValidCommands(); // If the current player has no pawns left if (validCmds.Count == 0) { MCTSGame.SetNextPlayer(); validCmds = MCTSGame.GetValidCommands(); } Simulation.SetUp(validCmds); simulatedCmds = Simulation.Execute(); int index = rnd.Next(simulatedCmds.Count); Player playerCp = MCTSGame.GetCurPlayer().Copy(MCTSGame.GetBoards()); Node nextState = new Node(simulatedCmds[index].Value(), MCTSGame.GetBoards(), simulatedCmds[index]); Expansion.Expand(state, nextState); MCTSGame.ChangeState(nextState, true); nextState.Cmd.SetCurPlayer(playerCp); state = nextState; playouts++; if (MCTSGame.IsOver()) { break; } } return(result); }
/// <summary> /// Simulate the circuit /// </summary> /// <param name="sim">The simulation that needs to be executed</param> public void Simulate(ISimulation sim) { // Setup the circuit Setup(); Simulation = sim; if (Objects.Count <= 0) { throw new CircuitException("Circuit contains no objects"); } if (Nodes.Count <= 1) { throw new CircuitException("Circuit contains no nodes"); } // Do temperature-dependent calculations foreach (var c in Objects) { c.Temperature(this); } // Execute the simulation sim.Execute(this); }