1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2026-04-23 09:31:23 +00:00

rogueviz:: sag:: use the new logging system

This commit is contained in:
Zeno Rogue
2025-10-19 16:30:47 +02:00
parent bbd493f8f4
commit ceb3be4228
6 changed files with 82 additions and 68 deletions

View File

@@ -68,7 +68,8 @@ void saiter() {
if(should_good) {
auto dcost = cost;
compute_cost();
println(hlog, "dcost=", dcost, " change=", change, " cost=", cost, " error = ", dcost + change - cost);
if(debug_progress)
println(hlog, "dcost=", dcost, " change=", change, " cost=", cost, " error = ", dcost + change - cost);
if(abs(dcost + change - cost) > .1) throw hr_exception("dcost fail");
cost = dcost;
}
@@ -116,7 +117,6 @@ int hillclimb() {
int checkmark_hillclimb() {
compute_cost();
if(cost > checkmark_cost) {
println(hlog, "checkmark failed");
throw hr_exception("checkmark failed");
return 0;
}
@@ -144,7 +144,7 @@ void dofullsa(ld satime) {
if(t2 - tl > view_each * .98) {
tl = t2;
println(hlog, format("it %12lld temp %7.4f [1/e at %13.6f] cost = %f ",
if(debug_progress) println(hlog, format("it %12lld temp %7.4f [1/e at %13.6f] cost = %f ",
numiter, double(sag::temperature), (double) exp(sag::temperature),
double(sag::cost)));
}
@@ -205,23 +205,12 @@ void dofullsa_iterations(long long saiter) {
if(cpct > lpct && output_fullsa) {
lpct = cpct;
if(debug_progress)
println(hlog, format("it %12lld ratio %6.3f temp %8.4f step %9.3g cost %9.2f R=%8.4f T=%8.4f",
numiter, last_ratio, double(sag::temperature), (double) exp(sag::temperature), cost, lgsag.R, lgsag.T));
}
/* if(numiter % 10000 == 0) {
auto t2 = SDL_GetTicks();
if(int(t2 - t1) > view_each) {
t1 = t2;
println(hlog, format("it %12Ld temp %6.4f [1/e at %13.6f] cost = %f ",
numiter, double(sag::temperature), (double) exp(sag::temperature),
double(sag::cost)));
}
} */
}
// println(hlog, "after dofullsa_iterations, cost = ", double(sag::cost));
temperature = -5;
sagmode = sagOff;
create_viz();

View File

@@ -13,6 +13,8 @@ namespace sag {
namespace cells {
debugflag debug_sag_cells("sag_cells");
bool angular = false;
using subcell = pair<cell*, int>;
@@ -79,6 +81,7 @@ struct sagdist_t {
#ifdef LINUX
void map(string fname) {
DEBBI(debug_init_sag, ("map " + fname));
clear();
fd = open(fname.c_str(), O_RDONLY | O_LARGEFILE);
if(fd == -1) throw hr_exception("open failed in map");
@@ -92,12 +95,12 @@ struct sagdist_t {
}
tab = (distance*) (((char*)tabmap) + 8);
println(hlog, "test: ", test());
if(debug_init_sag) println(hlog, "test: ", test());
}
#endif
void load_no_map(string fname) {
println(hlog, "clearing old map");
DEBBI(debug_init_sag, ("load_no_map ", fname));
clear();
#ifdef O_BINARY
fd = open(fname.c_str(), O_RDONLY | O_BINARY);
@@ -122,7 +125,7 @@ struct sagdist_t {
if(block <= 0) throw hr_exception("file error reading table");
offset += block;
}
println(hlog, "test: ", test());
if(debug_init_sag) println(hlog, "test: ", test());
::close(fd); fd = 0;
}
@@ -156,13 +159,15 @@ struct sagdist_t {
}
void save(string fname) {
DEBBI(debug_init_sag, ("save ", fname));
fd = open(fname.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
if(!fd) return file_error(fname);
if(write(fd, &N, 8) < 8) throw hr_exception("write error");
size_t size = N*N*sizeof(distance);
#ifdef LINUX
println(hlog, "size is ", hr::format("%zd", size));
if(debug_init_sag) println(hlog, "size is ", hr::format("%zd", size));
#else
println(hlog, "size is ", hr::format("%lld", (long long) size));
if(debug_init_sag) println(hlog, "size is ", hr::format("%lld", (long long) size));
#endif
char *p = (char*) tab;
while(size) {
@@ -170,7 +175,7 @@ struct sagdist_t {
if(written <= 0) throw hr_exception("bad written");
p += written; size -= written;
}
println(hlog, "test: ", test());
if(debug_init_sag) println(hlog, "test: ", test());
::close(fd);
}
@@ -197,7 +202,8 @@ vector<hyperpoint> subcell_points;
void generate_subcellpoints() {
start_game();
subcell_points.clear();
println(hlog, currentmap->get_cellshape(cwt.at).vertices_only);
if(debug_sag_cells)
println(hlog, currentmap->get_cellshape(cwt.at).vertices_only);
ld mx = 1, my = 1, mz = 1;
if(sol) mx = my = mz = log(2);
for(int x=0; x<4; x++)
@@ -205,7 +211,8 @@ void generate_subcellpoints() {
for(int z=0; z<4; z++) if((x&1) == (z&1)) {
subcell_points.push_back(point31(mx * (x+.5-2)/4, my * (y+.5-2)/4, mz * (z+.5-2)/4));
}
println(hlog, subcell_points);
if(debug_sag_cells)
println(hlog, subcell_points);
}
void ensure_subcell_points() {
@@ -259,8 +266,8 @@ void compute_dists() {
sagdist.load(distance_file);
}
else if(gdist_prec && dijkstra_maxedge) {
DEBBI(debug_init_sag, ("Computing Dijkstra distances..."));
sagdist.init(N, N);
println(hlog, "Computing Dijkstra distances...");
vector<vector<pair<int, ld>>> dijkstra_edges(N);
for(int i=0; i<N; i++) {
celllister cl(sagcells[i].first, dijkstra_maxedge, 50000, nullptr);
@@ -271,7 +278,7 @@ void compute_dists() {
parallelize(N, [&] (int a, int b) {
vector<ld> distances(N);
for(int i=a; i<b; i++) {
if(i % 500 == 0) println(hlog, "computing dijkstra for ", i , "/", N);
if(debug_progress && i % 500 == 0) println(hlog, "computing dijkstra for ", i , "/", N);
for(int j=0; j<N; j++) distances[j] = HUGE_VAL;
std::priority_queue<pair<ld, int>> pq;
auto visit = [&] (int i, ld dist) {
@@ -291,26 +298,25 @@ void compute_dists() {
return 0;
}
);
println(hlog, "N0 = ", neighbors[0]);
println(hlog, "N1 = ", neighbors[1]);
if(debug_init_sag)
println(hlog, "N0 = ", neighbors[0], " N1 = ", neighbors[1]);
}
else if(gdist_prec) {
DEBBI(debug_init_sag, ("Computing distances... (N=", N, ")"));
sagdist.init(N, N);
println(hlog, "Computing distances... (N=", N, ")");
ld mx = 1;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++) {
ld d = pdist(cellpoint[i], cellpoint[j]);
sagdist[i][j] = (d + .5) * gdist_prec;
if(d > mx) {
if(d > mx && debug_sag_cells)
println(hlog, kz(cellpoint[i]), kz(cellpoint[j]), " :: ", mx = d);
}
}
}
else {
println(hlog, "no gdist_prec");
DEBBI(debug_init_sag, ("no gdist_prec"));
sagdist.init(N, N);
for(int i=0; i<N; i++) {
auto sdi = sagdist[i];
@@ -324,7 +330,8 @@ void compute_dists() {
max_sag_dist = 0;
for(auto x: sagdist) max_sag_dist = max<int>(max_sag_dist, x);
max_sag_dist++;
println(hlog, "max_sag_dist = ", max_sag_dist);
if(debug_init_sag)
println(hlog, "max_sag_dist = ", max_sag_dist);
}
bool legacy;
@@ -378,14 +385,15 @@ void compute_creq_neighbors() {
max_sag_dist = 0;
for(auto x: sagdist) max_sag_dist = max<int>(max_sag_dist, x);
max_sag_dist++;
println(hlog, neighbors[0]);
if(debug_init_sag)
println(hlog, "the neighors of 0 are ", neighbors[0]);
hlog.flush();
}
vector<vector<pair<ld, subcell>>> dijkstra_edges;
void find_cells() {
println(hlog, "cellcount = ", cellcount);
DEBBI(debug_init_sag, ("find_cells with ", cellcount, " cells"));
ensure_subcell_points();
struct qitem {
ld dist; subcell sc; transmatrix T;
@@ -405,7 +413,10 @@ void find_cells() {
visit(subcell{cwt.at,0}, 0, Id);
ld maxdist0 = 0;
for(int i=0;; i++) {
if(pq.empty()) { println(hlog, "no more"); break; }
if(pq.empty()) {
if(debug_init_sag) println(hlog, "all cells listed");
break;
}
auto p = pq.top();
pq.pop();
ld dist = p.dist;
@@ -419,7 +430,8 @@ void find_cells() {
sagsubcell_point.push_back(T * subcell_points[sc.second]);
sagsubcell_inv.push_back(inverse(T));
ids[sc] = i;
println(hlog, "cell ", i, " is ", sc, " at ", sagsubcell_point.back(), " in distance ", dist);
if(debug_sag_cells)
println(hlog, "cell ", i, " is ", sc, " at ", sagsubcell_point.back(), " in distance ", dist);
if(dijkstra_maxedge) {
dijkstra_edges.emplace_back();
@@ -459,7 +471,8 @@ void find_cells() {
}
}
int SN = isize(sagcells);
println(hlog, "number of cells found: ", SN, " dijkstra_maxedge = ", dijkstra_maxedge);
if(debug_init_sag)
println(hlog, "number of cells found: ", SN, " dijkstra_maxedge = ", dijkstra_maxedge);
all_disk_cells_sorted = {};
for(auto p: ids) if(all_disk_cells_sorted.empty() || p.first.first != all_disk_cells_sorted.back()) all_disk_cells_sorted.push_back(p.first.first);
@@ -467,7 +480,8 @@ void find_cells() {
}
void init_cell_request() {
println(hlog, "generating on cell request");
DEBBI(debug_init_sag, ("generating on cell request"));
find_cells();
if(isize(subcell_points) == 1) {
@@ -479,18 +493,18 @@ void init_cell_request() {
sagdist.init(SN, 0);
if(!dijkstra_maxedge) {
println(hlog, "computing sagdist ...");
DEBBI(debug_init_sag, ("computing sagdist ..."));
parallelize(SN, [&] (int a, int b) {
for(int i=a; i<b; i++) {
for(int j=0; j<SN; j++) {
ld dist = pdist(sagsubcell_point[i], sagsubcell_point[j]);
sagdist[i][j] = int(dist * gdist_prec + 0.5);
if(i < j && sagdist[i][j] == 0) println(hlog, "for ", tie(i,j), " pdist computed as ", dist);
if(i < j && sagdist[i][j] == 0 && debug_sag_cells)
println(hlog, "for ", tie(i,j), " pdist computed as ", dist);
}
}
return 0;
});
println(hlog, "... done");
}
else {
vector<vector<pair<ld, int>>> dijkstra_edges_2;
@@ -500,7 +514,7 @@ void init_cell_request() {
parallelize(SN, [&] (int a, int b) {
vector<ld> distances(SN);
for(int i=a; i<b; i++) {
if(i % 500 == 0) println(hlog, "computing dijkstra for ", i , "/", SN);
if(debug_progress && i % 500 == 0) println(hlog, "computing dijkstra for ", i , "/", SN);
for(int j=0; j<SN; j++) distances[j] = HUGE_VAL;
std::priority_queue<pair<ld, int>> pq;
auto visit = [&] (int i, ld dist) {
@@ -536,7 +550,7 @@ void init_cells() {
if(cell_request) {
if(distance_file != "") {
println(hlog, "loading graph ", distance_file);
DEBBI(debug_init_sag, ("loading graph ", distance_file));
sagdist.load(distance_file);
if(distance_only) {
sagcells.resize(sagdist.N, subcell{nullptr, 0});

View File

@@ -37,7 +37,7 @@ void compute_cost();
void prepare_graph() {
int DN = isize(sagid);
println(hlog, "prepare_graph with DN = ", DN);
DEBBI(debug_init_sag, ("prepare_graph with DN = ", DN));
set<pair<int, int>> alledges;
for(auto e: sagedges) {
@@ -134,8 +134,9 @@ void create_viz() {
/** save the SAG solution (sagid) */
void save_sag_solution(const string& fname) {
if(!(state & SS_DATA)) throw hr_exception("save_sag_solution with no data");
DEBBI(debug_init_sag, ("Saving the sag solution to: ", fname));
FILE *f = fopen(fname.c_str(), "wt");
if(!f) throw hr_exception("failed to save SAG solution");
if(!f) return file_error(fname);
for(int i=0; i<isize(sagid); i++)
fprintf(f, "%s;%d\n", vdata[i].name.c_str(), sagid[i]);
fclose(f);
@@ -144,9 +145,9 @@ void save_sag_solution(const string& fname) {
/** load the SAG solution (sagid) */
void load_sag_solution(const string& fname) {
if(!(state & SS_DATA)) throw hr_exception("load_sag_solution with no data");
printf("Loading the sag from: %s\n", fname.c_str());
DEBBI(debug_init_sag, ("Loading the sag solution from: ", fname));
FILE *sf = fopen(fname.c_str(), "rt");
if(!sf) throw hr_exception("failed to load SAG solution");
if(!sf) return file_error(fname);
int SN = isize(sagcells);
if(sf) while(true) {
string lab;
@@ -177,10 +178,12 @@ void load_sag_solution(const string& fname) {
void load_sag_solution_basic(const string& fname) {
if(!(state & SS_DATA)) throw hr_exception("load_sag_solution_basic with no data");
DEBBI(debug_init_sag, ("Loading the sag solution (basic) from: ", fname));
FILE *f = fopen(fname.c_str(), "rt");
for(auto& i: sagid) if(fscanf(f, "%d", &i) < 1) throw hr_exception("read error in load_sag_solution_basic");
if(!f) return file_error(fname);
for(auto& i: sagid) if(fscanf(f, "%d", &i) < 1) return file_format_error(fname);
fclose(f);
println(hlog, "loaded sagid = ", sagid);
if(debug_init_sag) println(hlog, "loaded sagid = ", sagid);
prepare_graph();
create_viz();
@@ -205,12 +208,13 @@ void after_data() {
void read_weighted(const char *fname) {
if(state & SS_DATA) return;
DEBBI(debug_init_sag, ("Loading the weighted daga for sag from: ", fname));
state |= SS_WEIGHTED;
init_cells();
maxweight = 0;
fhstream f(fname, "rt");
if(!f.f) throw hr_exception("readsag_weighted: failed to open");
if(!f.f) return file_error(fname);
while(!feof(f.f)) {
string l1, l2;
@@ -238,7 +242,6 @@ void read_weighted(const char *fname) {
}
after:
println(hlog, "weighted graph ", fname, " read successfully");
after_data();
}
@@ -246,10 +249,11 @@ void read_weighted(const char *fname) {
void read_unweighted(const char *fname) {
if(state & SS_DATA) return;
DEBBI(debug_init_sag, ("Loading the unweighted daga for sag from: ", fname));
init_cells();
fhstream f(fname, "rt");
if(!f.f) throw hr_exception("readsag_weighted: failed to open");
if(!f.f) return file_error(fname);
scanline(f);
set<pair<int, int> > edges;
@@ -272,17 +276,16 @@ void read_unweighted(const char *fname) {
sagedges.push_back(ei);
}
println(hlog, "unweighted graph ", fname, " read successfully");
println(hlog, "N = ", isize(vdata), " edges = ", good, "/", all);
after_data();
}
void read_hubs(const string& fname) {
if(!(state & SS_DATA)) throw hr_exception("read_hubs with no data");
DEBBI(debug_init_sag, ("Loading the hub daga for sag from: ", fname));
hubval.resize(isize(vdata), -1);
fhstream f(fname, "rt");
if(!f.f) { printf("Failed to open hub file: %s\n", fname.c_str()); exit(1); }
println(hlog, "loading hubs: ", fname);
if(!f.f) return file_error(fname);
while(!feof(f.f)) {
string l1, l2;
while(true) {
@@ -301,7 +304,7 @@ void read_hubs(const string& fname) {
}
if(!id_known(l1)) {
printf("label unknown: %s\n", l1.c_str());
exit(1);
throw hr_exception("unknown label in read_hubs");
}
hubval[getid(l1)] = atoi(l2.c_str());
}
@@ -309,6 +312,7 @@ void read_hubs(const string& fname) {
void generate_fake_data(int n, int m) {
if(state & SS_DATA) return;
DEBBI(debug_init_sag, ("Generating fake data ", tie(n, m)));
init_cells();
state |= SS_WEIGHTED;

View File

@@ -189,9 +189,9 @@ void sag_test_mul() {
}
void write_colors(string s) {
auto_orth(true);
if(s == "-") return;
DEBBI(debug_init_sag, ("writing SAG colors to ", s));
fhstream f(s, "wt");
if(!f.f) return file_error(s);
for(int i=0; i<isize(vdata); i++) {
println(f, vdata[i].name, ",", format("%8x", vdata[i].cp.color1));
}
@@ -267,11 +267,15 @@ int exp_read_args() {
method = smLogistic;
lgsag.R = max_sag_dist;
lgsag.T = 1;
opt_debug = true;
debug_opt.enabled = true;
twoway = true; allow_doubles = true;
optimize_sag_loglik_auto();
}
else if(argis("-sag-opt-debug")) {
debug_opt.enabled = true;
}
else if(argis("-sagstats-logid")) {
shift(); logid = argi();
}

View File

@@ -26,7 +26,7 @@ vector<ld> loglik_tab_y, loglik_tab_n;
dhrg::logistic best;
bool opt_debug = false;
debugflag debug_opt("sag_opt");
bool should_good = false;
@@ -136,7 +136,7 @@ void optimize_sag_loglik_logistic() {
pedge[sagdist[sagid[ei.i]][sagid[ei.j]] * mul]++;
}
if(opt_debug) for(int d=0; d<max_sag_dist; d++)
if(debug_opt) for(int d=0; d<max_sag_dist; d++)
if(indist[d])
printf("%2d: %7d/%7d %7.3lf\n",
d, pedge[d], indist[d], double(pedge[d] * 100. / indist[d]));
@@ -147,11 +147,11 @@ void optimize_sag_loglik_logistic() {
int q = pq - p;
if(p && q) {
loglik += p * log(p) + q * log(q) - pq * log(pq);
if(opt_debug) println(hlog, tie(d, p, q), loglik);
if(debug_opt) println(hlog, tie(d, p, q), loglik);
}
}
if(opt_debug) println(hlog, "loglikelihood best = ", fts(loglik));
if(debug_opt) println(hlog, "loglikelihood best = ", fts(loglik));
auto logisticf = [&] (dhrg::logistic& l) {
ld loglik = 0;
@@ -163,16 +163,16 @@ void optimize_sag_loglik_logistic() {
return loglik;
};
if(opt_debug) println(hlog, "cost = ", cost, " logisticf = ", logisticf(lgsag), " R= ", lgsag.R, " T= ", lgsag.T);
if(debug_opt) println(hlog, "cost = ", cost, " logisticf = ", logisticf(lgsag), " R= ", lgsag.R, " T= ", lgsag.T);
if(should_good && abs(cost + logisticf(lgsag)) > 0.1) throw hr_exception("computation error");
dhrg::fast_loglik_cont(lgsag, logisticf, nullptr, 1, 1e-5);
if(opt_debug) println(hlog, "loglikelihood logistic = ", logisticf(lgsag), " R= ", lgsag.R, " T= ", lgsag.T);
if(debug_opt) println(hlog, "loglikelihood logistic = ", logisticf(lgsag), " R= ", lgsag.R, " T= ", lgsag.T);
if(method == smLogistic) {
compute_loglik_tab();
compute_cost();
if(opt_debug) println(hlog, "cost = ", cost);
if(debug_opt) println(hlog, "cost = ", cost);
}
}
@@ -190,7 +190,7 @@ void optimize_sag_loglik_match() {
match_a = solution[0];
match_b = solution[1];
println(hlog, "got a = ", match_a, " b = ", match_b);
if(debug_opt) println(hlog, "got a = ", match_a, " b = ", match_b);
if(method == smMatch)
prepare_graph();
}
@@ -244,7 +244,7 @@ pair<ld, ld> compute_mAP() {
ld kendall;
void compute_kendall() {
compute_cost(); println(hlog, "cost = ", cost);
compute_cost();
vector<vector<ld> > weights;
int DN = isize(sagid);
weights.resize(DN);

View File

@@ -34,6 +34,9 @@ extern vector<int> sagid;
void init();
void clear();
debugflag debug_sag("sag");
debugflag debug_init_sag("init_sag", true);
}
}