// Licensed under the MIT License . // SPDX-License-Identifier: MIT // Copyright (c) 2021 Noah H. and Tom H. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "robin_hood.h" namespace hash_tuple { template struct hash { size_t operator()(TT const& tt) const { return robin_hood::hash()(tt); } }; } namespace hash_tuple{ namespace { template inline void hash_combine(std::size_t& seed, T const& v) { seed ^= hash_tuple::hash()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); } } } namespace hash_tuple{ namespace { // Recursive template code derived from Matthieu M. template ::value - 1> struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { HashValueImpl::apply(seed, tuple); hash_combine(seed, std::get(tuple)); } }; template struct HashValueImpl { static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); } }; } template struct hash> { size_t operator()(std::tuple const& tt) const { size_t seed = 0; HashValueImpl >::apply(seed, tt); return seed; } }; } // LEATH TERMINATION CONDITION // 0 TERMINATE AT NUM VERTICES // 1 TERMINATE AT DISTANCE #define TERM 1 using namespace std; typedef tuple Vertex; vector vertex_neighbours(Vertex); tuple LeathRun(int,double); vector rand_bernoulli(float,int); void write_to_file(vector> &vec, string file_name) { std::ofstream f(file_name); for(vector>::const_iterator i = vec.begin(); i != vec.end(); ++i) { f << get<0>(*i) << ','<< get<1>(*i) << ','<(*i) <<','<(*i)<< '\n'; } } vector vertex_neighbours(Vertex v) { int x = get<0>(v); int y = get<1>(v); int z = get<2>(v); int w = get<3>(v); vector ret{make_tuple(x+1,y,z,w), make_tuple(x-1,y,z,w), make_tuple(x,y+1,z,w), make_tuple(x,y-1,z,w), make_tuple(x,y,z+1,w), make_tuple(x,y,z-1,w), make_tuple(x,y,z,w+1), make_tuple(x,y,z,w-1)}; return ret; } vector rand_bernoulli(double p,int n) { thread_local static random_device rd; thread_local static mt19937 rng(rd()); thread_local static bernoulli_distribution dist(p); thread_local auto gen = [](){ return dist(rng); }; vector open(n); generate(begin(open), end(open), gen); return open; } tuple LeathRun(int n, double p) { Vertex initial_vertex = std::make_tuple(0,0,0,0); robin_hood::unordered_set > visited_vertices; queue vertex_frontier; vertex_frontier.push(initial_vertex); int visited = 1; int num_open_edges = 0; int num_closed_edges = 0; int max_dist = 0; #if TERM == 0 while(visited(current))+abs(get<1>(current))+abs(get<2>(current))+abs(get<3>(current))); if (visited_vertices.find(current) != visited_vertices.end()) { continue; } visited_vertices.insert(current); vector neighbours = vertex_neighbours(current); vector open = rand_bernoulli(p,8); int num_open = 0; int num = 0; for(int i=0; i < 8; i++) { if(visited_vertices.find(neighbours[i]) == visited_vertices.end()) { if(open[num] == true) { num_open += 1; vertex_frontier.push(neighbours[i]); } num+=1; } } num_open_edges += num_open; num_closed_edges += (num-num_open); visited+=1; } return make_tuple(visited,num_open_edges,num_closed_edges,max_dist); } // RUN LEATH ALGORITHM // 1. n = max distance/number of steps (depending on TERM) // 2. p = percolation probability // 3. N = number of samples // 4. filename = output path int main(int argc, char** argv) { int n = stoi(argv[1]); double p = stod(argv[2]); int N = stoi(argv[3]); string filename = argv[4]; std::cout << "n="<> file_results(num_this_file); for (int w=0; w