public void TC_SearchResults() { var problem = new Planner.SAS.Problem(new SASInputData(GetFilePath("TC_SearchResults.sas"))); var heuristic = new PDBHeuristic(problem); var problem2 = new Planner.SAS.Problem(new SASInputData(GetFilePath("TC_SearchResults2.sas"))); var heuristic2 = new PDBHeuristic(problem2); var heap = new RegularBinaryHeap(); var search1 = new AStarSearch(problem, heuristic, heap); var solution1 = search1.Start(); Assert.AreEqual(ResultStatus.SolutionFound, solution1); Assert.IsNotNull(search1.GetSolutionPlan()); var search2 = new AStarSearch(problem2, heuristic2, heap); var solution2 = search2.Start(); Assert.AreEqual(ResultStatus.NoSolutionFound, solution2); Assert.IsNull(search2.GetSolutionPlan()); var search3 = new AStarSearch(problem, heuristic, heap, false, new TimeSpan(0), 5000); var solution3 = search3.Start(); Assert.AreEqual(ResultStatus.TimeLimitExceeded, solution3); Assert.IsNull(search3.GetSolutionPlan()); var search4 = new AStarSearch(problem, heuristic, heap, false, new TimeSpan(0, 1, 0), 0); var solution4 = search4.Start(); Assert.AreEqual(ResultStatus.MemoryLimitExceeded, solution4); Assert.IsNull(search4.GetSolutionPlan()); }
static void testPatterns(bool[] isSelected, int selectedCount, int position, int limit, Problem d, System.IO.StreamWriter writer) { if (selectedCount == limit) { if (TestResult.currentID < TestResult.IDStart) { TestResult.currentID++; return; } HashSet <int> pattern = new HashSet <int>(); bool intersectsWithGoal = false; for (int i = 0; i < isSelected.Length; i++) { if (isSelected[i]) { pattern.Add(i); if (d.GoalConditions.IsVariableConstrained(i)) { intersectsWithGoal = true; } } } if (!intersectsWithGoal) { TestResult.currentID++; return; } DateTime buildingStarted = DateTime.Now; PDBHeuristic h = new PDBHeuristic(d, true, new List <HashSet <int> > { pattern }); DateTime buildingEnded = DateTime.Now; AStarSearch ast = new AStarSearch(d, h); DateTime searchStarted = DateTime.Now; ast.Start(); DateTime searchEnded = DateTime.Now; writer.WriteLine(TestResult.currentID + "\t" + pattern.Count + "\t" + String.Format("{0:0.##}", (buildingEnded - buildingStarted).TotalSeconds) + "\t" + String.Format("{0:0.##}", (searchEnded - searchStarted).TotalSeconds) + "\t" + h.Statistics.HeuristicCallsCount); //res.Add(new TestResult(d, pattern, (buildingEnded - buildingStarted).TotalSeconds, (searchEnded - searchStarted).TotalSeconds, nodes)); TestResult.currentID++; return; } if (selectedCount < limit - (isSelected.Length - position)) { return; } if (position >= isSelected.Length) { return; } isSelected[position] = true; testPatterns(isSelected, selectedCount + 1, position + 1, limit, d, writer); isSelected[position] = false; testPatterns(isSelected, selectedCount, position + 1, limit, d, writer); }
private static double Search(IProblem problem) { var searchEngine = new AStarSearch(problem, new StripsHeuristic(problem), new RegularBinaryHeap()); searchEngine.Start(); return(searchEngine.GetSolutionCost()); }
public static void DeleteTooEasyTasks(string domainsFolder, TimeSpan timeLimit) { Logger logger = new Logger(); Problem d; AStarSearch ast; //HillClimbingSearch ast; var directories = Directory.EnumerateDirectories(domainsFolder); foreach (var directory in directories) { bool isSomethingSolvedInThisDirectory = false, previousFileNotSolved = false, removingAllFiles = false; var files = Directory.EnumerateFiles(directory); foreach (var item in files) { logger.Log(" ----- new problem ----- "); logger.Log(directory + "\\" + item); if (removingAllFiles) { logger.Log("deleting file " + item); File.Delete(item); continue; } d = new Problem(item, false); //ast = new AStarSearch(d, new FFHeuristic(d)); ast = new AStarSearch(d, new FFHeuristic(d), new RedBlackTreeHeap()); ast.TimeLimitOfSearch = timeLimit; var searchResult = ast.Start(); if (searchResult == ResultStatus.SolutionFound || searchResult == ResultStatus.NoSolutionFound) { //task solved. it will be removed logger.Log("deleting file " + item); File.Delete(item); if (previousFileNotSolved) { //if two successive tasks are not solved within the timelimit, we assume the following tasks wont be solved either and remove them all //removingAllFiles = true; } previousFileNotSolved = true; } else { isSomethingSolvedInThisDirectory = true; previousFileNotSolved = false; } logger.Log(); } if (!isSomethingSolvedInThisDirectory) { //no task has been solved - removing empty folder logger.Log("deleting directory " + directory); Directory.Delete(directory); } logger.Log(" ----- new domain ----- "); } }
static void runHeapsTests(string domainsFolder, List <ISearchHeap> dataStrucutures, TimeSpan timeLimit) { Logger logger = new Logger(); string heapsResultsFolder = @".\..\tests\heapsResults"; Problem d; AStarSearch ast; //HillClimbingSearch ast; foreach (var ds in dataStrucutures) { if (!Directory.Exists(heapsResultsFolder)) { Directory.CreateDirectory(heapsResultsFolder); } using (var writer = new StreamWriter(heapsResultsFolder + "\\" + ds.GetName() + ".txt")) { var directories = Directory.EnumerateDirectories(domainsFolder); foreach (var directory in directories) { var files = Directory.EnumerateFiles(directory); foreach (var item in files) { logger.Log(" ----- new problem ----- "); logger.Log(directory + "\\" + item); d = new Problem(item, false); //ast = new AStarSearch(d, new FFHeuristic(d)); ast = new AStarSearch(d, new FFHeuristic(d), ds); DirectoryInfo currentDirectory = new DirectoryInfo(directory); FileInfo currentFile = new FileInfo(item); ds.Clear(); if (ast.OpenNodes is MeasuredHeap <PAD.Planner.IState> ) { ((MeasuredHeap <PAD.Planner.IState>)ast.OpenNodes).SetLoggingOutputFile(currentDirectory.Name + "_" + currentFile.Name); } ast.TimeLimitOfSearch = timeLimit; var result = ast.Start(); writer.WriteLine(currentDirectory.Name + "_" + currentFile.Name + "\t" + ast.GetSearchTime().TotalSeconds + "\t" + result); writer.Flush(); logger.Log(); } logger.Log(" ----- new domain ----- "); } } } }
static void runHeapsTestsOLD(string domainsFolder, List <ISearchHeap> dataStrucutures, TimeSpan timeLimit) { Logger logger = new Logger(); List <List <string> > results = new List <List <string> >(); results.Add(new List <string>()); results[0].Add(""); foreach (var directory in Directory.EnumerateDirectories(domainsFolder)) { foreach (var file in Directory.EnumerateFiles(directory)) { results[0].Add(file); } } foreach (var ds in dataStrucutures) { results.Add(new List <string>()); results[results.Count - 1].Add(ds.GetName()); Problem d; AStarSearch ast; //HillClimbingSearch ast; foreach (var directory in Directory.EnumerateDirectories(domainsFolder)) { foreach (var item in Directory.EnumerateFiles(directory)) { logger.Log(" ----- new problem ----- "); ds.Clear(); d = new Problem(item, false); //ast = new AStarSearch(d, new FFHeuristic(d)); ast = new AStarSearch(d, new FFHeuristic(d), ds); ast.Start(); logger.Log(); results[results.Count - 1].Add(ast.GetSearchTime().TotalSeconds.ToString()); } logger.Log(" ----- new domain ----- "); } } foreach (var row in results) { foreach (var item in row) { Console.Write(item + "\t"); } logger.Log(); } }
public IEnumerable <(Problem planningProblem, IState state, double trueGoalDistance, List <double> heuristicValues)> enumerateStatesWithDistances(int repeats = 10) { for (int i = 0; i < repeats; i++) { Console.WriteLine("Heuristic multiplier: " + h.multiplier); problem.SetInitialState(originalInitialState); h.perfectDistances.Clear(); astar = new AStarSearch(this.problem, h, null); astar.TimeLimitOfSearch = TimeSpan.FromHours(5); astar.Start(); foreach (var item in h.perfectDistances) { yield return(problem, (IState)item.Key, item.Value.realVal, item.Value.heuristicsVals); } Console.WriteLine(); } }
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()); }
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()); } }