diff --git a/cpp/switchchain_dsp.cpp b/cpp/switchchain_dsp.cpp index e7b052a0827d70c0d93e8499e01c935d770d18ae..845a31981bada3d777c1fdcd7a74f7a478790fb5 100644 --- a/cpp/switchchain_dsp.cpp +++ b/cpp/switchchain_dsp.cpp @@ -1,6 +1,6 @@ #include "exports.hpp" #include "graph.hpp" -#include "powerlaw.hpp" +#include "graph_powerlaw.hpp" #include "switchchain.hpp" #include #include @@ -9,7 +9,7 @@ #include #include -double getProperty(const DegreeSequence& ds) { +double getDSTN(const DegreeSequence& ds) { std::vector> vals(ds.size()); for (auto& v : vals) { v.resize(ds.size(), 0); @@ -38,56 +38,60 @@ double getProperty(const DegreeSequence& ds) { return result; } -int main() { - // Generate a random degree sequence - std::mt19937 rng(std::random_device{}()); - - // 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.5f, 2.9f}; +int main(int argc, char* argv[]) { + // Simulation parameters + const int numVerticesMin = 100; + const int numVerticesMax = 1000; + const int numVerticesStep = 100; + + float tauValues[] = {2.1f, 2.2f, 2.3f, 2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f}; + + const int totalDegreeSamples = 2000; + + auto getMixingTime = [](int n, float tau) { + return int(30.0f * (50.0f - 30.0f * (tau - 2.0f)) * n); + }; + constexpr int measurements = 50; + constexpr int measureSkip = 200; // Take a sample every ... steps + + // Output file + std::ofstream outfile; + if (argc >= 2) + outfile.open(argv[1]); + else + outfile.open("graphdata_dsp.m"); + if (!outfile.is_open()) { + std::cout << "ERROR: Could not open output file.\n"; + return 1; + } - Graph g; + // 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: 30 * (50 - 30 (tau - 2)) n\n"; + outfile << "data:\n"; + outfile << "1: {n,tau}\n"; + outfile << "2: avgTriangles\n"; + outfile << "3: dstn\n"; + outfile << "*)" << std::endl; - std::ofstream outfile("graphdata_dsp.m"); outfile << '{'; bool outputComma = false; - for (int numVertices = 1000; numVertices <= 1000; numVertices += 1000) { + std::mt19937 rng(std::random_device{}()); + Graph g; + for (int numVertices = numVerticesMin; numVertices <= numVerticesMax; + numVertices += numVerticesStep) { for (float tau : tauValues) { - - DegreeSequence ds(numVertices); - powerlaw_distribution degDist(tau, 1, numVertices); - //std::poisson_distribution<> degDist(12); - // For a single n,tau take samples over several instances of // the degree distribution. - // 500 samples seems to give reasonable results - for (int degreeSample = 0; degreeSample < 2000; ++degreeSample) { - // Generate a graph - // might require multiple tries - for (int i = 1; ; ++i) { - std::generate(ds.begin(), ds.end(), - [°Dist, &rng] { return degDist(rng); }); - // First make the sum even - unsigned int sum = std::accumulate(ds.begin(), ds.end(), 0); - if (sum % 2) { - continue; - // Can we do this: ?? - ds.back()++; - } - - if (g.createFromDegreeSequence(ds)) - break; - - // When 10 tries have not worked, output a warning - if (i % 10 == 0) { - std::cerr << "Warning: could not create graph from " - "degree sequence. Trying again...\n"; - } - } + for (int degreeSample = 0; degreeSample < totalDegreeSamples; + ++degreeSample) { + DegreeSequence ds; + generatePowerlawGraph(numVertices, tau, g, ds, rng); SwitchChain chain; if (!chain.initialize(g)) { @@ -98,12 +102,8 @@ int main() { std::cout << "Running n = " << numVertices << ", tau = " << tau << ". \t" << std::flush; - int mixingTime = 32*(32.0f - 10.0f*(tau - 2.0f)) * numVertices; //40000; - constexpr int measurements = 50; - constexpr int measureSkip = - 200; // Take a sample every ... steps - int movesDone = 0; + int mixingTime = getMixingTime(numVertices,tau); long long trianglesTotal = 0; @@ -117,24 +117,25 @@ int main() { ++movesDone; trianglesTotal += chain.g.countTriangles(); } + float avgTriangles = + float(trianglesTotal) / float(measurements); - std::cout << movesDone << '/' << mixingTime + measurements * measureSkip + std::cout << movesDone << '/' + << mixingTime + measurements * measureSkip << " moves succeeded (" << 100.0f * float(movesDone) / float(mixingTime + measurements * measureSkip) << "%)."; std::cout << std::flush; - //std::cout << std::endl; if (outputComma) outfile << ',' << '\n'; outputComma = true; - float avgTriangles = - float(trianglesTotal) / float(measurements); outfile << '{' << '{' << numVertices << ',' << tau << '}'; outfile << ',' << avgTriangles; - outfile << ',' << getProperty(ds) << '}' << std::flush; + outfile << ',' << getDSTN(ds); + outfile << '}' << std::flush; std::cout << std::endl; }