moved genhoneycomb to rulegen3

This commit is contained in:
Zeno Rogue 2022-07-13 18:59:04 +02:00
parent 5ef8092af2
commit 94a243a825
3 changed files with 155 additions and 125 deletions

View File

@ -1568,113 +1568,6 @@ void animate_to(int i) {
println(hlog, "steps = ", steps);
}
void genhoneycomb(string fname) {
if(WDIM != 3) throw hr_exception("genhoneycomb not in honeycomb");
int qc = isize(t_origin);
vector<short> data;
string side_data;
map<int, vector<int>> rev_roadsign_id;
for(auto& rs: roadsign_id) rev_roadsign_id[rs.second] = rs.first;
int N = isize(treestates);
using classdata = pair<vector<int>, int>;
vector<classdata> nclassify(N);
for(int i=0; i<N; i++) nclassify[i] = {{0}, i};
int numclass = 1;
while(true) {
println(hlog, "N = ", N, " numclass = ", numclass);
for(int i=0; i<N; i++) {
auto& ts = treestates[i];
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r = ts.rules[j1];
if(r < 0) nclassify[i].first.push_back(r);
else nclassify[i].first.push_back(nclassify[r].first[0]);
}
}
sort(nclassify.begin(), nclassify.end());
vector<int> last = {}; int newclass = 0;
for(int i=0; i<N; i++) {
if(nclassify[i].first != last) {
newclass++;
last = nclassify[i].first;
}
nclassify[i].first = {newclass-1};
}
sort(nclassify.begin(), nclassify.end(), [] (const classdata& a, const classdata& b) { return a.second < b.second; });
if(numclass == newclass) break;
numclass = newclass;
}
vector<int> representative(numclass);
for(int i=0; i<isize(treestates); i++) representative[nclassify[i].first[0]] = i;
println(hlog, "Minimized rules (", numclass, " states):");
for(int i=0; i<numclass; i++) {
auto& ts = treestates[representative[i]];
print(hlog, lalign(4, i), ":");
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r =ts.rules[j1];
if(r == DIR_PARENT) print(hlog, " P");
else if(r >= 0) print(hlog, " ", nclassify[r].first[0]);
else print(hlog, " S", r);
}
println(hlog);
}
println(hlog);
for(int i=0; i<numclass; i++) {
auto& ts = treestates[representative[i]];
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r =ts.rules[j1];
if(r == DIR_PARENT) {
data.push_back(-1);
side_data += ('A' + j);
side_data += ",";
}
else if(r >= 0) {
data.push_back(nclassify[r].first[0]);
}
else {
data.push_back(-1);
auto& str = rev_roadsign_id[r];
bool next = true;
for(auto ch: str) {
if(next) side_data += ('a' + ch);
next = !next;
}
side_data += ",";
}
}
}
shstream ss;
auto& fp = currfp;
hwrite_fpattern(ss, fp);
vector<int> root(qc, 0);
for(int i=0; i<qc; i++) root[i] = nclassify[get_treestate_id(t_origin[i]).second].first[0];
println(hlog, "root = ", root);
hwrite(ss, root);
println(hlog, "data = ", data);
hwrite(ss, data);
println(hlog, "side_data = ", side_data);
hwrite(ss, side_data);
println(hlog, "compress_string");
string s = compress_string(ss.s);
fhstream of(fname, "wb");
print(of, s);
}
void animate_steps(int i) {
while(i--) {
if(state != 1) break;
@ -1728,10 +1621,6 @@ int testargs() {
else if(argis("-trv")) {
shift(); test_rotate_val = argi();
}
else if(argis("-ruleflag")) {
shift();
rulegen::flags ^= Flag(argi());
}
else if(argis("-ruleflag-sub")) {
swap(rulegen::flags, sub_rulegen_flags);
}
@ -1817,10 +1706,6 @@ int testargs() {
shift(); tesgen(args());
}
else if(argis("-gen-honeycomb")) {
shift(); genhoneycomb(args());
}
else if(argis("-tes-animate")) {
animate();
}
@ -1860,16 +1745,6 @@ int testargs() {
else
println(hlog, "wrong dseek index");
}
else if(argis("-urq")) {
// -urq 7 to generate honeycombs
stop_game();
shift(); int i = argi();
reg3::reg3_rule_available = (i & 8) ? 0 : 1;
fieldpattern::use_rule_fp = (i & 1) ? 1 : 0;
fieldpattern::use_quotient_fp = (i & 2) ? 1 : 0;
reg3::minimize_quotient_maps = (i & 4) ? 1 : 0;
}
else return 1;
return 0;

View File

@ -2581,5 +2581,23 @@ EX void show() {
dialog::display();
}
#if CAP_COMMANDLINE
int readRuleArgs() {
using namespace arg;
if(0) ;
else if(argis("-ruleflag")) {
shift();
rulegen::flags ^= Flag(argi());
}
else return 1;
return 0;
}
auto hook = addHook(hooks_args, 100, readRuleArgs);
#endif
EX }
}

View File

@ -110,6 +110,143 @@ EX void cleanup3() {
next_roadsign_id = -100;
}
void genhoneycomb(string fname) {
if(WDIM != 3) throw hr_exception("genhoneycomb not in honeycomb");
int qc = isize(t_origin);
vector<short> data;
string side_data;
map<int, vector<int>> rev_roadsign_id;
for(auto& rs: roadsign_id) rev_roadsign_id[rs.second] = rs.first;
int N = isize(treestates);
using classdata = pair<vector<int>, int>;
vector<classdata> nclassify(N);
for(int i=0; i<N; i++) nclassify[i] = {{0}, i};
int numclass = 1;
while(true) {
println(hlog, "N = ", N, " numclass = ", numclass);
for(int i=0; i<N; i++) {
auto& ts = treestates[i];
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r = ts.rules[j1];
if(r < 0) nclassify[i].first.push_back(r);
else nclassify[i].first.push_back(nclassify[r].first[0]);
}
}
sort(nclassify.begin(), nclassify.end());
vector<int> last = {}; int newclass = 0;
for(int i=0; i<N; i++) {
if(nclassify[i].first != last) {
newclass++;
last = nclassify[i].first;
}
nclassify[i].first = {newclass-1};
}
sort(nclassify.begin(), nclassify.end(), [] (const classdata& a, const classdata& b) { return a.second < b.second; });
if(numclass == newclass) break;
numclass = newclass;
}
vector<int> representative(numclass);
for(int i=0; i<isize(treestates); i++) representative[nclassify[i].first[0]] = i;
println(hlog, "Minimized rules (", numclass, " states):");
for(int i=0; i<numclass; i++) {
auto& ts = treestates[representative[i]];
print(hlog, lalign(4, i), ":");
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r =ts.rules[j1];
if(r == DIR_PARENT) print(hlog, " P");
else if(r >= 0) print(hlog, " ", nclassify[r].first[0]);
else print(hlog, " S", r);
}
println(hlog);
}
println(hlog);
for(int i=0; i<numclass; i++) {
auto& ts = treestates[representative[i]];
for(int j=0; j<isize(ts.rules); j++) {
int j1 = gmod(j - ts.giver.spin, isize(ts.rules));
auto r =ts.rules[j1];
if(r == DIR_PARENT) {
data.push_back(-1);
side_data += ('A' + j);
side_data += ",";
}
else if(r >= 0) {
data.push_back(nclassify[r].first[0]);
}
else {
data.push_back(-1);
auto& str = rev_roadsign_id[r];
bool next = true;
for(auto ch: str) {
if(next) side_data += ('a' + ch);
next = !next;
}
side_data += ",";
}
}
}
shstream ss;
auto& fp = currfp;
hwrite_fpattern(ss, fp);
vector<int> root(qc, 0);
for(int i=0; i<qc; i++) root[i] = nclassify[get_treestate_id(t_origin[i]).second].first[0];
println(hlog, "root = ", root);
hwrite(ss, root);
println(hlog, "data = ", data);
hwrite(ss, data);
println(hlog, "side_data = ", side_data);
hwrite(ss, side_data);
println(hlog, "compress_string");
string s = compress_string(ss.s);
fhstream of(fname, "wb");
print(of, s);
}
#if CAP_COMMANDLINE
int readRuleArgs3() {
using namespace arg;
if(0) ;
else if(argis("-gen-honeycomb")) {
shift(); genhoneycomb(args());
}
else if(argis("-urq")) {
// -urq 7 to generate honeycombs
stop_game();
shift(); int i = argi();
reg3::reg3_rule_available = (i & 8) ? 0 : 1;
fieldpattern::use_rule_fp = (i & 1) ? 1 : 0;
fieldpattern::use_quotient_fp = (i & 2) ? 1 : 0;
reg3::minimize_quotient_maps = (i & 4) ? 1 : 0;
}
else if(argis("-subrule")) {
shift(); reg3::other_rule = args();
shift(); reg3::subrule = argi();
}
else return 1;
return 0;
}
auto hook = addHook(hooks_args, 100, readRuleArgs3);
#endif
}
}