From c051e4ae1c78df3f4a290877d0f0ec4bfeb573c2 2017-07-11 11:52:23 From: Tom Bannink Date: 2017-07-11 11:52:23 Subject: [PATCH] Update exponent cpp file --- diff --git a/cpp/Makefile b/cpp/Makefile index e4cb5af5c37838f2f7d8882049d02064b6334bbb..569442ea869954ee83b0529705aaccb75384da60 100644 --- a/cpp/Makefile +++ b/cpp/Makefile @@ -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 diff --git a/cpp/switchchain_exponent.cpp b/cpp/switchchain_exponent.cpp index 8b6b27afa50f9dfcd6b940307d0e2e4ef3982306..897b728f1b944058313a63c7003a59f7a043a50d 100644 --- a/cpp/switchchain_exponent.cpp +++ b/cpp/switchchain_exponent.cpp @@ -1,6 +1,7 @@ #include "exports.hpp" #include "graph.hpp" #include "graph_powerlaw.hpp" +#include "graph_spectrum.hpp" #include "switchchain.hpp" #include #include @@ -9,28 +10,69 @@ #include #include -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; } diff --git a/cpp/switchchain_exponent_new.cpp b/cpp/switchchain_exponent_new.cpp deleted file mode 100644 index 3366283f49ba09c0512e994f03e0422951159d27..0000000000000000000000000000000000000000 --- a/cpp/switchchain_exponent_new.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#include "exports.hpp" -#include "graph.hpp" -#include "graph_powerlaw.hpp" -#include "graph_spectrum.hpp" -#include "switchchain.hpp" -#include -#include -#include -#include -#include -#include - -int main(int argc, char* argv[]) { - // Simulation parameters - const int numVerticesMin = 20000; - const int numVerticesMax = 50000; - const int numVerticesStep = 10000; - - //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}; - - const int totalDegreeSamples = 1000; - - auto getMixingTime = [](int n, float tau) { - return int(50.0f * (50.0f - 30.0f * (tau - 2.0f)) * n); - }; - constexpr int measurements = 10; - constexpr int measureSkip = 1000; // 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 - 30 (tau - 2)) n\n"; - outfile << "data:\n"; - outfile << "1: {n,tau}\n"; - outfile << "2: avgTriangles\n"; - outfile << "*)" << std::endl; - - // Mathematica does not accept normal scientific notation - outfile << std::fixed; - outfile << '{'; - bool outputComma = false; - - 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 < totalDegreeSamples; - ++degreeSample) { - DegreeSequence ds; - generatePowerlawGraph(numVertices, tau, g, ds, rng); - - SwitchChain chain; - if (!chain.initialize(g)) { - std::cerr << "Could not initialize Markov chain.\n"; - return 1; - } - - std::cout << "Running (n,tau) = (" << numVertices << ',' - << tau << "). " << std::flush; - - // Mix - int mixingTime = getMixingTime(numVertices, tau); - for (int i = 0; i < mixingTime; ++i) { - chain.doMove(); - } - - std::cout << "Mixing done. " << std::flush; - - long long trianglesTotal = 0; - for (int i = 0; i < measurements; ++i) { - for (int j = 0; j < measureSkip; ++j) - chain.doMove(); - trianglesTotal += chain.g.countTriangles(); - } - float avgTriangles = - float(trianglesTotal) / float(measurements); - - std::cout << "Measuring done." << std::flush; - - if (outputComma) - outfile << ',' << '\n'; - outputComma = true; - - outfile << '{' << '{' << numVertices << ',' << tau << '}'; - outfile << ',' << avgTriangles; - outfile << '}' << std::flush; - - std::cout << "Output done." << std::endl; - } - } - } - outfile << '}'; - return 0; -}