void copy() { for (int i = 0; i < listOfAllNodes.Count; i++) { cNode pnn = new cNode(); pnn.c = listOfAllNodes[i].c; pnn.r = listOfAllNodes[i].r; pnn.direct = listOfAllNodes[i].direct; for (int z = 0; z < listOfAllNodes[i].connectedTo.Count; z++) { cNode pnn2 = new cNode(); } } }
//Greedy Search public void SearchGraph(cNode root, ref string path, ref bool foundGoal) { //MessageBox.Show("" + root.name); if (root == null) { return; } root.isVisted = true; //path += root.name + "=>"; root.connectedTo.Sort((x, y) => x.heurScore.CompareTo(y.heurScore)); foreach (var node in root.connectedTo) { //MessageBox.Show(node.name); //node.path += root.path + root.name; node.paths.AddRange(root.paths); node.paths.Add(root.name); //MessageBox.Show("" + node.name + ":" + node.path); nodeQ.Enqueue(node); } nodeQ = new Queue <cNode>(nodeQ.OrderBy(q => q.heurScore)); nodeQ = new Queue <cNode>(nodeQ.Distinct()); /*foreach (var id in nodeQ) * { * MessageBox.Show("q:" + id.name); * }*/ cNode n = nodeQ.Dequeue(); if (n.heurScore == 0) { foundGoal = true; Path.AddRange(n.paths); Path.Add(n.name);; return; } SearchGraph(n, ref path, ref foundGoal); }
public void dataentery(List <cNode> listOfNeededNodes, List <string> Open, List <cNode> listOfAllNodes) { for (int r = 0; r < Open.Count; r++) { for (int i = 0; i < listOfAllNodes.Count; i++) { if (listOfAllNodes[i].name == Open[r] && listOfAllNodes[i].isVisted == false) { cNode pnn = new cNode(); pnn.connectedTo = listOfAllNodes[i].connectedTo; pnn.direct = listOfAllNodes[i].direct; pnn.isVisted = listOfAllNodes[i].isVisted; pnn.name = listOfAllNodes[i].name; pnn.r = listOfAllNodes[i].r; pnn.c = listOfAllNodes[i].c; pnn.path = listOfAllNodes[i].path; listOfNeededNodes.Add(pnn); } } } }
void hardCodedInput() { ///list of all nodes with heur //string[] inputLines = { "a 1", "b 2", "c 3", "d 4", "e 5", "f 6", "g 7", "h 8", "i 9", "j 10", "k 11", "l 11" , "m 12", "n 13", "o 14", "p 15", "q 16", "r 17", "s 18", "t 19", "u 20", "v 21", "w 22", "x 23", "y 24", "z 25", "a2 26", "b2 27", "c2 28", "d2 29", "f2 30", "g2 31", "h2 32", "i2 33", "j2 34", "k2 35" , "l2 36" ,"m2 37"};//length=38 string[] inputLines = { "a 1", "b 2", "c 3", "d 4", "e 5", "f 6", "g 7", "h 8", "i 9", "j 10", "k 11", "l 11", "m 12", "n 13", "o 14", "p 15", "q 16", "r 17", "s 18", "t 19", "u 20", "v 21", "w 22", "x 23", "y 24", "z 25", "a2 26", "b2 27", "c2 28", "d2 29", "f2 30", "g2 31", "h2 32", "i2 0", "j2 34", "k2 5", "l2 36", "m2 37" };//length=38 //Heuristic Cost for (int i = 0; i < inputLines.Length; i++) { string[] words = inputLines[i].Split(' '); cNode n = new cNode(); cNode pnn = new cNode(); n.name = words[0]; n.heurScore = int.Parse(words[1]); pnn.name = words[0]; pnn.heurScore = int.Parse(words[1]); for (int r = 0; r < dataGridView1.Rows.Count; r++) { for (int c = 0; c < dataGridView1.Rows[r].Cells.Count; c++) { if (dataGridView1.Rows[r].Cells[c].Value != null && dataGridView1.Rows[r].Cells[c].Value.ToString() == words[0].ToString()) { ct++; n.r = r; n.c = c; pnn.r = r; pnn.c = c; //Text = ct + ""; } } } listOfAllNodes.Add(n); original.Add(pnn); //MessageBox.Show(n.name); } // //// ///NODE CONNECTEDTO DIRECT //string[] inputLines2 = { "a l2 r", "a b d", "l2 m2 d", "m2 e l", "b f r", "f g d", "g i r", "i h u", "h l r", "g c l", "c d d", "d n r", "n m u", "m q r", "q r d", "q p u", "p o u", "o k l", "k j u", "j s r", "s t d", "t x r", "x y d", "y a2 r", "a2 z u", "x w u", "j2 h2 d", "h2 d2 l", "d2 f2 d", "f2 j2 r", "j2 i2 u", "p u r", "u v d", "v b2 r", "b2 f2 r", "b2 c2 d", "c2 k2 r", "r c2 r" }; string[] inputLines2 = { "a l2 r", "a b d", "l2 m2 d", "m2 e l", "b f r", "f g d", "g i r", "i h u", "h l r", "g c l", "c d d", "d n r", "n m u", "m q r", "q r d", "q p u", "p o u", "o k l", "k j u", "j s r", "s t d", "t x r", "x y d", "y a2 r", "a2 z u", "x w u", "j2 h2 d", "h2 d2 l", "d2 f2 d", "f2 j2 r", "j2 i2 u", "p u r", "u v d", "v b2 r", "b2 f2 r", "b2 c2 d", "c2 k2 r", "r c2 r" }; //Graph (ConnectedTo) for (int i = 0; i < inputLines2.Length; i++) { string[] words = inputLines2[i].Split(' '); cNode n1 = listOfAllNodes.Find(x => x.name == words[0]); cNode n2 = listOfAllNodes.Find(x => x.name == words[1]); cNode pnn1 = original.Find(x => x.name == words[0]); cNode pnn2 = original.Find(x => x.name == words[1]); n1.direct.Add(words[2]); n1.connectedTo.Add(n2); pnn1.direct.Add(words[2]); pnn1.connectedTo.Add(pnn2); //MessageBox.Show(""+n1.name +"/"+n2.name); } }