示例#1
0
        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 Create_NewAStarSnake()
        {
            World    w             = new World(4, 2);
            var      heuristicFunc = new SnakeNoneHeuristic();
            Snake    snake         = new Snake(w, 0, heuristicFunc);
            AStarMax astar         = new AStarMax(snake, new ImplicitGoal());

            Assert.IsNotNull(astar);
        }
示例#3
0
        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));
        }
示例#4
0
        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 Create_NewAStarGrid4X4WithBlocked16_FindPath()
        {
            GridSearchNode initialState = _basicWorld4X416.GetInitialSearchNode <GridSearchNode>();
            AStarMax       astar        = new AStarMax(initialState, new GoalOnLocation(_basicWorld4X416.Goal));

            Assert.IsNotNull(astar);
            astar.Run(Int32.MaxValue);
            var maxGoal = astar.GetMaxGoal();

            Assert.AreEqual(10, maxGoal.g);
        }
        public void Create_NewAStarGrid3X3_findPath()
        {
            GridSearchNode initialState = _basicWorld3X3.GetInitialSearchNode <GridSearchNode>();
            AStarMax       astar        = new AStarMax(initialState, new GoalOnLocation(_basicWorld3X3.Goal));

            Assert.IsNotNull(astar);
            astar.Run(Int32.MaxValue); //Prevent stoping by time, should stop only when goal found
            var maxGoal = astar.GetMaxGoal();

            Assert.AreEqual(8, maxGoal.g);
        }
        public void Run_NewAStarSnake()
        {
            World    w             = new World(4, 2);
            var      heuristicFunc = new SnakeLegalHeuristic();
            Snake    snake         = new Snake(w, 0, heuristicFunc);
            AStarMax astar         = new AStarMax(snake, new ImplicitGoal());

            astar.Run(100);
            var maxGoal = astar.GetMaxGoal();

            Assert.IsNotNull(maxGoal);
        }
        public void AStarGrid_BasicRun_BCCHeuristicsHBSD_Success()
        {
            GridSearchNode initialState = _basicWorldT1S0.GetInitialSearchNode <GridSearchNode>();
            AStarMax       astar        = new AStarMax(initialState, new HashedBasicSymmetryDetectionPrunning(), new GoalOnLocation(_basicWorldV1.Goal));

            Assert.IsNotNull(astar);
            var howEnded = astar.Run(Int32.MaxValue);
            var maxGoal  = astar.GetMaxGoal();

            Assert.IsNotNull(maxGoal);
            Assert.AreEqual(State.Ended, howEnded);
        }
        public void AStarGrid_BasicRunWithBCCHeuristics_Success()
        {
            GridSearchNode initialState = _basicWorldV1.GetInitialSearchNode <GridSearchNode>();
            AStarMax       astar        = new AStarMax(initialState, new GoalOnLocation(_basicWorldV1.Goal));

            Assert.IsNotNull(astar);
            var howEnded = astar.Run(Int32.MaxValue);
            var maxGoal  = astar.GetMaxGoal();

            Assert.IsNotNull(maxGoal);
            Assert.AreEqual(State.Ended, howEnded);
        }
        public void Basic_Hbsd_findPathAstar()
        {
            GridSearchNode  initialState   = _basicBlocked5X5World.GetInitialSearchNode <GridSearchNode>();
            IPrunningMethod prunningMethod = new HashedBasicSymmetryDetectionPrunning();
            Solver          solver         = new AStarMax(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_AStarGrid4X4WithBlocked16_StopsByMemoryConstraint()
        {
            GridSearchNode initialState = _basicWorld4X416.GetInitialSearchNode <GridSearchNode>();
            AStarMax       astar        = new AStarMax(initialState, new GoalOnLocation(_basicWorld4X416.Goal));

            Assert.IsNotNull(astar);
            var howEnded = astar.Run(Int32.MaxValue, 1); //One MB memory limit
            var maxGoal  = astar.GetMaxGoal();

            Assert.IsNull(maxGoal);
            Assert.AreEqual(State.StoppedByMemoryLimit, howEnded);
        }
示例#12
0
        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 Run_NewAStarBox()
        {
            World w             = new World(5, 2, 2);
            var   heuristicFunc = new SnakeNoneHeuristic();

            int[]        snakeHeads = new int[] { 0, 31 };
            BoxCartesian b          = new BoxCartesian(w, snakeHeads, new BoxNoneHeuristic(), heuristicFunc);
            AStarMax     astar      = new AStarMax(b, new ImplicitGoal());

            astar.Run(1);
            var maxGoal = astar.GetMaxGoal();

            Assert.IsNotNull(maxGoal);
        }
示例#15
0
 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);
     }
 }
示例#16
0
        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);
        }
示例#17
0
        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);
        }
示例#18
0
        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));
        }
示例#19
0
        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);
        }
示例#20
0
        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++;
            }
        }