From 2d35e40dba73b8c4a7d82cbfedea416928aed598 Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Thu, 18 Aug 2022 18:03:16 +0200 Subject: [PATCH] rulegen3:: optimize the information --- rulegen3.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/rulegen3.cpp b/rulegen3.cpp index 4bb197fa..36217a58 100644 --- a/rulegen3.cpp +++ b/rulegen3.cpp @@ -1455,6 +1455,91 @@ EX void check_upto(int lev, int t) { } } +EX void optimize() { + + imp_as_set.clear(); + for(auto t: important) imp_as_set.insert(t.at); + + /* optimize givers */ + set seen; + vector oqueue; + + int changes = 0; + int errors = 0; + + auto visit = [&] (twalker w, int expected, twalker parent) { + int id = get_treestate_id(w).second; + if(expected >= 0 && expected != id) { + errors++; + important.push_back(parent); + important.push_back(w); + return; + } + if(seen.count(id)) return; + seen.insert(id); + oqueue.push_back(id); + auto& g = treestates[id].giver; + if(g != w) changes++; + g = w; + }; + + for(auto t: t_origin) visit(t, -1, t); + + for(int ii=0; ii= 0) + visit(treestates[i].giver + j + wstep, treestates[i].rules[j], treestates[i].giver); + } + + int N = isize(treestates); + + println(hlog, "optimize: changes = ", changes, " errors = ", errors, " unreachable = ", N - isize(seen)); + + if(errors) throw rulegen_retry("error found in optimize"); + + int steps = 0; + for(int i=0; idist == 0) throw rulegen_failure("reached the root"); + steps++; + get_parent_dir(at); + if(at.at->parent_dir == -1) throw rulegen_failure("no parent_dir for at"); + at.spin = at.at->parent_dir; + at += wstep; + get_parent_dir(at); + if(at.at->parent_dir == -1) throw rulegen_failure("no parent_dir for at2"); + int r2 = get_treestate_id(at).second; + auto at2 = at; + at2.spin = at.at->parent_dir; + if(at.at->dist == 0) at.at->parent_dir = 0; + int j = -1; + for(int k=0; ktype; k++) if(at2 + k == at) j = k; + if(treestates[r2].rules.empty()) { + important.push_back(at); + break; + } + + // println(hlog, "found: ", r2, " seen: ", int(seen.count(r2)), " expected: ", r, " found: ", treestates[r2].rules[j], " dist=", at.at->dist); + + if(treestates[r2].rules[j] != r) { + // println(hlog, "expected: ", r, " found: ", treestates[r2].rules[j], " add ", at+wstep, at2); + if(imp_as_set.count((at+wstep).at) && imp_as_set.count(at2.at)) + throw rulegen_failure("already in imp"); + important.push_back(at+wstep); important.push_back(at2); break; + } + r = r2; steps++; + } + } + + if(steps) { println(hlog, "steps = ", steps); throw rulegen_retry("unreachable found in optimize"); } + + important.clear(); + for(auto s: seen) important.push_back(treestates[s].giver); + } + EX void check_road_shortcuts() { println(hlog, "road shortcuts = ", qroad, " treestates = ", isize(treestates), " roadsigns = ", next_roadsign_id, " tcellcount = ", tcellcount, " try = ", try_count); if(qroad > last_qroad) { @@ -1467,6 +1552,7 @@ EX void check_road_shortcuts() { throw rulegen_retry("new road shortcuts"); } println(hlog, "checking validity, important = ", isize(important)); + optimize(); imp_as_set.clear(); for(auto t: important) imp_as_set.insert(t.at); impcount = isize(important);