list<int> suc(int i,int j,int n,int* P,double* &T,int* Suc1,int* Suc2,bool* flag,bool* & leaf,int* & Pre,list<int> &sucL,list<int> &sucI){ list<int> result; int s1=Suc1[j]; int s2=Suc2[j]; if (i==0 && j!=0){ leaf[j]=true; T[j]=T[0];} if (flag[s1]){ Pre[s1]=i; if (leaf[s1]) sucL.push_back(s1); else sucI.push_back(s1); } else{ list<int> l1=suc(i,s1,n,P,T,Suc1,Suc2,flag,leaf,Pre,sucL,sucI); concatPos(l1,result); } if (flag[s2]){ Pre[s2]=i; if (leaf[s2]) sucL.push_back(s2); else sucI.push_back(s2); } else{ list<int> l2=suc(i,s2,n,P,T,Suc1,Suc2,flag,leaf,Pre,sucL,sucI); concatPos(l2,result); } if (j!=i) result.push_back(j); return result; }
void MAC(uint8_t* k, BitstreamIn input, BitstreamOut out) { uint8_t zeroes_32[] = {0,0,0,0}; BitstreamIn input_32_zeroes = {zeroes_32,sizeof(zeroes_32)*8,0}; State initState = suc(k,init(k),&input); output(k,initState,&input_32_zeroes,&out); }
void computeFeuilles(int n,int m,int* & P,int* & Suc1,int* & Suc2,double* & T,bool* & flag,bool* & leaf,stack<int>* &feuilles,list<int> active_set){ int count=0; for (list<int>::iterator iter=active_set.begin();iter!=active_set.end();iter++){ int i = *iter; if (i>=n && i!=m+1 && i!=m+2){ int j=i; list<int> ai; while (!flag[j]){ feuilles[count].push(j); int k=j; j=P[j]; leaf[j]=true; T[j]=T[i]; if (Suc1[j]==k && !flag[Suc2[j]]){ ai.push_back(Suc2[j]); } if (Suc2[j]==k && !flag[Suc1[j]]){ ai.push_back(Suc1[j]); } } for (list<int>::iterator it=ai.begin();it!=ai.end();it++){ concat(feuilles[count],suc(*it,n,P,Suc1,Suc2,leaf,flag,T)); } count++; } } }
void gfx_parse_request(t_env *e, int fd, char *str) { char **req; int i; i = 0; req = (char **)try_void(ft_strsplit(str, ' '), NULL, "malloc"); if (!req[0]) printf("Client #%d (GFX): Invalid request (too few arguments)\n", fd); else { while (i < 9) { if (ft_strequ(req[0], g_gfx_parse[i].cmd)) { if (i < 5) g_gfx_parse[i].fct(e, fd, req, 0); else g_gfx_parse[i].fct(e, fd); break ; } i++; } } if (i == 9) suc(e, fd); ft_free_strtab(req); }
/** * We define the successor function suc which takes a key k ∈ (F 82 ) 8 , a state s and * an input y ∈ F 2 and outputs the successor state s ′ . We overload the function suc * to multiple bit input x ∈ F n 2 which we define as * @param k - array containing 8 bytes **/ State suc(uint8_t* k,State s, BitstreamIn *bitstream) { if(bitsLeft(bitstream) == 0) { return s; } bool lastbit = tailBit(bitstream); return successor(k,suc(k,s,bitstream), lastbit); }
list<int> suc(int i,int n,int* & P,int* & Suc1,int* & Suc2,bool* & leaf,bool* & flag,double* & T){ //flag[i]=false list<int> result; result.push_back(i); leaf[i]=true; T[i]=T[P[i]]; if (i<n){ int s1=Suc1[i]; int s2=Suc2[i]; if (!flag[s1]){ list<int> l1=suc(s1,n,P,Suc1,Suc2,leaf,flag,T); concat(result,l1); } if (!flag[s2]){ list<int> l2=suc(s2,n,P,Suc1,Suc2,leaf,flag,T); concat(result,l2); } } return result; }
void reduceTree(int n,int m,int* & P,double* &T,int* & Pre,int* & Suc1,int* & Suc2,list<int>* &SucL,list<int>* &SucI,bool* flag,bool* &leaf,list<int>* &internal,list<int> active_set){ int count=0; Pre[0]=-1; for (int i=0;i<n;i++){ int s1=Suc1[i]; int s2=Suc2[i]; if (flag[i]){ if ((!leaf[i] || P[i]==-1) && (!flag[s1] || !flag[s2])){ internal[count]=suc(i,i,n,P,T,Suc1,Suc2,flag,leaf,Pre,SucL[i],SucI[i]); count++; } else { Pre[s1]=i; Pre[s2]=i; if (leaf[s1]) SucL[i].push_back(s1); else SucI[i].push_back(s1); if (leaf[s2]) SucL[i].push_back(s2); else SucI[i].push_back(s2); } } else if (leaf[i]){ Pre[s1]=i; Pre[s2]=i; if (leaf[s1]) SucL[i].push_back(s1); else SucI[i].push_back(s1); if (leaf[s2]) SucL[i].push_back(s2); else SucI[i].push_back(s2); } } }
int main(int argc, char *argv[]) { srand(1); cout.precision(10); //-------------------- program parameters --------------------------- // 10 parameters required if(argc != 10) { cerr << "need 10 parameters" << endl; cerr << "santa.x datadir start_file_name BETA cool_speed lon_width lat_width stopping_cond save_file_name N_THREADS" << endl; return 0; } string datadir = string(argv[1]); // data directory // read input solution trips = load_data(datadir + string(argv[2])); for(auto &t:trips) t.update_info(); cout << "total trips " << trips.size() << endl; double ss = totalscore(trips); cout << "initial score: " << ss << endl; int N = trips.size(); // initial temperature and cooling rate BETA = atof(argv[3]); //annealing starting beta = 1/T (temperature) double cooling_speed = atof(argv[4]); //annealing cooling speed // local search range limitation longitude_difference_threshold = atof(argv[5]); latitude_difference_threshold = atof(argv[6]); // stopping condition: stop when number of performed moves in the last iteration is less than a given value (stopping_suc) int stopping_suc = atoi(argv[7]); bool sep = false; //-------------------------------------------------------------------- //initialize the random generator for(int i = 0; i < 32; i++) rs[i] = rand(); prob[1] = prob[2] = prob[3] = 1; NMB_THREADS = atoi(argv[9]); assert(NMB_THREADS <= 16); //don't use too many cores or else wasted double goodresult = min(1.240e13, ss) - 1e7; time_t start = time(NULL); vector<int> suc(prob.size(), 0); // Simulated Annealing procedure int nmbIters = 5000000; for(int i = 1; i < nmbIters; i++) { assert(trips.size() < MAXTRIP); //mtx array size limit for(auto &t:trips) t.update_range(); int Npair = set_working_pairs(trip_pairs, trips, sep, longitude_difference_threshold); // do it in parallel vector<thread> jobs(NMB_THREADS); for(int l = 0; l < NMB_THREADS; l++) jobs[l] = thread(parallel_job, &suc, l); for(int l = 0; l < NMB_THREADS; l++) jobs[l].join(); if(i % 10 == 0) { for(auto &t:trips){ t.update_info(); } //recalculate auxiliary variables to avoid accumulated errors //remove empty trips int b = trips.size(); trips.erase(remove_if(trips.begin(),trips.end(), [](Trip &t){return t.size()<=2;}), trips.end()); int e = trips.size(); if(b != e) cout << " empty trips removed : " << b - e << endl; double cutscore = selfupdateCut(trips, 0); time_t now = time(NULL); cout << " - Estimated time remaining: " << log(2000/BETA) / log(cooling_speed) * difftime(now,start)/i/3600.0 << " hours" << endl; } if(i % 2 == 0) { ss = totalscore(trips); cout << i << " :"; for(auto x:suc) cout << x << ' '; cout << shift11_count << ' '; shift11_count = 0; cout << " score: " << ss << endl; if(ss < goodresult) { cout << "save at beta = " << BETA << endl; save_result(trips,datadir + to_string(int(ss/1e7))+".csv"); goodresult = ss - 1e7; } if(suc[0] < stopping_suc)break; for(int k = 0; k < suc.size(); k++) suc[k] = 0; } BETA *= cooling_speed; } //end time_t end = time(NULL); cout << "time: " << difftime(end,start) << " seconds" << endl; double ss2 = totalscore(trips,true); cout << "check true total score " << ss2 << ' ' << ss2 - ss << endl; int count = 0; for(auto &trip:trips) if(trip.size() > 3) count++; cout << "total trips remained" << count << endl; save_result(trips,datadir+string(argv[8])); return 0; }
//Outer vector: list //Inner vector: triples of nodeids void add_facts(vector<vector<nodeid>> facts) { //std::map<nodeid,std::vector<std::pair<Thing*,Thing*>>> ths_t &ths = *new ths_t; ths_garbage = &ths;///.push_back(ths); //Map each pred to a list of it's subject/object pairs for (auto f: facts) ths[f[1]].push_back({ create_node(f[0]), create_node(f[2])}); coro suc, ouc; //For each pred in the ths: //std::pair<nodeid,std::vector<std::pair<Thing*,Thing*>>> for (auto ff:ths) { //std::vector<std::pair<Thing*, Thing*>> auto &pairs = ff.second; EEE; //char entry = 0; pos_t pi = 0; //Map each pred to a coro on it's subject/object builtins[ff.first].push_back([suc,ouc,pi,pairs,entry](Thing *s_, Thing *o_) mutable{ switch(entry) { case 0: //hrmm.. where is pi being incremented? looks //like this just does pi=0 then exits. /*yea looks like you found a bug this thing looks pretty suboptimal btw...buti guess it should get the job done*/ if (pi < pairs.size())//fixed? { //Generate a unify coro for the subject; //on the fly. //Then run it. suc = unify(s_,&pairs[pi].first); while(suc()){ //Again for the object ouc = unify(o_,&pairs[pi].second); while(ouc()) { //If both succeed then yield //true. //not quite entry = LAST; return true; /* entry = LAST; so...wanna fix this? */ case_LAST:; } } } END; } }); } }