Esempio n. 1
0
        public ClassPuzzle SolveIt()
        {
            ClkSt           = DateTime.Now;
            P.InitNode.Cost = 0;
            S.Push(P.InitNode);
            MaxS++;
            Hist.Add(_KeyBuild(P.InitNode), P.InitNode);

            while (!P.b_abort && S.Count > 0)
            {
                N = S.Pop();
                ExpCnt++;
                if (_AreNodesSame(N, P.GoalNode))
                {
                    P.DFSSolved = true;
                    break;
                }
                Ns = N.GetSuccessors();
                foreach (ClassNode SN in Ns)
                {
                    string key       = _KeyBuild(SN);
                    bool   KeyInHist = true;    // For Exeption handling when stopping the puzzle solving
                    try
                    { KeyInHist = Hist.ContainsKey(key); }
                    catch
                    { }

                    if (KeyInHist)      // I wonder whether it works or not
                    {
                        //    if (Hist[key].Cost > SN.Cost)
                        //    {
                        //        Hist[key].Cost = SN.Cost;
                        //        S.Push(SN);
                        //        if (S.Count > MaxS)
                        //            MaxS = S.Count;
                        //    }
                    }
                    else         // Then add only successors that are not opened yet
                    {
                        S.Push(SN);
                        Hist.Add(_KeyBuild(SN), SN);
                        if (S.Count > MaxS)
                        {
                            MaxS = S.Count;
                        }
                    }
                }
            }

            if (P.b_abort)
            {
                return(P);
            }
            ClassNode DN = N;

            while (DN != null)
            {
                Sol.Add(DN);
                DN = DN.GetParent();
            }

            Sol.Reverse();



            // THEY NEED TO BE CHECKED...
            ClkEnd           = DateTime.Now;
            SolTime          = (ClkEnd.Millisecond - ClkSt.Millisecond) + (ClkEnd.Second - ClkSt.Second) * 1000 + (ClkEnd.Minute - ClkSt.Minute) * 60 * 1000 + (ClkEnd.Hour - ClkSt.Hour) * 60 * 60 * 1000 + (ClkEnd.DayOfYear - ClkSt.DayOfYear) * 24 * 60 * 60 * 1000; // DO NOT USE THIS PROGRAM AT NEW YEAR PARTY!!!
            P.DFSSolTime     = SolTime;                                                                                                                                                                                                                                  // in milli sec
            P.DFSNumStoNodes = MaxS;
            P.DFSNumExpNodes = ExpCnt;
            P.DFSSolStep     = Sol.Count - 1;      // It is derived from History List
            P.DFSSolSteps    = Sol;

            return(P);
        }
Esempio n. 2
0
        public ClassPuzzle SolveIt()
        {
            ClassComparer Comp            = new ClassComparer();
            SortedList <int, ClassNode> L = new SortedList <int, ClassNode>(Comp);

            ClkSt  = DateTime.Now;
            ExpCnt = 0;

            N      = P.InitNode;
            N.Cost = 0;
            N.Heur = _CalcHeurMan(N);
            L.Add(N.Heur + N.Cost, N);
            Hist.Add(_KeyBuild(N), N);

            while (!P.b_abort && L.Count > 0)
            {
                N = L.First().Value;
                L.RemoveAt(0);
                ExpCnt++;           // Node is about to be processed
                if (_AreNodesSame(N, P.GoalNode))
                {
                    P.AStarManSolved = true;
                    break;
                }
                Ns = N.GetSuccessors();
                foreach (ClassNode SN in Ns)
                {
                    SN.Heur = _CalcHeurMan(SN);
                    string key = _KeyBuild(SN);

                    try { KeyInHist = Hist.ContainsKey(key); }      // For Exeption handling when stopping the puzzle solving
                    catch { }
                    if (KeyInHist)
                    {
                        try { CostInHistLarger = (Hist[key].Cost > SN.Cost); }     // For Exeption handling when stopping the puzzle solving
                        catch { }
                        if (CostInHistLarger)
                        {
                            Hist[key].Cost = SN.Cost;
                            L.Add(SN.Heur + SN.Cost, SN);

                            try { LgotLarger = (L.Count > MaxL); }      // For Exeption handling when stopping the puzzle solving
                            catch { }
                            if (LgotLarger)
                            {
                                MaxL = L.Count;
                            }
                        }
                    }
                    else         // Then add only successors that are not opened yet
                    {
                        L.Add(SN.Heur + SN.Cost, SN);
                        Hist.Add(key, SN);
                        try { LgotLarger = (L.Count > MaxL); }      // For Exeption handling when stopping the puzzle solving
                        catch { }
                        if (LgotLarger)
                        {
                            MaxL = L.Count;
                        }
                    }
                }
            }

            if (P.b_abort)
            {
                return(P);
            }

            ClassNode DN = N;

            while (DN != null)
            {
                Sol.Add(DN);
                DN = DN.GetParent();
            }

            Sol.Reverse();

            // THEY NEED TO BE CHECKED...
            ClkEnd                = DateTime.Now;
            SolTime               = (ClkEnd.Millisecond - ClkSt.Millisecond) + (ClkEnd.Second - ClkSt.Second) * 1000 + (ClkEnd.Minute - ClkSt.Minute) * 60 * 1000 + (ClkEnd.Hour - ClkSt.Hour) * 60 * 60 * 1000 + (ClkEnd.DayOfYear - ClkSt.DayOfYear) * 24 * 60 * 60 * 1000; // DO NOT USE THIS PROGRAM AT NEW YEAR PARTY!!!
            P.AStarManSolTime     = SolTime;                                                                                                                                                                                                                                  // in milli sec
            P.AStarManNumStoNodes = MaxL;
            P.AStarManNumExpNodes = ExpCnt;
            P.AStarManSolStep     = Sol.Count - 1;      // It is derived from History List
            P.AStarManSolSteps    = Sol;

            return(P);
        }
Esempio n. 3
0
        public ClassPuzzle SolveIt()
        {
            ClkSt        = DateTime.Now;
            P.IDFSSolved = false; // Since we need to get out of for loop also, I need to set the solved info to false to avoid undesired break after if statement at the very end
            MaxS++;               // For the very first push
            for (int limit = 0; limit < 1000; limit++)
            {
                S.Clear();
                N      = P.InitNode;
                N.Cost = 0;
                S.Push(N);
                Hist.Clear();                   // Clear Hist for each iteration...
                Hist.Add(_KeyBuild(P.InitNode), P.InitNode);
                while (!P.b_abort && (S.Count > 0))
                {
                    N = S.Pop();
                    ExpCnt++;
                    if (_AreNodesSame(N, P.GoalNode))
                    {
                        P.IDFSSolved = true;
                        break;      // That also breaks from for...
                    }
                    Ns = N.GetSuccessors();
                    foreach (ClassNode SN in Ns)
                    {
                        string key = _KeyBuild(SN);
                        // For Exeption handling when stopping the puzzle solving
                        try
                        { KeyInHist = Hist.ContainsKey(key); }
                        catch
                        { }
                        if (KeyInHist)
                        {
                            bool CostInHistLarger = false;
                            try
                            { CostInHistLarger = (Hist[key].Cost > SN.Cost); }
                            catch
                            { }
                            if (CostInHistLarger)
                            {
                                Hist[key].Cost = SN.Cost;
                                S.Push(SN);
                                if (S.Count > MaxS)
                                {
                                    MaxS = S.Count;
                                }
                            }
                        }
                        else         // Then add only successors that are not opened yet
                        {
                            try { LimitNotReached = SN.Cost <= limit; }
                            catch { }
                            if (LimitNotReached)    // 0th node is the init node...
                            {
                                S.Push(SN);         // So that if cost is larger, it is not pushed in to stack...
                                Hist.Add(_KeyBuild(SN), SN);
                            }
                            try { SgotLarger = (S.Count > MaxS); }
                            catch { }
                            if (SgotLarger)
                            {
                                MaxS = S.Count;
                            }
                        }
                    }
                }
                try { PuzzleSolved = (P.IDFSSolved == true); }
                catch { }
                if (PuzzleSolved)
                {
                    break;
                }
            }

            if (P.b_abort)
            {
                return(P);
            }
            ClassNode DN = N;

            while (DN != null)
            {
                Sol.Add(DN);
                DN = DN.GetParent();
            }

            Sol.Reverse();

            // THEY NEED TO BE CHECKED...
            ClkEnd            = DateTime.Now;
            SolTime           = (ClkEnd.Millisecond - ClkSt.Millisecond) + (ClkEnd.Second - ClkSt.Second) * 1000 + (ClkEnd.Minute - ClkSt.Minute) * 60 * 1000 + (ClkEnd.Hour - ClkSt.Hour) * 60 * 60 * 1000 + (ClkEnd.DayOfYear - ClkSt.DayOfYear) * 24 * 60 * 60 * 1000; // DO NOT USE THIS PROGRAM AT NEW YEAR PARTY!!!
            P.IDFSSolTime     = SolTime;                                                                                                                                                                                                                                  // in milli sec
            P.IDFSNumStoNodes = MaxS;
            P.IDFSNumExpNodes = ExpCnt;
            P.IDFSSolStep     = Sol.Count - 1;      // It is derived from History List
            P.IDFSSolSteps    = Sol;

            return(P);
        }
Esempio n. 4
0
        public ClassPuzzle(XmlNode PuzzleInXML)
        {
            XmlNodeList xList;
            XmlNodeList xListAlgs;              // Just makes simpler
            string      DummyStr;

            xList      = PuzzleInXML.ChildNodes;
            DummyStr   = xList[0].InnerText;
            PuzzleSize = Convert.ToInt32(DummyStr);     // Puzzle size is set

            DummyStr = xList[1].InnerText;
            string[] StatesArray = DummyStr.Split(',');
            int[,] StatesVals = new int[PuzzleSize, PuzzleSize];
            for (int i = 0; i < PuzzleSize; i++)
            {
                for (int j = 0; j < PuzzleSize; j++)
                {
                    StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                }
            }
            InitNode = new ClassNode(StatesVals);       // InitNode and GoalNode are created.
            GoalNode = new ClassNode(PuzzleSize);       // Having 2 different contructors, useful

            DummyStr = xList[2].InnerText;
            if (DummyStr == "N/A")
            {
            }
            else
            {
                DesiredStep = Convert.ToInt32(DummyStr);
            }

            xList     = PuzzleInXML.LastChild.ChildNodes;   // BFS, DFS, IDFE A*Mis, A*Man. I will call their childs to xListAlgs
            xListAlgs = xList[0].ChildNodes;                // Child Nodes of BFS Nodes

            // SAME FOR ALL 5 ALGORITHMS
            DummyStr = xListAlgs[0].InnerText;              // Get Whwthwe Puzzle Solved With BFS
            if (DummyStr == "F")
            {
                BFSSolved = false;
            }
            else if (DummyStr == "T")
            {
                BFSSolved = true;
            }

            DummyStr = xListAlgs[1].InnerText;              // Get solution Time in Seconds
            if (DummyStr != "")
            {
                BFSSolTime = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[2].InnerText;              // Get Number of Stored Nodes
            if (DummyStr != "")
            {
                BFSNumStoNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[3].InnerText;              // Get Number of Expanded Nodes
            if (DummyStr != "")
            {
                BFSNumExpNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[4].InnerText;              // Get Number of Solution Step
            if (DummyStr != "")
            {
                BFSSolStep = Convert.ToInt32(DummyStr);
            }

            xListAlgs = xListAlgs[5].ChildNodes;            // Construct Solution Steps
            foreach (XmlNode xmlN in xListAlgs)
            {
                DummyStr    = xmlN.InnerText;
                StatesArray = DummyStr.Split(',');
                StatesVals  = new int[PuzzleSize, PuzzleSize];
                for (int i = 0; i < PuzzleSize; i++)
                {
                    for (int j = 0; j < PuzzleSize; j++)
                    {
                        StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                    }
                }
                BFSSolSteps.Add(new ClassNode(StatesVals));
            }

            xListAlgs = xList[1].ChildNodes;                // Child Nodes of DFS Nodes
            // SAME FOR ALL 5 ALGORITHMS
            DummyStr = xListAlgs[0].InnerText;              // Get Whwthwe Puzzle Solved With BFS
            if (DummyStr == "F")
            {
                DFSSolved = false;
            }
            else if (DummyStr == "T")
            {
                DFSSolved = true;
            }

            DummyStr = xListAlgs[1].InnerText;              // Get solution Time in Seconds
            if (DummyStr != "")
            {
                DFSSolTime = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[2].InnerText;              // Get Number of Stored Nodes
            if (DummyStr != "")
            {
                DFSNumStoNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[3].InnerText;              // Get Number of Expanded Nodes
            if (DummyStr != "")
            {
                DFSNumExpNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[4].InnerText;              // Get Number of Solution Step
            if (DummyStr != "")
            {
                DFSSolStep = Convert.ToInt32(DummyStr);
            }

            xListAlgs = xListAlgs[5].ChildNodes;            // Construct Solution Steps
            foreach (XmlNode xmlN in xListAlgs)
            {
                DummyStr    = xmlN.InnerText;
                StatesArray = DummyStr.Split(',');
                StatesVals  = new int[PuzzleSize, PuzzleSize];
                for (int i = 0; i < PuzzleSize; i++)
                {
                    for (int j = 0; j < PuzzleSize; j++)
                    {
                        StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                    }
                }
                DFSSolSteps.Add(new ClassNode(StatesVals));
            }


            xListAlgs = xList[2].ChildNodes;                // Child Nodes of IDFS Nodes
            // SAME FOR ALL 5 ALGORITHMS
            DummyStr = xListAlgs[0].InnerText;              // Get Whwthwe Puzzle Solved With BFS
            if (DummyStr == "F")
            {
                IDFSSolved = false;
            }
            else if (DummyStr == "T")
            {
                IDFSSolved = true;
            }

            DummyStr = xListAlgs[1].InnerText;              // Get solution Time in Seconds
            if (DummyStr != "")
            {
                IDFSSolTime = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[2].InnerText;              // Get Number of Stored Nodes
            if (DummyStr != "")
            {
                IDFSNumStoNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[3].InnerText;              // Get Number of Expanded Nodes
            if (DummyStr != "")
            {
                IDFSNumExpNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[4].InnerText;              // Get Number of Solution Step
            if (DummyStr != "")
            {
                IDFSSolStep = Convert.ToInt32(DummyStr);
            }

            xListAlgs = xListAlgs[5].ChildNodes;            // Construct Solution Steps
            foreach (XmlNode xmlN in xListAlgs)
            {
                DummyStr    = xmlN.InnerText;
                StatesArray = DummyStr.Split(',');
                StatesVals  = new int[PuzzleSize, PuzzleSize];
                for (int i = 0; i < PuzzleSize; i++)
                {
                    for (int j = 0; j < PuzzleSize; j++)
                    {
                        StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                    }
                }
                IDFSSolSteps.Add(new ClassNode(StatesVals));
            }


            xListAlgs = xList[3].ChildNodes;                // Child Nodes of AStarMis Nodes
            // SAME FOR ALL 5 ALGORITHMS
            DummyStr = xListAlgs[0].InnerText;              // Get Whwthwe Puzzle Solved With BFS
            if (DummyStr == "F")
            {
                AStarMisSolved = false;
            }
            else if (DummyStr == "T")
            {
                AStarMisSolved = true;
            }

            DummyStr = xListAlgs[1].InnerText;              // Get solution Time in Seconds
            if (DummyStr != "")
            {
                AStarMisSolTime = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[2].InnerText;              // Get Number of Stored Nodes
            if (DummyStr != "")
            {
                AStarMisNumStoNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[3].InnerText;              // Get Number of Expanded Nodes
            if (DummyStr != "")
            {
                AStarMisNumExpNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[4].InnerText;              // Get Number of Solution Step
            if (DummyStr != "")
            {
                AStarMisSolStep = Convert.ToInt32(DummyStr);
            }

            xListAlgs = xListAlgs[5].ChildNodes;            // Construct Solution Steps
            foreach (XmlNode xmlN in xListAlgs)
            {
                DummyStr    = xmlN.InnerText;
                StatesArray = DummyStr.Split(',');
                StatesVals  = new int[PuzzleSize, PuzzleSize];
                for (int i = 0; i < PuzzleSize; i++)
                {
                    for (int j = 0; j < PuzzleSize; j++)
                    {
                        StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                    }
                }
                AStarMisSolSteps.Add(new ClassNode(StatesVals));
            }


            xListAlgs = xList[4].ChildNodes;                // Child Nodes of AStarMan Nodes
            // SAME FOR ALL 5 ALGORITHMS
            DummyStr = xListAlgs[0].InnerText;              // Get Whwthwe Puzzle Solved With BFS
            if (DummyStr == "F")
            {
                AStarManSolved = false;
            }
            else if (DummyStr == "T")
            {
                AStarManSolved = true;
            }

            DummyStr = xListAlgs[1].InnerText;              // Get solution Time in Seconds
            if (DummyStr != "")
            {
                AStarManSolTime = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[2].InnerText;              // Get Number of Stored Nodes
            if (DummyStr != "")
            {
                AStarManNumStoNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[3].InnerText;              // Get Number of Expanded Nodes
            if (DummyStr != "")
            {
                AStarManNumExpNodes = Convert.ToInt32(DummyStr);
            }

            DummyStr = xListAlgs[4].InnerText;              // Get Number of Solution Step
            if (DummyStr != "")
            {
                AStarManSolStep = Convert.ToInt32(DummyStr);
            }

            xListAlgs = xListAlgs[5].ChildNodes;            // Construct Solution Steps
            foreach (XmlNode xmlN in xListAlgs)
            {
                DummyStr    = xmlN.InnerText;
                StatesArray = DummyStr.Split(',');
                StatesVals  = new int[PuzzleSize, PuzzleSize];
                for (int i = 0; i < PuzzleSize; i++)
                {
                    for (int j = 0; j < PuzzleSize; j++)
                    {
                        StatesVals[i, j] = Convert.ToInt32(StatesArray[(i * PuzzleSize) + j]);
                    }
                }
                AStarManSolSteps.Add(new ClassNode(StatesVals));
            }
        }