From 4c30e6051ff74d53ce377f7acb1005edde97b2f5 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sun, 23 Oct 2022 15:49:16 +0200 Subject: [PATCH] rogueviz::sag:: Dijkstra edges, save/load distances from file --- rogueviz/sag.cpp | 75 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/rogueviz/sag.cpp b/rogueviz/sag.cpp index b59084e5..343feb5d 100644 --- a/rogueviz/sag.cpp +++ b/rogueviz/sag.cpp @@ -67,6 +67,9 @@ namespace sag { /** precision of geometric distances */ int gdist_prec; + + /** max edge for dijkstra */ + int dijkstra_maxedge; /** the maximum value in sagdist +1 */ int max_sag_dist; @@ -92,6 +95,8 @@ namespace sag { void optimize_sag_loglik(); + string distance_file; + void compute_dists() { int N = isize(sagcells); @@ -102,39 +107,77 @@ namespace sag { for(cell *c1: adj_minefield_cells(sagcells[i])) if(ids.count(c1)) neighbors[i].push_back(ids[c1]); - const ld ERROR = -17.3; - transmatrix unknown = Id; unknown[0][0] = ERROR; + const ld ERRORV = -17.3; + transmatrix unknown = Id; unknown[0][0] = ERRORV; cell_matrix.clear(); cell_matrix.resize(N, unknown); vector visited; auto visit = [&] (int id, const transmatrix& T) { - if(cell_matrix[id][0][0] != ERROR) return; + if(cell_matrix[id][0][0] != ERRORV) return; cell_matrix[id] = T; visited.push_back(id); }; - + visit(0, Id); for(int i=0; itype; d++) if(ids.count(c0->move(d))) visit(ids[c0->move(d)], T0 * currentmap->adj(c0, d)); } - if(gdist_prec) { + sagdist.clear(); + sagdist.resize(N); + for(int i=0; i>> dijkstra_edges(N); + for(int i=0; i distances(N); + for(int i=a; i> pq; + auto visit = [&] (int i, ld dist) { + if(distances[i] <= dist) return; + distances[i] = dist; + pq.emplace(-dist, i); + }; + visit(i, 0); + while(!pq.empty()) { + ld d = -pq.top().first; + int at = pq.top().second; + pq.pop(); + for(auto e: dijkstra_edges[at]) visit(e.first, d + e.second); + } + for(int j=0; j q; auto visit = [&] (int j, int dist) { if(sdi[j] < N) return; sdi[j] = dist; q.push_back(j); }; visit(i, 0); @@ -967,6 +1010,18 @@ int readArgs() { else if(argis("-sag_gdist")) { shift(); sag::gdist_prec = argi(); } + else if(argis("-sag_gdist_dijkstra")) { + shift(); sag::dijkstra_maxedge = argi(); + } + else if(argis("-sag_gdist_save")) { + shift(); + fhstream f(args(), "wt"); + f.write(sagdist); + } + else if(argis("-sag_gdist_load")) { + shift(); distance_file = args(); + } + else if(argis("-sagrt")) { shift(); sag::lgsag.R = argf(); shift(); sag::lgsag.T = argf();