public void TestClusterNetwork() { ClusterNetwork network = new ClusterNetwork(2000, 5000, 100, 0.9d, false); Assert.LessOrEqual(Math.Abs(network.NewmanModularityUndirected - 0.9d), 0.02); Assert.LessOrEqual(Math.Abs(network.EdgeCount - 5000), 200); Assert.AreEqual(network.VertexCount, 2000); Assert.AreEqual(network.ClusterIDs.Length, 100); Assert.AreEqual(network.EdgeCount, network.InterClusterEdgeNumber + network.IntraClusterEdgeNumber); try{ foreach(Edge e in network.IntraClusterEdges) { int id1 = network.GetClusterForNode(e.Source); int id2 = network.GetClusterForNode(e.Target); Assert.AreEqual(id1, id2); } foreach(Edge e in network.InterClusterEdges) { int id1 = network.GetClusterForNode(e.Source); int id2 = network.GetClusterForNode(e.Target); Assert.AreNotEqual(id1, id2); } foreach(Vertex v in network.Vertices) { int id = network.GetClusterForNode(v); Assert.LessOrEqual(id, network.ClusterIDs.Length); } List<Vertex> vertices = new List<Vertex>(); foreach(int id in network.ClusterIDs) { vertices.AddRange(network.GetNodesInCluster(id)); } Assert.AreEqual(vertices.Count, network.VertexCount); } catch(Exception ex) { Assert.Fail(ex.Message); } }
static void Main(string[] args) { GlobValues glob = new GlobValues(); string configFile, dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), inputPath, outputPath; String[] path = dir.Split(new string[] { "Launch" }, StringSplitOptions.None); inputPath = path[0] + "Launch" + Path.DirectorySeparatorChar + "input"; configFile = inputPath + Path.DirectorySeparatorChar + "config.param.txt"; // outputPath = path[0] + "Launch" + Path.DirectorySeparatorChar + "output"; Console.WriteLine("File is : " + configFile); Console.WriteLine("Starting the program to generate the network based on modularity.."); try { // Read parameters from param.config file read_parameters(configFile, glob); } catch { Console.WriteLine("Usage: mono Demo.exe [nodes] [edges] [clusters] [resultfile]"); return; } // Displays the information Console.WriteLine("Given Parameter values"); Console.WriteLine("\n Nodes: " + glob.nodes + "\n Edges: " + glob.edges + "\n Clusters: " + glob.clusters + "\n Modularity MinValue: " + glob.modularityMinValue + "\n Modularity MaxValue: " + glob.modularityMaxValue); Console.WriteLine(" Number of runs: " + glob.numberOfGraphs + "\n Coupling probability value: "+glob.couplingProb*100); string sourceNetworkFile = inputPath = path[0] + "Launch" + Path.DirectorySeparatorChar + "Launch" + Path.DirectorySeparatorChar + "bin" + Path.DirectorySeparatorChar + "Debug" + Path.DirectorySeparatorChar + "network.edges"; string sourceResultFile = inputPath = path[0] + "Launch" + Path.DirectorySeparatorChar + "Launch" + Path.DirectorySeparatorChar + "bin" + Path.DirectorySeparatorChar + "Debug" + Path.DirectorySeparatorChar + "result.dat"; // For loop to make n number of Networks with the given size and modularity ... double modularity = glob.modularityMinValue; while (modularity <= glob.modularityMaxValue) { String outputFile = "Result_M" + modularity; outputPath = path[0] + "Launch" + Path.DirectorySeparatorChar + outputFile; System.IO.Directory.CreateDirectory(outputPath); try { for (int n = 1; n <= glob.numberOfGraphs; n++) { network = new ClusterNetwork(glob.nodes, glob.edges, glob.clusters, modularity, true); // Restricting the modularity value upto 1 decimal place // modularity = Math.Round(network.NewmanModularityUndirected, 1); String memberOutputFile = outputPath + Path.DirectorySeparatorChar + "membership.dat"; System.IO.StreamWriter sw = System.IO.File.CreateText(memberOutputFile); int i = 0; foreach (Vertex v in network.Vertices) { v.Label = (i++).ToString(); sw.WriteLine(network.GetClusterForNode(v).ToString()); } sw.Close(); Network.SaveToEdgeFile(network, "network.edges"); Console.WriteLine("Created network with {0} vertices, {1} edges and modularity {2:0.00}", network.VertexCount, network.EdgeCount, modularity); // To move a file or folder to a new location without renaming it. We rename the files after running the Kuramoto model. string destinationResultFile = outputPath + Path.DirectorySeparatorChar + n + "_res_N" + network.VertexCount + "_E" + network.EdgeCount + "_C" + glob.clusters + "_M" + modularity + "_K" + glob.couplingStrength + ".dat"; string destinationNetworkFile = outputPath + Path.DirectorySeparatorChar + n + "_network_N" + network.VertexCount + "_E" + network.EdgeCount + "_C" + glob.clusters + "_M" + modularity + "_K" + glob.couplingStrength + ".edges"; System.IO.File.Move(outputPath + Path.DirectorySeparatorChar + "membership.dat", outputPath + Path.DirectorySeparatorChar + n + "_mem_N" + network.VertexCount + "_E" + network.EdgeCount + "_C" + glob.clusters + "_M" + modularity + "_K" + glob.couplingStrength + ".dat"); try { Console.WriteLine("Moving the generated files to output directory.."); System.IO.File.Move(sourceNetworkFile, destinationNetworkFile); } catch (IOException e) { Console.WriteLine(e.Message); } // Run the Kuramoto model here and store the results in the output directory NetworkColorizer colorizer = new NetworkColorizer(); // Distribution of natural frequencies double mean_frequency = 1d; Normal normal = new Normal(mean_frequency, mean_frequency / 5d); sync = new Kuramoto(network, glob.couplingStrength, glob.couplingProb, colorizer, new Func<Vertex, Vertex[]>(v => { return new Vertex[] { v.RandomNeighbor }; }) ); foreach (Vertex v in network.Vertices) sync.NaturalFrequencies[v] = normal.Sample(); foreach (int g in network.ClusterIDs) pacemaker_mode[g] = false; sync.OnStep += new Kuramoto.StepHandler(recordOrder); Logger.AddMessage(LogEntryType.AppMsg, "Press enter to start synchronization experiment..."); Console.ReadLine(); // Run the simulation sync.Run(); // Write the time series to the resultfile if (sourceResultFile != null) sync.WriteTimeSeries(sourceResultFile); // Moving results of kuramoto model into output directory System.IO.File.Move(sourceResultFile, destinationResultFile); } } catch (Exception e) { Console.WriteLine("Error: " + e); } modularity = modularity + 0.1; } // End line of the program Console.WriteLine("Program ended successfully.."); }
static void Main(string[] args) { try { // The resultfile is given as command line argument //nodes = Int32.Parse(args[0]); //edges = Int32.Parse(args[1]); //clusters = Int32.Parse(args[2]); resultfile = args[3]; } catch { Console.WriteLine("Usage: mono Demo.exe [nodes] [edges] [clusters] [resultfile]"); return; } // Create a network of the given size and modularity ... network = new ClusterNetwork(nodes, edges, clusters, 0.63d, true); System.IO.StreamWriter sw = System.IO.File.CreateText("membership.dat"); int i = 0; foreach (Vertex v in network.Vertices) { v.Label = (i++).ToString(); sw.WriteLine(network.GetClusterForNode(v).ToString()); } sw.Close(); Network.SaveToEdgeFile(network, "network.edges"); Console.WriteLine("Created network with {0} vertices, {1} edges and modularity {2:0.00}", network.VertexCount, network.EdgeCount, network.NewmanModularityUndirected); // Run the real-time visualization NetworkColorizer colorizer = new NetworkColorizer(); //NetworkVisualizer.Start(network, new NETGen.Layouts.FruchtermanReingold.FruchtermanReingoldLayout(15), colorizer); //NetworkVisualizer.Layout.DoLayoutAsync(); // Distribution of natural frequencies double mean_frequency = 1d; Normal normal = new Normal(mean_frequency, mean_frequency/5d); sync = new Kuramoto( network, K, colorizer, new Func<Vertex, Vertex[]>(v => { return new Vertex[] {v.RandomNeighbor}; }) ); foreach(Vertex v in network.Vertices) sync.NaturalFrequencies[v] = normal.Sample(); foreach(int g in network.ClusterIDs) pacemaker_mode[g] = false; sync.OnStep += new Kuramoto.StepHandler(recordOrder); Logger.AddMessage(LogEntryType.AppMsg, "Press enter to start synchronization experiment..."); Console.ReadLine(); // Run the simulation sync.Run(); // Write the time series to the resultfile if(resultfile!=null) sync.WriteTimeSeries(resultfile); }
public static SIRResult RunSpreading(ClusterNetwork net, double bias, double k, int delay = 0) { SIRResult res = new SIRResult(); Dictionary<Vertex, bool> infections = new Dictionary<Vertex, bool>(); foreach (Vertex v in net.Vertices) infections[v] = false; List<Vertex> infected = new List<Vertex>(); List<Vertex> active = new List<Vertex>(); Vertex seed = net.RandomVertex; infected.Add(seed); active.Add(seed); int i = 0; while (active.Count > 0) { foreach (Vertex v in active.ToArray()) { // Biasing strategy Vertex neighbor = v.RandomNeighbor; double r = net.NextRandomDouble(); // First classify neighbors as intra- or inter-cluster neighbors List<Vertex> intraNeighbors = new List<Vertex>(); List<Vertex> interNeighbors = new List<Vertex>(); ClassifyNeighbors(net, v, intraNeighbors, interNeighbors); Console.Write("Local choice: {0} intra, {1} inter neighbors, ", intraNeighbors.Count, interNeighbors.Count); // with a probability given by bias select a random inter-cluster neighbor if (r <= bias && interNeighbors.Count > 0) neighbor = interNeighbors.ElementAt(net.NextRandom(interNeighbors.Count)); if (net.GetClusterForNode(neighbor) == net.GetClusterForNode(v)) Console.WriteLine("intra-cluster neighbor chosen!"); else Console.WriteLine("inter-cluster neighbor chosen!"); if (neighbor != null && !infections[neighbor]) { infections[neighbor] = true; infected.Add(neighbor); active.Add(neighbor); } else if (neighbor != null) if (net.NextRandomDouble() <= 1d / (double) k) active.Remove(v); } if (delay > 0) System.Threading.Thread.Sleep(delay); i++; } res.Duration = i; res.InfectedRatio = (double)infected.Count / (double)net.VertexCount; res.Modularity = (net as ClusterNetwork).NewmanModularity; return res; }