public Queue <string> search(int[,] initstate, int[,] desiredstate) { Modelisation.Node rootn = new Modelisation.Node( initstate, new[] { 0, 0 }, 0, 0, false, "root" ); Modelisation.Node goaln = new Modelisation.Node( desiredstate, new[] { 0, 0 }, 0, 0, false, "goal" ); tree_fs.Clear(); tree_fg.Clear(); tree_fs.Add(rootn); tree_fg.Add(goaln); int borderindex = 0; while (true) { Dictionary <string, Modelisation.Node> s_successors = problem.succession(tree_fs[borderindex]); Dictionary <string, Modelisation.Node> g_successors = problem.retrosuccession(tree_fg[borderindex]); foreach (KeyValuePair <string, Modelisation.Node> entry in s_successors) { Modelisation.Node[] x = problem.isPresent(entry.Value, tree_fg.GetRange(0, borderindex)); if (x != null) { return(generateTasklist(x[0], x[1])); } tree_fs.Add(entry.Value); } foreach (KeyValuePair <string, Modelisation.Node> entry in g_successors) { Modelisation.Node[] x = problem.isPresent(entry.Value, tree_fs.GetRange(0, borderindex)); if (x != null) { return(generateTasklist(x[1], x[0])); } tree_fg.Add(entry.Value); } borderindex++; } }
public Modelisation.Node[] isPresent(Modelisation.Node node, List <Modelisation.Node> visited) { foreach (Modelisation.Node n in visited) { if (node.getSignature() == n.getSignature()) { return(new[] { node, n }); } } return(null); }
public Dictionary <string, Modelisation.Node> retrosuccession(Modelisation.Node currentNode) { Dictionary <string, Modelisation.Node> newStates = new Dictionary <string, Modelisation.Node>(); Floor testingFloor = new Floor(currentNode.getState(), currentNode.getPathcost()); if (currentNode.getLastAction() == "nothing") { Modelisation.Node newnode = new Modelisation.Node( testingFloor.getState(), testingFloor.getAspXY(), currentNode.getDepth() + 1, //currentNode.getPathcost() + entry.Value.getCost(), testingFloor.account(), false, "nothing", currentNode ); newStates.Add("nothing", newnode); return(newStates); } foreach (KeyValuePair <string, Action> entry in actions) { entry.Value.reverse(testingFloor, testingFloor.getAspXY()); if (isArrayEqual(testingFloor.getState(), currentNode.getState()) && entry.Key != "nothing") { testingFloor.reset(); continue; } Modelisation.Node newnode = new Modelisation.Node( testingFloor.getState(), testingFloor.getAspXY(), currentNode.getDepth() + 1, //currentNode.getPathcost() + entry.Value.getCost(), testingFloor.account(), false, entry.Key, currentNode ); newStates.Add(entry.Key, newnode); testingFloor.reset(); } return(newStates); }
// Generate tasklist from a node list public Queue <string> generatetasklist(List <Modelisation.Node> l) { Queue <string> tasklist = new Queue <string>(); if (l.Count != 0) { Modelisation.Node node = l[l.Count - 1]; // While the node is not the root node, we add its lastaction to the tasklist while (node.getLastAction() != "nothing") { tasklist.Enqueue(node.getLastAction()); node = node.getParent(); } //tasklist.Enqueue(node.getLastAction()); } return(tasklist); }
private Queue <string> generateTasklist(Modelisation.Node ns, Modelisation.Node ng) { List <string> actions = new List <string>(); while (ns.getLastAction() != "root") { actions.Insert(0, ns.getLastAction()); ns = ns.getParent(); } while (ng.getLastAction() != "goal") { actions.Add(ng.getLastAction()); ng = ng.getParent(); } Queue <string> queue = new Queue <string>(actions); return(queue); }
// Method search public Queue <string> searchInforme(int[,] initstate, List <int[, ]> desireStates, int[] vacXY) { // First check if initstate is one of our desireStates //* foreach (int[,] d in desireStates) { // If one of them is equal, then no need to search a tasklist if (isArrayEqual(initstate, d)) { // Stop method Queue <string> q = new Queue <string>(); return(q); } } //*/ // Clear tree tree.Clear(); tasklist.Clear(); // Create a root node with initial state Modelisation.Node root = new Modelisation.Node( initstate, // initial state vacXY, // vacXY 0, // depth 100, // heuristic false, // visited "nothing" // lastaction ); tree.Add(root); // Represent index of exploration : here it's Breadth first search //int borderindex = 0; List <Modelisation.Node> succ = new List <Modelisation.Node>(); bool b = true; while (b) { // We check if the last node state of tree is on dirt or jewel //Floor f = new Floor(tree[borderindex].getState()); int d = isJewelDirt(tree[tree.Count - 1].getState(), tree[tree.Count - 1].getVacXY()); // If the vaccum is on jewel or dirt if (d != 0) { switch (d) { case 3: tasklist.Enqueue("pickup"); return(tasklist); case 5: tasklist.Enqueue("clean"); return(tasklist); case 7: tasklist.Enqueue("pickup"); tasklist.Enqueue("clean"); return(tasklist); default: b = false; break; } } // If not else { tree = problem.successionInforme(tree[tree.Count - 1], tree); // Get successor } tasklist = generatetasklist(tree); } return(tasklist); }