public void InitTree(TyStateAnalyzer analyzer, POGame root, List <PlayerTask> options) { _sortedNodes.Clear(); _explorableNodes.Clear(); _nodesToEstimate.Clear(); _analyzer = analyzer; _rootGame = root; //var initialResults = TyStateUtility.GetSimulatedGames(root, options, _analyzer); for (int i = 0; i < options.Count; i++) { PlayerTask task = options[i]; var node = new TyTaskNode(this, _analyzer, task, 0.0f); //end turn is pretty straight forward, should not really be looked at later in the simulations, just simulate once and keep the value: if (task.PlayerTaskType == PlayerTaskType.END_TURN) { TySimResult sim = TyStateUtility.GetSimulatedGame(root, task, _analyzer); node.AddValue(sim.value); } else { _explorableNodes.Add(node); _sortedNodes.Add(node); } _nodesToEstimate.Add(task, node); } }
public void SimulateEpisode(Random random, int curEpisode, bool shouldExploit) { TyTaskNode nodeToExplore = null; //exploiting: if (shouldExploit) { _sortedNodes.Sort((x, y) => y.TotalValue.CompareTo(x.TotalValue)); //exploit only 50% best nodes: int count = ((int)(_sortedNodes.Count * 0.5 + 0.5)); nodeToExplore = _sortedNodes.GetUniformRandom(random, count); } //explore: else { nodeToExplore = _explorableNodes[curEpisode % _explorableNodes.Count]; } //should not be possible: if (nodeToExplore == null) { return; } PlayerTask task = nodeToExplore.Task; TySimResult result = TyStateUtility.GetSimulatedGame(_rootGame, task, _analyzer); nodeToExplore.Explore(result, random); }
private PlayerTask GetSimulationTreeTask(POGame poGame, List <PlayerTask> options) { double time = TyUtility.GetSecondsSinceStart() - _turnTimeStart; if (time >= TyConst.MAX_TURN_TIME) { TyDebug.LogError("Turn takes too long, fall back to greedy."); return(GetGreedyBestTask(poGame, options)); } _simTree.InitTree(_analyzer, poGame, options); //-1 because TurnEnd won't be looked at: int optionCount = options.Count - 1; int numEpisodes = (int)((optionCount) * _curEpisodeMultiplier); double simStart = TyUtility.GetSecondsSinceStart(); for (int i = 0; i < numEpisodes; i++) { if (!IsAllowedToSimulate(simStart, i, numEpisodes, optionCount)) { break; } bool shouldExploit = ((double)i / (double)numEpisodes) > EXPLORE_TRESHOLD; _simTree.SimulateEpisode(_random, i, shouldExploit); } TyTaskNode bestNode = _simTree.GetBestNode(); return(bestNode.Task); }