/// <summary> /// Randomly choose attackers and victim. /// </summary> private void Initialize() { if (Nodes.Count == 0) { throw new Exception("Initilaize() Fail: There are 0 nodes in the network."); } // Create random array. int[] randomArray = DataUtility.RandomArray(Nodes.Count); int randomArrayIndex = 0; numberOfAttackers = Convert.ToInt32(Math.Round(percentageOfAttackers * Nodes.Count / 100, 0, MidpointRounding.AwayFromZero)); //numberOfNormalUsers = Convert.ToInt32(Math.Round(percentageOfNormalUser * Nodes.Count / 100 , 0, MidpointRounding.AwayFromZero)); // Select victims. for (; randomArrayIndex < numberOfVictims; randomArrayIndex++) { idOfVictims.Add(Nodes[randomArray[randomArrayIndex]].ID); Nodes[randomArray[randomArrayIndex]].Type = NodeType.Victim; } // Select attackers. for (; randomArrayIndex < numberOfAttackers + numberOfVictims; randomArrayIndex++) { Nodes[randomArray[randomArrayIndex]].Type = NodeType.Attacker; } //// Select normal users. //for (; randomArrayIndex < numberOfNormalUsers + numberOfAttackers + numberOfVictims; randomArrayIndex++) // Nodes[randomArray[randomArrayIndex]].Type = NodeType.Normal; // Finding diameter of the network topology. m_diameter = int.MinValue; foreach (var node in Nodes) { if (m_diameter < node.Eccentricity) { m_diameter = node.Eccentricity; } } m_hop_count_distrib = new int[m_diameter]; m_prob_hop = new double[m_diameter]; int sum_hop_cout = 0; for (int i = 0; i < Nodes.Count; i++) { for (int j = 0; j < Nodes.Count; j++) { if (AdjacentMatrix[i, j] != null) { m_hop_count_distrib[AdjacentMatrix[i, j].PathCount - 1]++; } else { m_hop_count_distrib[0]++; } } } for (int i = 1; i < m_hop_count_distrib.Length; i++) { m_hop_count_distrib[i] /= 2; sum_hop_cout += m_hop_count_distrib[i]; } for (int i = 1; i < m_prob_hop.Length; i++) { m_prob_hop[i] = (double)m_hop_count_distrib[i] / (double)sum_hop_cout; } }
/// <summary> /// Reading brite file and convert to our data structure. /// </summary> /// <param name="fileName">The path of brite file.</param> public void ReadBriteFile(string fileName) { this.fileName = fileName; Nodes.Clear(); Edges.Clear(); //try //{ DataUtility.Log("Reading brite file..."); int numberOfNodes; int numberOfEdges; string[] lines = File.ReadAllLines(fileName); string shortestPathFileName = string.Format(@"{0}.ShortestPath", fileName); string eccDegPathFileName = string.Format(@"{0}.EccDeg", fileName); string nodeDistribFileName = string.Format(@"{0}.NodeDistrib", fileName); for (int i = 0; i < lines.Length; i++) { // Reading nodes if (Regex.IsMatch(lines[i], @"^Nodes")) { numberOfNodes = Convert.ToInt32(Regex.Match(lines[i], @"\d+").Value); for (int j = ++i; i < j + numberOfNodes; i++) { string[] data = lines[i].Split(' ', '\t'); Nodes.Add(new Node() { ID = Convert.ToInt32(data[0]), Xpos = Convert.ToDouble(data[1]), Ypos = Convert.ToDouble(data[2]) }); } } // Reading edges else if (Regex.IsMatch(lines[i], @"(^Edges)")) { numberOfEdges = Convert.ToInt32(Regex.Match(lines[i], @"\d+").Value); for (int j = ++i; i < j + numberOfEdges; i++) { string[] data = lines[i].Split(' ', '\t'); int node1 = Convert.ToInt32(data[1]); int node2 = Convert.ToInt32(data[2]); double length = Convert.ToDouble(data[3]); double delay = Convert.ToDouble(data[4]); Edges.Add(new Edge() { Node1 = node1, Node2 = node2, Length = length, Delay = delay }); } } } DataUtility.Log("Done.\n", false); m_src_nodes = new List <Node>(Nodes); // If the file of shortest path is exist, then load into memory if (File.Exists(shortestPathFileName)) { ReadShortestPathFile(shortestPathFileName); } // Computing shortest path else { ComputingShortestPath(); // Output the result to the file. (XXX.ShortestPath) WriteShortestPathFile(shortestPathFileName); } if (File.Exists(nodeDistribFileName)) { ReadNodeIDDistribFile(nodeDistribFileName); } else { ComputeNodeDistrib(); WriteNodeIDDistribFile(nodeDistribFileName); } if (File.Exists(eccDegPathFileName)) { ReadEccentricityDegreeFile(eccDegPathFileName); } else { ComputingAllDegree(); ComputingAllEccentricity(); WriteEccentricityDegreeFile(eccDegPathFileName); } Initialize(); //} //catch(Exception exception) //{ // throw exception; //} }
/// <summary> /// Using Floyd-Warshar algorithm computing shortest path. /// </summary> public void ComputingShortestPath() { DataUtility.Log("Computing shortest path..."); // Create the space of adjacent matrix AdjacentMatrix = null; AdjacentMatrix = new Adjacency[Nodes.Count, Nodes.Count]; foreach (var edge in Edges) { AdjacentMatrix[NodeID2Index(edge.Node1), NodeID2Index(edge.Node2)] = new Adjacency() { Delay = edge.Delay, Length = edge.Length }; AdjacentMatrix[NodeID2Index(edge.Node2), NodeID2Index(edge.Node1)] = new Adjacency() { Delay = edge.Delay, Length = edge.Length }; } // Floyd-Warshall algorithm for (int i = 0; i < Nodes.Count; i++) { for (int j = 0; j < Nodes.Count; j++) { for (int k = j + 1; k < Nodes.Count; k++) { if ((AdjacentMatrix[j, k] == null && AdjacentMatrix[j, i] != null && AdjacentMatrix[i, k] != null) || (AdjacentMatrix[j, k] != null && AdjacentMatrix[j, i] != null && AdjacentMatrix[i, k] != null && AdjacentMatrix[j, k].Length > AdjacentMatrix[j, i].Length + AdjacentMatrix[i, k].Length)) { if (AdjacentMatrix[k, j] == null) { AdjacentMatrix[k, j] = new Adjacency(); AdjacentMatrix[j, k] = new Adjacency(); } AdjacentMatrix[k, j].Length = AdjacentMatrix[j, k].Length = AdjacentMatrix[j, i].Length + AdjacentMatrix[i, k].Length; AdjacentMatrix[k, j].Delay = AdjacentMatrix[j, k].Delay = AdjacentMatrix[j, i].Delay + AdjacentMatrix[i, k].Delay; AdjacentMatrix[k, j].Predecessor = AdjacentMatrix[j, k].Predecessor = i; } } } } for (int i = 0; i < Nodes.Count; i++) { for (int j = 0; j < Nodes.Count; j++) { if (AdjacentMatrix[i, j] != null) { AdjacentMatrix[i, j].PathCount = GetShortestPath(Nodes[i].ID, Nodes[j].ID).Count; } if (AdjacentMatrix[j, i] != null) { AdjacentMatrix[j, i].PathCount = GetShortestPath(Nodes[j].ID, Nodes[i].ID).Count; } } } DataUtility.Log("Done.\n", false); }