Esempio n. 1
0
        static void Main(string[] args)
        {
            if (args.Length == 0 || args[0] == null)
            {
                PrintUsage();
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadLine();
                return;
            }

            _solverType = GetCharInput(@"Please select search mode. Default: Genetic Algorithm
1. Depth First Search
2. Genetic Algorithm", new Dictionary <int, SolverType> {
                { 1, SolverType.DepthFirstSearch },
                { 2, SolverType.GeneticAlgorithm }
            }, SolverType.GeneticAlgorithm);

            var fileLocation = args[0];

            _solutionFilename = "Solution" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".txt";
            _movesFilename    = "Solution" + DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-moves.txt";

            if (File.Exists(fileLocation))
            {
                var game = FlowerzGame.CreateGameFromFile(fileLocation);

                if (_solverType == SolverType.GeneticAlgorithm)
                {
                    var solver = SolverFactory.GetGeneticAlgorithmSolver();

                    solver.GenerationCreated += gasolver_GenerationCreated;
                    solver.SolverCreated     += gasolver_SolverCreated;

                    GeneticAlgorithmOptions.PopulationSize = GetNumberInput($"What should the size of the population be? Default: {DefaultPopulationSize}", DefaultPopulationSize);
                    GeneticAlgorithmOptions.NumberOfGenerationsToCreate = GetNumberInput($"How many generations should be created? Default: {DefaultNumberOfGenerations}", DefaultNumberOfGenerations);
                    GeneticAlgorithmOptions.CrossOverChance             = GetNumberInput($"How big should the chance be to select the crossover operation? Default: {DefaultCrossoverChance}%", DefaultCrossoverChance);
                    GeneticAlgorithmOptions.MutationChance = GetNumberInput($"How big should the chance be that a mutation occurs? Default: {DefaultMutationChance}%", DefaultMutationChance);

                    var useGaImporttxt = GetCharInput(@"Use GA_Import.txt? Default: True
1. Yes
2. No", new Dictionary <int, bool> {
                        { 1, true },
                        { 2, false }
                    }, true);

                    if (useGaImporttxt)
                    {
                        if (File.Exists(GaImportFileName))
                        {
                            Console.WriteLine("Importing existing solutions . . .");
                            var numberOfImportedSolutions = 0;
                            using (var sr = new StreamReader(new FileStream(GaImportFileName, FileMode.Open))) {
                                string s;
                                while ((s = sr.ReadLine()) != null)
                                {
                                    solver.ImportMovelist(s);
                                    numberOfImportedSolutions++;
                                }
                            }
                            Console.WriteLine(numberOfImportedSolutions + " solutions imported");
                        }
                        else
                        {
                            Console.WriteLine("Unable to import solutions. Could not find file '" + GaImportFileName + "'.");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Not importing solutions");
                    }

                    Console.WriteLine(GeneticAlgorithmOptions);
                    solver.GeneticAlgorithmOptions = GeneticAlgorithmOptions;
                    Solve(solver, game);
                }
                else if (_solverType == SolverType.DepthFirstSearch)
                {
                    DepthFirstSearchOptions.SearchRandomly = GetCharInput(@"Use random search? Default: no
1. Yes
2. No", new Dictionary <int, bool> {
                        { 1, true },
                        { 2, false }
                    }, false);
                    var solver = SolverFactory.GetDfsSolver();
                    solver.SearchOptions = DepthFirstSearchOptions;
                    Solve(solver, game);
                }
                else
                {
                    Console.WriteLine("No mode specified. Exiting.");
                    Console.WriteLine("Press any key to continue . . . ");
                    Console.ReadLine();
                }
            }
            else
            {
                Console.WriteLine("No file found at '{0}' ", fileLocation);
                Console.WriteLine("Press any key to continue . . . ");
                Console.ReadLine();
            }
        }