void simulate(int arg) { CLOCK::time_point mstart_time; CLOCK::time_point mstop_time; MICROSECONDS mtime_diff_us; Ran ran; int status; int ran_num; bool ok; bool time_exceeded; ran_num = arg; time_exceeded = false; ran.init(ran_num); ran.conn_mme(); while (1) { // Run duration check g_utils.time_check(g_start_time, g_req_dur, time_exceeded); if (time_exceeded) { break; } // Start time mstart_time = CLOCK::now(); // Initial attach ran.initial_attach(); // Authentication ok = ran.authenticate(); if (!ok) { TRACE(cout << "ransimulator_simulate:" << " autn failure" << endl;) return; }
NxReal DrawFromBoundedPowerLaw( float a0, float a1, float alfa ) { // using the inverse transformation law for probabilities. see for example, NR3 p.362, although the explanation sucks. // for the pdf p(a)=C*a^-alfa, between a0 and a1. float t=gRNG.doub(); float onemalfa=1-alfa; float a = NxMath::pow(t*(NxMath::pow(a1,onemalfa)-NxMath::pow(a0,onemalfa)) + NxMath::pow(a0,onemalfa) , 1.0f/(onemalfa)); return a; }
bool ThrowStone() { bool success=false; // figure out grain size. draw a size from a distribution dN = C * D^-alpha dD (Asphaug 2009 AnnRev) NxReal drawnSize = DrawFromBoundedPowerLaw(gsd[0],gsd[1],gsd[2]); // CHOOSE GOOD VALUES HERE // make a random grain, use default material NxActor* actor=NULL; actor=CreateConvexPolyGrain(NxVec3(0,2,0),gDefaultDensity,drawnSize,false,(8 + gRNG.int32()%8)); if (actor) { actor->setName("rubble"); if (gRavitatorThresholdMass>0 && actor->getMass()>gRavitatorThresholdMass) actor->setName("boulder"); } // randomize and let fly if (actor) { ReOrientActor(actor); SpinActor(actor,1); LaunchActor(actor,1); } success=true; return success; }
/* Utility method to boot-strap the current node set size. Randomly select 80% of nodes from current node set. */ void getSubSample( vector<int> &keys ) { vector<node> Cn; vector<edge> Celist; for(int i=1; i<Gn.size(); i++) { Gn[i].c = -1; Cn.push_back(Gn[i].Clone()); } for(int i=0; i<Gelist.size(); i++) { Celist.push_back(Gelist[i].Clone()); } vector<int> rmKeys; vector<int> rmEdgeKeys; int N = Cn.size(); int sample_size = 0; double subN = 0.8; //set to sample 80% of the current node set size. //generate a sub-sample using bootstrapped method while( sample_size < N*subN ) { int rnd_ind = (int)floor( _rand.doub() * (N+1) ); if(Cn[rnd_ind].c == -1) { Cn[rnd_ind].c = 1; sample_size++; } } for(int i=0; i<Cn.size(); i++) { if(Cn[i].c == -1) { keys[i] = -1; rmKeys.push_back(Cn[i].k); } } for(int i=0; i<rmKeys.size(); i++) { for(int k=0; k<Celist.size(); k++) { if( Celist[k].so == rmKeys[i]) { Celist[k].so = -1; Celist[k].si = -1; } } for(int k=0; k<Celist.size(); k++) { if( Celist[k].si == rmKeys[i]) { Celist[k].so = -1; Celist[k].si = -1; } } } reader.enumerateNodeList(Cn, Celist, n, elist); Cn.swap(emptyn); Celist.swap(emptye); }
//--- MAIN PROGRAM //------------------------------------------------------------------------------------- int main(int argc, char * argv[]) { int seed; int a_type; int w_type; string title; string if_weighted; string if_help; const char *file_network; const char *file_names; if ( argc != 5 ) { printHelpMessage( argv[0] ); } if_help = argv[1]; if( if_help.compare("-h") == 0 || if_help.compare("-help") == 0 ) { printHelpMessage( argv[0] ); } seed = atoi(argv[1]); cout << "> seed is " << seed << endl; //--- Initialize random seed: _rand.setSeed(seed); a_type = atoi(argv[2]); if( a_type < 1 || a_type > 3 ) { cout << "argument 2: the type of algorithm to run needs to be either (1,2,3): " << endl; cout << " : 1 = Geodesic edge Betweenness" << endl; cout << " : 2 = Random edge Betweenness" << endl; cout << " : 3 = Spectral Betweenness" << endl; exit(1); } switch(a_type) { case 1: cout << "> Using Geodesic edge Betweenness." << endl; title = "Geodesic edge Betweenness."; break; case 2: cout << "> Using Random edge Betweenness." << endl; title = "RandomWalk edge Betweenness."; break; case 3: cout << "> Using Spectral Betweenness." << endl; title = "Spectral Betweenness."; break; default: break; } if_weighted = argv[3]; if( if_weighted.compare("w") == 0 ) { w_type = 3; cout << "> Using a weighted network " << endl; } else { if( if_weighted.compare("nw") == 0 ) { w_type = 2; cout << "> Using a non-weighted network " << endl; } else { cout << "argument 3: specify if network file is weighted or not: " << endl; cout << " : w = Using a weighted network file " << endl; cout << " : nw = Using a non-weighted network file " << endl; exit(1); } } file_network = argv[4]; //--- Default values for parameters which may be modified from the commandline ihelper = Helper(); reader.readFile(file_network, w_type); Gn = reader.getNodeSet(); Gelist = reader.getEdgeSet(); vector<int> key_listi; vector<int> key_listj; vector<int> key_listk; cout << "> The Global node list..." << endl; for(int i=1; i<Gn.size(); i++) { key_listi.push_back(Gn[i].ID); key_listj.push_back(Gn[i].ID); key_listk.push_back(-1); Gn[i].print(); Gn[i].printEdges(); } //--- To use getSubSample, comment following two lines, and //--- uncomment getSubSample(key_listj). n = Gn; elist = Gelist; //getSubSample(key_listj); //cout << "The sub-node list ... " << endl; //for(int i=1; i<n.size(); i++){ //n[i].print(); //n[i].printEdges(); //} cout << "> The Global edge list..." << endl; for(int i=0; i<elist.size(); i++) { elist[i].print(); } forcytoscape = new fstream("OUT/communities_newman.txt",ios_base::out); (*forcytoscape) << "communities" << endl; removededges = new fstream("OUT/removededges.txt",ios_base::out); (*removededges) << "Removed Edges" << endl; (*removededges) << "so \t IDso \t si \t IDsi \t we \t Globalweight \t key" << endl; totallist = ihelper.cloneEdgeList(elist); com_max = 0; specQ = 0.0; double Q = 0.0; double Q_SD = 0.0; double Q_old = 0.0; double Q_SD_old = 0.0; int loop = elist.size(); int E = loop; double Q_max = 0.0; double Q_limit = 1.0; bool stopping = false; int N = n.size()-1; R.resize(N,N); Ri.resize(N,N); A.resize(N,N); Ai.resize(N,N); Bi.resize(N,N); C.resize(N); S.resize(N,1); V.resize(N,1); T.resize(N,N); Ti.resize(N,N); Rc.resize((N-1),(N-1)); Vi.resize(C.size(),1); B.resize(N,N); Bm.resize(N,N); Bgi.resize(N,N); keys_p.resize(N); keys_n.resize(N); u.resize(N,N); //eigenvectors betai.resize(N);//eigenvalues SI.resize(N,2); si.resize(N); visited.resize(N); setupMatrices(); cout << "> Running " << title.c_str() << endl; cstart = clock(); if( a_type == 3 ) { //--- Calculate betweenness using the Spectral algorithm calculateSpectralModularity(); } else { while( loop !=0 && !stopping ) { int old_max_com = com_max; //--- Calculate betweenness using Geodesic or RandomWalk algorithms if( a_type == 1 ) calculateEdgeBetweennessGeodesic(); else calculateEdgeBetweennessRandom(); //--- Calculate the Modularity Q_old = Q; Q_SD_old = Q_SD; Q = 0.0; Q_SD = 0.0; Modularity(Q, Q_SD); //--- Store networks state if Modularity has increased during this iteraction if(com_max > old_max_com) { vec_mod.push_back(Q); vec_mod_err.push_back(Q_SD); vec_com_max.push_back(com_max); vec_nodes.push_back(storeNodes()); } //--- Record the maximum Modularity value if( Q > Q_max ) { Q_max = Q; } else { if( Q_max > 0.0 && (Q_max - Q)/Q_max > Q_limit ) stopping = true; } //--- Find edge with maximum edge betweenness score and remove edge _max; _max = totallist[1].Clone(); for(int i=1; i<totallist.size(); i++) { if( totallist[i].removed == false ) { if(totallist[i].we >= _max.we) { if(totallist[i].we > _max.we) _max = totallist[i]; else { int rdm = rand()%2; if(rdm == 1) _max = totallist[i]; } } } totallist[i].we = 0; } //--- Record the removed edges. _max.print( removededges ); n[elist[_max.key-1].so].removeEdge(_max.key); n[elist[_max.key-1].si].removeEdge(_max.key); n[elist[_max.key-1].so].setDegree( (n[elist[_max.key-1].so].getDegree() - 1) ); n[elist[_max.key-1].si].setDegree( (n[elist[_max.key-1].si].getDegree() - 1) ); totallist[_max.key].removed = true; elist[_max.key-1].removed = true; --loop; //--- Calculate the remaining processor time DrawProgressBar( 20, ((double)E - (double)loop)/(double)E ); } } //--- Recored the CPU-time taken cend = clock(); double cpu_time_used = ((double) (cend - cstart)) / CLOCKS_PER_SEC; cout << "" << endl; cout << "> cputime: " << cpu_time_used << " seconds " << endl; cout << "> Network (nodes): " << N << " (edges): " << E << endl; if( a_type != 3 ) { //--- Print all stored Modularity values modularityscore = new fstream("OUT/modularityscore.txt",ios_base::out); (*modularityscore) << title.c_str() << endl; for(int i=0; i<vec_mod.size(); i++) { (*modularityscore) << vec_mod[i] << " " << vec_mod_err[i] << " " << vec_com_max[i] << endl; } modularityscore->close(); int ind = findMax(vec_mod); int com = 1; int _size = 0; int c_max = com_max; //--- Print node communities for maximum Modularity value, for Geodesic or RandomWalk runs communityout = new fstream("OUT/communityout.txt",ios_base::out); (*communityout) << "Max Q: " << vec_mod[ind] << " +- " << vec_mod_err[ind] << endl; (*communityout) << "cputime: " << cpu_time_used << " seconds " << endl; (*communityout) << "Network (nodes): " << N << " (edges): " << E << endl; while(com<(c_max+1)) { _size = 0; for(int i=0; i<vec_nodes[ind].size(); i++) { if(vec_nodes[ind][i].c == com ) { (*communityout) << vec_nodes[ind][i].ID << "\t" << vec_nodes[ind][i].c << endl; //vec_nodes[ind][i].print( communityout ); _size++; } } if(_size != 0) (*communityout) << "community: " << com << " size: " << _size << endl; com++; } for(int i=0; i<vec_nodes[ind].size(); i++) { for(int j=0; j<key_listi.size(); j++) { if(vec_nodes[ind][i].ID == key_listi[j]) { key_listk[j] = vec_nodes[ind][i].c; break; } } } //--- Print node communities for maximum Modularity for the consensus matrix consensusout = new fstream("OUT/consensusout.txt",ios_base::out); (*consensusout) << "key list" << endl; for(int i=0; i<key_listi.size(); i++) { if(key_listk[i] == -1 && key_listj[i] != -1) key_listj[i] = -1; (*consensusout) << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl; //(*consensusout) << key_listi[i] << " = " << key_listk[i] << endl; (*forcytoscape) << key_listi[i] << " = " << key_listk[i] << endl; cout << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl; } } else { int com = 1; int _size = 0; int c_max = maxCommunity(); //--- Store node communities for maximum Modularity for the Spectral Modularity run communityout = new fstream("OUT/communityout.txt",ios_base::out); (*communityout) << "communityout" << endl; (*communityout) << "Max Q: " << specQ << endl; (*communityout) << "cputime: " << cpu_time_used << " seconds " << endl; (*communityout) << "Network (nodes): " << N << " (edges): " << E << endl; while(com<(c_max+1)) { _size = 0; for(int i=0; i<n.size(); i++) { if(n[i].c == com ) { n[i].print( communityout ); _size++; } } if(_size != 0) (*communityout) << "community: " << com << " size: " << _size << endl; com++; } for(int i=1; i<n.size(); i++) { for(int j=0; j<key_listi.size(); j++) { if(n[i].ID == key_listi[j]) { key_listk[j] = n[i].c; break; } } } //--- Print node communities for maximum Modularity the consensus matrix consensusout = new fstream("OUT/consensusout.txt",ios_base::out); (*consensusout) << "key list" << endl; for(int i=0; i<key_listi.size(); i++) { if(key_listk[i] == -1 && key_listj[i] != -1) key_listj[i] = -1; (*consensusout) << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl; //(*consensusout) << key_listi[i] << " = " << key_listk[i] << endl; (*forcytoscape) << key_listi[i] << " = " << key_listk[i] << endl; cout << key_listi[i] << " " << key_listj[i] << " " << key_listk[i] << endl; } } //--- Remove data structures communityout->close(); forcytoscape->close(); vec_mod.clear(); vec_mod_err.clear(); vec_nodes.clear(); exit(1); }
int fun(){ int k; bool flag; for(k=1; k<=num; k++){ sum[k].undef = true; } sum[0].l = 0; sum[0].r = 0; sum[0].undef = false; Ran one; flag = true; while( flag ){ flag = false; for(int i=0; i<cnt; i++){ if(sum[tree[i].s].undef && sum[tree[i].n].undef){ continue; } if(sum[tree[i].s].undef){ flag = true; one = sum[tree[i].n]; if(tree[i].cmp == 1){ one.less(-tree[i].v); } else { one.great(-tree[i].v); } sum[tree[i].s] = one; } else if(sum[tree[i].n].undef){ flag = true; one = sum[tree[i].s]; if(tree[i].cmp ==1){ one.great(tree[i].v); } else { one.less(tree[i].v); } sum[tree[i].n] = one; } else { // detect conflicts. one = sum[tree[i].s]; if(tree[i].cmp ==1){ one.great(tree[i].v); } else { one.less(tree[i].v); } one.inter(sum[tree[i].n]); if(one.r < one.l) return 0; if(sum[tree[i].n] != one){ sum[tree[i].n] = one; flag = true; } one = sum[tree[i].n]; if(tree[i].cmp == 1){ one.less(-tree[i].v); } else { one.great(-tree[i].v); } one.inter(sum[tree[i].s]); if(one != sum[tree[i].s]){ sum[tree[i].s] = one; flag = true; } } } if(flag == false){ for(k=1;k<=num;k++){ if(sum[k].undef){ flag = true; sum[k].undef = false; sum[k].l = sum[k].r = 0; break; } } } } return 1; }