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); }
public static void TestAdmissibility(int run, string runType, int nnIndex, double?confLevel) { string nnFileName = string.Format(Global.NNFN, nnIndex); string outputFileName = string.Format(Global.TESTADMISFN, confLevel == null ? "" : confLevel.ToString()); string nnLoadPath = string.Format(Global.RUNPATH, run, runType, nnFileName); string csvWritePath = string.Format(Global.RUNPATH, run, runType, outputFileName); CSVWriter csvWriter = new CSVWriter(csvWritePath, ','); MyNN nnSolve = MyNN.Load(nnLoadPath); TestDomainData[] data = ReadTestDomainData(Global.TESTTASKPATH); IHeuristic heuristic = new NNPreTrainedHeuristic(nnSolve, representationSolve, confLevel, true); SolveTestData(heuristic, data, csvWriter, null); double sumExpanded = 0; double sumGenerated = 0; double sumOptimal = 0; double sumCost = 0; double sumSubOpt = 0; for (int i = 0; i < data.Length; i++) { TestDomainData testDomainData = data[i]; sumExpanded += testDomainData.SolvedExpanded; sumGenerated += testDomainData.SolvedGenerated; sumOptimal += testDomainData.OptimalCost; sumCost += testDomainData.SolvedCost; sumSubOpt += testDomainData.SolvedCost / (double)testDomainData.OptimalCost; } Console.WriteLine("-----"); Console.WriteLine(sumExpanded / data.Length); Console.WriteLine(sumGenerated / data.Length); Console.WriteLine(sumOptimal / data.Length); Console.WriteLine(sumCost / data.Length); Console.WriteLine((sumSubOpt / data.Length) - 1); csvWriter.Write(); }
public static void TestEfficiency(int run, string runType, int nnIndex, TestDomainData[][] testData) { string csvWritePath = string.Format(Global.RUNPATH, run, runType, Global.TESTEFFICFN); string csvSummWritePath = string.Format(Global.RUNPATH, run, runType, Global.TESTEFFICFNSUMM); CSVWriter csvWriter = new CSVWriter(csvWritePath, ','); CSVWriter csvSummWriter = new CSVWriter(csvSummWritePath, ','); int timeout = 60000; string nnFileName = string.Format(Global.NNFN, nnIndex); string nnLoadPath = string.Format(Global.RUNPATH, run, runType, nnFileName); MyNN nnSolve = MyNN.Load(nnLoadPath); for (int i = 0; i < testData.GetLength(0); i++) { csvWriter.Add(i.ToString()); csvWriter.EndLine(); csvWriter.Write(); csvWriter.Clear(); IHeuristic heuristic = new NNPreTrainedHeuristic(nnSolve, representationSolve, null, true); int numSolved = SolveTestData(heuristic, testData[i], csvWriter, timeout); csvSummWriter.Add(i.ToString()); csvSummWriter.Add(numSolved.ToString()); csvSummWriter.EndLine(); csvSummWriter.Write(); csvSummWriter.Clear(); } }
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(); }