public bool Equals(PuzzleMap pm2) { if (pm2 == null) return false; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (data[i, j] != pm2.data[i, j]) return false; return true; }
static bool GrandfatherPruningCheckOK(List<PuzzleMap> thePath, PuzzleMap newNode) { if (!EnableGrandfatherPruning) return true; int total = thePath.Count; if (total == 0) return true; if (total == 1) return (!thePath[0].Equals(newNode)); if (thePath[total - 1].Equals(newNode) || thePath[total - 2].Equals(newNode)) return false; return true; }
public PuzzleMap Clone() { try { PuzzleMap ans = new PuzzleMap(); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) ans.data[i, j] = data[i, j]; return ans; } catch (Exception) { return null; throw; } }
public Visualizer(PuzzleMap yourPuzzleMap) { puzzleMap = yourPuzzleMap; }
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; }; }
static bool LoopEliminationCheckOK(List<PuzzleMap> thePath, PuzzleMap newNode) { if (!EnableLoopElimination) return true; foreach (PuzzleMap node in thePath) { if (node.Equals(newNode)) return false; } return true; }
static void InitDataSource(string filePath) { try { finalState = new PuzzleMap(finalStateStr); using (StreamReader sr = new StreamReader(filePath)) { Problems.Clear(); string tmpLine = null; while (!sr.EndOfStream) { tmpLine = sr.ReadLine(); if (tmpLine.StartsWith(prefix)) { string stateStr = tmpLine.Substring(prefix.Length, tmpLine.Length - prefix.Length - suffix.Length); Problems.Add(new PuzzleMap(stateStr)); // used to generated the output file InitialStateStr.Add(stateStr); } } } Console.WriteLine("File loaded: \n" + filePath + "\n" + "Total elements: " + Problems.Count.ToString() + "\n"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }