static TestDomainData[] GenTest(IHeuristic heuristic, int numTasks = 100, int numSteps = Global.MAXSTEPS, bool writeCSV = true, bool computeCost = true) { TestDomainData[] data = new TestDomainData[numTasks]; string csvWritePath = string.Format(Global.ROOTPATH, Global.TESTTASKFN); CSVWriter csv = new CSVWriter(csvWritePath, ','); for (int i = 0; i < numTasks; i++) { DomainContainer domainContainer = new DomainContainer(heuristic, numSteps); ISearchDomain domain = domainContainer.Domain; IState initState = domain.Initial(); string initStateStr = string.Join(" ", initState.Arr); int cost = 0; long generated = 0; if (computeCost == true) { if (Global.DOMAINTYPE == typeof(BlocksWorld.BlocksWorld) && heuristic == null) { cost = PostBlocksWorld(initState, domain.Goal()); } else { IDAStar <ISearchDomain> planner = new IDAStar <ISearchDomain>(domain); List <IState> plan = planner.Search(initState); cost = (plan.Count - 1); generated = planner.Generated; } } data[i] = new TestDomainData(initState.Arr, cost, generated); csv.Add(initStateStr); csv.Add(cost.ToString()); csv.Add(generated.ToString()); csv.EndLine(); } if (writeCSV == true) { csv.Write(); } return(data); }
static int SolveTestData(IHeuristic heuristic, TestDomainData[] data, CSVWriter csvWriter, int?timeout) { Stopwatch sw = new Stopwatch(); sw.Start(); int countSolved = 0; for (int i = 0; i < data.Length; i++) { Console.WriteLine("iter: " + i.ToString()); if (timeout != null && sw.ElapsedMilliseconds > timeout) { break; } heuristic.ClearCache(); GC.Collect(); TestDomainData testDomainData = data[i]; DomainContainer domainContainer = new DomainContainer(heuristic, testDomainData.Init); ISearchDomain domain = domainContainer.Domain; IDAStar <ISearchDomain> planner = new IDAStar <ISearchDomain>(domain); List <IState> plan = planner.Search(domain.Initial(), timeout); testDomainData.SolvedCost = plan.Count - 1; testDomainData.SolvedExpanded = planner.Expanded; testDomainData.SolvedGenerated = planner.Generated; if (testDomainData.SolvedCost > 0) { csvWriter.Add(planner.Expanded.ToString()); csvWriter.Add(planner.Generated.ToString()); csvWriter.Add(testDomainData.OptimalCost.ToString()); csvWriter.Add(testDomainData.SolvedCost.ToString()); csvWriter.Add(planner.SwopWatch.ElapsedMilliseconds.ToString()); csvWriter.EndLine(); csvWriter.Write(); csvWriter.Clear(); countSolved++; } Console.WriteLine("cost: " + (plan.Count - 1).ToString()); Console.WriteLine("generated: " + planner.Generated); Console.WriteLine("elapsed: " + planner.SwopWatch.ElapsedMilliseconds); } return(countSolved); }
public static void Train(int run, int iter, int numOutputSolve, bool genDomainFromUncert, int lengthInc, bool effRun) { string runType = GetRunType(numOutputSolve, genDomainFromUncert, lengthInc, effRun); string csvWritePath = string.Format(Global.RUNPATH, run, runType, Global.TRAINFN); int numHiddenSolve = 20; if (multHeuristic != null) { numHiddenSolve = 8; } int?numHiddenUncert = numHiddenSolve; if (genDomainFromUncert == false) { numHiddenUncert = null; } float dropout = 0F; if (multHeuristic == null && numOutputSolve > 1) { dropout = 0.025F; } NNBayesHeuristic heuristic = new NNBayesHeuristic(representationSolve, representationUncert, numHiddenSolve, numOutputSolve, numHiddenUncert, dropout, true); heuristic.ConfLevel = null; if (numOutputSolve > 1) { heuristic.ConfLevel = 0.01; } double incConfLevel = 0.05; double percSolvedThresh = 0.6; int numTasks = 10; int tMax; if (effRun == true) { tMax = 1000; } else if (multHeuristic == null) { tMax = 60000; } else { tMax = 5 * 60000; } CSVWriter csv = new CSVWriter(csvWritePath, ','); Stopwatch sw = new Stopwatch(); sw.Start(); int length = lengthInc; for (int n = 0; n < iter; n++) { List <List <IState> > plans = new List <List <IState> >(numTasks); int countSolved = 0; for (int i = 0; i < numTasks; i++) { heuristic.ClearCache(); DomainContainer domainContainer; if (genDomainFromUncert == true) { if (heuristic.IsTrained) { domainContainer = new DomainContainer(heuristic, Global.UNCERTTHRESH, Global.MAXSTEPS); } else { domainContainer = new DomainContainer(heuristic, 1); } } else { domainContainer = new DomainContainer(heuristic, length); } ISearchDomain domain = domainContainer.Domain; heuristic.ClearCache(); GC.Collect(); IDAStar <ISearchDomain> planner = new IDAStar <ISearchDomain>(domain); List <IState> plan = planner.Search(domain.Initial(), tMax); Console.WriteLine("expanded: " + planner.Expanded); Console.WriteLine("generated: " + planner.Generated); Console.WriteLine("elapsed: " + planner.SwopWatch.ElapsedMilliseconds); if (plan.Count > 0) { countSolved++; Console.WriteLine("solved solution length:" + plan.Count); plans.Add(plan); } csv.Add(n.ToString()); csv.Add(planner.SwopWatch.ElapsedMilliseconds.ToString()); csv.Add(planner.Expanded.ToString()); csv.Add(planner.Generated.ToString()); csv.Add(heuristic.ConfLevel == null ? "NULL" : heuristic.ConfLevel.ToString()); csv.Add(plan.Count.ToString()); csv.Add(domain.NumSteps.ToString()); csv.EndLine(); } string nnFileName = string.Format(Global.NNFN, n); string nnSavePath = string.Format(Global.RUNPATH, run, runType, nnFileName); heuristic.NNSolve.Save(nnSavePath, true); double percSolved = countSolved / (double)numTasks; if (percSolved < percSolvedThresh) { heuristic.UpdateBeta = false; if (heuristic.ConfLevel != null && heuristic.ConfLevel < 0.5) { heuristic.ConfLevel += incConfLevel; if (heuristic.ConfLevel > 0.5) { heuristic.ConfLevel = 0.5; } } } else { heuristic.UpdateBeta = true; } if (plans.Count > 0) { double avg = plans.Select(x => x.Count).Average(); double max = plans.Select(x => x.Count).Max(); double min = plans.Select(x => x.Count).Min(); Console.WriteLine("solved: " + plans.Count + " avg: " + avg + " max: " + max + " min: " + min); Console.WriteLine("current conf level: " + heuristic.ConfLevel == null ? "NULL" : heuristic.ConfLevel.ToString()); } long t = sw.ElapsedMilliseconds; length += lengthInc; csv.Add(t.ToString()); csv.EndLine(); csv.Write(); csv.Clear(); heuristic.Update(plans.ToArray()); } heuristic.Dispose(); }