int main() { igraph_t g; igraph_vs_t vertices; igraph_vector_t result; long int i; igraph_vs_seq(&vertices, 1, 101); igraph_barabasi_game(&g, 100000, /*power=*/ 1, 3, 0, 0, /*A=*/ 1, IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, /*start_from=*/ 0); igraph_vector_init(&result, 0); for (i=0; i<1; i++) { igraph_transitivity_local_undirected2(&g, &result, igraph_vss_all(), IGRAPH_TRANSITIVITY_NAN); } for (i=0; i<1; i++) { igraph_transitivity_local_undirected4(&g, &result, igraph_vss_all(), IGRAPH_TRANSITIVITY_NAN); } /* for (i=0; i<igraph_vector_size(&result); i++) { */ /* printf("%f ", VECTOR(result)[i]); */ /* } */ /* printf("\n"); */ igraph_vector_destroy(&result); igraph_vs_destroy(&vertices); igraph_destroy(&g); return 0; }
int main() { igraph_t g; igraph_integer_t result; igraph_integer_t from, to; igraph_vector_t path; igraph_rng_t rng; igraph_rng_init(&rng, &igraph_rngtype_mt19937); igraph_barabasi_game(&g, 30, /*power=*/ 1, 30, 0, 0, /*A=*/ 1, IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, /*start_from=*/ 0, &rng); igraph_diameter(&g, &result, 0, 0, 0, IGRAPH_UNDIRECTED, 1); /* printf("Diameter: %li\n", (long int) result); */ igraph_destroy(&g); igraph_ring(&g, 10, IGRAPH_DIRECTED, 0, 0); igraph_vector_init(&path, 0); igraph_diameter(&g, &result, &from, &to, &path, IGRAPH_DIRECTED, 1); printf("diameter: %li, from %li to %li\n", (long int) result, (long int) from, (long int) to); print_vector(&path); igraph_vector_destroy(&path); igraph_destroy(&g); igraph_rng_destroy(&rng); return 0; }
/* call-seq: * IGraph::GenerateRandom.barabasi_game(n,m,outpref,directed) -> IGraph * * Generates a graph based on the Barab�si-Albert model. * * n: The number of vertices in the graph. * m: The number of outgoing edges generated for each vertex. * * outpref: Boolean, if true not only the in- but also the out-degree of a * vertex increases its citation probability. Ie. the citation probability is * determined by the total degree of the vertices. * * directed: Boolean, whether to generate a directed graph. */ VALUE cIGraph_barabasi_game(VALUE self, VALUE nodes, VALUE m, VALUE outpref, VALUE directed) { igraph_t *graph; VALUE new_graph; new_graph = cIGraph_alloc(cIGraph); Data_Get_Struct(new_graph, igraph_t, graph); igraph_destroy(graph); igraph_barabasi_game(graph, NUM2INT(nodes), NUM2INT(m), NULL, outpref == Qtrue ? 1: 0, directed == Qtrue ? 1: 0); return new_graph; }
int main() { igraph_t g; igraph_real_t result; igraph_rng_t rng; igraph_rng_init(&rng, &igraph_rngtype_mt19937); igraph_barabasi_game(&g, 30, /*power=*/ 1, 30, 0, 0, /*A=*/ 1, IGRAPH_DIRECTED, IGRAPH_BARABASI_BAG, /*start_from=*/ 0, &rng); igraph_average_path_length(&g, &result, IGRAPH_UNDIRECTED, 1); /* printf("Length of the average shortest paths: %f\n", (float) result); */ igraph_destroy(&g); igraph_rng_destroy(&rng); return 0; }
int main() { igraph_t g; igraph_vector_t v, v2; int i, ret; igraph_barabasi_game(&g, 10, 2, 0, 0, 1); if (igraph_ecount(&g) != 18) { return 1; } if (igraph_vcount(&g) != 10) { return 2; } if (!igraph_is_directed(&g)) { return 3; } igraph_vector_init(&v, 0); igraph_get_edgelist(&g, &v, 0); for (i=0; i<igraph_ecount(&g); i++) { if (VECTOR(v)[2*i] <= VECTOR(v)[2*i+1]) { return 4; } } igraph_destroy(&g); /* out degree sequence */ igraph_vector_resize(&v, 10); VECTOR(v)[0]=0; VECTOR(v)[1]=1; VECTOR(v)[2]=3; VECTOR(v)[3]=3; VECTOR(v)[4]=4; VECTOR(v)[5]=5; VECTOR(v)[6]=6; VECTOR(v)[7]=7; VECTOR(v)[8]=8; VECTOR(v)[9]=9; igraph_barabasi_game(&g, 10, 0, &v, 0, 1); if (igraph_ecount(&g) != igraph_vector_sum(&v)) { return 5; } igraph_vector_init(&v2, 0); igraph_degree(&g, &v2, igraph_vss_all(), IGRAPH_OUT, 1); for (i=0; i<igraph_vcount(&g); i++) { if (VECTOR(v)[i] != VECTOR(v2)[i]) { return 6; } } igraph_vector_destroy(&v); igraph_vector_destroy(&v2); igraph_destroy(&g); /* outpref, we cannot really test this quantitatively, would need to set random seed */ igraph_barabasi_game(&g, 10, 2, 0, 1, 1); igraph_vector_init(&v, 0); igraph_get_edgelist(&g, &v, 0); for (i=0; i<igraph_ecount(&g); i++) { if (VECTOR(v)[2*i] <= VECTOR(v)[2*i+1]) { return 7; } } if (!igraph_is_directed(&g)) { return 8; } igraph_vector_destroy(&v); igraph_destroy(&g); /* Error tests */ igraph_set_error_handler(igraph_error_handler_ignore); ret=igraph_barabasi_game(&g, -10, 1, 0, 0, 0); if (ret != IGRAPH_EINVAL) { return 9; } ret=igraph_barabasi_game(&g, 10, -2, 0, 0, 0); if (ret != IGRAPH_EINVAL) { return 10; } igraph_vector_init(&v, 9); ret=igraph_barabasi_game(&g, 10, 0, &v, 0, 0); if (ret != IGRAPH_EINVAL) { return 11; } igraph_vector_destroy(&v); return 0; }
int main() { igraph_t g; igraph_vector_t bet, bet2, weights, edges; igraph_vector_t bbet, bbet2; igraph_real_t nontriv[] = { 0, 19, 0, 16, 0, 20, 1, 19, 2, 5, 3, 7, 3, 8, 4, 15, 4, 11, 5, 8, 5, 19, 6, 7, 6, 10, 6, 8, 6, 9, 7, 20, 9, 10, 9, 20, 10, 19, 11, 12, 11, 20, 12, 15, 13, 15, 14, 18, 14, 16, 14, 17, 15, 16, 17, 18 }; igraph_real_t nontriv_weights[] = { 0.5249, 1, 0.1934, 0.6274, 0.5249, 0.0029, 0.3831, 0.05, 0.6274, 0.3831, 0.5249, 0.0587, 0.0579, 0.0562, 0.0562, 0.1934, 0.6274, 0.6274, 0.6274, 0.0418, 0.6274, 0.3511, 0.3511, 0.1486, 1, 1, 0.0711, 0.2409 }; igraph_real_t nontriv_res[] = { 20, 0, 0, 0, 0, 19, 80, 85, 32, 0, 10, 75, 70, 0, 36, 81, 60, 0, 19, 19, 86 }; /*******************************************************/ igraph_barabasi_game(/* graph= */ &g, /* n= */ 1000, /* power= */ 1, /* m= */ 3, /* outseq= */ 0, /* outpref= */ 0, /* A= */ 1, /* directed= */ 0, /* algo= */ IGRAPH_BARABASI_BAG, /* start_from= */ 0); igraph_simplify(&g, /* multiple= */ 1, /* loops= */ 1, /*edge_comb=*/ 0); igraph_vector_init(&bet, 0); igraph_vector_init(&bbet, 0); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bet, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 2, /* weights= */ 0, /* nobigint= */ 1); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bbet, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 2, /* weights= */ 0, /* nobigint= */ 0); check(&bet, &bbet, 10); igraph_vector_destroy(&bet); igraph_vector_destroy(&bbet); igraph_destroy(&g); /*******************************************************/ igraph_tree(&g, 20000, 10, IGRAPH_TREE_UNDIRECTED); igraph_vector_init(&bet, 0); igraph_vector_init(&bbet, 0); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bet, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 3, /* weights= */ 0, /* nobigint= */ 1); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bbet, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 3, /* weights= */ 0, /* nobigint= */ 0); check(&bet, &bbet, 20); igraph_vector_init(&bet2, 0); igraph_vector_init(&bbet2, 0); igraph_vector_init(&weights, igraph_ecount(&g)); igraph_vector_fill(&weights, 1.0); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bet2, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 3, /* weights= */ &weights, /* nobigint= */ 1); igraph_betweenness_estimate(/* graph= */ &g, /* res= */ &bbet2, /* vids= */ igraph_vss_all(), /* directed = */ 0, /* cutoff= */ 3, /* weights= */ &weights, /* nobigint= */ 0); if (!igraph_vector_all_e(&bet, &bet2)) { return 1; } /* if (!igraph_vector_all_e(&bbet, &bbet2)) { */ /* return 2; */ /* } */ check(&bet, &bbet, 30); check(&bet2, &bbet2, 40); igraph_vector_destroy(&bet); igraph_vector_destroy(&bet2); igraph_vector_destroy(&bbet); igraph_vector_destroy(&bbet2); igraph_vector_destroy(&weights); igraph_destroy(&g); /* Non-trivial weighted graph */ igraph_vector_view(&edges, nontriv, sizeof(nontriv)/sizeof(igraph_real_t)); igraph_create(&g, &edges, 0, /* directed= */ 0); igraph_vector_view(&weights, nontriv_weights, sizeof(nontriv_weights)/sizeof(igraph_real_t)); igraph_vector_init(&bet, 0); igraph_vector_init(&bbet, 0); igraph_betweenness(/*graph=*/ &g, /*res=*/ &bet, /*vids=*/ igraph_vss_all(), /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 1); igraph_betweenness(/*graph=*/ &g, /*res=*/ &bbet, /*vids=*/ igraph_vss_all(), /*directed=*/0, /*weights=*/ &weights, /*nobigint=*/ 0); igraph_vector_view(&bet2, nontriv_res, sizeof(nontriv_res)/sizeof(igraph_real_t)); if (!igraph_vector_all_e(&bet, &bet2)) { return 2; } check(&bet, &bbet, 50); igraph_vector_destroy(&bet); igraph_vector_destroy(&bbet); igraph_destroy(&g); if (IGRAPH_FINALLY_STACK_SIZE() != 0) return 3; return 0; }
int main(int argc, char* argv[]) { if (argc != 9) { cout << "Usage: ./release/fixating <Update Rule: \"Bd\", \"dB\"> <integer: population size> <\"directed\" or \"undirected\"> <double: fitness of mutant> <category of graph: \"complete\", \"ER\", \"BB\", \"WS\", \"geo\", or \"custom\" > <secondary parameter for the category of graph: \"GNM\" or \"GNP\" for Erdos Reny, double power of preference for Barabasi, int dimension for small world, bool periodic for geometric, , adjacency matrix for custom> <tertiary parameter for the category of graph: probability for every edge in Erdos-Reny GNP and geometric, number of edges for Erdos-Reny GNM, m for barabasi, probability of rewiring for small world, 0 for custom> <output: \"probability\", \"conditional\", \"unconditional\", or \"all\">" << endl; return -1; } // ---------- If you want to stop time, uncomment all comments with //CLOCK// //CLOCK// std::clock_t start; //CLOCK// double bt = 0; //CLOCK// double st = 0; //counting variable for the graph generators int counts = 0; const unsigned int popSize = atoi(argv[2]); if (popSize > 23) { cout << "Code only possible for population size up to 23... aborting..." << endl; return -1; } const unsigned int numStates = 1 << popSize; string update = argv[1]; if (update != "dB" && update != "Bd") { cout << "Only \"Bd\" or \"dB\" possible for update rule!... aborting..." << endl; return -1; } float fitnessMutants = atof(argv[4]); string direction = argv[3]; string category = argv[5]; igraph_t graph; int admat[popSize * popSize]; string output = argv[8]; if (output != "probability" && output != "conditional" && output != "unconditional" && output != "all") { cout << "Only \"probability\", \"unconditional\", \"conditional\" or \"all\" possible for output!... aborting..." << endl; return -1; } // ---------- Code snippet for fully connected graph ---------- if (category == "complete") { if (direction == "undirected") { igraph_full(&graph, popSize, false, false); } else if (direction == "directed") { igraph_full(&graph, popSize, true, false); } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } } // ---------- Code snippet for random graph ---------- else if (category == "ER") { string gn = argv[6]; igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { if (gn == "GNP") { double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, popSize, edgeprob, true, false); } else if (gn == "GNM") { int edgenumber = atoi(argv[7]); if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1))) { cout << "number of edges must be greater than 1 and smaller than N*(N-1) ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM, popSize, edgenumber, true, false); } else { cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl; } igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } else if (direction == "undirected") { int counts = 0; while ((isConnected == 0) & (counts < maxcount)) { if (gn == "GNP") { double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP, popSize, edgeprob, false, false); } else if (gn == "GNM") { int edgenumber = atoi(argv[7]); if ((edgenumber < 1) || (edgenumber > popSize*(popSize-1)/2)) { cout << "number of edges must be greater than 1 and smaller than N*(N-1)/2 ...aborting..." << endl; return -1; } igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNM, popSize, edgenumber, false, false); } else { cout << "Only \"GNM\" and \"GNP\" possible ... aborting..." << endl; } igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } } //---------------------------- Code snippet for small world network --------------------------------// else if (category == "WS") { igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } int dim = atoi(argv[6]); int latSize = pow(popSize,1/double(dim)); if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { igraph_watts_strogatz_game(&graph, dim, latSize, 1, edgeprob, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_watts_strogatz_game(&graph, dim, latSize, 1, edgeprob, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } //---------------------------- Code snippet for geometric generator --------------------------------// else if(category == "geo") { igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; double edgeprob = atof(argv[7]); if ((edgeprob > 1) || (edgeprob < 0)) { cout << "probabilities larger than 1 or smaller than 0 ...aborting..." << endl; return -1; } bool torus = (atoi(argv[6]) == 1); double radius = sqrt(edgeprob/3.14); if (direction == "directed") { while ((isConnected == 0) & (counts < maxcount)) { igraph_grg_game(&graph, popSize, radius, torus, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_grg_game(&graph, popSize, radius, torus, 0, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Probability or number of edges too low... Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } //---------------------------- Code snippet for barabasi generator --------------------------------// else if(category == "BB") { double power = atof(argv[6]); int m = atoi(argv[7]); igraph_rng_seed(igraph_rng_default(), std::clock()); igraph_bool_t isConnected = 0; if (direction == "directed") { cout << "directed Barabasi-Albert never creates connected graphs, use undirected instead! aborting..." << endl; return -1; } else if (direction == "undirected") { while ((isConnected == 0) & (counts < maxcount)) { igraph_barabasi_game(&graph, popSize, power, m, 0, 0, 1.0, false, IGRAPH_BARABASI_PSUMTREE, 0); igraph_is_connected(&graph, &isConnected, IGRAPH_STRONG); counts++; } } else { cout << "Only \"directed\" and \"undirected\" possible for direction of graph!... aborting..." << endl; return -1; } if (counts == maxcount) { cout << "Did not find a connected graph after "<< maxcount <<" attempts... aborting..." << endl; return -1; } } // ---------- Code snippet for custom graph ---------- else if(category == "custom") { std::string admats = argv[6]; if (admats.size() != popSize*popSize) { cout << "adjacency matrix has the wrong size... aborting..." << endl; return -1; } std::vector<int> ints; std::transform(std::begin(admats), std::end(admats), std::back_inserter(ints), [](char c) { return c - '0'; } ); std::copy(ints.begin(), ints.end(), admat); } else { cout << "Only \"complete\", \"ER\", \"BB\", \"WS\", or \"geo\" as categories... aborting..." << endl; return -1; } // ---------- Here the adjacency matrix gets copied into an array ---------- if(category!="custom") { igraph_matrix_t admatv; igraph_matrix_init(&admatv, 0,0); igraph_get_adjacency( &graph, &admatv,IGRAPH_GET_ADJACENCY_BOTH,false); for(unsigned int i = 0 ; i < popSize ; i++) { for(unsigned int k = 0 ; k < popSize ; k++) { admat[ i*popSize + k] = MATRIX(admatv,i,k ); } } igraph_destroy(&graph); igraph_matrix_destroy(&admatv); } for (unsigned int i=0; i<popSize; i++) { for (unsigned int j=0; j<popSize; j++) { // If you want to print the adjacency matrix: cout<<admat[i * popSize + j]<<" "; } } cout<<endl; t_vectorFP data; t_vectorInt row; t_vectorInt col; data.reserve(popSize * numStates); row.reserve(popSize * numStates); col.reserve(popSize * numStates); //CLOCK// start = std::clock(); createTransitionMatrix(popSize, numStates, fitnessMutants, update, admat, data, row, col); std::vector<T> tripletList; tripletList.reserve(popSize * numStates); for( unsigned int j = 0 ; j < data.size() ; j++) { tripletList.push_back(T(col.at(j),row.at(j),data.at(j))); } SpMat mat(numStates,numStates); mat.setFromTriplets(tripletList.begin(), tripletList.end()); // Stopping time after creating transition matrix //CLOCK// bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; //for (int i = 0; i<data.size(); i++) // cout<<"unconditional: transition prob from state "<<row[i]<<" to state "<<col[i]<<" is "<<data[i]<<endl; string s1; /* ---------- No distinguishing between "probability", "unconditional" time, and "conditional" time ---------- */ float * fixProbAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); fixProb(mat, popSize, numStates, fixProbAllStates); // Stopping time after solving fixation probabilities //CLOCK// st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt; float probOne = 0.0; for(unsigned int i = 0; i < popSize; i++) { int j = 1 << i; probOne = probOne + fixProbAllStates[j]; } probOne = probOne / (float)(popSize); cout << "fixation probability:" << probOne << endl; /* ---------- Printing the fixation probability starting from all states ---------- */ /* for(unsigned int i = 0; i < numStates; i++) { bitset<23> b1(i); s1 = b1.to_string(); cout<<"fixation probability in state "; cout<< s1.substr(23-popSize,popSize); cout <<" is "<<fixProbAllStates[i]<<endl; } */ if((output == "unconditional")||(output == "all")) { float * uncondFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); // Stopping the time for solving for unconditional fixation time //CLOCK// start = std::clock(); //CLOCK// bt = ( std::clock() - start ) / (double) CLOCKS_PER_SEC; time(mat, popSize, numStates, uncondFixTimeAllStates); //CLOCK// float avUncondTime = 0.0; for(unsigned int i = 0 ; i < popSize ; i++) { int j = 1 << i; avUncondTime = avUncondTime + uncondFixTimeAllStates[j]; } avUncondTime = avUncondTime / (float)(popSize); free(uncondFixTimeAllStates); cout<< "unconditional fixation time:" << avUncondTime << endl; } /* ---------- Printing the average unconditional fixation time starting from all states ---------- */ //for(unsigned int i = 0; i < numStates; i++) //{ // bitset<23> b1(i); // s1 = b1.to_string(); //cout<<"Unconditional fixation time in state "; //cout<< s1.substr (23-popSize,popSize); //cout <<" is "<<uncondFixTimeAllStates[i]<<endl; //} //float * fixProbAllStates = (float*) malloc(numStates * sizeof(float)); //fixProb(mat, popSize, numStates, fixProbAllStates); if((output == "conditional")||(output == "all")) { createConditionalTransitionMatrix(popSize, numStates, fixProbAllStates, data, row, col); std::vector<T> tripletListCond; tripletListCond.reserve(popSize * numStates); for( unsigned int j = 0 ; j < data.size() ; j++) { tripletListCond.push_back(T(col.at(j),row.at(j),data.at(j))); } SpMat conditionalMatrix(numStates,numStates); conditionalMatrix.setFromTriplets(tripletListCond.begin(), tripletListCond.end()); float * condFixTimeAllStates = static_cast<float*> (malloc(numStates * sizeof(float))); time(conditionalMatrix, popSize, numStates, condFixTimeAllStates); float avCondTime = 0.0; for(unsigned int i = 0 ; i < popSize ; i++) { int j = 1 << i; avCondTime = avCondTime + condFixTimeAllStates[j]; } avCondTime = avCondTime / (float)(popSize); free(condFixTimeAllStates); cout << "conditional fixation time:" << avCondTime << endl; } free(fixProbAllStates); /* ---------- Printing the average conditional fixation time starting from all states ---------- */ //for(unsigned int i = 0; i < numStates; i++) //{ //bitset<23> b1(i); //s1 = b1.to_string(); //cout<<"Conditional fixation time in state "; //cout<< s1.substr (23-popSize,popSize); //cout <<" is "<<condFixTimeAllStates[i]<<endl; //} st = ( std::clock() - start) / (double) CLOCKS_PER_SEC - bt; //CLOCK// cout<<"building time: "<< bt <<'\n'; //CLOCK// cout<<"solving time: "<< st << "\n\n"; }
main (int argc,char *argv[]) { int ia,ib,ic,id,it,inow,ineigh,icont; int in,ia2,ia3,irun,icurrent,ORTOGONALFLAG; int RP, P,L,N,NRUNS,next,sweep,SHOWFLAG; double u,field1,field2,field0,q,aux1,aux2; double alfa,aux,Q1,Q2,QZ,RZQ,rho,R; double pm,D,wmax,mQ,wx,wy,h_sigma,h_mean; double TOL,MINLOGF,E; double DELTA; double E_new,Ex,DeltaE,ER; double EW,meanhist,hvalue,wE,aratio; double logG_old,logG_new,lf; size_t i_old,i_new; long seed; double lGvR,lGv,DlG; size_t iL,iR,i1,i2; int I_endpoint[NBINS]; double lower,upper; size_t i0; FILE * wlsrange; FILE * dos; FILE * thermodynamics; FILE * canonical; FILE * logfile; //FILE * pajek; //*********************************** // Help //*********************************** if (argc<15){ help(); return(1); } else{ DELTA = atof(argv[1]); P = atoi(argv[2]); RP = atoi(argv[3]); L = atoi(argv[4]); N = atoi(argv[5]); TOL = atof(argv[6]); MINLOGF = atof(argv[7]); } wlsrange=fopen(argv[8],"w"); dos=fopen(argv[9],"w"); thermodynamics=fopen(argv[10],"w"); canonical=fopen(argv[11],"w"); logfile=fopen(argv[12],"w"); SHOWFLAG = atoi(argv[13]); ORTOGONALFLAG = atoi(argv[14]); if ((ORTOGONALFLAG==1) && (P>L)) P=L; //maximum number of orthogonal issues if (SHOWFLAG==1){ printf("# parameters are DELTA=%1.2f P=%d ",DELTA,P); printf("D=%d L=%d M=%d TOL=%1.2f MINLOGF=%g \n",L,N,RP,TOL,MINLOGF); } fprintf(logfile,"# parameters are DELTA=%1.2f P=%d D=%d",DELTA,P,L); fprintf(logfile,"L=%d M=%d TOL=%1.2f MINLOGF=%g\n",L,RP,TOL,MINLOGF); //********************************************************************** // Alocate matrices //********************************************************************** gsl_matrix * sociedade = gsl_matrix_alloc(SIZE,L); gsl_matrix * issue = gsl_matrix_alloc(P,L); gsl_vector * current_issue = gsl_vector_alloc(L); gsl_vector * v0 = gsl_vector_alloc(L); gsl_vector * v1 = gsl_vector_alloc(L); gsl_vector * Z = gsl_vector_alloc(L); gsl_vector * E_borda = gsl_vector_alloc(NBINS); //********************************************************************** // Inicialization //********************************************************************** const gsl_rng_type * T; gsl_rng * r; gsl_rng_env_setup(); T = gsl_rng_default; r=gsl_rng_alloc (T); seed = time (NULL) * getpid(); //seed = 13188839657852; gsl_rng_set(r,seed); igraph_t graph; igraph_vector_t neighbors; igraph_vector_t result; igraph_vector_t dim_vector; igraph_real_t res; igraph_bool_t C; igraph_vector_init(&neighbors,1000); igraph_vector_init(&result,0); igraph_vector_init(&dim_vector,DIMENSION); for(ic=0;ic<DIMENSION;ic++) VECTOR(dim_vector)[ic]=N; gsl_histogram * HE = gsl_histogram_alloc (NBINS); gsl_histogram * logG = gsl_histogram_alloc (NBINS); gsl_histogram * LG = gsl_histogram_alloc (NBINS); //******************************************************************** // Social Graph //******************************************************************** //Barabasi-Alberts network igraph_barabasi_game(&graph,SIZE,RP,&result,1,0); /* for (inow=0;inow<SIZE;inow++){ igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT); printf("%d ",inow); for(ic=0;ic<igraph_vector_size(&neighbors);ic++) { ineigh=(int)VECTOR(neighbors)[ic]; printf("%d ",ineigh); } printf("\n"); }*/ //pajek=fopen("graph.xml","w"); // igraph_write_graph_graphml(&graph,pajek); //igraph_write_graph_pajek(&graph, pajek); //fclose(pajek); //********************************************************************** //Quenched issues set and Zeitgeist //********************************************************************** gsl_vector_set_zero(Z); gera_config(Z,issue,P,L,r,1.0); if (ORTOGONALFLAG==1) gsl_matrix_set_identity(issue); for (ib=0;ib<P;ib++) { gsl_matrix_get_row(current_issue,issue,ib); gsl_blas_ddot(current_issue,current_issue,&Q1); gsl_vector_scale(current_issue,1/sqrt(Q1)); gsl_vector_add(Z,current_issue); } gsl_blas_ddot(Z,Z,&QZ); gsl_vector_scale(Z,1/sqrt(QZ)); //********************************************************************** // Ground state energy //********************************************************************** double E0; gera_config(Z,sociedade,SIZE,L,r,0); E0 = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph); double EMIN=E0; double EMAX=-E0; double E_old=E0; gsl_histogram_set_ranges_uniform (HE,EMIN,EMAX); gsl_histogram_set_ranges_uniform (logG,EMIN,EMAX); if (SHOWFLAG==1) printf("# ground state: %3.0f\n",E0); fprintf(logfile,"# ground state: %3.0f\n",E0); //********************************************************************** // Find sampling interval //********************************************************************** //printf("#finding the sampling interval...\n"); lf=1; sweep=0; icont=0; int iflag=0; int TMAX=NSWEEPS; while(sweep<=TMAX){ if (icont==10000) { //printf("%d sweeps\n",sweep); icont=0; } for(it=0;it<SIZE;it++){ igraph_vector_init(&neighbors,SIZE); //choose a random site do{ inow=gsl_rng_uniform_int(r,SIZE); }while((inow<0)||(inow>=SIZE)); gsl_matrix_get_row(v1,sociedade,inow); igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT); //generates a random vector v1 gsl_vector_memcpy(v0,v1); gera_vetor(v1,L,r); //calculates energy change when v0->v1 // in site inow DeltaE=variacaoE(v0,v1,inow,sociedade, issue,N,L,P,DELTA,graph,neighbors); E_new=E_old+DeltaE; //WL: accepts in [EMIN,EMAX] if ((E_new>EMIN) && (E_new<EMAX)) { gsl_histogram_find(logG,E_old,&i_old); logG_old=gsl_histogram_get(logG,i_old); gsl_histogram_find(logG,E_new,&i_new); logG_new=gsl_histogram_get(logG,i_new); wE = GSL_MIN(exp(logG_old-logG_new),1); if (gsl_rng_uniform(r)<wE){ E_old=E_new; gsl_matrix_set_row(sociedade,inow,v1); } } //WL: update histograms gsl_histogram_increment(HE,E_old); gsl_histogram_accumulate(logG,E_old,lf); igraph_vector_destroy(&neighbors); } sweep++; icont++; } gsl_histogram_fprintf(wlsrange,HE,"%g","%g"); double maxH=gsl_histogram_max_val(HE); //printf("ok\n"); Ex=0; hvalue=maxH; while((hvalue>TOL*maxH)&&(Ex>EMIN)){ gsl_histogram_find(HE,Ex,&i0); hvalue=gsl_histogram_get(HE,i0); Ex-=1; if(Ex<=EMAX)TMAX+=10000; } EMIN=Ex; Ex=0; hvalue=maxH; while((hvalue>TOL*maxH)&&(Ex<EMAX)) { gsl_histogram_find(HE,Ex,&i0); hvalue=gsl_histogram_get(HE,i0); Ex+=1; if(Ex>=EMAX)TMAX+=10000; } EMAX=Ex; EMAX=GSL_MIN(10.0,Ex); if (SHOWFLAG==1) printf("# the sampling interval is [%3.0f,%3.0f] found in %d sweeps \n\n" ,EMIN,EMAX,sweep); fprintf(logfile, "# the sampling interval is [%3.0f,%3.0f] found in %d sweeps \n\n" ,EMIN,EMAX,sweep); gsl_histogram_set_ranges_uniform (HE,EMIN-1,EMAX+1); gsl_histogram_set_ranges_uniform (logG,EMIN-1,EMAX+1); gsl_histogram_set_ranges_uniform (LG,EMIN-1,EMAX+1); //********************************************************************** // WLS //********************************************************************** int iE,itera=0; double endpoints[NBINS]; double w = WINDOW; //(EMAX-EMIN)/10.0; //printf("W=%f\n",w); lf=1; //RESOLUTION ----> <------RESOLUTION***** do{ int iverify=0,iborda=0,flat=0; sweep=0; Ex=EMAX; EW=EMAX; E_old=EMAX+1; iE=0; endpoints[iE]=EMAX; iE++; gsl_histogram_reset(LG); //WINDOWS --> <--WINDOWS******* while((Ex>EMIN)&&(sweep<MAXSWEEPS)){ //initial config gera_config(Z,sociedade,SIZE,L,r,0); E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph); while( (E_old<EMIN+1)||(E_old>Ex) ){ //printf("%d %3.1f\n",E_old); do{ inow=gsl_rng_uniform_int(r,SIZE); }while((inow<0)||(inow>=SIZE)); gsl_matrix_get_row(v0,sociedade,inow); gera_vetor(v1,L,r); gsl_matrix_set_row(sociedade,inow,v1); E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph); if (E_old>Ex){ gsl_matrix_set_row(sociedade,inow,v0); E_old = hamiltoneana(sociedade,issue,SIZE,L,P,DELTA,graph); } //printf("%3.1f %3.1f %3.1f\n",EMIN+1,E_old, Ex); } if (SHOWFLAG==1){ printf("# sampling [%f,%f]\n",EMIN,Ex); printf("# walking from E=%3.0f\n",E_old); } fprintf(logfile,"# sampling [%f,%f]\n",EMIN,Ex); fprintf(logfile,"# walking from E=%3.0f\n",E_old); do{ //FLAT WINDOW------> <------FLAT WINDOW***** //MC sweep ----> <------MC sweep******** for(it=0;it<SIZE;it++){ igraph_vector_init(&neighbors,SIZE); //escolhe sítio aleatoriamente do{ inow=gsl_rng_uniform_int(r,SIZE); }while((inow<0)||(inow>=SIZE)); gsl_matrix_get_row(v1,sociedade,inow); igraph_neighbors(&graph,&neighbors,inow,IGRAPH_OUT); //gera vetor aleatorio v1 gsl_vector_memcpy(v0,v1); gera_vetor(v1,L,r); //calculates energy change when //v0->v1 in site inow DeltaE=variacaoE(v0,v1,inow,sociedade,issue, N,L,P,DELTA,graph,neighbors); E_new=E_old+DeltaE; //WL: accepts in [EMIN,Ex] if ((E_new>EMIN) && (E_new<Ex)) { gsl_histogram_find(logG,E_old,&i_old); logG_old=gsl_histogram_get(logG,i_old); gsl_histogram_find(logG,E_new,&i_new); logG_new=gsl_histogram_get(logG,i_new); wE = GSL_MIN(exp(logG_old-logG_new),1); if (gsl_rng_uniform(r)<wE){ E_old=E_new; gsl_matrix_set_row(sociedade,inow,v1); } } //WL: updates histograms gsl_histogram_increment(HE,E_old); gsl_histogram_accumulate(logG,E_old,lf); itera++; igraph_vector_destroy(&neighbors); } //MC sweep ----> <--------MC sweep**** sweep++; iverify++; if( (EMAX-EMIN)<NDE*DE ) { EW=EMIN; }else{ EW=GSL_MAX(Ex-w,EMIN); } if (iverify==CHECK){//Verify flatness if (SHOWFLAG==1) printf(" #verificando flatness em [%f,%f]\n",EW,Ex); fprintf(logfile," #verificando flatness em [%f,%f]\n" ,EW,Ex); iverify=0; flat=flatness(HE,EW,Ex,TOL,itera,meanhist,hvalue); if (SHOWFLAG==1) printf("#minH= %8.0f\t k<H>=%8.0f\t %d sweeps\t ", hvalue,TOL*meanhist,sweep,flat); fprintf(logfile, "#minH= %8.0f\t k<H>=%8.0f\t %d sweeps\t ", hvalue,TOL*meanhist,sweep,flat); } }while(flat==0);// <------FLAT WINDOW****** flat=0; //Find ER //printf("# EMAX=%f EMIN = %f Ex =%f\n",EMAX, EMIN, Ex); if( (EMAX-EMIN)<NDE*DE ) { Ex=EMIN; endpoints[iE]=EMIN; } else { if (EW>EMIN){ ER=flatwindow(HE,EW,TOL,meanhist); if (SHOWFLAG==1) printf("# extending flatness to[%f,%f]\n",ER,Ex); fprintf(logfile, "# extending flatness to [%f,%f]\n",ER,Ex); if((ER-EMIN)<1){ ER=EMIN; Ex=EMIN; endpoints[iE]=EMIN; }else{ endpoints[iE]=GSL_MIN(ER+DE,EMAX); Ex=GSL_MIN(ER+2*DE,EMAX); } } else{ endpoints[iE]=EMIN; Ex=EMIN; ER=EMIN; } } if (SHOWFLAG==1) printf("# window %d [%3.0f,%3.0f] is flat after %d sweeps \n", iE,endpoints[iE],endpoints[iE-1],sweep); fprintf(logfile,"# window %d [%3.0f,%3.0f] is flat after %d sweeps\n", iE,endpoints[iE],endpoints[iE-1],sweep); //saves histogram if (iE==1){ gsl_histogram_find(logG,endpoints[iE],&i1); gsl_histogram_find(logG,endpoints[iE-1],&i2); for(i0=i1;i0<=i2;i0++){ lGv=gsl_histogram_get(logG,i0); gsl_histogram_get_range(logG,i0,&lower,&upper); E=0.5*(upper+lower); gsl_histogram_accumulate(LG,E,lGv); } }else{ gsl_histogram_find(logG,endpoints[iE],&i1); gsl_histogram_find(logG,endpoints[iE-1],&i2); lGv=gsl_histogram_get(logG,i2); lGvR=gsl_histogram_get(LG,i2); DlG=lGvR-lGv; //printf("i1=%d i2=%d lGv=%f lGvR=%f DlG=%f\n",i1,i2,lGv,lGvR,DlG); for(i0=i1;i0<i2;i0++){ lGv=gsl_histogram_get(logG,i0); lGv=lGv+DlG; gsl_histogram_get_range(logG,i0,&lower,&upper); E=(upper+lower)*0.5; //printf("i0=%d E=%f lGv=%f\n",i0,E,lGv); gsl_histogram_accumulate(LG,E,lGv); } } //printf("#########################################\n"); //gsl_histogram_fprintf(stdout,LG,"%g","%g"); //printf("#########################################\n"); iE++; if((Ex-EMIN)>NDE*DE) { if (SHOWFLAG==1) printf("# random walk is now restricted to [%3.0f,%3.0f]\n" ,EMIN,Ex); fprintf(logfile,"# random walk is now restricted to [%3.0f,%3.0f]\n" ,EMIN,Ex); } gsl_histogram_reset(HE); } //WINDOWS --> if(sweep<MAXSWEEPS){ if (SHOWFLAG==1) printf("# log(f)=%f converged within %d sweeps\n\n",lf,sweep); fprintf(logfile,"# log(f)=%f converged within %d sweeps\n\n",lf,sweep); lf=lf/2.0; gsl_histogram_reset(HE); gsl_histogram_memcpy(logG,LG); }else { if (SHOWFLAG==1) printf("# FAILED: no convergence has been attained."); fprintf(logfile, "# FAILED: no convergence has been attained. Simulation ABANDONED."); return(1); } }while(lf>MINLOGF); //RESOLUTION --> <-----RESOLUTION**** //***************************************************************** //Density of states //***************************************************************** double minlogG=gsl_histogram_min_val(logG); gsl_histogram_shift(logG,-minlogG); gsl_histogram_fprintf(dos,logG,"%g","%g"); //***************************************************************** //Thermodynamics //***************************************************************** double beta,A,wT,Zmin_beta; double lGvalue,maxA,betaC,CTMAX=0; double Z_beta,U,U2,CT,F,S; for (beta=0.01;beta<=30;beta+=0.01) { //****************************************************************** //Energy, free-energy, entropy, specific heat and Tc //****************************************************************** maxA=0; for (ia2=0; ia2<NBINS;ia2++) { lGvalue=gsl_histogram_get(logG,ia2); gsl_histogram_get_range(logG,ia2,&lower,&upper); E=(lower+upper)/2; A=lGvalue-beta*E; if (A>maxA) maxA=A; } gsl_histogram_find(logG,EMIN,&i0); Z_beta=0;U=0;U2=0; for (ia2=0; ia2<NBINS;ia2++) { lGvalue=gsl_histogram_get(logG,ia2); gsl_histogram_get_range(logG,ia2,&lower,&upper); E=(lower+upper)/2; A=lGvalue-beta*E-maxA; Z_beta+=exp(A); U+=E*exp(A); U2+=E*E*exp(A); if(ia2==i0) Zmin_beta=exp(A); } wT=Zmin_beta/Z_beta; F=-log(Z_beta)/beta - maxA/beta; U=U/Z_beta; S= (U-F)*beta; U2=U2/Z_beta; CT=(U2-U*U)*beta*beta; if(CT>CTMAX){ CTMAX=CT; betaC=beta; } fprintf(thermodynamics,"%f %f %f %f %f %f %f \n" ,beta,1/beta,F/(double)(SIZE),S/(double)(SIZE), U/(double)(SIZE),CT/(double)(SIZE),wT); } if (SHOWFLAG==1) printf("# BETAc: %f Tc:%f \n",betaC,1/betaC); fprintf(logfile,"# BETAc: %f Tc:%f \n",betaC,1/betaC); //****************************************************************** //canonical distribuition at Tc //****************************************************************** beta=betaC; double distr_canonica; maxA=0; for (ia2=0; ia2<NBINS;ia2++) { lGvalue=gsl_histogram_get(logG,ia2); gsl_histogram_get_range(logG,ia2,&lower,&upper); E=(lower+upper)/2; A=lGvalue-beta*E; if (A>maxA) maxA=A; } for (ia2=0; ia2<NBINS;ia2++) { lGvalue=gsl_histogram_get(logG,ia2); gsl_histogram_get_range(logG,ia2,&lower,&upper); E=(lower+upper)/2; A=lGvalue-beta*E-maxA; distr_canonica=exp(A); fprintf(canonical,"%f %f %f\n", E/(double)(SIZE),distr_canonica,A); } //***************************************************************** // Finalization //***************************************************************** igraph_destroy(&graph); igraph_vector_destroy(&neighbors); igraph_vector_destroy(&result); gsl_matrix_free(issue); gsl_vector_free(current_issue); gsl_vector_free(v1); gsl_vector_free(v0); gsl_matrix_free(sociedade); gsl_rng_free(r); fclose(wlsrange); fclose(dos); fclose(thermodynamics); fclose(canonical); fclose(logfile); return(0); }
int check_multi() { igraph_t g; igraph_vector_t vec; igraph_vector_t eids, eids2; int ret; long int i; igraph_real_t q1[] = { 0,1, 0,1 }; igraph_real_t q2[] = { 0,1, 0,1, 0,1 }; igraph_real_t q3[] = { 1,0, 3,4, 1,0, 0,1, 3,4, 0,1 }; igraph_vector_init(&eids, 0); /*********************************/ igraph_small(&g, /*n=*/ 10, /*directed=*/ 1, 0,1, 0,1, 1,0, 1,2, 3,4, 3,4, 3,4, 3,5, 3,7, 9,8, -1); igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t)); igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/ 1); igraph_vector_sort(&eids); print_vector(&eids, stdout); igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t)); igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 0, /*error=*/ 1); igraph_vector_sort(&eids); print_vector(&eids, stdout); igraph_vector_view(&vec, q2, sizeof(q2) / sizeof(igraph_real_t)); igraph_set_error_handler(igraph_error_handler_ignore); ret=igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/ 1, /*error=*/1); if (ret != IGRAPH_EINVAL) { return 1; } igraph_set_error_handler(igraph_error_handler_abort); igraph_destroy(&g); /*********************************/ /*********************************/ igraph_small(&g, /*n=*/10, /*directed=*/0, 0,1, 1,0, 0,1, 3,4, 3,4, 5,4, 9,8, -1); igraph_vector_view(&vec, q1, sizeof(q1) / sizeof(igraph_real_t)); igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/1, /*error=*/ 1); igraph_vector_sort(&eids); print_vector(&eids, stdout); igraph_vector_view(&vec, q3, sizeof(q3) / sizeof(igraph_real_t)); igraph_set_error_handler(igraph_error_handler_ignore); ret=igraph_get_eids_multi(&g, &eids, &vec, 0, /*directed=*/0, /*error=*/ 1); if (ret != IGRAPH_EINVAL) { return 2; } igraph_set_error_handler(igraph_error_handler_abort); igraph_destroy(&g); /*********************************/ igraph_vector_destroy(&eids); /*********************************/ /* Speed tests */ #define NODES 10000 igraph_barabasi_game(&g, /*n=*/ NODES, /*power=*/ 1.0, /*m=*/ 3, /*outseq=*/ 0, /*outpref=*/ 0, /*A=*/ 1, /*directed=*/ 1, IGRAPH_BARABASI_BAG, /*start_from=*/ 0); igraph_simplify(&g, /*multiple=*/ 1, /*loops=*/ 0, /*edge_comb=*/ 0); igraph_vector_init(&eids, NODES/2); igraph_random_sample(&eids, 0, igraph_ecount(&g)-1, NODES/2); igraph_vector_init(&vec, NODES); for (i=0; i<NODES/2; i++) { VECTOR(vec)[2*i] = IGRAPH_FROM(&g, VECTOR(eids)[i]); VECTOR(vec)[2*i+1] = IGRAPH_TO(&g, VECTOR(eids)[i]); } igraph_vector_init(&eids2, 0); igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 1, /*error=*/ 1); if (!igraph_vector_all_e(&eids, &eids2)) { return 3; } /**/ for (i=0; i<NODES/2; i++) { VECTOR(vec)[2*i] = IGRAPH_TO(&g, VECTOR(eids)[i]); VECTOR(vec)[2*i+1] = IGRAPH_FROM(&g, VECTOR(eids)[i]); } igraph_get_eids_multi(&g, &eids2, &vec, 0, /*directed=*/ 0, /*error=*/ 1); if (!igraph_vector_all_e(&eids, &eids2)) { return 4; } igraph_vector_destroy(&eids); igraph_vector_destroy(&eids2); igraph_vector_destroy(&vec); igraph_destroy(&g); /*********************************/ return 0; }