public void Create_NewDfBnbGrid4X4WithBlocked16_FindPath() { GridSearchNode initialState = _basicWorld4X416.GetInitialSearchNode <GridSearchNode>(); Solver solver = new DfBnbMax(initialState, new GoalOnLocation(_basicWorld4X416.Goal)); Assert.IsNotNull(solver); solver.Run(Int32.MaxValue); var maxGoal = solver.GetMaxGoal(); Assert.AreEqual(10, maxGoal.g); }
public void Create_NewDfBnbGrid3X3_findPath() { GridSearchNode initialState = _basicWorld3X3.GetInitialSearchNode <GridSearchNode>(); Solver solver = new DfBnbMax(initialState, new GoalOnLocation(_basicWorld3X3.Goal)); Assert.IsNotNull(solver); solver.Run(Int32.MaxValue); //Prevent stoping by time, should stop only when goal found var maxGoal = solver.GetMaxGoal(); Assert.AreEqual(8, maxGoal.g); }
public void Basic_Hbsd_findPathDfBnB() { GridSearchNode initialState = _basicBlocked5X5World.GetInitialSearchNode <GridSearchNode>(); IPrunningMethod prunningMethod = new HashedBasicSymmetryDetectionPrunning(); Solver solver = new DfBnbMax(initialState, prunningMethod, new GoalOnLocation(_basicBlocked5X5World.Goal)); Assert.IsNotNull(solver); solver.Run(Int32.MaxValue); //Prevent stoping by time, should stop only when goal found var maxGoal = solver.GetMaxGoal(); Assert.AreEqual(20, maxGoal.g); }
public void Run_NewAStarSnake() { World w = new World(5, 3); var heuristicFunc = new SnakeNoneHeuristic(); Snake snake = new Snake(w, 0, heuristicFunc); DfBnbMax dfBnbMax = new DfBnbMax(snake, new ImplicitGoal()); dfBnbMax.Run(1); var maxGoal = dfBnbMax.GetMaxGoal(); Assert.IsNotNull(maxGoal); }
public void Run_NewAStarBox() { World w = new World(5, 2, 2); var heuristicFunc = new SnakeNoneHeuristic(); int[] snakeHeads = new int[] { 0, 31 }; BoxOD b = new BoxOD(w, snakeHeads, new BoxNoneHeuristic(), heuristicFunc); DfBnbMax dfBnbMax = new DfBnbMax(b, new ImplicitGoal()); dfBnbMax.Run(1); var maxGoal = dfBnbMax.GetMaxGoal(); Assert.IsNotNull(maxGoal); }
public void Compare_AStar_To_DfBNB() { foreach (var world in _worlds) { AStarMax astar = new AStarMax(world.GetInitialSearchNode <GridSearchNode>(), new GoalOnLocation(world.Goal)); Assert.IsNotNull(astar); astar.Run(Int32.MaxValue); var AstarMaxGoal = astar.GetMaxGoal(); DfBnbMax dfbnb = new DfBnbMax(world.GetInitialSearchNode <GridSearchNode>(), new GoalOnLocation(world.Goal)); Assert.IsNotNull(dfbnb); dfbnb.Run(Int32.MaxValue); var DfbnbMaxGoal = dfbnb.GetMaxGoal(); Assert.AreEqual(AstarMaxGoal.g, DfbnbMaxGoal.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); }
static void Main(string[] args) { if (args.Length == 0) { System.Console.WriteLine(@"Please Provide arguments to run:"); System.Console.WriteLine(@"all args should be in the form of: [key]=[value] with space between them"); System.Console.WriteLine(@"Arguments:"); System.Console.WriteLine(@"----------"); System.Console.WriteLine(@"problem: [snake/box/box-od] snake is single agent & box is multi-agent"); System.Console.WriteLine(@" if you choose box you must provide snakes initial locations"); System.Console.WriteLine(@" with arguments Sx=location"); System.Console.WriteLine(@"Sx: starting location of snake number x, counting from 0"); System.Console.WriteLine(@" when using snake you can have only 1 Sx argument"); System.Console.WriteLine(@"numOfSnakes: instead of Sx (above arg.), will generate all possible positions"); System.Console.WriteLine(@" by using virtual node (box only)"); System.Console.WriteLine(@"snakeH: [none/legal/reachable] the snake heuristic"); System.Console.WriteLine(@"boxH: [none/snakes-sum/legal/reachable/shortest] the box heuristic"); System.Console.WriteLine(@"alg: [astar/dfbnb] the solving algorithm"); System.Console.WriteLine(@"dim: the number of dimentions for the problem (N)"); System.Console.WriteLine(@"snakeSpread: the intra-snake spread (sK)"); System.Console.WriteLine(@"boxSpread: the inter-snake spread (bK)"); System.Console.WriteLine(@"timeLimit: limit run time to X minutes (default 120), 0 for no time limit"); System.Console.WriteLine(@"memTest: if set to true, will not solve nothing, only fill memory"); System.Console.WriteLine(@" allocation to check 64bit issue"); System.Console.WriteLine(@""); System.Console.WriteLine(@"Start examples:"); System.Console.WriteLine(@"---------------"); System.Console.WriteLine(@"this is how to solve single snake problem with A* (head at 0=(00000))"); System.Console.WriteLine(@"when the dimention is set to 5 and snake spread is 2:"); System.Console.WriteLine(@"MaSiB problem=snake S0=0 alg=astar dim=5 snakeSpread=2"); System.Console.WriteLine(@""); System.Console.WriteLine(@"this is how to solve multiple snake problem with A*-OD"); System.Console.WriteLine(@"when the dimention is set to 7, intra-snake spread is 2"); System.Console.WriteLine(@"and inter-snake spread is 3, the starting locations are 0-(0000000)"); System.Console.WriteLine(@"and 127-(1111111) so we have 2 snakes"); System.Console.WriteLine(@"MaSiB problem=box-od s0=0 s1=127 alg=dfbnb dim=7 snakeSpread=2 boxSpread=3 boxh=snakes-sum snakeh=reachable"); return; } Dictionary <string, string> splitedArgs = SplitArguments(args); if (splitedArgs.ContainsKey("memtest") && splitedArgs["memtest"].Equals("true")) { MemTest(); } int n = Int32.Parse(splitedArgs["dim"]); int sk = Int32.Parse(splitedArgs["snakespread"]); int bk = 2; if (splitedArgs.ContainsKey("boxspread")) { bk = Int32.Parse(splitedArgs["boxspread"]); } else { Log.WriteLineIf("boxSpread not found, setting it to:2", TraceLevel.Warning); } World w = new World(n, sk, bk); ISibNode initState; ISnakeHeuristic snakeh; IBoxHeuristic boxh; Solver solver; if (!splitedArgs.ContainsKey("boxh")) //default boxh { splitedArgs.Add("boxh", "none"); } switch (splitedArgs["boxh"]) { case "none": boxh = new BoxNoneHeuristic(); break; case "snakes-sum": boxh = new BoxSnakesSumHeuristic(); break; case "legal": boxh = new BoxLegalHeuristic(); break; case "reachable": boxh = new BoxReachableHeuristic(); break; case "shortest": boxh = new BoxShortestSnakeReachableHeuristic(); break; default: Log.WriteLineIf("Box heuristic: " + splitedArgs["boxh"] + " is not supported!", TraceLevel.Error); return; } if (!splitedArgs.ContainsKey("snakeh")) //default snakeh { splitedArgs.Add("snakeh", "none"); } switch (splitedArgs["snakeh"]) { case "none": snakeh = new SnakeNoneHeuristic(); break; case "legal": snakeh = new SnakeLegalHeuristic(); break; case "reachable": snakeh = new SnakeReachableHeuristic(); break; default: Log.WriteLineIf("Snake heuristic: " + splitedArgs["snakeh"] + " is not supported!", TraceLevel.Error); return; } if (!splitedArgs.ContainsKey("timelimit")) //default snakeh { splitedArgs.Add("timelimit", "120"); } int timelimit = Int32.Parse(splitedArgs["timelimit"]); if (splitedArgs["problem"].Equals("snake")) { initState = new Snake(w, Int32.Parse(splitedArgs["s0"]), snakeh); } else if (splitedArgs["problem"].StartsWith("box")) { if (splitedArgs.ContainsKey("numofsnakes")) {//virtual node if (splitedArgs["problem"].Equals("box")) { initState = new BoxVirtualNode <BoxCartesian>(w, Int32.Parse(splitedArgs["numofsnakes"]), boxh, snakeh); } else if (splitedArgs["problem"].Equals("box-od")) { initState = new BoxVirtualNode <BoxOD>(w, Int32.Parse(splitedArgs["numofsnakes"]), boxh, snakeh); } else { Log.WriteLineIf("Problem: " + splitedArgs["problem"] + " this box is not supported!", TraceLevel.Error); return; } } else {//User selected start positions List <int> heads = new List <int>(); int i = 0; while (splitedArgs.ContainsKey("s" + i)) { heads.Add(Int32.Parse(splitedArgs["s" + i])); i++; } if (splitedArgs["problem"].Equals("box")) { initState = new BoxCartesian(w, heads.ToArray(), boxh, snakeh); } else if (splitedArgs["problem"].Equals("box-od")) { initState = new BoxOD(w, heads.ToArray(), boxh, snakeh); } else { Log.WriteLineIf("Problem: " + splitedArgs["problem"] + " this box is not supported!", TraceLevel.Error); return; } } } else { Log.WriteLineIf("Problem: " + splitedArgs["problem"] + " is not supported!", TraceLevel.Error); return; } switch (splitedArgs["alg"]) { case "astar": solver = new AStarMax(initState, new ImplicitGoal()); break; case "dfbnb": solver = new DfBnbMax(initState, new ImplicitGoal()); break; default: Log.WriteLineIf("Solver algorithm: " + splitedArgs["alg"] + " is not supported!", TraceLevel.Error); return; } Log.WriteLineIf(@"Solviong snakes in the box problem:", TraceLevel.Info); Log.WriteLineIf(@"[[Algorithm:" + solver.GetType().Name + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[Problem:" + splitedArgs["problem"] + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[WorldDimentions:" + n + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[SnakeSpread:" + sk + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[BoxSpread:" + bk + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[SnakeHeuristics:" + snakeh.GetType().Name + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[BoxHeuristics:" + boxh.GetType().Name + "]]", TraceLevel.Info); Log.WriteLineIf(@"[[NumOfSnakes:" + Int32.Parse(splitedArgs["numofsnakes"]) + "]]", TraceLevel.Info); var startTime = DateTime.Now; var howEnded = solver.Run(timelimit); var totalTime = DateTime.Now - startTime; var goal = (ISibNode)solver.GetMaxGoal(); Log.WriteLineIf("[[TotalTime(MS):" + totalTime.TotalMilliseconds + "]]", 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("[[G-Value:" + goal.g + "]]", TraceLevel.Off); Log.WriteLineIf("[[GoalBits:" + goal.GetBitsString() + "]]", TraceLevel.Off); Log.WriteLineIf("[[Goal:" + goal.GetNodeStringV2() + "]]", TraceLevel.Off); Log.WriteLineIf("[[HowEnded:" + Enum.GetName(typeof(State), howEnded) + "]]", TraceLevel.Off); var snakeFreeSpots = goal.GetSnakeSpreadFreeSpots(); Log.WriteLineIf("[[SnakeSpreadFreeSpotsCount:" + snakeFreeSpots.Count + "]]", TraceLevel.Off); Log.WriteLineIf("[[SnakeSpreadFreeSpotsPlaces:" + string.Join("-", snakeFreeSpots) + "]]", TraceLevel.Off); if (goal is Box) { var boxFreeSpots = ((Box)goal).GetBoxSpreadFreeSpots(); Log.WriteLineIf("[[BoxSpreadFreeSpotsCount:" + boxFreeSpots.Count + "]]", TraceLevel.Off); Log.WriteLineIf("[[BoxSpreadFreeSpotsPlaces:" + string.Join("-", boxFreeSpots) + "]]", TraceLevel.Off); } var sLoop = 0; while (splitedArgs.ContainsKey("s" + sLoop)) { Log.WriteLineIf("[[S" + sLoop + ":" + splitedArgs["s" + sLoop] + "]]", TraceLevel.Info); sLoop++; } }