Exemplo n.º 1
0
        static void Main(string [] args)
        {
            Stopwatch sw = Stopwatch.StartNew();

            //string sudokuPattern = "123456789123456789123456789123456789123456789123456789123456789123456789123456789"; //sudoku w postaci stringa np.: " 010330218... "
            //string sudokuPattern = "294167358315489627678253491456312879983574216721698534562941783839726145147835962"; //sudoku w postaci stringa np.: " 010330218... "
            string sudokuPattern = "102345678"; //sudoku w postaci stringa np.: " 010330218... "
            //string sudokuPattern = "004100308010000620008200400000302809000070000701608000562001703030000040100005000";

            PuzzleState  startState = new PuzzleState(sudokuPattern);
            PuzzleSearch searcher   = new PuzzleSearch(startState);

            searcher.DoSearch();

            IState state = searcher.Solutions [0];

            List <PuzzleState> solutionPath = new List <PuzzleState>();

            while (state != null)
            {
                solutionPath.Add(( PuzzleState )state);
                state = state.Parent;
            }
            solutionPath.Reverse();

            foreach (PuzzleState s in solutionPath)
            {
                s.Print();
            }
            sw.Stop();
            Console.WriteLine("Czas: " + sw.ElapsedMilliseconds + "ms");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            PuzzleState  theState    = new PuzzleState("1234567890");             // tworzymy puzzle w stanie rozwiazanym
            PuzzleSearch theSearcher = new PuzzleSearch(theState);                // tworzymy szukacz

            theState.Shuffle();                                                   // mieszamy puzzle
            //theState.Write();
            theSearcher.DoSearch();                                               // wykonujemy szukanie drogi do rozwiazania
            PuzzleState        theResult = (PuzzleState)theSearcher.Solutions[0]; // przypisujemy stan rozwiazany od szukacza
            List <PuzzleState> states    = new List <PuzzleState>();              // tworzymy liste kolejnych stanow

            while (theResult != null)
            {
                states.Add(theResult); // przypisujemy kolejne stany od zmieszania az do rozwiazania
                theResult = (PuzzleState)theResult.Parent;
            }

            int k = states.Count();

            for (int i = states.Count() - 1; i >= 0; --i)
            {
                Console.Clear();
                Console.WriteLine("Manhattan stany to:" + k);
                Console.WriteLine("Pozostalo " + i + " stanow do rozwiazania");  // wypisujemy kolejne stany na konsoli
                states[i].Write();
                Thread.Sleep(200);
            }
        }
Exemplo n.º 3
0
        public static void start(int size)
        {
            Stopwatch stopWatch_search = new Stopwatch();
            Stopwatch stopWatch_print  = new Stopwatch();

            puzzleSize = size;

            PuzzleState  startState = new PuzzleState();
            PuzzleSearch searcher   = new PuzzleSearch(startState);


            stopWatch_search.Start();

            searcher.DoSearch();
            stopWatch_search.Stop();

            TimeSpan t_search = stopWatch_search.Elapsed;

            IState state = searcher.Solutions[0];

            List <PuzzleState> solutionPath = new List <PuzzleState>();

            while (state != null)
            {
                solutionPath.Add((PuzzleState)state);
                state = state.Parent;
            }
            solutionPath.Reverse();

            int[,] table_tmp1 = new int[puzzleSize, puzzleSize];
            int[,] table_tmp2 = new int[puzzleSize, puzzleSize];

            table_tmp1 = solutionPath[0].Table;


            stopWatch_print.Start();

            foreach (PuzzleState s in solutionPath)
            {
                table_tmp2 = table_tmp1;
                table_tmp1 = s.Table;

                s.Print(table_tmp2, table_tmp1);
            }
            stopWatch_print.Stop();
            TimeSpan t_print = stopWatch_print.Elapsed;

            string SearchTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                              t_search.Hours, t_search.Minutes, t_search.Seconds,
                                              t_search.Milliseconds / 10);

            Console.WriteLine("Czas przeszukiwania " + SearchTime);

            string PrintTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                             t_print.Hours, t_print.Minutes, t_print.Seconds,
                                             t_print.Milliseconds / 10);

            Console.WriteLine("Czas wyswietlania " + PrintTime);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int metoda        = 1;
            int ilosc_mieszan = 100;

            if (metoda == 0)
            {
                Console.WriteLine("Metoda Misplaced Tiles");
            }
            else
            {
                Console.WriteLine("Metoda Manhattan");
            }

            PuzzleState  startState = new PuzzleState(ilosc_mieszan, metoda);
            PuzzleSearch searcher   = new PuzzleSearch(startState);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            searcher.DoSearch();

            IState state = searcher.Solutions [0];

            List <PuzzleState> solutionPath = new List <PuzzleState>();

            solutionPath.Sort();

            while (state != null)
            {
                solutionPath.Add(( PuzzleState )state);
                state = state.Parent;
            }

            stopwatch.Stop();

            Console.WriteLine();
            Console.WriteLine("Czas obliczen: {0}", stopwatch.Elapsed);


            solutionPath.Reverse();
            Console.WriteLine();
            Console.Write("Pokaz rozwiazanie <wcisnij klawisz>");
            Console.ReadKey();

            foreach (PuzzleState s in solutionPath)
            {
                Console.Clear();
                s.Print(metoda);

                System.Threading.Thread.Sleep(1000);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Gotowe!");
            Console.ReadKey();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            bool puzzle = true;
            bool sudoku = false;

            if (sudoku)
            {
                List <string> sudokuPatterns = new List <string>();
                sudokuPatterns.Add("800030000930007000071520900005010620000050000046080300009076850060100032000040006");
                sudokuPatterns.Add("000000600600700001401005700005900000072140000000000080326000010000006842040002930");
                sudokuPatterns.Add("457682193600000007100000004200000006584716239300000008800000002700000005926835471");
                //sudokuPatterns.Add("000012034000056017000000000000000000480000051270000048000000000350061000760035000"); //not enough memory
                //sudokuPatterns.Add("000700800000040030000009001600500000010030040005001007500200600030080090007000002"); //not enough memory
                //sudokuPatterns.Add("100040002050000090008000300000509000700080003000706000007000500090000040600020001"); //not enough memory

                foreach (string pattern in sudokuPatterns)
                {
                    //Sprawdzenie ilości pustych pól
                    int emptyFieldsCount = 0;
                    for (int i = 0; i < pattern.Length; ++i)
                    {
                        if (pattern[i] == '0')
                        {
                            emptyFieldsCount++;
                        }
                    }

                    SudokuState  startState = new SudokuState(pattern); //Stan startowy
                    SudokuSearch searcher   = new SudokuSearch(startState);

                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    searcher.DoSearch();
                    watch.Stop();

                    IState             state        = searcher.Solutions[0];
                    List <SudokuState> solutionPath = new List <SudokuState>();

                    while (state != null)
                    {
                        solutionPath.Add((SudokuState)state);
                        state = state.Parent;
                    }
                    solutionPath.Reverse();

                    foreach (SudokuState s in solutionPath)
                    {
                        s.Print();
                        Console.WriteLine();
                    }
                    Console.WriteLine("Empty fields count: {0}", emptyFieldsCount);
                    Console.WriteLine("States count in Open: {0}", searcher.Open.Count);
                    Console.WriteLine("States count in Closed: {0}", searcher.Closed.Count);
                    Console.WriteLine("Time: {0}s", watch.ElapsedMilliseconds / 1000.0);

                    Console.ReadKey();
                }
            }

            if (puzzle)
            {
                const int TESTS_NUMBER = 100;
                Console.WriteLine("Creating {0} puzzles.", TESTS_NUMBER);

                PuzzleState[] startStatesMisplaced = new PuzzleState[TESTS_NUMBER];
                PuzzleState[] startStatesManhattan = new PuzzleState[TESTS_NUMBER];
                Random        random = new Random();

                for (int i = 0; i < TESTS_NUMBER; ++i) //Tworzenie 100 stanów startowych puzzli
                {
                    startStatesMisplaced[i] = new PuzzleState(random, true);
                    startStatesManhattan[i] = new PuzzleState(startStatesMisplaced[i], false);
                }

                double misplacedTilesTimeSum = 0.0;
                double manhattanTimeSum      = 0.0;

                long misplacedTilesOpenSum   = 0;
                long misplacedTilesClosedSum = 0;
                long manhattanOpenSum        = 0;
                long manhattanClosedSum      = 0;

                double misplacedTilesPathLength = 0.0;
                double manhattanPathLength      = 0.0;

                Console.WriteLine("Attempting to solve the puzzles using Misplaced Tiles heuristic:");
                for (int i = 0; i < TESTS_NUMBER; ++i)
                {
                    Console.WriteLine("Puzzle no. " + i);
                    PuzzleSearch searcher = new PuzzleSearch(startStatesMisplaced[i]);
                    searcher.DoSearch();
                    IState             state        = searcher.Solutions[0];
                    List <PuzzleState> solutionPath = new List <PuzzleState>();

                    while (state != null)
                    {
                        solutionPath.Add((PuzzleState)state);
                        state = state.Parent;
                    }
                    solutionPath.Reverse();

                    foreach (PuzzleState s in solutionPath)
                    {
                        s.Print();
                        Console.WriteLine();
                    }

                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    searcher.DoSearch();
                    watch.Stop();

                    misplacedTilesTimeSum    += watch.ElapsedMilliseconds;
                    misplacedTilesOpenSum    += searcher.Open.Count;
                    misplacedTilesClosedSum  += searcher.Closed.Count;
                    misplacedTilesPathLength += searcher.Solutions[0].G;

                    Console.WriteLine("\tNumber of states in Open set: " + searcher.Open.Count);
                    Console.WriteLine("\tNumber of states in Closed set: " + searcher.Closed.Count);
                    Console.WriteLine("\tSolution path length: " + searcher.Solutions[0].G);
                    Console.WriteLine("\tElapsed time: {0}s.", watch.ElapsedMilliseconds / 1000.0);
                }

                Console.WriteLine();
                Console.WriteLine("Attempting to solve the puzzles using Manhattan heuristic: ");
                for (int i = 0; i < TESTS_NUMBER; ++i)
                {
                    Console.WriteLine("Puzzle no. {0}.", i + 1);
                    PuzzleSearch searcher = new PuzzleSearch(startStatesManhattan[i]);

                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    searcher.DoSearch();
                    watch.Stop();

                    manhattanTimeSum    += watch.ElapsedMilliseconds;
                    manhattanOpenSum    += searcher.Open.Count;
                    manhattanClosedSum  += searcher.Closed.Count;
                    manhattanPathLength += searcher.Solutions[0].G;

                    Console.WriteLine("\tNumber of states in Open set: " + searcher.Open.Count);
                    Console.WriteLine("\tNumber of states in Closed set: " + searcher.Closed.Count);
                    Console.WriteLine("\tSolution path length: " + searcher.Solutions[0].G);
                    Console.WriteLine("\tElapsed time: {0}s.", watch.ElapsedMilliseconds / 1000.0);
                }
                Console.WriteLine();

                Console.WriteLine("Misplaced Tiles heuristic: ");
                Console.WriteLine("\tAverage time: {0}s.", misplacedTilesTimeSum / 1000.0 / TESTS_NUMBER);
                Console.WriteLine("\tAverage Open states number: " + misplacedTilesOpenSum / TESTS_NUMBER);
                Console.WriteLine("\tAverage Closed states number: " + misplacedTilesClosedSum / TESTS_NUMBER);
                Console.WriteLine("\tAverage solution path length: " + misplacedTilesPathLength / TESTS_NUMBER);
                Console.WriteLine();

                Console.WriteLine("Manhattan heuristic: ");
                Console.WriteLine("\tAverage time: {0}s.", manhattanTimeSum / 1000.0 / TESTS_NUMBER);
                Console.WriteLine("\tAverage Open states number: " + manhattanOpenSum / TESTS_NUMBER);
                Console.WriteLine("\tAverage Closed states number: " + manhattanClosedSum / TESTS_NUMBER);
                Console.WriteLine("\tAverage solution path length: " + manhattanPathLength / TESTS_NUMBER);
                Console.ReadKey();
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            int  ile    = 0;
            bool sposob = false;     // false - Manhattan; true - Misplaced Tiles
            int  sciezka;
            long czas;


            Console.WriteLine("PUZZLE PRZESUWNE  -   Porownanie metod Manhattan i Misplaced Tiles");
            Console.WriteLine();

            Console.Write("Ilosc puzzli: ");
            string linia        = Console.ReadLine();
            int    ilosc_puzzli = int.Parse(linia);

            Console.Write("Ilosc ruchow mieszajacych: ");
            linia = Console.ReadLine();
            int ilosc_ruchow = int.Parse(linia);

            for (int metoda = 0; metoda < 2; metoda++)
            {
                if (metoda == 0)
                {
                    Console.WriteLine("Metoda Manhattan");
                    Console.WriteLine();
                }

                else
                {
                    sposob = true;
                    Console.WriteLine("Metoda Misplaced Tiles");
                    Console.WriteLine();
                }

                sciezka = 0;
                czas    = 0;

                for (int i = 0; i < ilosc_puzzli; i++)
                {
                    PuzzleState  startState = new PuzzleState(ilosc_ruchow, sposob);
                    PuzzleSearch searcher   = new PuzzleSearch(startState);

                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();

                    searcher.DoSearch();

                    IState state = searcher.Solutions[0];

                    List <PuzzleState> solutionPath = new List <PuzzleState>();
                    solutionPath.Sort();

                    while (state != null)
                    {
                        solutionPath.Add((PuzzleState)state);
                        state = state.Parent;
                    }

                    stopwatch.Stop();
                    czas += stopwatch.ElapsedMilliseconds;
                    solutionPath.Reverse();

                    Console.Write(i + 1);
                    Console.Write(". Czas: ");
                    Console.Write(stopwatch.ElapsedMilliseconds);
                    Console.Write("ms");
                    Console.Write(" || Sciezka: ");

                    ile = 0;
                    foreach (PuzzleState s in solutionPath)
                    {
                        ile++;
                    }

                    sciezka += ile;
                    Console.WriteLine(ile);
                }

                float czas_sr    = (float)czas / 100000;
                float sciezka_sr = (float)sciezka / ilosc_puzzli;

                Console.WriteLine();
                Console.Write("Czas sredni: ");
                Console.Write(czas_sr);
                Console.WriteLine("s");

                Console.Write("Srednia sciezka: ");
                Console.WriteLine(sciezka_sr);
                Console.WriteLine();

                if (metoda == 0)
                {
                    Console.WriteLine("Wcisnij przycisk, aby zobaczyc 2 metode");
                }

                Console.ReadKey();
            }
        }
Exemplo n.º 7
0
        static void puzzle(bool useManhattan)
        {
            int N   = getInt("NxN N=", 3);
            int mix = getInt("How many mix: ", 1000);
            int numOfRandomBoards = getInt("How many boards: ", 100);



            int openAverage    = 0;
            int closeAverage   = 0;
            int objectsAverage = 0;


            PuzzleState  startState;
            PuzzleSearch searcher;

            var stopwatch      = new Stopwatch();
            var elapsedTime    = new Stopwatch();
            var allElapsedTime = new Stopwatch();

            for (int i = 0; i < numOfRandomBoards; i++)
            {
                stopwatch.Start();
                startState = new PuzzleState(N, useManhattan, mix);
                searcher   = new PuzzleSearch(startState);
                allElapsedTime.Start();
                searcher.DoSearch();
                allElapsedTime.Stop();
                //Console.Write(searcher.Open.Count);
                //Console.ReadKey();
                IState             state        = searcher.Solutions[0];
                List <PuzzleState> solutionPath = new List <PuzzleState>();

                stopwatch.Stop();

                int openCounter = 0;
                while (state != null)
                {
                    openCounter += state.Children.Count;

                    solutionPath.Add((PuzzleState)state);
                    state = state.Parent;
                }
                solutionPath.Reverse();

                openAverage  += searcher.Open.Count;
                closeAverage += searcher.Closed.Count;

                objectsAverage     += PuzzleState.counter;
                PuzzleState.counter = 0;

                if (N < 5 && i == 0)
                {
                    foreach (PuzzleState s in solutionPath)
                    {
                        s.Print();
                    }
                    Console.Write("\nComputing");
                }
                if (i % 10 == 0)
                {
                    Console.Write(".");
                }

                solutionPath = null;
            }
            openAverage    /= numOfRandomBoards;
            closeAverage   /= numOfRandomBoards;
            objectsAverage /= numOfRandomBoards;



            string[] methods = { "Missplaced tiles", "Manhattan" };

            StringBuilder datas = new StringBuilder();

            datas.Append(String.Format("\n{0}\n", useManhattan == false ? methods[0] : methods[1]));
            datas.Append(String.Format("Openned: {0}, Closed: {1} \nCreated {2} boards objects\n", openAverage, closeAverage, objectsAverage));
            datas.Append(String.Format("Average time: {0}\n", allElapsedTime.ElapsedMilliseconds / numOfRandomBoards));

            Console.Write(datas);
        }