예제 #1
0
        static void IDAStarSearch(PuzzleMap iPuzzleMap)
        {
            SearchSpace.Add(new TreeNode()
            {
                puzzleMap = iPuzzleMap,
                NodeDepth = 0,
                SolutionPath = new List<PuzzleMap>() { iPuzzleMap }
            });

            int nodeIndex = 0;
            TreeNode myNode = SearchSpace[nodeIndex];

            while (true)
            {
                myNode = SearchSpace[nodeIndex];

                // should be (myNode.NodeDepth > depthLimit)
                if (myNode.NodeDepth > fileNum)
                {
                    //SearchSpace.Add(myNode);
                    break;
                }

                if (myNode.puzzleMap.Equals(finalState))
                {
                    WriteOutputFile(myNode);
                    break;
                }

                // begin expanding this node
                myNode.NodeDepth += 1;
                PuzzleMap childNodePuzzleMap = myNode.puzzleMap.Clone();
                SearchSpace.RemoveAt(nodeIndex);

                // increase the nodes-expanded counter
                CurrentNodeExpanded += 1;

                for (int i = 0; i < 3; i++)
                    for (int j = 0; j < 3; j++)
                        for (int x = -1; x < 2; x++)
                            for (int y = -1; y < 2; y++)
                            {
                                childNodePuzzleMap = myNode.puzzleMap.Clone();
                                if (myNode.puzzleMap.AbleToMove(i, j, x, y))
                                {
                                    // new children nodes are generated here
                                    childNodePuzzleMap.MoveTile(i, j, x, y);
                                    if (GrandfatherPruningCheckOK(myNode.SolutionPath, childNodePuzzleMap)
                                        && LoopEliminationCheckOK(myNode.SolutionPath, childNodePuzzleMap))
                                    {
                                        TreeNode newChildNode = new TreeNode()
                                        {
                                            puzzleMap = childNodePuzzleMap,
                                            NodeDepth = myNode.NodeDepth,
                                            SolutionPath = myNode.CloneSolutionPath()
                                        };
                                        newChildNode.SolutionPath.Add(childNodePuzzleMap);
                                        SearchSpace.Add(newChildNode);

                                        // increase the nodes-generated counter
                                        CurrentNodeGenerated += 1;
                                    }
                                }
                            }

                int minimumValueIndex = 0;
                int minimumValue = int.MaxValue;
                for (int k = 0; k < SearchSpace.Count; k++)
                {
                    if (SearchSpace[k].fValue() < minimumValue)
                    {
                        minimumValueIndex = k;
                        minimumValue = SearchSpace[k].fValue();
                    }
                }

                nodeIndex = minimumValueIndex;
            };
        }
예제 #2
0
 static void WriteOutputFile(TreeNode myNode)
 {
     try
     {
         problemId += 1;
         if (diskProtectMode)
             return;
         string outputpath = String.Format(outputDir, problemId);
         using (StreamWriter sw = new StreamWriter(outputpath, false))
         {
             sw.WriteLine("Steps = " + (myNode.NodeDepth).ToString());
             sw.WriteLine("Nodes Generated = " + CurrentNodeGenerated.ToString());
             sw.WriteLine("Nodes Expanded = " + CurrentNodeExpanded.ToString());
             foreach (PuzzleMap item in myNode.SolutionPath)
             {
                 sw.WriteLine(item.ToVisual());
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         //Console.WriteLine("Found solution {0}! ", outputNum);
     }
 }