Ejemplo n.º 1
0
        /// <summary>
        /// /// ProblemLevels.Insane -
        /// from google sheets add-in @ https://chrome.google.com/webstore/detail/sudoku-sheets/eagolleeideiojopioiiaadjkneafmen?hl=en
        /// solved quickly using numberorganims = 100, maxepochs = 19,000
        /// </summary>
        /// <returns></returns>
        public static int[][] insaneProblem()
        {
            int[][] m = SudokuTool.prepMatrix();
            m[0] = new int[] { 0, 0, 0, 5, 0, 0, 0, 0, 0 };
            m[1] = new int[] { 5, 7, 0, 0, 4, 0, 9, 0, 8 };
            m[2] = new int[] { 0, 0, 8, 6, 0, 0, 0, 0, 0 };

            m[3] = new int[] { 0, 5, 7, 0, 0, 0, 0, 3, 0 };
            m[4] = new int[] { 3, 9, 0, 0, 0, 0, 0, 4, 0 };
            m[5] = new int[] { 0, 0, 1, 0, 7, 0, 5, 0, 0 };

            m[6] = new int[] { 0, 0, 0, 0, 2, 0, 0, 0, 6 };
            m[7] = new int[] { 0, 1, 4, 0, 0, 0, 3, 0, 0 };
            m[8] = new int[] { 0, 0, 0, 8, 0, 0, 0, 1, 5 };
            return(m);
        }
Ejemplo n.º 2
0
        /// ProblemLevels.Easy - from google sheets add-in @ https://chrome.google.com/webstore/detail/sudoku-sheets/eagolleeideiojopioiiaadjkneafmen?hl=en
        /// Solve with 100 organisms, 5000 maxEpochs after 00:25
        public static int[][] easyProblem()
        {
            int[][] m = SudokuTool.prepMatrix();
            m[0] = new int[] { 0, 0, 6, 7, 0, 3, 0, 2, 4 };
            m[1] = new int[] { 1, 0, 0, 2, 0, 0, 7, 0, 0 };
            m[2] = new int[] { 5, 0, 0, 0, 0, 1, 0, 0, 0 };

            m[3] = new int[] { 0, 0, 8, 0, 2, 0, 3, 4, 6 };
            m[4] = new int[] { 0, 0, 0, 0, 0, 8, 0, 0, 0 };
            m[5] = new int[] { 0, 0, 3, 0, 0, 0, 0, 0, 8 };

            m[6] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            m[7] = new int[] { 3, 0, 0, 0, 0, 4, 0, 0, 9 };
            m[8] = new int[] { 0, 9, 0, 6, 0, 0, 0, 8, 2 };
            return(m);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///  ProblemLevels.Hard -
        /// from google sheets add-in @ https://chrome.google.com/webstore/detail/sudoku-sheets/eagolleeideiojopioiiaadjkneafmen?hl=en
        /// Solved with 150 organisms, 5000 maxEpochs after 00:12
        /// </summary>
        /// <returns></returns>
        public static int[][] hardProblem()
        {
            int[][] m = SudokuTool.prepMatrix();
            m[0] = new int[] { 5, 0, 0, 0, 0, 0, 0, 0, 3 };
            m[1] = new int[] { 0, 0, 0, 0, 4, 0, 0, 2, 1 };
            m[2] = new int[] { 0, 0, 1, 0, 7, 9, 0, 0, 0 };

            m[3] = new int[] { 6, 0, 5, 4, 0, 0, 0, 0, 0 };
            m[4] = new int[] { 0, 2, 0, 0, 6, 0, 0, 0, 0 };
            m[5] = new int[] { 0, 0, 3, 0, 0, 8, 6, 0, 0 };

            m[6] = new int[] { 0, 0, 0, 3, 0, 0, 0, 0, 8 };
            m[7] = new int[] { 0, 4, 0, 0, 9, 0, 0, 0, 0 };
            m[8] = new int[] { 1, 0, 0, 0, 0, 6, 7, 0, 0 };
            return(m);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///  ProblemLevels.Medium -
        /// from google sheets add-in @ https://chrome.google.com/webstore/detail/sudoku-sheets/eagolleeideiojopioiiaadjkneafmen?hl=en
        /// Solve with 50 organisms, 5000 maxEpochs after 00:41
        /// Solve with 100 organisms, 5000 maxEpochs after 00:25
        /// </summary>
        /// <returns></returns>
        public static int[][] mediumProblem()
        {
            int[][] m = SudokuTool.prepMatrix();
            m[0] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 1 };
            m[1] = new int[] { 0, 8, 6, 3, 0, 0, 0, 0, 2 };
            m[2] = new int[] { 0, 0, 0, 2, 1, 0, 0, 4, 0 };

            m[3] = new int[] { 0, 0, 9, 0, 0, 6, 7, 5, 0 };
            m[4] = new int[] { 4, 0, 0, 0, 5, 0, 1, 0, 0 };
            m[5] = new int[] { 0, 0, 0, 0, 0, 9, 0, 0, 3 };

            m[6] = new int[] { 0, 0, 0, 0, 9, 0, 0, 0, 0 };
            m[7] = new int[] { 8, 0, 5, 0, 0, 2, 0, 0, 0 };
            m[8] = new int[] { 0, 0, 0, 0, 0, 0, 0, 6, 9 };
            return(m);
        }
Ejemplo n.º 5
0
        public void CAN_I_SOLVE_VARIOUS_PROBLEMS(ProblemLevels diff, int organisms, int maxEpochs, int maxExtinctions)
        {
            Problem problem = ProblemGenerator.GetProblem(diff);

            _evo = new EvolutionSolution(_disp);
            Debug.WriteLine($"Attempting solution of {diff.ToString()} with {organisms} organisms \n");
            int[][] proposedSolution = _evo.SolveB4ExtinctCount_Async(problem.Rows, organisms, maxEpochs, maxExtinctions).Result;
            var     testResult       = SudokuTool.Test(proposedSolution);
            var     resultStatus     = testResult.resultStatus;
            var     resultErrors     = testResult.errorCount;
            bool    optimalResult    = testResult.isOptimal;
            var     stats            = _evo.Stats;

            Debug.WriteLine(resultStatus);
            // show some stats
            System.Diagnostics.Debug.WriteLine($@"Final Status: {testResult.resultStatus}
Errors: {testResult.errorCount}
-- Stats -- 
| Epochs (years): {stats.epochsCompleted} 
| Mass Extinctions (resets): {stats.extinctions} 
| Organisms Created: {stats.organismsBorn} | Died of Old Age (>999 years): {stats.diedOfOldAge} 
| Mutation Fails: {stats.mutationFailed} | Evolution Successes: {stats.evolutionWorked} 
| Worst Replaced by Mating Results: {stats.culledCount} | Mating Results Were Best: {stats.bestBabies} 
| Rare Mutation Was Best: {stats.bestMutationsEver} | Random Solution Was Best: {stats.randomWasTheBest} ");

            var mutationvsevolution = ((double)stats.bestMutationsEver / stats.evolutionWorked);

            System.Diagnostics.Debug.WriteLine(
                $@"Evolution that turned out for the best: {mutationvsevolution:P1}");

            System.Diagnostics.Debug.WriteLine(
                $@"Mating that resulted in KHAN: {stats.bestBabies / stats.matingPairs * 100}%");
            Debug.WriteLine(resultErrors == 0 ? "Optimal solution found. \n" : "Did not find optimal solution \n");
            Debug.WriteLineIf(resultErrors != 0, "\nBest solution found: \n");
            _disp.DisplayMatrix(proposedSolution);
            Assert.True(optimalResult);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("For the following parameters, enter a value or press ENTER to take defaults.");
            Console.WriteLine(" --------------- ");

            Console.WriteLine("Please select puzzle difficulty");
            Console.WriteLine($"E (easy), M (medium), H (hard), I (insane), U (impossible)  *default {_thisLevel}*");
            string reqDifficulty = Console.ReadLine();

            if (reqDifficulty == null)
            {
                _thisLevel = ProblemLevels.Medium;
            }
            else
            {
                switch (reqDifficulty.ToUpperInvariant())
                {
                case "E":
                    _thisLevel = ProblemLevels.Easy;
                    break;

                case "M":
                    _thisLevel = ProblemLevels.Medium;
                    break;

                case "H":
                    _thisLevel = ProblemLevels.Hard;
                    break;

                case "I":
                    _thisLevel = ProblemLevels.Insane;
                    break;

                case "U":
                    _thisLevel = ProblemLevels.Impossible;
                    break;

                default:
                    _thisLevel = ProblemLevels.Medium;
                    break;
                }
            }
            Console.WriteLine($" --LEVEL SET TO {_thisLevel}-- ");

            int mo  = 100;
            int mep = 5000;
            int me  = 20;

            Console.WriteLine("  ");
            Console.WriteLine($"Please enter a number of hive organisms to use (50 - 200) *default {mo}*");
            var maxOrganisms = Console.ReadLine();

            if (!int.TryParse(maxOrganisms, out mo) || mo < 50 || mo > 200)
            {
                mo = 100;
                Console.WriteLine($" DEFAULT TO {mo} ");
            }

            Console.WriteLine("  ");
            Console.WriteLine($"Please enter a number Epochs for the organisms to work the problem (2000 - 5000) *default {mep}*");
            var maxEpochs = Console.ReadLine();

            if (!int.TryParse(maxEpochs, out mep) || mep < 2000 || mep > 5000)
            {
                mep = 5000;
                Console.WriteLine($" DEFAULT TO {mep} ");
            }

            Console.WriteLine("  ");
            Console.WriteLine($"Please enter a number Mass Extinctions to allow if all Epochs fail to resolve the puzzle (10 - 30) *default {me}*");
            var maxExtinctions = Console.ReadLine();

            if (!int.TryParse(maxExtinctions, out me) || me < 10 || me > 30)
            {
                me = 20;
                Console.WriteLine($" DEFAULT TO {me} ");
            }

            Console.WriteLine("  ");
            Console.WriteLine($"You have chosen a {_thisLevel} puzzle.");
            Console.WriteLine($"A hive of {mo} Organisms will spawn and work for {mep} Epochs.");
            Console.WriteLine($"If they fail to find an optimal solution, the hive will go extinct and another will spawn. (up to {me} times)");
            Console.WriteLine("   ");

            Console.WriteLine(" ---- Press Enter to Begin ----- ");
            Console.ReadLine();

            long startTime = DateTime.UtcNow.Ticks;

            _disp  = new ConsoleDisplayMatrix();
            _stats = new EvolutionStats();
            _evo   = new EvolutionSolution(_disp, _stats);


            Problem problem = ProblemGenerator.GetProblem(_thisLevel);

            Console.WriteLine("Starting problem resolution for a " + _thisLevel + " puzzle.");

            int[][] result = _evo.SolveB4ExtinctCount_Async(problem.Rows, mo, mep, me).Result;


            var  testResults = SudokuTool.Test(result);
            long stopTime    = DateTime.UtcNow.Ticks;


            _disp.SplashView(testResults, _evo.Stats, problem.Rows, result);


            Console.WriteLine();
            Console.ForegroundColor = (ConsoleColor.Cyan);
            Console.WriteLine("This run took: " + TimeSpan.FromTicks(stopTime - startTime).TotalSeconds.ToString("#.##") + " seconds");

            Console.ReadLine();
        }