// 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; /* penalty, to improve control. 0 = no penalty */ ld penalty = 0; int lastmoves; int movearound() { indenter_finish im("movearound"); int N = isize(rogueviz::vdata); 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); ld with_penalty = llo2[d]; if(penalty) with_penalty += cgi.expansion->get_descendants(mc->lev).log_approx() * penalty; if(penalty) with_penalty -= cgi.expansion->get_descendants(mc2[d]->lev).log_approx() * penalty; if(with_penalty > bestllo) bestd = d, bestllo = with_penalty; 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); if(penalty) println(hlog, "penalty = ", penalty); 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; int N = isize(rogueviz::vdata); {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 * rogueviz::embeddings::cont_logistic.R, factor * rogueviz::embeddings::cont_logistic.T); saved_logistic = current_logistic; // for(int u=0; uqty[j]) distlimit = min(j+2, BOXSIZE-1); println(hlog, "Using distlimit = ", distlimit); } struct dhrg_embedding : public rogueviz::embeddings::tiled_embedding { pair as_location(int id) override { return { vertices[id]->ascell(), C0 }; } ld distance(int i, int j) override { return quickdist(vertices[i], vertices[j]); } ld zero_distance(int i) override { return vertices[i]->lev; } void save(fhstream& f) override { int N = isize(rogueviz::vdata); 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); vdata[i].m->at = Id; #else hyperpoint T0 = rogueviz::embeddings::current->as_hyperpoint(i); vertices[i] = find_mycell_by_path(computePath(T0)); #endif pb++; // printf("%s %s\n", vdata[i].name.c_str(), computePath(vdata[i].m->base).c_str()); } } recycle_compute_map(); preparegraph(); rogueviz::embeddings::enable_embedding(std::make_shared()); } 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 load_embedded(const string& s) { if(s == "-") return graph_from_rv(); if(true) { int N = isize(rogueviz::vdata); progressbar pb(N, "reading embedding"); vertices.resize(N, NULL); fhstream f(s, "rt"); while(true) { string who = scan(f); if(who == "") break; string where = scan(f); if(where == "X") where = ""; vertices[rogueviz::labeler.at(who)] = find_mycell_by_path(where); pb++; } for(int i=0; i()); } }