// Insert logic for processing found files here. public static void ProcessFile(string path) { MLInput.mapName = path; System.Console.WriteLine(path); StreamReader sr = new StreamReader(path); //System.Console.WriteLine("1"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); //System.Console.WriteLine("1"); Map map = null; MapLoad.loadMap(sr, out map); //System.Console.WriteLine("1"); Heuristic h = new BFS(map); Search search = new Search(h); Console.Error.WriteLine("Initialized after {0:0.000}", stopwatch.Elapsed.TotalSeconds); Map finalmap = solver(search, map, stopwatch); stopwatch.Stop(); //System.Console.WriteLine("1"); if (finalmap == null) { Console.Error.WriteLine("Frontier was emptied! No solution found. Explored: {0}", search.exploredSize()); } else { Console.Error.WriteLine("Finished!"); Console.Error.Write("Time: {0:0.000}\t Steps: {1}\t Explored: {2}\t Frontier: {3}\n\n", stopwatch.Elapsed.TotalSeconds, finalmap.steps, search.exploredSize() - search.frontierSize(), search.frontierSize()); MLInput mlin = new MLInput(map); mlin.run(finalmap.steps); /* * Map printmap = finalmap; * while (true) // Outputs posistions for debugging. * { * Node[] aBoxes = printmap.getAllBoxes(); * Actor[] actors = printmap.getActors(); * foreach (Actor actor in actors) * { * Console.Error.Write("Act{0} Pos: {1},{2}\n", actor.id, actor.x, actor.y); * } * int count = 1; * foreach (Node box in aBoxes) * { * Console.Error.Write("Box{0} Pos: {1},{2}\n", count, box.x, box.y); * count++; * } * Console.Error.Write("Steps: {0}\n\n", printmap.steps); * if (printmap.parent == null) { break; } * else { printmap = printmap.parent; } * }*/ LinkedList <act[]> actionlist = restoreactions(finalmap); foreach (act[] actiongroup in actionlist) { string line = "["; for (int i = 0; i < actiongroup.Count() - 1; i++) { line = line + actiongroup[i].ToString(); line = line + ", "; } line = line + actiongroup[actiongroup.Count() - 1].ToString(); line = line + "]"; // Console.Error.WriteLine(line); // Debug. //System.Console.WriteLine(line); } } }
public static Map solver(Search search, Map map, Stopwatch stopwatch) { search.addToFrontier(map); int i = -1; while (search.frontierSize() > 0) { i++; if (i == 10000) { Console.Error.Write("Time: {0:0.000}\t Explored: {1}\t Frontier: {2}\n", stopwatch.Elapsed.TotalSeconds, search.exploredSize() - search.frontierSize(), search.frontierSize()); i = 0; } Map smap = search.getFromFrontier(); //if (smap.isGoal()) { return smap; } Heuristic h2 = new BFS(smap); Search search2 = new Search(h2); //Console.Error.WriteLine("Initialized after {0:0.000}", stopwatch.Elapsed.TotalSeconds); Map finalmap = solver2(search2, smap, stopwatch); if (finalmap == null) { Console.Error.WriteLine("Frontier was emptied! No solution found. Explored: {0}", search.exploredSize()); return(null); } else { if (finalmap.steps > 0) { MLInput mlin = new MLInput(smap); mlin.run(finalmap.steps); } } //if (smap.isGoal()) { return smap; } HashSet <act>[] actionlist = smap.getAllActions(); IEnumerator <act>[] enumerators = new IEnumerator <act> [actionlist.Count()]; for (int j = 0; j < actionlist.Count(); j++) { enumerators[j] = actionlist[j].GetEnumerator(); enumerators[j].MoveNext(); } bool run = true; while (run) { act[] actions = new act[enumerators.Count()]; for (int j = 0; j < enumerators.Count(); j++) { actions[j] = enumerators[j].Current; } int k = 0; while (!enumerators[k].MoveNext()) { enumerators[k].Reset(); enumerators[k].MoveNext(); k++; if (k == enumerators.Count()) { run = false; break; } } Map nmap = new Map(smap); if (nmap.PerformActions(actions)) { //if (nmap.isGoal()) { return nmap; } search.addToFrontier(nmap); } } /*foreach (HashSet<act> actorlist in actionlist) * { * foreach (act action in actorlist) * { * * act[] actions = new act[1]; * actions[0] = action; * if (nmap.PerformActions(actions)) * { * if (nmap.isGoal()) { return nmap; } * search.addToFrontier(nmap); * } * } * }*/ } return(null); }