Changeset - c051e4ae1c78
[Not reviewed]
1 2 0
Tom Bannink - 8 years ago 2017-07-11 11:52:23
tom.bannink@cwi.nl
Update exponent cpp file
3 files changed with 76 insertions and 152 deletions:
0 comments (0 inline, 0 general)
cpp/Makefile
Show inline comments
 
@@ -16,7 +16,6 @@ TARGETS += switchchain_ccm_initialtris
 
TARGETS += switchchain_ccm_timeevol
 
TARGETS += switchchain_properties
 
TARGETS += switchchain_exponent
 
TARGETS += switchchain_exponent_new
 
TARGETS += switchchain_mixingtime
 
TARGETS += switchchain_spectrum
 
TARGETS += switchchain_successrates
cpp/switchchain_exponent.cpp
Show inline comments
 
#include "exports.hpp"
 
#include "graph.hpp"
 
#include "graph_powerlaw.hpp"
 
#include "graph_spectrum.hpp"
 
#include "switchchain.hpp"
 
#include <algorithm>
 
#include <fstream>
 
@@ -9,28 +10,69 @@
 
#include <random>
 
#include <vector>
 

	
 
int main() {
 
    // Generate a random degree sequence
 
    std::mt19937 rng(std::random_device{}());
 
int main(int argc, char* argv[]) {
 
    // Simulation parameters
 
    const int numVerticesMin = 10000;
 
    const int numVerticesMax = 50000;
 
    const int numVerticesStep = 10000;
 

	
 
    // Goal:
 
    // Degrees follow a power-law distribution with some parameter tau
 
    // Expect:  #tri = const * n^{ something }
 
    // The goal is to find the 'something' by finding the number of triangles
 
    // for different values of n and tau
 
    float tauValues[] = {2.1f, 2.2f, 2.3f, 2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f};
 
    //float tauValues[] = {2.1f, 2.2f, 2.3f, 2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f};
 
    float tauValues[] = {2.1f, 2.3f, 2.5f, 2.7f, 2.9f};
 

	
 
    Graph g;
 
    const int totalDegreeSamples = 1000;
 

	
 
    auto getMixingTime = [](int n, float tau) {
 
        return int(50.0f * (50.0f - 5.0f * (tau - 2.0f)) * n);
 
    };
 
    auto getMeasurements = [](int n, float tau) {
 
        (void)n;
 
        (void)tau;
 
        return 500;
 
    };
 
    auto getMeasureSkip = [](int n, float tau) {
 
        (void)tau;
 
        return 30 * n; // Take a sample every ... steps
 
    };
 

	
 
    // Output file
 
    std::ofstream outfile;
 
    if (argc >= 2)
 
        outfile.open(argv[1]);
 
    else
 
        outfile.open("graphdata_exponent_new.m");
 
    if (!outfile.is_open()) {
 
        std::cout << "ERROR: Could not open output file.\n";
 
        return 1;
 
    }
 

	
 
    // Output Mathematica-style comment to indicate file contents
 
    outfile << "(*\n";
 
    outfile << "n from " << numVerticesMin << " to " << numVerticesMax
 
            << " step " << numVerticesStep << std::endl;
 
    outfile << "tauValues: " << tauValues << std::endl;
 
    outfile << "degreeSamples: " << totalDegreeSamples << std::endl;
 
    outfile << "mixingTime: 50 * (50 - 5 (tau - 2)) n\n";
 
    outfile << "measurements: 500\n";
 
    outfile << "measureSkip: 30 n\n";
 
    outfile << "data:\n";
 
    outfile << "1: {n,tau}\n";
 
    outfile << "2: avgTriangles\n";
 
    outfile << "*)" << std::endl;
 

	
 
    std::ofstream outfile("graphdata_exponent_hightau.m");
 
    outfile << '{';
 
    // Mathematica does not accept normal scientific notation
 
    outfile << std::fixed;
 
    outfile << '{' << '\n';
 
    bool outputComma = false;
 

	
 
    for (int numVertices = 1000; numVertices <= 10000; numVertices += 1000) {
 
    std::mt19937 rng(std::random_device{}());
 
    Graph g;
 
    for (int numVertices = numVerticesMin; numVertices <= numVerticesMax;
 
         numVertices += numVerticesStep) {
 
        for (float tau : tauValues) {
 
            // For a single n,tau take samples over several instances of
 
            // the degree distribution.
 
            for (int degreeSample = 0; degreeSample < 2000; ++degreeSample) {
 
            for (int degreeSample = 0; degreeSample < totalDegreeSamples;
 
                 ++degreeSample) {
 
                DegreeSequence ds;
 
                generatePowerlawGraph(numVertices, tau, g, ds, rng);
 

	
 
@@ -40,49 +82,44 @@ int main() {
 
                    return 1;
 
                }
 

	
 
                std::cout << "Running n = " << numVertices << ", tau = " << tau
 
                          << ". \t" << std::flush;
 
                std::cout << "Running (n,tau) = (" << numVertices << ','
 
                          << tau << "). " << std::flush;
 

	
 
                int mixingTime = 32*(32.0f - 15.0f*(tau - 2.0f)) * numVertices; //40000;
 
                constexpr int measurements = 50;
 
                constexpr int measureSkip =
 
                    200; // Take a sample every ... steps
 
                // Mix
 
                int mixingTime = getMixingTime(numVertices, tau);
 
                for (int i = 0; i < mixingTime; ++i) {
 
                    chain.doMove();
 
                }
 

	
 
                int movesDone = 0;
 
                std::cout << "Mixing done. " << std::flush;
 

	
 
                long long trianglesTotal = 0;
 
                chain.g.getTrackedTriangles() = chain.g.countTriangles();
 

	
 
                for (int i = 0; i < mixingTime; ++i) {
 
                    if (chain.doMove())
 
                        ++movesDone;
 
                }
 
                int measurements = getMeasurements(numVertices, tau);
 
                int measureSkip = getMeasureSkip(numVertices, tau);
 
                for (int i = 0; i < measurements; ++i) {
 
                    for (int j = 0; j < measureSkip; ++j)
 
                        if (chain.doMove())
 
                            ++movesDone;
 
                    trianglesTotal += chain.g.countTriangles();
 
                        chain.doMove(true);
 
                    trianglesTotal += chain.g.getTrackedTriangles();
 
                }
 
                float avgTriangles =
 
                    float(trianglesTotal) / float(measurements);
 

	
 
                std::cout << movesDone << '/' << mixingTime + measurements * measureSkip
 
                          << " moves succeeded ("
 
                          << 100.0f * float(movesDone) /
 
                                 float(mixingTime + measurements * measureSkip)
 
                          << "%).";
 
                //std::cout << std::endl;
 
                std::cout << "Measuring done." << std::flush;
 

	
 
                if (outputComma)
 
                    outfile << ',' << '\n';
 
                outputComma = true;
 

	
 
                float avgTriangles =
 
                    float(trianglesTotal) / float(measurements);
 
                outfile << '{' << '{' << numVertices << ',' << tau << '}';
 
                outfile << ',' << avgTriangles << '}' << std::flush;
 
                outfile << ',' << avgTriangles;
 
                outfile << '}' << std::flush;
 

	
 
                std::cout << std::endl;
 
                std::cout << "Output done." << std::endl;
 
            }
 
        }
 
    }
 
    outfile << '}';
 
    outfile << ',' << '}';
 
    return 0;
 
}
cpp/switchchain_exponent_new.cpp
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)