public static void farthestInsertion(List<Edge> edgelist, List<node> nodelist, node startnode, Form1 f) { Thread thisThread = Thread.CurrentThread; int count = nodelist.Count(); Edge greatestEdge = edgelist[0]; List<node> markednodes = new List<node>(); List<Edge> Route = new List<Edge>(); f.route = Route; List<node> nodelistcopy = new List<node>(nodelist); f.secondThread = thisThread; f.marked = markednodes; markednodes.Add(startnode); nodelist.Remove(startnode); for (int i = 0; i < edgelist.Count(); i++) { if (edgelist[i].FromNode == startnode) { greatestEdge = edgelist[i]; break; } } for (int i = 0; i < edgelist.Count(); i++) { if (edgelist[i].FromNode == startnode) { if (edgelist[i].distance > greatestEdge.distance) greatestEdge = edgelist[i]; } } Route.Add(greatestEdge); Route.Add(new Edge(greatestEdge.ToNode, greatestEdge.FromNode)); markednodes.Add(greatestEdge.ToNode); nodelist.Remove(greatestEdge.ToNode); f.Invoke(f.addPanelDelegate); Edge NodetoEdgeStart; Edge NodetoEdgeEnd; Edge NodetoEdge2Start; Edge NodetoEdge2End; foreach (node item in nodelist) { NodetoEdgeStart = edgelist.Find(i => ((i.ToNode == item) && (i.FromNode == startnode))); float CrossProduct = Math.Abs((greatestEdge.xdif * NodetoEdgeStart.ydif) - (greatestEdge.ydif * NodetoEdgeStart.xdif)); item.ClosestEdgeDist = CrossProduct / greatestEdge.distance; item.ClosestEdge = greatestEdge; } node closestnode = nodelist[0]; foreach (node item in nodelist) { if (item.ClosestEdgeDist < closestnode.ClosestEdgeDist) { closestnode = item; } } markednodes.Add(closestnode); nodelist.Remove(closestnode); Route.Remove(Route[0]); Route.Insert(0, new Edge(closestnode, greatestEdge.ToNode)); Route.Insert(0, new Edge(greatestEdge.FromNode, closestnode)); f.Invoke(f.refresherDelegate); while (Route.Count() < count) { closestnode = nodelist[0]; List<node> nodelistcopy2 = new List<node>(nodelist); Edge edge2; foreach (node item in nodelistcopy2) { foreach (Edge edge in Route) { NodetoEdgeStart = edgelist.Find(i => ((i.ToNode == item) && (i.FromNode == edge.FromNode))); NodetoEdgeEnd = edgelist.Find(i => ((i.ToNode == edge.ToNode) && (i.FromNode == item))); float CrossProduct = Math.Abs((edge.xdif * NodetoEdgeStart.ydif) - (edge.ydif * NodetoEdgeStart.xdif)); float D = CrossProduct / edge.distance; float Dotproduct = edge.xdif * NodetoEdgeStart.xdif + edge.ydif * NodetoEdgeStart.ydif; float t = Dotproduct / (edge.distance * edge.distance); edge2 = Route.Find(i => (i.ToNode == edge.FromNode)); NodetoEdge2Start = edgelist.Find(i => ((i.ToNode == item) && (i.FromNode == edge2.FromNode))); NodetoEdge2End = edgelist.Find(i => ((i.ToNode == edge2.ToNode) && (i.FromNode == item))); float D2 = NodetoEdge2Start.distance + NodetoEdge2End.distance + edge.distance; float D1 = NodetoEdgeStart.distance + NodetoEdgeEnd.distance + edge2.distance; if ((t > 0) && (t < 1)) { if (!Route.Contains(item.ClosestEdge)) { item.ClosestEdgeDist = D; item.ClosestEdge = edge; } else if (D < item.ClosestEdgeDist) { item.ClosestEdgeDist = D; item.ClosestEdge = edge; } } else if (!Route.Contains(item.ClosestEdge)) { item.ClosestEdgeDist = NodetoEdgeStart.distance; item.ClosestEdge = edge; } if (NodetoEdgeStart.distance <= item.ClosestEdgeDist) { item.ClosestEdgeDist = NodetoEdgeStart.distance; if (D1 < D2) { item.ClosestEdge = edge; } else if (D1 > D2) item.ClosestEdge = edge2; else item.ClosestEdge = edge; } else { } } if (item.ClosestEdgeDist < closestnode.ClosestEdgeDist) { closestnode = item; } } markednodes.Add(closestnode); nodelist.Remove(closestnode); Route.Insert(Route.IndexOf(closestnode.ClosestEdge), new Edge(closestnode, closestnode.ClosestEdge.ToNode)); Route.Insert(Route.IndexOf(closestnode.ClosestEdge) - 1, new Edge(closestnode.ClosestEdge.FromNode, closestnode)); markednodes.Add(closestnode); nodelist.Remove(closestnode); Route.Remove(closestnode.ClosestEdge); f.Invoke(f.refresherDelegate); } }
public void bruteFindFastest(List<node> nodes, node startnode) { int[] nodeIDlist = new int[nodes.Count() - 1]; float[,] distancelist = this.distanceList(nodes); int numperms = this.FactNum(nodes.Count() - 1); int[] fastestpath = new int[nodes.Count()]; int[] currentpath = new int[nodes.Count()]; route fastestroute; route currentroute; fastestpath[0] = startnode.id; currentpath[0] = startnode.id; int count = 0; for (int i = 0; i < nodes.Count(); i++) { if (nodes[i].id != startnode.id) { nodeIDlist[count] = nodes[i].id; count++; } } nodeIDlist.CopyTo(fastestpath, 1); fastestroute = new route(distancelist, fastestpath); for (int i = 0; i < numperms - 1; i++) { nodeIDlist = NextPermutation(nodeIDlist); nodeIDlist.CopyTo(currentpath, 1); currentroute = new route(distancelist, currentpath); if (currentroute.netdistance < fastestroute.netdistance) { currentpath.CopyTo(fastestpath, 0); fastestroute = new route(distancelist, fastestpath); } } fastestroute.printPath(); Console.WriteLine("\nDistance: " + fastestroute.netdistance); Console.WriteLine("Traveling from and back to Node# " + startnode.id); }
public static float distance(node fromNode, node toNode) { return distance(fromNode.x, fromNode.y, toNode.x, toNode.y); }
private void button2_Click_1(object sender, EventArgs e) { int numNodes = int.Parse(maskedTextBox1.Text); maskedTextBox1.ReadOnly = true; nodelist = Driver.createRandomNodeList(numNodes); edgelist = Driver.createEdgeList(nodelist); startnode = nodelist[0]; this.count = nodelist.Count(); this.Controls.Add(panel1); panel1.Show(); button2.Hide(); this.Controls.Add(startButton); this.AcceptButton = startButton; }