private void OnMyTurnEnd() { _isTurnBegin = true; var timeNeeded = TyUtility.GetSecondsSinceStart() - _turnTimeStart; if (AdjustEpisodeMultiplier && UsedAlgorithm == Algorithm.SearchTree) { const double MAX_DIFF = 4.0; double diff = Math.Min(TyConst.DECREASE_SIMULATION_TIME - timeNeeded, MAX_DIFF); double factor = 0.05; //reduce more if above the time limit: if (diff <= 0.0f) { factor = 0.2; } //simulate at max this value * _defaultEpisodeMultiplier: const int MAX_EPISODE_MULTIPLIER = 4; _curEpisodeMultiplier = Math.Clamp(_curEpisodeMultiplier + (int)(factor * diff * _defaultEpisodeMultiplier), _defaultEpisodeMultiplier, _defaultEpisodeMultiplier * MAX_EPISODE_MULTIPLIER); } if (PrintTurnTime) { TyDebug.LogInfo("Turn took " + timeNeeded.ToString("0.000") + "s"); } if (timeNeeded >= TyConst.MAX_TURN_TIME) { TyDebug.LogWarning("Turn took " + timeNeeded.ToString("0.000") + "s"); } }
private PlayerTask GetSimulationTreeTask(POGame.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); } var bestNode = _simTree.GetBestNode(); return(bestNode.Task); }
/// <summary> False if there is not enough time left to do simulations. </summary> private bool IsAllowedToSimulate(double startTime, int curEpisode, int maxEpisode, int options) { double time = TyUtility.GetSecondsSinceStart() - startTime; if (time >= TyConst.MAX_SIMULATION_TIME) { TyDebug.LogWarning("Stopped simulations after " + time.ToString("0.000") + "s and " + curEpisode + " of " + maxEpisode + " episodes. Having " + options + " options."); return(false); } return(true); }
private void OnMyTurnBegin(POGame.POGame state) { _isTurnBegin = false; _turnTimeStart = TyUtility.GetSecondsSinceStart(); }