private List <Episode> EpisodesFromBestRollouts(Rollout[] bestRollouts, HeroType chosenHero) { return(Enumerable.Range(0, ReplaysPerIteration).AsParallel() .Select(_ => { Random random = _threadContext.Value.Random; while (true) { Rollout rollout = bestRollouts[random.Next(bestRollouts.Length)]; RolloutTick tick = rollout.Ticks[random.Next(rollout.Ticks.Count)]; World world = tick.World; Unit hero = world.Units.FirstOrDefault(u => u.Team == 0 && u.UnitType == UnitType.Hero && u.HeroType == chosenHero); if (hero == null) { continue; } return new Episode { Hero = hero, Tactic = tick.HeroTactics[hero.UnitId], Weight = 1.0, World = world, }; } }) .ToList()); }
private static IEnumerable <HeroPairing> PairingsFromMatchup(Rollout matchupResult) { foreach (HeroType myHero in matchupResult.Matchup.Team0) { foreach (HeroType enemyHero in matchupResult.Matchup.Team1) { yield return(new HeroPairing { MyHero = myHero, EnemyHero = enemyHero, WinRate = matchupResult.WinRate, }); } } }
private double IntermediateWinner(Rollout rollout) { double team0 = IntermediateEvaluator.Evaluate(rollout.FinalWorld, 0).Score; double team1 = IntermediateEvaluator.Evaluate(rollout.FinalWorld, 1).Score; if (team0 > team1) { return(1); } else if (team0 < team1) { return(0); } else { return(0.5); } }
private List <Episode> GenerateTrainingSet(Policy incumbent, Policy currentPolicy, Rollout[] starters, HeroType chosenHero) { List <Episode> trainingSet = new List <Episode>(); int generated = trainingSet.Count; int nextMessage = NextStatusInterval(generated, StatusInterval); Stopwatch generatingStopwatch = Stopwatch.StartNew(); Console.WriteLine(string.Format("Generating up to {0} replays", ReplaysPerIteration)); Console.Write("> "); trainingSet.AddRange( Enumerable.Range(0, ReplaysPerIteration) .AsParallel() .SelectMany(_ => { Random random = _threadContext.Value.Random; while (true) { Rollout rollout = starters[random.Next(starters.Length)]; World initialWorld = rollout.Ticks[random.Next(rollout.Ticks.Count)].World; Unit hero = initialWorld.Units.FirstOrDefault(u => u.Team == 0 && u.UnitType == UnitType.Hero && u.HeroType == chosenHero); if (hero == null) { continue; } List <Episode> episodes = Replayer.GenerateRollout(initialWorld, hero.UnitId, currentPolicy, incumbent); int nowGenerated = Interlocked.Add(ref generated, episodes.Count); int nowMessage = nextMessage; int newMessage = NextStatusInterval(nowGenerated, StatusInterval); if (nowGenerated >= nowMessage && Interlocked.CompareExchange(ref nextMessage, newMessage, nowMessage) == nowMessage) { Console.Write(nowMessage + " "); } return(episodes); } }) .WhereNotNull()); Console.WriteLine(); Console.WriteLine(string.Format("{0} episodes generated in {1:F1} seconds", trainingSet.Count, generatingStopwatch.Elapsed.TotalSeconds)); return(trainingSet); }