Пример #1
0
        internal async Task <(Dictionary <IAiFactory, (Double meanMoves, Double meanPower, Double meanSamples, Double sampleStdDev)> aggregates, CompletedSimulation worstSim, IAiFactory worstAi)> Simulate(IList <IAiFactory> aiFactories, Int32 runCount)
        {
            var aggregates = new Dictionary <IAiFactory, (Double meanMoves, Double meanPower, Double meanSamples, Double sampleStdDev)>();
            CompletedSimulation worstSim = null;
            IAiFactory          worstAi  = null;
            Int32 levelSeed = Rando.Next(Int32.MinValue, Int32.MaxValue);

            foreach (var aiFactory in aiFactories)
            {
                var levelRand             = new Random(levelSeed);
                var levelGeneratorFactory = new OpenCheckingGeneratorFactory(new DefaultLevelGeneratorFactory(), 6);
                var roverFactory          = new DefaultRoverFactory();
                var simulator             = new Simulator(levelGeneratorFactory, roverFactory, aiFactory);

                using (Completer completer = Completer.Create(Path.Combine(OutputDirectory, $"RoverSim-{aiFactory.Name}.csv")))
                {
                    await simulator.SimulateAsync(runCount, completer.Consume);

                    aggregates[aiFactory] = completer.GetAggregates();
                    worstSim = Completer.ChooseWorst(worstSim, completer.WorstSim);
                    if (worstSim == completer.WorstSim)
                    {
                        worstAi = aiFactory;
                    }
                }
            }

            return(aggregates, worstSim, worstAi);
        }
Пример #2
0
        internal async Task SimulateAsync(IReadOnlyList <IAiFactory> aiFactories, Int32 runCount)
        {
            var tasks   = new List <Task>(aiFactories.Count);
            var results = new Dictionary <IAiFactory, List <CompletedSimulation> >(aiFactories.Count);

            foreach (var aiFactory in aiFactories)
            {
                var levelRand             = new Random(LevelSeed);
                var levelGeneratorFactory = new OpenCheckingGeneratorFactory(new DefaultLevelGeneratorFactory(), 6);
                var roverFactory          = new DefaultRoverFactory();
                var simulator             = new Simulator(levelGeneratorFactory, roverFactory, aiFactory);

                results[aiFactory] = new List <CompletedSimulation>(runCount);
                tasks.Add(simulator.SimulateAsync(runCount, sim => results[aiFactory].Add(sim)));
            }

            await Task.WhenAll(tasks);

            foreach (var ai in aiFactories)
            {
                Simulations.AddRange(results[ai].Select(sim => new SimulationRowViewModel(ai, sim)));
            }
        }