public TimeSpan Generate(Network network/*, ref BackgroundWorker baw*/) { Stopwatch time = new Stopwatch(); time.Start(); try { nodeReferences.Add(network.nodeDict.First().Key); while (nodeReferences.Count != 0) { //List<Direction> directions = UtilityFunctions.RandomSortList<Direction>(_directions, ref _random); Node nodeRef = nodeReferences[nodeReferences.Count - 1]; nodeRef.visited = true; bool unvisitedNeighbourFound = false; List<NodeLink> srcList = new List<NodeLink>(nodeRef.LinkList); List<NodeLink> randList = new List<NodeLink>(nodeRef.LinkList.Count); while (srcList.Count > 0) { int index = _random.Next(0, srcList.Count); randList.Add(srcList[index]); srcList.RemoveAt(index); } foreach (NodeLink l in randList) { if(l != null && l.Other(nodeRef) != null && l.Other(nodeRef).visited == false) { l.visited = true; /*num++; // TODO: Move reporting of progress into if loop so that progress is reported only when the number of completed cells changes. decimal pp = (num / (Width * Height)) * 100; int percentProgress = (int)Math.Round(pp, 0, MidpointRounding.AwayFromZero); baw.ReportProgress(percentProgress); */ nodeReferences.Add(l.Other(nodeRef)); unvisitedNeighbourFound = true; break; } } if (!unvisitedNeighbourFound) { nodeReferences.RemoveAt(nodeReferences.Count - 1); } } } catch { } time.Stop(); return time.Elapsed; }
public TimeSpan Generate(Network network/*, ref BackgroundWorker baw*/) { Stopwatch time = new Stopwatch(); time.Start(); try { nodeReferences.Add(network.nodeDict.First().Key); while (nodeReferences.Count != 0) { //List<Direction> directions = UtilityFunctions.RandomSortList<Direction>(_directions, ref _random); Node nodeRef = nodeReferences[nodeReferences.Count - 1]; nodeRef.visited = true; bool unvisitedNeighbourFound = false; List<NodeLink> validLinks = new List<NodeLink>(nodeRef.LinkList.Count); float totalWeight = 0.0f; foreach (NodeLink l in nodeRef.LinkList) { if ( l != null // If the link object exists && l.Other(nodeRef) != null // and it links to a node && l.Other(nodeRef).visited == false) // and that node hasn't been visited { validLinks.Add(l); totalWeight += l.weight; } } if (validLinks.Count > 0) { NodeLink link = null; if (totalWeight == 0.0f) { // No weights exist in the list, so randomly pick an entry. int index = _random.Next(0, validLinks.Count); link = validLinks[index]; } else { int value = _random.Next(0, (int)(totalWeight * 1000.0f)); int index = 0; while (index < validLinks.Count) { link = validLinks[index++]; value -= (int)(link.weight * 1000.0f); if (value < 0) { break; } } } link.visited = true; nodeReferences.Add(link.Other(nodeRef)); unvisitedNeighbourFound = true; } if (!unvisitedNeighbourFound) { nodeReferences.RemoveAt(nodeReferences.Count - 1); } } } catch { } time.Stop(); return time.Elapsed; }
public TimeSpan Generate(Network network/*, ref BackgroundWorker baw*/) { Stopwatch time = new Stopwatch(); time.Start(); try { Node n = network.nodeDict.First().Key; VisitNode(n); while (weightedLinks.Count > 0 || unweightedLinks.Count > 0) { int linkIndex; NodeLink link; // Get a weighted link if (weightedLinks.Count > 0) // always pick a weighted link over an unweighted one { linkIndex = _random.Next(weightedLinks.Count); link = weightedLinks[linkIndex]; weightedLinks.RemoveAt(linkIndex); if (link.a.visited && link.b.visited) { // do not use link continue; } } else if (unweightedLinks.Count > 0) { linkIndex = _random.Next(unweightedLinks.Count); link = unweightedLinks[linkIndex]; unweightedLinks.RemoveAt(linkIndex); if (link.a.visited && link.b.visited) { // do not use link continue; } } else { break; } link.visited = true; if (link.a.visited == false) { VisitNode(link.a); link.a.visited = true; } else if (link.b.visited == false) { VisitNode(link.b); link.b.visited = true; } } } catch { } time.Stop(); return time.Elapsed; }