public void PruneNode_ReturnsTrueOnRelevantStateOnly_TriggredWhenNeededByReachableSymmetryDetectionCase2() { //More Complicated case - prune inside the ShouldPrune method, return Yes for the should prune but replace nodes RsdGridSearchNode initialState = _basicWorld5X5Blocked.GetInitialSearchNode <RsdGridSearchNode>(); ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicWorld5X5Blocked.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicWorld5X5Blocked.Goal)); rsd.setAstarOpenList(solver.OpenList); Assert.IsFalse(rsd.ShouldPrune(initialState)); //Flow 1: //↓→* //→↑# //### var Flow2Node = new RsdGridSearchNode(initialState, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Up); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); //Flow 2: //→→* // # //### var Flow1Node = new RsdGridSearchNode(initialState, MoveDirection.Right); Assert.IsTrue(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Right); Assert.IsTrue(rsd.ShouldPrune(Flow1Node)); }
public void IsPrune_NotWorkingWithoutAstarOpenList_Exception() { RsdGridSearchNode initialState = _basicClean5X5World.GetInitialSearchNode <RsdGridSearchNode>(); ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicClean5X5World.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicClean5X5World.Goal)); Assert.IsNotNull(rsd); Assert.IsFalse(rsd.ShouldPrune(initialState)); }
public void setAstarOpenList_CanBeCalledOnlyOnce_CallingTwiceResultsInException() { ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicClean5X5World.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicClean5X5World.Goal)); Assert.IsNotNull(rsd); rsd.setAstarOpenList(solver.OpenList); rsd.setAstarOpenList(solver.OpenList); }
public void PruneNode_ReturnsTrueOnRelevantStateOnly_TriggredWhenNeededByBasicSymmetryCase() { RsdGridSearchNode initialState = _basicClean5X5World.GetInitialSearchNode <RsdGridSearchNode>(); ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicClean5X5World.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicClean5X5World.Goal)); rsd.setAstarOpenList(solver.OpenList); Assert.IsFalse(rsd.ShouldPrune(initialState)); //Flow 1: //↓→↓ //↓↑↓ //→↑* var Flow1Node = new RsdGridSearchNode(initialState, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Up); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Up); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); //Flow 2: //→→↓ //↓←← //→→* var Flow2Node = new RsdGridSearchNode(initialState, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Left); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Left); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsTrue(rsd.ShouldPrune(Flow2Node)); }
public void AStarGrid_BasicRun_BCCHeuristicsRSD_Success2() { RsdGridSearchNode initialState = _basicWorldBccAndPruningCheck.GetInitialSearchNode <RsdGridSearchNode>(); ReachableSymmetryDetectionPrunning pruning = new ReachableSymmetryDetectionPrunning(); AStarMax astar = new AStarMax(initialState, pruning, new GoalOnLocation(_basicWorldV1.Goal)); Assert.IsNotNull(astar); pruning.setAstarOpenList(astar.OpenList); var howEnded = astar.Run(Int32.MaxValue); var maxGoal = astar.GetMaxGoal(); Assert.IsNotNull(maxGoal); Assert.AreEqual(State.Ended, howEnded); }
public void setAstarSolve_BasicWorld4x4_SolveCorrectly() { ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicWorld4x4.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicWorld4x4.Goal)); rsd.setAstarOpenList(solver.OpenList); Assert.IsNotNull(rsd); Assert.IsNotNull(solver); var howEnded = solver.Run(10); Assert.AreEqual(State.Ended, howEnded); var goal = (RsdGridSearchNode)solver.GetMaxGoal(); Assert.IsNotNull(goal); Assert.AreEqual(14, goal.g); }
public void Integration_altbccWithRsdBug_ShouldFindSolution() { _specialCase01 = new World(File.ReadAllText(@"..\..\altbcc-rsd-bug001.grd"), new AlternateStepsBiconnectedComponentsHeuristic()); var prune = new ReachableSymmetryDetectionPrunning(); AStarMax astar = new AStarMax(_specialCase01.GetInitialSearchNode <RsdGridSearchNode>(), prune, new GoalOnLocation(_specialCase01.Goal)); Assert.IsNotNull(astar); prune.setAstarOpenList(astar.OpenList); astar.Run(Int32.MaxValue); var AstarMaxGoal = astar.GetMaxGoal(); DfBnbMax dfbnb = new DfBnbMax(_specialCase01.GetInitialSearchNode <GridSearchNode>(), new GoalOnLocation(_specialCase01.Goal)); Assert.IsNotNull(dfbnb); dfbnb.Run(Int32.MaxValue); var DfbnbMaxGoal = dfbnb.GetMaxGoal(); Assert.AreEqual(AstarMaxGoal.g, DfbnbMaxGoal.g); }
public void PruneNode_ReturnsTrueOnRelevantStateOnly_TriggredWhenNeededByReachableSymmetryDetectionCase1() { RsdGridSearchNode initialState = _basicWorld5X5Blocked.GetInitialSearchNode <RsdGridSearchNode>(); ReachableSymmetryDetectionPrunning rsd = new ReachableSymmetryDetectionPrunning(); AStarMax solver = new AStarMax(_basicWorld5X5Blocked.GetInitialSearchNode <RsdGridSearchNode>(), rsd, new GoalOnLocation(_basicWorld5X5Blocked.Goal)); rsd.setAstarOpenList(solver.OpenList); Assert.IsFalse(rsd.ShouldPrune(initialState)); //Flow 1: //→→* // # //### var Flow1Node = new RsdGridSearchNode(initialState, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); solver.OpenList.Add(Flow1Node); Flow1Node = new RsdGridSearchNode(Flow1Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow1Node)); solver.OpenList.Add(Flow1Node); //Flow 2: //↓→* //→↑# //### var Flow2Node = new RsdGridSearchNode(initialState, MoveDirection.Down); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); solver.OpenList.Add(Flow2Node); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsFalse(rsd.ShouldPrune(Flow2Node)); solver.OpenList.Add(Flow2Node); Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Up); Assert.IsTrue(rsd.ShouldPrune(Flow2Node)); //Prune = no adding here (psss... it will be added from the pruning replace) Flow2Node = new RsdGridSearchNode(Flow2Node, MoveDirection.Right); Assert.IsTrue(rsd.ShouldPrune(Flow2Node)); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(@"Please Provide arguments to run:"); Console.WriteLine(@"all args should be in the form of: [key]=[value] with space between them"); Console.WriteLine(@"Arguments:"); Console.WriteLine(@"----------"); Console.WriteLine(@"problem: problem filename"); Console.WriteLine(@"time-limit: limit run time to X minutes (default 120), 0 for no time limit"); Console.WriteLine(@"alg: [astar/dfbnb/greedy/greedyloops] the solving algorithm"); Console.WriteLine(@"heuristic: [none/untouched/bcc/alternate/altbcc/sepaltbcc] the heuristic being used"); Console.WriteLine(@"prune: [none/bsd/rsd/hbsd] pruning technique"); Console.WriteLine(@"bcc-init: [true/false] remove non-reachable areas from the graph on init"); Console.WriteLine(@"----------"); Console.WriteLine(@"memTest: if set to true, will not solve nothing, only fill memory"); Console.WriteLine(@" allocation to check 64bit issue"); Console.WriteLine(@"-----------------------------[Version:" + VERSION + "]---------------------------------"); Console.WriteLine(@"time-limit & bcc-init can be set in app.config XML file"); return; } Dictionary <string, string> splitedArgs = ConsoleAppHelper.SplitArguments(args); if (splitedArgs.ContainsKey("memtest") && splitedArgs["memtest"].Equals("true")) { MemTest(); } if (!splitedArgs.ContainsKey("time-limit")) //default time limit { splitedArgs.Add("time-limit", TIME_LIMIT); } if (!splitedArgs.ContainsKey("bcc-init")) //default pre-bcc { splitedArgs.Add("bcc-init", BCC_INIT); } int timelimit = Int32.Parse(splitedArgs["time-limit"]); bool bccInit = Boolean.Parse(splitedArgs["bcc-init"]); string problemFileName = splitedArgs["problem"]; IGridHeuristic heuristic; if (splitedArgs["heuristic"] == "none") { heuristic = new NoneHeuristic(); } else if (splitedArgs["heuristic"] == "untouched") { if (splitedArgs["prune"] == "rsd") { heuristic = new RsdUntouchedAroundTheGoalHeuristic(); } else { heuristic = new UntouchedAroundTheGoalHeuristic(); } } else if (splitedArgs["heuristic"] == "bcc") { //TODO: finish impl. + support RSD heuristic = new BiconnectedComponentsHeuristic(); } else if (splitedArgs["heuristic"] == "alternate") { heuristic = new AlternateStepsHeuristic(); } else if (splitedArgs["heuristic"] == "altbcc") { heuristic = new AlternateStepsBiconnectedComponentsHeuristic(); } else if (splitedArgs["heuristic"] == "sepaltbcc") { heuristic = new SeparateAlternateStepsBiconnectedComponentsHeuristic(); } else { throw new NotImplementedException(); } World world = new World(File.ReadAllText(problemFileName), heuristic); IPrunningMethod prune; GridSearchNode initialNode; switch (splitedArgs["prune"]) { case "none": prune = new NoPrunning(); initialNode = world.GetInitialSearchNode <GridSearchNode>(); break; case "bsd": prune = new BasicSymmetryDetectionPrunning(); initialNode = world.GetInitialSearchNode <GridSearchNode>(); break; case "hbsd": prune = new HashedBasicSymmetryDetectionPrunning(); initialNode = world.GetInitialSearchNode <GridSearchNode>(); break; case "rsd": prune = new ReachableSymmetryDetectionPrunning(); initialNode = world.GetInitialSearchNode <RsdGridSearchNode>(); break; default: Log.WriteLineIf("Prunning Method: " + splitedArgs["prune"] + " is not supported!", TraceLevel.Error); return; } Solver solver; switch (splitedArgs["alg"]) { case "astar": solver = new AStarMax(initialNode, prune, new GoalOnLocation(world.Goal)); break; case "dfbnb": solver = new DfBnbMax(initialNode, prune, new GoalOnLocation(world.Goal)); break; case "greedy": solver = new GreedyMax(initialNode, new GoalOnLocation(world.Goal)); if (splitedArgs["prune"] != "none") { Log.WriteLineIf("Greedy doesn't support pruning!", TraceLevel.Error); return; } break; case "greedyloops": solver = new GreedyLoopMax(initialNode, new GoalOnLocation(world.Goal), 50); if (splitedArgs["prune"] != "none") { Log.WriteLineIf("GreedyLoops doesn't support pruning!", TraceLevel.Error); return; } break; default: Log.WriteLineIf("Solver algorithm: " + splitedArgs["alg"] + " is not supported!", TraceLevel.Error); return; } if (splitedArgs["prune"] == "rsd") { //Sorry but RSD must use AStarMax - DFBnB not supported ((ReachableSymmetryDetectionPrunning)prune).setAstarOpenList(((AStarMax)solver).OpenList); } if (bccInit) { world.InitBcc(); } Log.WriteLineIf(@"Solviong 2D-Grid problem from file:", TraceLevel.Off); Log.WriteLineIf(@"[[Folder:" + Path.GetFileName(Environment.CurrentDirectory) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Problem:" + problemFileName + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Algorithm:" + solver.GetType().Name + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Heuristic:" + heuristic.GetName() + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Prunning:" + prune.GetName() + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[BccInit:" + bccInit + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[TimeLimit:" + timelimit + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Width:" + world.Width + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[Height:" + world.Height + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[TotalLocations:" + world.Height * world.Width + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[TotalFreeLocationsPreBcc:" + ((world.Height * world.Width) - world.TotalBlockedPreBccInit) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[TotalFreeLocationsPostBcc:" + ((world.Height * world.Width) - world.TotalBlockedPostBccInit) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[BlockedCountPreBccInit:" + world.TotalBlockedPreBccInit + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[BlockedCountPostBccInit:" + world.TotalBlockedPostBccInit + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[NumberOfEvenLocations:" + AlternateStepsHeuristic.GetNumberOfEvenLocations(world.Width, world.Height) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[NumberOfOddLocations:" + AlternateStepsHeuristic.GetNumberOfOddLocations(world.Width, world.Height) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[EvenBlockedCount:" + world.EvenBlocked + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[OddBlockedCount:" + world.OddBlocked + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[EvenFreeCount:" + (AlternateStepsHeuristic.GetNumberOfEvenLocations(world.Width, world.Height) - world.EvenBlocked) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[OddFreeCount:" + (AlternateStepsHeuristic.GetNumberOfOddLocations(world.Width, world.Height) - world.OddBlocked) + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[EvenLocationsPercent:" + (decimal)AlternateStepsHeuristic.GetNumberOfEvenLocations(world.Width, world.Height) / world.LinearSize + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[OddLocationsPercent:" + (decimal)AlternateStepsHeuristic.GetNumberOfOddLocations(world.Width, world.Height) / world.LinearSize + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[EvenBlockedPercent:" + (decimal)world.EvenBlocked / world.LinearSize + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[OddBlockedPercent:" + (decimal)world.OddBlocked / world.LinearSize + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[EvenFreePercent:" + (decimal)(AlternateStepsHeuristic.GetNumberOfEvenLocations(world.Width, world.Height) - world.EvenBlocked) / world.LinearSize + "]]", TraceLevel.Off); Log.WriteLineIf(@"[[OddFreePercent:" + (decimal)(AlternateStepsHeuristic.GetNumberOfOddLocations(world.Width, world.Height) - world.OddBlocked) / world.LinearSize + "]]", TraceLevel.Off); var startTime = DateTime.Now; ulong startCycles = NativeMethods.GetThreadCycles(); var howEnded = solver.Run(timelimit); ulong totalCycles = NativeMethods.GetThreadCycles() - startCycles; var totalTime = DateTime.Now - startTime; var goal = (GridSearchNode)solver.GetMaxGoal(); Log.WriteLineIf("[[TotalTime(MS):" + totalTime.TotalMilliseconds + "]]", TraceLevel.Off); Log.WriteLineIf("[[CPU Cycles:" + totalCycles + "]]", TraceLevel.Off); Log.WriteLineIf("[[Expended:" + solver.Expended + "]]", TraceLevel.Off); Log.WriteLineIf("[[Generated:" + solver.Generated + "]]", TraceLevel.Off); Log.WriteLineIf("[[AlgPruned:" + solver.AlgPruned + "]]", TraceLevel.Off); Log.WriteLineIf("[[ExternalPruned:" + solver.ExternalPruned + "]]", TraceLevel.Off); Log.WriteLineIf("[[TotalPruned:" + solver.ExternalPruned + solver.AlgPruned + "]]", TraceLevel.Off); if (goal != null) { Log.WriteLineIf("[[G-Value:" + goal.g + "]]", TraceLevel.Off); Log.WriteLineIf("[[GoalBits:" + goal.GetBitsString() + "]]", TraceLevel.Off); Log.WriteLineIf("[[Goal:" + goal.GetNodeStringV2() + "]]", TraceLevel.Off); } else { Log.WriteLineIf("[[G-Value:" + -1 + "]]", TraceLevel.Off); Log.WriteLineIf("[[GoalBits:NOGOAL]]", TraceLevel.Off); Log.WriteLineIf("[[Goal:NOGOAL]]", TraceLevel.Off); } Log.WriteLineIf("[[HowEnded:" + Enum.GetName(typeof(State), howEnded) + "]]", TraceLevel.Off); Log.WriteLineIf("[[GridSolverVersion:" + VERSION + "]]", TraceLevel.Off); }