Esempio 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();
        }
Esempio 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);
            }
        }
Esempio n. 3
0
        public PuzzleState(int ilosc, int metoda)
        {
            table = new int[GRID_SIZE, GRID_SIZE];
            for (int i = 0; i < GRID_SIZE; i++)
            {
                for (int j = 0; j < GRID_SIZE; j++)
                {
                    table[i, j] = i * GRID_SIZE + j;
                }
            }

            mieszaj(ilosc);

            if (metoda == 1)
            {
                Man();
            }


            rodzic  = null;
            this.id = generujID();
            this.Print(metoda);

            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Szukanie rozwiazania");
        }
Esempio n. 4
0
        public PuzzleState(int ilosc, bool ktory)
        {
            metoda = ktory;

            table = new int[GRID_SIZE, GRID_SIZE];
            for (int i = 0; i < GRID_SIZE; i++)
            {
                for (int j = 0; j < GRID_SIZE; j++)
                {
                    table[i, j] = i * GRID_SIZE + j;
                }
            }

            mieszaj(ilosc);

            if (!metoda)
            {
                ComputeHeuristicGrade();
            }
            else
            {
                Manhattan();
            }

            rodzic  = null;
            this.id = generujID();
        }
Esempio n. 5
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);
        }
Esempio n. 6
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();
        }
Esempio n. 7
0
 //Konstruktor kopiujący (potrzebny do testowania takich samych plansz dla dwóch różnych heurystyk)
 public PuzzleState(PuzzleState startState, bool useMisplaced) : base()
 {
     table = new int[GRID_SIZE];
     Array.Copy(startState.table, this.table, this.table.Length);
     this.id = String.Copy(startState.id);
     MisplacedTilesHeuristics = useMisplaced;
     this.EmptyIndex          = startState.EmptyIndex;
     this.h = ComputeHeuristicGrade();
 }
Esempio n. 8
0
        protected override void buildChildren(IState parent)
        {
            PuzzleState state  = (PuzzleState)parent;
            List <int>  fields = state.Possibilities(); //Wyznacz liste możliwych ruchów dla danego stanu (max 4, min 2)

            foreach (int i in fields)                   //Dla każdego możliwego ruchu, stwórz stan potomny
            {
                parent.Children.Add(new PuzzleState(state, i));
            }
        }
        protected override void buildChildren(IState aParent)
        {
            PuzzleState state  = (PuzzleState)aParent; // rzutujemy podany stan na PuzzleState
            int         row    = 0;
            int         column = 0;

            for (int i = 0; i < 3; ++i)
            {
                for (int j = 0; j < 3; ++j)
                {
                    if (state.Board[i, j] == 0) // cala ta petla w petli ma na celu znalezienie gdzie jest puste pole
                    {
                        row    = i;
                        column = j;
                        goto outside;
                    }
                }
            }
outside:
            if (column < 2)                                                  // tworzymy potencjalne cele potomne; jesli column < 2 to mozliwy jest ruch w lewo
            {
                state.Board[row, column]     = state.Board[row, column + 1]; // chwilowo zmieniamy tablice na taka, jaka powinien miec stan potomny
                state.Board[row, column + 1] = 0;
                new PuzzleState(state);                                      // podanie new PuzzleState i state jako argument powoduje ze ten nowy stan podpina sie jako dziecko
                state.Board[row, column + 1] = state.Board[row, column];     // przywracamy domyslny stan tablicy
                state.Board[row, column]     = 0;
            }
            // ruchy w pozostale kierunki analogicznie
            if (column > 0)
            {
                state.Board[row, column]     = state.Board[row, column - 1];
                state.Board[row, column - 1] = 0;
                new PuzzleState(state);
                state.Board[row, column - 1] = state.Board[row, column];
                state.Board[row, column]     = 0;
            }
            if (row < 2)
            {
                state.Board[row, column]     = state.Board[row + 1, column];
                state.Board[row + 1, column] = 0;
                new PuzzleState(state);
                state.Board[row + 1, column] = state.Board[row, column];
                state.Board[row, column]     = 0;
            }
            if (row > 0)
            {
                state.Board[row, column]     = state.Board[row - 1, column];
                state.Board[row - 1, column] = 0;
                new PuzzleState(state);
                state.Board[row - 1, column] = state.Board[row, column];
                state.Board[row, column]     = 0;
            }
        }
Esempio n. 10
0
        public PuzzleState(PuzzleState parent, int Kierunek)
            : base(parent)
        {
            this.metoda = parent.metoda;                             //ta sama metoda co rodzic
            this.table  = new int[GRID_SIZE, GRID_SIZE];             // ten sam rozmiar
            Array.Copy(parent.table, this.table, this.table.Length); // skopiowanie stanu

            this.sprawdz_0();                                        //sprawdz polozenie 0
            this.przesun(Kierunek);

            this.id = generujID();
            this.g  = parent.g + 1;
        }
Esempio n. 11
0
        protected override void buildChildren(IState parent)
        {
            PuzzleState state = (PuzzleState)parent;

            state.checkDirections();

            for (int i = 0; i < 4; i++)
            {
                if (state.Directions_LTRB[i])
                {
                    PuzzleState child = new PuzzleState(state, i);
                    parent.Children.Add(child);
                }
            }
        }
 public PuzzleState(PuzzleState aState)
 {
     this.board = new int[3, 3];
     for (int i = 0; i < 3; ++i)
     {
         for (int j = 0; j < 3; ++j)
         {
             this.board [i, j] = aState.board [i, j];                     // kopiujemy tablice stanu
         }
     }
     this.Parent = aState;
     aState.Children.Add(this); // dodajemy ten stan jako dziecko stanu przekazanego w argumencie
     this.h = ComputeHeuristicGrade();
     this.g = parent.G + 1;
 }
Esempio n. 13
0
        public PuzzleState(PuzzleState parent, int Kierunek)
            : base(parent)
        {
            //Console.WriteLine("konstruktor glowny");
            this.table = new int[GRID_SIZE, GRID_SIZE];
            // Skopiowanie stanu sudoku do nowej tabeli
            Array.Copy(parent.table, this.table, this.table.Length);
            // Ustawienie nowej wartosci w wybranym polu sudoku

            this.sprawdz_0();
            this.przesun(Kierunek);

            //            Print();

            this.id = generujID();
            this.g  = parent.g + 1;
        }
Esempio n. 14
0
        public void sortuj(int ile)
        {
            PuzzleState tmp = new PuzzleState();

            for (int i = 0; i < ile; i++)
            {
                for (int j = 1; j < ile; j++)
                {
                    if (heury[j - 1].F > heury[j].F)
                    {
                        tmp          = heury[j - 1];
                        heury[j - 1] = heury[j];
                        heury[j]     = tmp;
                    }
                }
            }
        }
Esempio n. 15
0
        public PuzzleState(PuzzleState parent, int dir) : base(parent)
        {
            counter++;

            GRIDSIZE = parent.GRIDSIZE;
            Table    = new int[GRIDSIZE, GRIDSIZE];
            Array.Copy(parent.Table, this.Table, this.Table.Length);

            emptyFieldPosition = new int[2];
            Array.Copy(parent.emptyFieldPosition, emptyFieldPosition, emptyFieldPosition.Length);

            move(dir);

            this.id = getTableString();

            this.h = ComputeHeuristicGrade();

            this.g = parent.g + 1;
        }
Esempio n. 16
0
        public PuzzleState(PuzzleState parent, int newValue, int x, int y, int xNew, int yNew) :
            base(parent)
        {
            this.table = new int [GRID_SIZE, GRID_SIZE];
            // Skopiowanie stanu sudoku do nowej tabeli
            Array.Copy(parent.table, this.table, this.table.Length);
            // Ustawienie nowej wartosci w wybranym polu sudoku
            this.table [x, y]      = 0;
            this.table[xNew, yNew] = newValue;
            // Utworzenie nowego id odpowiadajacemu aktualnemu stanowi planszy
            StringBuilder builder = new StringBuilder(parent.id);

            builder [x * GRID_SIZE + y] = ( char )(newValue + 48);
            this.id = builder.ToString();

            //Console.WriteLine("lol2");
            this.h = ComputeHeuristicGrade();
            this.g = parent.g + 1;
        }
Esempio n. 17
0
        protected override void buildChildren(IState parent)
        {
            PuzzleState state = (PuzzleState)parent;

            foreach (string id in state.id_list)
            {
                foreach (Heuristic_ways obj in state.Heuristic_vetor)
                {
                    if (obj.ID == id)
                    {
                        obj.h = PuzzleState.infinity;
                    }
                }
            }


            bool isZero = false;

            for (int i = 0; i <= ((PuzzleState.PuzzleSize * PuzzleState.PuzzleSize) + state.G); i++)
            {
                foreach (Heuristic_ways obj in state.Heuristic_vetor)
                {
                    if (obj.F == i)
                    {
                        PuzzleState child = new PuzzleState(state, obj);
                        if (child.H == 0)
                        {
                            //isZero = true;
                        }
                        parent.Children.Add(child);
                        //state.Print(state.Table, child.Table);
                    }
                    if (isZero)
                    {
                        break;
                    }
                }
                if (isZero)
                {
                    break;
                }
            }
        }
Esempio n. 18
0
        public PuzzleState(PuzzleState parent, Heuristic_ways tmp) : base(parent)
        {
            this.table = new int[PuzzleSize, PuzzleSize];

            Array.Copy(tmp.Table, this.table, this.table.Length);
            // ciało konstruktora

            this.id = tmp.ID;
            id_list.Add(this.id);

            this.x = tmp.x;
            this.y = tmp.y;

            if (tmp.h != 0)
            {
                Heuristic_vector(x, y);
            }
            this.h = tmp.h;

            //W stanie w ktorym jestesmy droga jest o jeden większa niż w rodzicu
            this.g = parent.g + 1;
        }
Esempio n. 19
0
        //Konstruktor budujący stany potomne
        public PuzzleState(PuzzleState parent, int newValue) : base(parent)
        {
            //Skopiuj dane stanu rodzicielskiego
            this.table = new int[GRID_SIZE];
            Array.Copy(parent.table, this.table, this.table.Length);
            EmptyIndex = parent.EmptyIndex;

            SwapTiles(newValue); //Wykonaj ruch ("stwórz stan potomny")

            //Zaktualizuj ID stanu
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < GRID_SIZE; ++i)
            {
                builder.Append((char)(this.table[i] + 48));
            }
            this.id = builder.ToString();
            this.MisplacedTilesHeuristics = parent.MisplacedTilesHeuristics;
            //Zaktualizuj wartość heurystyczną i głębokość przeszukiwania
            this.h = ComputeHeuristicGrade();
            this.g = parent.g + 1;
        }
Esempio n. 20
0
        protected override void buildChildren(IState parent)
        {
            PuzzleState state = (PuzzleState)parent;

            // poszukiwanie wolnego pola
            state.sprawdz_0();
            int ile = 0;

            state.sprawdz_moz();


            for (int i = 0; i < 4; i++)
            {
                if (state.mozliwosci[i] != false)
                {
                    ile++;
                }
            }

            heury = new PuzzleState[ile];

            int n = 0;

            for (int i = 0; i < 4; i++)
            {
                if (state.mozliwosci[i] != false)
                {
                    PuzzleState child = new PuzzleState(state, i);
                    heury[n] = child;
                    n++;
                }
            }

            for (int i = 0; i < ile; i++)
            {
                parent.Children.Add(heury[i]);
            }
        }
Esempio n. 21
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();
            }
        }
Esempio n. 22
0
        protected override bool isSolution(IState state)
        {
            PuzzleState s = (PuzzleState)state;

            return(s.Rozwiazanie());
        }
Esempio n. 23
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);
        }
Esempio n. 24
0
        static void Main(string[] args)
        {
            Console.BufferHeight = 1000;

            DialogResult dialogResult_sudoku = MessageBox.Show("Chesz zobaczyć sudoku? ( ͡° ͜ʖ ͡°)", "", MessageBoxButtons.YesNo);

            if (dialogResult_sudoku == DialogResult.Yes)
            {
                string sudokuPattern = "083279465409583710270461893342058176597106284618720359954812037701645908826397540"; // sudoku w postaci stringa np .:" 010330218... "
                #region test
                //040000065469583712275461893342958176597136284618724359954812637731645928826397541
                //183279465469583712275461893342958176597136284618724359954812637731645928826397541

                /*"040 000 065
                 * 469 583 712
                 * 275 461 893
                 *
                 * 342 958 176
                 * 597 136 284
                 * 618 724 359
                 *
                 * 954 812 637
                 * 731 645 928
                 * 826 397 541"
                 */
                //000000465469583712275461893342958176597136284618724359954812637731645928826397541
                //000079065000003002005060093340050106000000000608020059950010600700600000820390000
                #endregion //test

                SudokuState.start(sudokuPattern);
                Console.ReadLine();
            }

            DialogResult dialogResult_puzzle = MessageBox.Show("A puzzle? ( ͡° ͜ʖ ͡°)", "", MessageBoxButtons.YesNo);
            if (dialogResult_puzzle == DialogResult.Yes)
            {
                int puzzlesize = 3;
                PuzzleState.PuzzleSize = puzzlesize;
                Laboratory1_m.PuzzleState.PuzzleSize = puzzlesize;
                gen();

                Stopwatch stopWatch_tails = new Stopwatch();
                stopWatch_tails.Start();
                PuzzleState.start(puzzlesize);
                stopWatch_tails.Stop();

                Stopwatch stopWatch_man = new Stopwatch();
                stopWatch_man.Start();
                Laboratory1_m.PuzzleState.start(puzzlesize);
                stopWatch_man.Stop();

                TimeSpan t_search   = stopWatch_tails.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("Calkowity czas tails: " + SearchTime);

                t_search   = stopWatch_man.Elapsed;
                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("Calkowity czas man: " + SearchTime);

                Console.ReadLine();
            }
        }
Esempio n. 25
0
 public PuzzleSearch(PuzzleState state) : base(state, true, true)
 {
 }
 public PuzzleSearch(PuzzleState aState) : base(aState, true, true)
 {
 }
Esempio n. 27
0
        protected override void buildChildren(IState parent)
        {
            PuzzleState state = ( PuzzleState )parent;

            // poszukiwanie wolnego pola
            //Console.WriteLine("trzy");
            for (int i = 0; i < PuzzleState.GRID_SIZE; ++i)
            {
                for (int j = 0; j < PuzzleState.GRID_SIZE; ++j)
                {
                    if (state.Table[i, j] == 0)
                    {
                        // wstawianie kolejnych potomkow w wolne pole

                        /*for (int k = 1; k < PuzzleState.GRID_SIZE + 1; ++k)
                         * {
                         *  PuzzleState child = new PuzzleState(state, k, i, j);
                         *  parent.Children.Add(child);
                         * }*/
                        if (i == 0 && j == 0)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 1], 0, 1, 0, 0);
                            parent.Children.Add(child);

                            child = new PuzzleState(state, state.Table[1, 0], 1, 0, 0, 0);
                            parent.Children.Add(child);
                        }
                        if (i == 0 && j == 1)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 0], 0, 0, 0, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[0, 2], 0, 2, 0, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 1], 1, 1, 0, 1);
                            parent.Children.Add(child);
                        }
                        if (i == 0 && j == 2)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 1], 0, 1, 0, 2);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 2], 1, 2, 0, 2);
                            parent.Children.Add(child);
                        }
                        if (i == 1 && j == 0)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 0], 0, 0, 1, 0);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 1], 1, 1, 1, 0);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[2, 0], 2, 0, 1, 0);
                            parent.Children.Add(child);
                        }
                        if (i == 1 && j == 1)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 1], 0, 1, 1, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 0], 1, 0, 1, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 2], 1, 2, 1, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[2, 1], 2, 1, 1, 1);
                            parent.Children.Add(child);
                        }
                        if (i == 1 && j == 2)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[0, 2], 0, 2, 1, 2);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 1], 1, 1, 1, 2);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[2, 2], 2, 2, 1, 2);
                            parent.Children.Add(child);
                        }
                        if (i == 2 && j == 0)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[1, 0], 1, 0, 2, 0);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[2, 1], 2, 1, 2, 0);
                            parent.Children.Add(child);
                        }
                        if (i == 2 && j == 1)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[2, 0], 2, 0, 2, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 1], 1, 1, 2, 1);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[2, 2], 2, 2, 2, 1);
                            parent.Children.Add(child);
                        }
                        if (i == 2 && j == 2)
                        {
                            PuzzleState child = new PuzzleState(state, state.Table[2, 1], 2, 1, 2, 2);
                            parent.Children.Add(child);
                            child = new PuzzleState(state, state.Table[1, 2], 1, 2, 2, 2);
                            parent.Children.Add(child);
                        }
                        break;
                    }
                }
            }
        }
Esempio n. 28
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();
            }
        }