// local search namespace dhrg { int newmoves = 0; int iterations = 0; int distlimit; void dispnewmoves() { if(newmoves == 0) printf("."); else if(newmoves < 10) printf("%c", '0'+newmoves); else printf("%c", 'a' + newmoves/10); newmoves = 0; } vector tomove; bool smartmove = false; bool dorestart = false; int lastmoves; int movearound() { indenter_finish im("movearound"); int total = 0; if(smartmove) for(bool b: tomove) if(b) total++; if(total == 0) { tomove.resize(0), tomove.resize(N, true); } int moves = 0; ld llo = loglik_chosen(); // int im = 0; {progressbar pb(N, "tomove: " + its(total) + " (last: " + its(lastmoves) + ")"); for(int i=0; ilev >= distlimit) continue; tallyedgesof(i, 1, mc2[d]); add_to_tally(mc2[d], 1, 0); if(lc_type == 'C') add_to_set(mc2[d], 1, 0); llo2[d] = loglik_chosen(); if(lc_type == 'C') add_to_set(mc2[d], -1, 0); if(llo2[d] > bestllo) bestd = d, bestllo = llo2[d]; add_to_tally(mc2[d], -1, 0); tallyedgesof(i, -1, mc2[d]); } if(bestd >= 0) { moves++; newmoves++; vertices[i] = mc = mc2[bestd]; llo = llo2[bestd]; tomove[i] = true; for(auto p: rogueviz::vdata[i].edges) { int j = p.second->i ^ p.second->j ^ i; tomove[j] = true; } } tallyedgesof(i, 1, mc); add_to_tally(mc, 1, 0); add_to_set(mc, 1, 0); pb++; }} // dispnewmoves(); println(hlog, " moves = ", moves); return lastmoves = moves; } int move_restart() { indenter_finish im("move_restart"); ld llo = loglik_chosen(); array, 2> distances_map; for(int a=0; a<2; a++) for(int b=0; b<128; b++) distances_map[a][b] = 0; int moves = 0; // int im = 0; {progressbar pb(N, "move_restart"); for(int i=0; i bestllo) bestllo = newllo, mc2 = mcn, changed = true; } if(changed) whereto = mc2; } bool better = bestllo > llo; distances_map[better][quickdist(whereto, mc)]++; if(better) { llo = bestllo; vertices[i] = mc = whereto; moves++; } tallyedgesof(i, 1, mc); add_to_tally(mc, 1, 0); add_to_set(mc, 1, 0); pb++; }} // dispnewmoves(); println(hlog, " moves = ", moves); print(hlog, " stats:"); for(int a=0; a<2; a++) for(int b=0; b<128; b++) { int d = distances_map[a][b]; if(d) print(hlog, hr::format(" %d/%d:%d", a,b, d)); } println(hlog, "\n"); return lastmoves = moves; } // 7: 12694350 3847975716 // 6: 39472959 11969080911 void verifycs() { long long edgecs = 0, totalcs = 0; for(int u=0; uget_growth()); current_logistic.setRT(factor * graph_R, factor * graph_T); saved_logistic = current_logistic; // for(int u=0; uqty[j]) distlimit = min(j+2, BOXSIZE-1); println(hlog, "Using distlimit = ", distlimit); } void read_graph_full(const string& fname) { using namespace rogueviz; memoryInfo(); if(true) { indenter_finish im("Read graph"); // N = isize(vdata); read_graph(fname, false, false, false); vertices.resize(N); progressbar pb(N, "Translating to cells"); for(int i=0; igamestart(); hyperpoint T0 = vdata[i].m->at * C0; virtualRebase2(vdata[i].m->base, T0, true); vertices[i] = find_mycell(vdata[i].m->base); #else vertices[i] = find_mycell_by_path(computePath(vdata[i].m->at)); #endif vdata[i].m->at = Id; pb++; // printf("%s\n", computePath(vdata[i].m->base).c_str()); } } recycle_compute_map(); preparegraph(); } void graph_from_rv() { using namespace rogueviz; memoryInfo(); vertices.resize(N); progressbar pb(N, "converting RogueViz to DHRG"); for(int i=0; ibase); #else auto path1 = computePath(vdata[i].m->base); vertices[i] = find_mycell_by_path(path1); #endif vdata[i].m->at = Id; pb++; } preparegraph(); } bool iteration() { iterations++; indenter_finish im("Iteration #" + its(iterations)); int m = movearound(); if(!m && dorestart) m = move_restart(); if(!m) return false; fix_logistic_parameters(current_logistic, loglik_logistic, "logistic", 1e-6); writestats(); fflush(stdout); return true; } void embedder_loop(int max) { indenter_finish im("embedder_loop"); ld lastopt = loglik_chosen(); while(max-- && iteration()) { ld curopt = loglik_chosen(); println(hlog, "current = %", curopt); if(curopt <= lastopt) { println(hlog, "Not enough improvement -- breaking"); break; } lastopt = curopt; } } void save_embedding(const string s) { FILE *f = fopen(s.c_str(), "wt"); for(int i=0; i ids; for(int i=0; i