Esempio n. 1
0
    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);
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Runs the cluster synchronization experiments, reading simulation parameters from the .config file
    /// </summary>
    public override void RunSimulation()
    {
        // Setup the experiment by creating the network and the synchronization module
        ClusterNetwork net  = new ClusterNetwork(nodes, edges, clusters, modularity_tgt, true);
        Kuramoto       sync = new Kuramoto(net, K);

        // Couple to single random neighbor in each step
        sync.CouplingSelector = new Func <Vertex, Vertex[]>(v => {
            return(new Vertex[] { v.RandomNeighbor });
        });

        // Keeps track whether clusters have already switched to pacemaker mode
        Dictionary <int, bool> pacemaker_mode = new Dictionary <int, bool>();

        // Mixed distribution of natural frequencies
        Normal group_avgs = new Normal(global_mean, global_mean * global_mean_width_factor);

        foreach (int i in net.ClusterIDs)
        {
            double group_avg  = group_avgs.Sample();
            Normal group_dist = new Normal(group_avg, group_avg * cluster_width_factor);
            foreach (Vertex v in net.GetNodesInCluster(i))
            {
                sync.NaturalFrequencies[v] = group_dist.Sample();
            }
            pacemaker_mode[i] = false;
        }

        // Set up handler that will be called AFTER each simulation step
        sync.OnStep += new Kuramoto.StepHandler(
            delegate(double t)
        {
            // Compute global order parameter
            finalOrderParam = sync.GetOrder(net.Vertices.ToArray());

            normalizerIntegratedOrder += finalOrderParam;

            // Record order evolution
            sync.AddDataPoint("GlobalOrder", finalOrderParam);

            // Stop simulation if full ordered state is reached or time exceeded
            if (finalOrderParam >= orderThres || t > timeThres)
            {
                sync.Stop();
            }

            // Output will only been shown when pyspg module is started in debug mode
            //if(t % (sync.TimeDelta * 100d) == 0)
            Logger.AddMessage(LogEntryType.SimMsg, string.Format("Time {0}, Order = {1:0.00}", t, finalOrderParam));

            // Compute order parameter of individual clusters
            foreach (int g in net.ClusterIDs)
            {
                double localOrder = sync.GetOrder(net.GetNodesInCluster(g));
                sync.AddDataPoint(string.Format("ClusterOrder_{0}", g), localOrder);

                // Switch to pacemaker mode if cluster order exceeds threshold
                if (localOrder > orderThres && !pacemaker_mode[g])
                {
                    pacemaker_mode[g] = true;

                    // Probabilistically switch border nodes to pacemaker mode
                    // Note: CouplingStrengths[... v, w ... ] is the strength by which phase advance of v is influenced when coupling to node w
                    foreach (Vertex v in net.GetNodesInCluster(g))
                    {
                        if (net.HasInterClusterConnection(v))
                        {
                            foreach (Vertex w in v.Neigbors)
                            {
                                if (!net.HasInterClusterConnection(w) && net.NextRandomDouble() <= pacemakerProb)
                                {
                                    Logger.AddMessage(LogEntryType.AppMsg, string.Format("Vertex switched to pacemaker mode", g));
                                    sync.CouplingStrengths[new Tuple <Vertex, Vertex>(v, w)] = 0d;
                                }
                            }
                        }
                    }
                }
            }
        });

        // compute coupling density in the initial situation
        initialDensity = 0d;
        foreach (var t in sync.CouplingStrengths.Keys)
        {
            initialDensity += sync.CouplingStrengths[t];
        }

        // Synchronously run the experiment (blocks execution until experiment is finished)
        sync.Run();

        // compute final coupling density
        finalDensity = 0d;
        foreach (var t in sync.CouplingStrengths.Keys)
        {
            finalDensity += sync.CouplingStrengths[t];
        }

        // Write time-series of the order parameters (global and cluster-wise) to a file
        sync.WriteTimeSeries(dynamics);

        // Set results
        normalizerIntegratedOrder /= sync.Time;
        time            = sync.Time;
        finalOrderParam = sync.GetOrder(net.Vertices.ToArray());
        modularity_real = net.NewmanModularityUndirected;
    }