/// <summary>
        /// Runs a prepared benchmark.
        /// </summary>
        public static void RunBenchmark()
        {
            var scale   = new IntVector2(1, 1);
            var offsets = new List <int>()
            {
                2
            };
            const bool enableCorridors = false;

            //var mapDescriptions = GetMapDescriptionsSet(scale, enableCorridors, offsets);
            var mapDescriptions = GetMapDescriptionsForThesis(false);

            var layoutGenerator = LayoutGeneratorFactory.GetDefaultChainBasedGenerator <int>();
            // var layoutGenerator = LayoutGeneratorFactory.GetChainBasedGeneratorWithCorridors<int>(offsets);

            var benchmark = Benchmark.CreateFor(layoutGenerator);

            layoutGenerator.InjectRandomGenerator(new Random(0));
            layoutGenerator.SetLayoutValidityCheck(false);

            //layoutGenerator.SetChainDecompositionCreator(mapDescription => new OldChainDecomposition<int>(new GraphDecomposer<int>()));
            //// layoutGenerator.SetChainDecompositionCreator(mapDescription => new BreadthFirstChainDecomposition<int>(new GraphDecomposer<int>(), false));
            // layoutGenerator.SetGeneratorPlannerCreator(mapDescription => new SlowGeneratorPlanner<Layout<Configuration<EnergyData>, BasicEnergyData>>());
            //layoutGenerator.SetLayoutEvolverCreator((mapDescription, layoutOperations) =>
            //{
            //	var evolver =
            //		new SimulatedAnnealingEvolver<Layout<Configuration<EnergyData>, BasicEnergyData>, int,
            //			Configuration<EnergyData>>(layoutOperations);
            //	evolver.Configure(50, 500);
            //	evolver.SetRandomRestarts(true, SimulatedAnnealingEvolver<Layout<Configuration<EnergyData>, BasicEnergyData>, int, Configuration<EnergyData>>.RestartSuccessPlace.OnAccepted, false, 0.5f);

            //	return evolver;
            //});

            var scenario = BenchmarkScenario.CreateScenarioFor(layoutGenerator);

            scenario.SetRunsCount(2);

            var setups = scenario.MakeSetupsGroup();

            setups.AddSetup("Fixed generator", (generator) => generator.InjectRandomGenerator(new Random()));

            Benchmark.WithDefaultFiles((sw, dw) =>
            {
                benchmark.Execute(layoutGenerator, scenario, mapDescriptions, 100, 1, sw, dw);
            });
        }
Esempio n. 2
0
        protected virtual BenchmarkScenarioResult RunBenchmark(IEnumerable <DungeonGeneratorInput <int> > inputs, int iterations, string name)
        {
            var benchmarkRunner   = new BenchmarkRunner <IMapDescription <int> >();
            var benchmarkScenario = new BenchmarkScenario <IMapDescription <int> >(name, GetGeneratorRunnerFactory);

            var resultSaver = new BenchmarkResultSaver();

            var scenarioResult = benchmarkRunner.Run(benchmarkScenario, inputs, iterations, new BenchmarkOptions()
            {
                WithConsolePreview     = Options.WithConsolePreview,
                MultiThreaded          = Options.MaxThreads > 1,
                MaxDegreeOfParallelism = Options.MaxThreads,
                WithFileOutput         = false,
            });

            resultSaver.SaveResultDefaultLocation(scenarioResult, directory: DirectoryFullPath, name: $"{Directory}_{name}", withDatetime: false);

            return(scenarioResult);
        }
Esempio n. 3
0
        public static void RunOld(Options options)
        {
            // TODO: make better
            Directory = Path.Combine("DungeonGeneratorEvolutions", FileNamesHelper.PrefixWithTimestamp(options.Name));
            System.IO.Directory.CreateDirectory(Directory);
            Logger = new Logger(new ConsoleLoggerHandler(), new FileLoggerHandler(Path.Combine(Directory, "log.txt")));

            var allGraphs = new Dictionary <string, Tuple <string, IGraph <int> > >()
            {
                { "1", Tuple.Create("Example 1 (fig. 1)", GraphsDatabase.GetExample1()) },
                { "2", Tuple.Create("Example 2 (fig. 7 top)", GraphsDatabase.GetExample2()) },
                { "3", Tuple.Create("Example 3 (fig. 7 bottom)", GraphsDatabase.GetExample3()) },
                { "4", Tuple.Create("Example 4 (fig. 8)", GraphsDatabase.GetExample4()) },
                { "5", Tuple.Create("Example 5 (fig. 9)", GraphsDatabase.GetExample5()) },
            };

            var mapDescriptions = new Dictionary <string, Tuple <string, MapDescription <int> > >()
            {
                { "gungeon_1_1", Tuple.Create("Gungeon 1_1", LoadMapDescription("gungeon_1_1")) },
                { "gungeon_1_2", Tuple.Create("Gungeon 1_2", LoadMapDescription("gungeon_1_2")) },
                { "gungeon_2_1", Tuple.Create("Gungeon 2_1", LoadMapDescription("gungeon_2_1")) },
                { "gungeon_2_2", Tuple.Create("Gungeon 2_2", LoadMapDescription("gungeon_2_2")) },
                // { "gungeon_2_4", Tuple.Create("Gungeon 2_4", LoadMapDescription("gungeon_2_4")) },
            };

            var allAnalyzers = new Dictionary <string, Func <IMapDescription <int>, IPerformanceAnalyzer <DungeonGeneratorConfiguration <int>, Individual <int> > > >()
            {
                { "MaxStageTwoFailures", (_) => new MaxStageTwoFailuresAnalyzer <DungeonGeneratorConfiguration <int>, GeneratorData>() },
                { "MaxIterations", (_) => new MaxIterationsAnalyzer <DungeonGeneratorConfiguration <int>, GeneratorData>() },
                { "ChainMerge", (_) => new ChainMergeAnalyzer <DungeonGeneratorConfiguration <int>, int, GeneratorData>() },
                { "ChainOrder", (_) => new ChainOrderAnalyzer <DungeonGeneratorConfiguration <int>, int, GeneratorData>() },
                { "MaxBranching", (_) => new MaxBranchingAnalyzer <DungeonGeneratorConfiguration <int>, GeneratorData>() },
                { "ChainDecomposition", (mapDescription) => new ChainDecompositionAnalyzer <DungeonGeneratorConfiguration <int>, int, GeneratorData>(mapDescription) },
            };

            // Select graphs
            var graphs =
                options.Graphs.Count() != 0
                    ? options
                .Graphs
                .Select(x => allGraphs[x])
                .ToList()
                    : allGraphs.Values.ToList();

            graphs = options
                     .Graphs
                     .Select(x => allGraphs[x])
                     .ToList();

            // Select analyzers
            var analyzers =
                options.Mutations.Count() != 0
                    ? options
                .Mutations
                .Select(x => allAnalyzers[x])
                .ToList()
                    : allAnalyzers.Values.ToList();

            var inputs = new List <Input>();

            foreach (var graphPair in graphs)
            {
                foreach (var corridorOffset in options.CorridorOffsets)
                {
                    var corridorOffsets = corridorOffset.Split(",").Select(x => int.Parse(x)).ToList();
                    var withCorridors   = corridorOffsets[0] != 0;
                    var canTouch        = options.CanTouch || !withCorridors;

                    var name = MapDescriptionUtils.GetInputName(graphPair.Item1, options.Scale, withCorridors,
                                                                corridorOffsets, canTouch);
                    var graph = graphPair.Item2;

                    var basicRoomTemplates   = MapDescriptionUtils.GetBasicRoomTemplates(options.Scale);
                    var basicRoomDescription = new BasicRoomDescription(basicRoomTemplates);

                    var corridorRoomTemplates   = withCorridors ? MapDescriptionUtils.GetCorridorRoomTemplates(corridorOffsets) : null;
                    var corridorRoomDescription = withCorridors ? new CorridorRoomDescription(corridorRoomTemplates) : null;

                    var mapDescription = MapDescriptionUtils.GetBasicMapDescription(graph, basicRoomDescription,
                                                                                    corridorRoomDescription, withCorridors);

                    inputs.Add(new Input()
                    {
                        Name           = name,
                        MapDescription = mapDescription,
                        Configuration  = new DungeonGeneratorConfiguration <int>()
                        {
                            RoomsCanTouch = canTouch,
                        }
                    });
                }
            }

            foreach (var mapDescriptionKey in options.MapDescriptions)
            {
                var mapDescription = mapDescriptions[mapDescriptionKey];

                inputs.Add(new Input()
                {
                    Name           = mapDescription.Item1,
                    MapDescription = mapDescription.Item2,
                    Configuration  = new DungeonGeneratorConfiguration <int>()
                    {
                        RoomsCanTouch = options.CanTouch,
                    }
                });
            }

            var resultsDict = new Dictionary <Input, Result>();
            var partitioner = Partitioner.Create(inputs, EnumerablePartitionerOptions.NoBuffering);

            Parallel.ForEach(partitioner, new ParallelOptions {
                MaxDegreeOfParallelism = options.MaxThreads
            }, input =>
            {
                lock (Logger)
                {
                    Logger.WriteLine($"Started {input.Name}");
                }

                var result = RunEvolution(input, options, analyzers.Select(x => x(input.MapDescription)).ToList());

                lock (Logger)
                {
                    Logger.WriteLine($"Ended {input.Name}");
                }


                lock (resultsDict)
                {
                    resultsDict[input] = result;
                }
            });

            var results = inputs.Select(x => resultsDict[x]).ToList();

            Logger.WriteLine();
            AnalyzeMutations(results.ToList());

            var inputsNewConfigurations = results.Select(x =>
                                                         new DungeonGeneratorInput <int>(x.Input.Name, x.Input.MapDescription, x.NewConfiguration));
            var inputsOldConfigurations = results.Select(x =>
                                                         new DungeonGeneratorInput <int>(x.Input.Name, x.Input.MapDescription, x.Input.Configuration));

            var benchmarkRunner = new BenchmarkRunner <IMapDescription <int> >();

            var benchmarkScenarioNew = new BenchmarkScenario <IMapDescription <int> >("NewConfigurations", GetGeneratorRunnerFactory);
            var benchmarkScenarioOld = new BenchmarkScenario <IMapDescription <int> >("OldConfigurations", GetGeneratorRunnerFactory);

            var resultSaver = new BenchmarkResultSaver();

            var scenarioResultNew = benchmarkRunner.Run(benchmarkScenarioNew, inputsNewConfigurations, options.FinalEvaluationIterations, new BenchmarkOptions()
            {
                WithConsolePreview = false,
            });

            resultSaver.SaveResultDefaultLocation(scenarioResultNew, directory: Directory);

            var scenarioResultOld = benchmarkRunner.Run(benchmarkScenarioOld, inputsOldConfigurations, options.FinalEvaluationIterations, new BenchmarkOptions()
            {
                WithConsolePreview = false,
            });

            resultSaver.SaveResultDefaultLocation(scenarioResultOld, directory: Directory);
        }
        /// <summary>
        /// Benchmark our speed improvements.
        /// </summary>
        public static void CompareOldAndNew()
        {
            //var mapDescriptions = GetMapDescriptionsSet(new IntVector2(1, 1), false);
            var mapDescriptions = GetMapDescriptionsForThesis(false);

            var layoutGenerator = LayoutGeneratorFactory.GetDefaultChainBasedGenerator <int>();
            var benchmark       = Benchmark.CreateFor(layoutGenerator);

            layoutGenerator.InjectRandomGenerator(new Random(0));
            layoutGenerator.SetLayoutValidityCheck(false);

            var scenario = BenchmarkScenario.CreateScenarioFor(layoutGenerator);

            scenario.SetRunsCount(1);

            // Measure the difference between old and new approaches
            {
                var setups = scenario.MakeSetupsGroup();

                setups.AddSetup("Old", (generator) =>
                {
                    //generator.SetChainDecompositionCreator(mapDescription => new OriginalChainDecomposition());
                    generator.SetChainDecompositionCreator(mapDescription => new OldChainDecomposition <int>(new GraphDecomposer <int>()));
                    //generator.SetGeneratorPlannerCreator(mapDescription => new SlowGeneratorPlanner<Layout<Configuration<EnergyData>, BasicEnergyData>>());
                    //generator.SetLayoutEvolverCreator((mapDescription, layoutOperations) =>
                    //{
                    //	var evolver =
                    //		new SimulatedAnnealingEvolver<Layout<Configuration<EnergyData>, BasicEnergyData>, int,
                    //			Configuration<EnergyData>>(layoutOperations);
                    //	evolver.Configure(50, 500);
                    //	evolver.SetRandomRestarts(true, SimulatedAnnealingEvolver<Layout<Configuration<EnergyData>, BasicEnergyData>, int, Configuration<EnergyData>>.RestartSuccessPlace.OnAccepted, false, 0.5f);

                    //	return evolver;
                    //});

                    generator.SetGeneratorPlannerCreator(mapDescription => new BasicGeneratorPlanner <Layout <Configuration <EnergyData>, BasicEnergyData> >());
                    generator.SetLayoutEvolverCreator((mapDescription, layoutOperations) =>
                    {
                        var evolver =
                            new SimulatedAnnealingEvolver <Layout <Configuration <EnergyData>, BasicEnergyData>, int,
                                                           Configuration <EnergyData> >(layoutOperations);

                        return(evolver);
                    });

                    generator.InjectRandomGenerator(new Random(0));
                });
                setups.AddSetup("New", (generator) =>
                {
                    generator.SetChainDecompositionCreator(mapDescription =>
                                                           new BreadthFirstChainDecomposition <int>(new GraphDecomposer <int>()));
                    generator.SetGeneratorPlannerCreator(mapDescription => new BasicGeneratorPlanner <Layout <Configuration <EnergyData>, BasicEnergyData> >());
                    generator.SetLayoutEvolverCreator((mapDescription, layoutOperations) =>
                    {
                        var evolver =
                            new SimulatedAnnealingEvolver <Layout <Configuration <EnergyData>, BasicEnergyData>, int,
                                                           Configuration <EnergyData> >(layoutOperations);

                        return(evolver);
                    });

                    generator.InjectRandomGenerator(new Random(0));
                });
            }

            Benchmark.WithDefaultFiles((sw, dw) =>
            {
                benchmark.Execute(layoutGenerator, scenario, mapDescriptions, 80, 1, sw, dw);
            });
        }