예제 #1
0
        public static SearchResults runPlanner(string problem, Heuristic h, bool useTwoQueues = false, int maxTimeMinutes = 10)
        {
            Problem p = new Problem(problem, false);

            p.DomainName = Path.GetFileName(Path.GetDirectoryName(problem));

            if (useTwoQueues)
            {
                MultiHeuristicAStarSearch engine = new MultiHeuristicAStarSearch(p, new List <IHeuristic>()
                {
                    h, new FFHeuristic(p)
                });
                engine.TimeLimitOfSearch = TimeSpan.FromMinutes(maxTimeMinutes);
                engine.Start();
                return(engine.GetSearchResults(false));
            }
            else
            {
                AStarSearch engine = new AStarSearch(p, h);
                engine.TimeLimitOfSearch = TimeSpan.FromMinutes(maxTimeMinutes);
                engine.Start();
                return(engine.GetSearchResults(false));
            }
        }
        public void TC_AStarSearch()
        {
            var sasProblem = new Planner.SAS.Problem(new SASInputData(GetFilePath("TC_Gripper.sas")));
            var sasSearch  = new AStarSearch(sasProblem, new BlindHeuristic(), new RegularBinaryHeap());

            sasSearch.Start();
            var sasSolution = sasSearch.GetSearchResults();

            Assert.AreEqual("A* Search", sasSolution.Algorithm);
            Assert.AreEqual("TC_Gripper", sasSolution.ProblemName);
            Assert.AreEqual("Blind Heuristic", sasSolution.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, sasSolution.ResultStatus);
            Assert.IsTrue(sasSolution.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(11, sasSolution.SolutionCost);
            Assert.IsNotNull(sasSolution.SolutionPlan);
            Assert.AreEqual(11, sasSolution.SolutionPlan.GetCost());

            var sasSearch2 = new AStarSearch(sasProblem, new BlindHeuristic(), new RegularBinaryHeap());

            sasSearch2.Start(SearchType.BackwardWithConditions);
            var sasSolution2 = sasSearch2.GetSearchResults();

            Assert.AreEqual("A* Search (Backward)", sasSolution2.Algorithm);
            Assert.AreEqual("TC_Gripper", sasSolution2.ProblemName);
            Assert.AreEqual("Blind Heuristic", sasSolution2.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, sasSolution2.ResultStatus);
            Assert.IsTrue(sasSolution2.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(11, sasSolution2.SolutionCost);
            Assert.IsNotNull(sasSolution2.SolutionPlan);
            Assert.AreEqual(11, sasSolution2.SolutionPlan.GetCost());

            var sasSearch3 = new AStarSearch(sasProblem, new BlindHeuristic(), new RegularBinaryHeap());

            sasSearch3.Start(SearchType.BackwardWithStates);
            var sasSolution3 = sasSearch3.GetSearchResults();

            Assert.AreEqual("A* Search (Backward)", sasSolution3.Algorithm);
            Assert.AreEqual("TC_Gripper", sasSolution3.ProblemName);
            Assert.AreEqual("Blind Heuristic", sasSolution3.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, sasSolution3.ResultStatus);
            Assert.IsTrue(sasSolution3.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(11, sasSolution3.SolutionCost);
            Assert.IsNotNull(sasSolution3.SolutionPlan);
            Assert.AreEqual(11, sasSolution3.SolutionPlan.GetCost());

            var pddlProblem = new Planner.PDDL.Problem(new PDDLInputData(GetFilePath("TC_Gripper_D.pddl"), GetFilePath("TC_Gripper_P.pddl")));
            var pddlSearch  = new AStarSearch(pddlProblem, new BlindHeuristic(), new RegularBinaryHeap());

            pddlSearch.Start();
            var pddlSolution = pddlSearch.GetSearchResults();

            Assert.AreEqual("A* Search", pddlSolution.Algorithm);
            Assert.AreEqual("gripper", pddlSolution.DomainName);
            Assert.AreEqual("problem-1", pddlSolution.ProblemName);
            Assert.AreEqual("Blind Heuristic", pddlSolution.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, pddlSolution.ResultStatus);
            Assert.IsTrue(pddlSolution.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(5, pddlSolution.SolutionCost);
            Assert.IsNotNull(pddlSolution.SolutionPlan);
            Assert.AreEqual(5, pddlSolution.SolutionPlan.GetCost());

            var pddlSearch2 = new AStarSearch(pddlProblem, new BlindHeuristic(), new RegularBinaryHeap());

            pddlSearch2.Start(SearchType.BackwardWithConditions);
            var pddlSolution2 = pddlSearch2.GetSearchResults();

            Assert.AreEqual("A* Search (Backward)", pddlSolution2.Algorithm);
            Assert.AreEqual("gripper", pddlSolution2.DomainName);
            Assert.AreEqual("problem-1", pddlSolution2.ProblemName);
            Assert.AreEqual("Blind Heuristic", pddlSolution2.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, pddlSolution2.ResultStatus);
            Assert.IsTrue(pddlSolution2.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(5, pddlSolution2.SolutionCost);
            Assert.IsNotNull(pddlSolution2.SolutionPlan);
            Assert.AreEqual(5, pddlSolution2.SolutionPlan.GetCost());

            var pddlSearch3 = new AStarSearch(pddlProblem, new BlindHeuristic(), new RegularBinaryHeap());

            pddlSearch3.Start(SearchType.BackwardWithStates);
            var pddlSolution3 = pddlSearch3.GetSearchResults();

            Assert.AreEqual("A* Search (Backward)", pddlSolution3.Algorithm);
            Assert.AreEqual("gripper", pddlSolution3.DomainName);
            Assert.AreEqual("problem-1", pddlSolution3.ProblemName);
            Assert.AreEqual("Blind Heuristic", pddlSolution3.Heuristic);
            Assert.AreEqual(ResultStatus.SolutionFound, pddlSolution3.ResultStatus);
            Assert.IsTrue(pddlSolution3.SearchTime.TotalMilliseconds > 0);
            Assert.AreEqual(5, pddlSolution3.SolutionCost);
            Assert.IsNotNull(pddlSolution3.SolutionPlan);
            Assert.AreEqual(5, pddlSolution3.SolutionPlan.GetCost());
        }
예제 #3
0
        public static void runPlanningExperiments(string domainsFolder, TimeSpan timeLimit, int param)
        {
            Logger logger = new Logger();
            List <SearchResults> allResults = new List <SearchResults>();

            //try
            //{

            //the number of computes that participate on this job. Computation is distributed among them.
            int numberOfComputes = 2;

            //if set to true, problem file that don't have histogram computed will be skipped. Otherwise all problems will be processed.
            bool onlyWhenHistogramExists = false;

            Problem     d;
            AStarSearch ast;
            //HillClimbingSearch ast;
            var directories = Directory.EnumerateDirectories(domainsFolder);

            allResults = new List <SearchResults>();
            foreach (var directory in directories)
            {
                var files = Directory.EnumerateFiles(directory).ToList();
                foreach (var SASFile in files)
                {
                    int indexOfFile = files.IndexOf(SASFile);
                    if (indexOfFile % numberOfComputes != param)                     //if (indexOfFile != param)
                    {
                        continue;
                    }
                    if (onlyWhenHistogramExists && !IsHistogramComputed(directory, SASFile))
                    {
                        continue;
                    }

                    logger.Log(" ----- new problem ----- ");
                    logger.Log(directory + "\\" + SASFile);

                    d = new Problem(SASFile, false);

                    Heuristic h = new FFHeuristic(d);

                    /*
                     * string histogramFolder = @"C:\Users\Ota\Documents\Visual Studio 2017\Projects\PADD\heuristicStats";
                     * var samples = HistogramVisualizer.Form1.getHistograms(histogramFolder);
                     * List<HistogramVisualizer.Histograms> selectedSamples = new List<HistogramVisualizer.Histograms>();
                     * foreach (var item in samples[Path.GetFileName(directory)].Values)
                     * {
                     *      selectedSamples.AddRange(item);
                     * }
                     */

                    //string trainedNetworkFile = Path.Combine("..", "trainedNetwork.bin");
                    //Heuristic h = new NNHeuristic(d, trainedNetworkFile);

                    //string dataFile = Path.Combine(directory, "histograms", "dataToLearn.tsv");
                    //Heuristic h = new FileBasedHeuristic(d, dataFile, false);

                    //Heuristic h = new FFHeuristic(d);
                    //Heuristic h = new RegHeuristic(new FFHeuristic(d));
                    //Heuristic h = new FileBasedHeuristic(d, dataFile, false);

                    //h = getHeuristicByParam(param, d);
                    //h = getHeuristicByParam(6, d);

                    ISearchHeap heapStructure = null;
                    //heapStructure = getHeapByParam(param);

                    //ISearchHeap heapStructure = new Heaps.MeasuredHeap();
                    heapStructure = new RedBlackTreeHeap();
                    //ISearchHeap heapStructure = new Heaps.FibonacciHeap1();
                    //ISearchHeap heapStructure = new Heaps.FibonacciHeap2();
                    //ISearchHeap heapStructure = new Heaps.RegularBinaryHeap();
                    //ISearchHeap heapStructure = new Heaps.RegularTernaryHeap();
                    //ISearchHeap heapStructure = new Heaps.BinomialHeap();
                    //ISearchHeap heapStructure = new Heaps.LeftistHeap();

                    ast = new AStarSearch(d, h, heapStructure);
                    //ast = new MultiHeuristicAStarSearch(d, h);
                    //ast = new MultiHeuristicAStarSearch(d, new List<Heuristic>() { h, hNN });
                    //ast = new IterativeDeepeningAStarSearch(d, null);

                    DirectoryInfo currentDirectory = new DirectoryInfo(directory);
                    FileInfo      currentFile      = new FileInfo(SASFile);

                    if (ast.OpenNodes is MeasuredHeap <IState> )
                    {
                        ((MeasuredHeap <IState>)ast.OpenNodes).SetLoggingOutputFile(currentDirectory.Name + "_" + currentFile.Name);
                    }

                    ast.TimeLimitOfSearch = timeLimit;
                    ast.Start();

                    var searchResult = ast.GetSearchResults(false);
                    searchResult.DomainName            = (Path.GetFileName(directory));
                    searchResult.ProblemName           = (Path.GetFileName(SASFile));
                    searchResult.Heuristic             = h.GetDescription();
                    searchResult.Algorithm             = ast.GetDescription() + "+" + heapStructure.GetName();
                    searchResult.BestHeuristicValue    = h.Statistics.BestHeuristicValue;
                    searchResult.AverageHeuristicValue = h.Statistics.AverageHeuristicValue;

                    //foreach (var item in ast.GetSolution().GetOperatorSeqIndices())
                    //    Console.Write(item + " ");
                    allResults.Add(searchResult);
                    if (ast.OpenNodes is MeasuredHeap <IState> )
                    {
                        ((MeasuredHeap <IState>)ast.OpenNodes).ClearStats();
                    }
                    logger.Log();
                }
                logger.Log(" ----- new domain ----- ");
            }


            //			catch (Exception e)
            //			{
            //			    using (var writer = new System.IO.StreamWriter("results" + Environment.MachineName + ".txt"))
            //			        foreach (var item in allResults)
            //			        {
            //			            writer.WriteLine(item.ToString());
            //			        }
            //}
            using (var writer = new System.IO.StreamWriter("results" + Environment.MachineName + ".txt"))
                foreach (var item in allResults)
                {
                    writer.WriteLine(item.ToString());
                }
        }