public DagEdge(DagNode prevNode, DagNode nextNode, int commTime) { PrevNode = prevNode; NextNode = nextNode; CommTime = commTime; }
public Dag Generate() { Dag graph = new Dag(GetInfo()); Random r = new Random(); NumberOfLayers = r.Next(MaxLayers - MinLayers) + MinLayers; graph.GraphNodes = new List <DagNode> { new DagNode(0, 0, 0) }; for (int i = 1; i < Nodes + 1; i++) { int layer = r.Next(NumberOfLayers) + 1; //ensure we have atleast one node on each layer if (i < NumberOfLayers) { layer = i; } graph.GraphNodes.Add(new DagNode(i, r.Next(MaxComm - MinComm) + MinComm, layer)); } foreach (DagNode n in graph.GraphNodes) { foreach (DagNode nn in graph.GraphNodes) { if (n.Id == nn.Id) { continue; } //if first layer add connection to initial node if (n.LayerNumber == 0 && nn.LayerNumber == 1) { int communication = r.Next(MaxComp - MinComp) + MinComp; nn.Dependencies.Add(n.Id, communication); graph.InsertEdge(n, nn, communication); continue; } if (n.LayerNumber + 1 != nn.LayerNumber) { continue; } if (nn.PrevEdges.Count >= MaxEdge) { continue; } if (r.NextDouble() > Probability) { int comTime = r.Next(MinComp, MaxComp); nn.Dependencies.Add(n.Id, comTime); graph.InsertEdge(n, nn, comTime); } } } //add the end node /* * whilst we do not render the last node as vis.js renders it funny * we need to add it so that we can calculate the critical path from the start node * to the end node */ int layerz = NumberOfLayers + 1; graph.GraphNodes.Add(new DagNode(Nodes + 1, r.Next(MaxComm - MinComp) + MinComp, layerz)); //match all nodes without nextEdges to the final node foreach (DagNode n in graph.GraphNodes) { if (!n.NextEdges.Any() && n.Id != graph.GraphNodes.Last().Id) { graph.InsertEdge(n, graph.GraphNodes.Last(), 0); } } //after ensure that all nodes have ateast one incoming edge after layer 0 //todo add a loop that will go over and add more than 1 but less than three incoming edges. foreach (DagNode n in graph.GraphNodes) { if (n.PrevEdges.Count != 0 || n.LayerNumber == 0) { continue; } List <DagNode> layerBefore = graph.GraphNodes.Where(node => node.LayerNumber == n.LayerNumber - 1).ToList(); int commTime = r.Next(MinComm, MaxComm) + MinComm; DagNode prevNode = layerBefore[r.Next(layerBefore.Count)]; graph.InsertEdge(prevNode, n, commTime); } if (CreateCriticalPath) { graph.GetCriticalPath(); } return(graph); }