1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2025-02-23 06:20:09 +00:00

rogueviz::kohonen:: fixed bugs caused by adding vdatas

This commit is contained in:
Zeno Rogue 2018-07-05 09:55:11 +02:00
parent 3e742bbd24
commit a37cfb43bb
3 changed files with 47 additions and 37 deletions

View File

@ -17,7 +17,7 @@ struct sample {
vector<sample> data; vector<sample> data;
vector<int> samples_shown; map<int, int> sample_vdata_id;
int whattodraw[3] = {-2,-2,-2}; int whattodraw[3] = {-2,-2,-2};
@ -72,6 +72,10 @@ double vnorm(kohvec& a, kohvec& b) {
void sominit(int); void sominit(int);
void uninit(int); void uninit(int);
bool noshow = false;
vector<int> samples_to_show;
void loadsamples(const string& fname) { void loadsamples(const string& fname) {
FILE *f = fopen(fname.c_str(), "rt"); FILE *f = fopen(fname.c_str(), "rt");
if(!f) { if(!f) {
@ -93,8 +97,9 @@ void loadsamples(const string& fname) {
if(c == '!' && s.name == "") shown = true; if(c == '!' && s.name == "") shown = true;
else if(c != 32 && c != 9) s.name += c; else if(c != 32 && c != 9) s.name += c;
} }
if(shown) samples_shown.push_back(isize(data));
data.push_back(move(s)); data.push_back(move(s));
if(shown)
samples_to_show.push_back(isize(data)-1);
} }
bigbreak: bigbreak:
fclose(f); fclose(f);
@ -154,8 +159,6 @@ double maxudist;
neuron *distfrom; neuron *distfrom;
bool noshow = false;
void coloring() { void coloring() {
if(noshow) return; if(noshow) return;
setindex(false); setindex(false);
@ -170,9 +173,9 @@ void coloring() {
besttofind = false; besttofind = false;
for(neuron& n: net) { for(neuron& n: net) {
double bdiff = 1e20; double bdiff = 1e20;
for(int i=0; i<isize(samples_shown); i++) { for(auto p: sample_vdata_id) {
double diff = vnorm(n.net, data[samples_shown[i]].val); double diff = vnorm(n.net, data[p.first].val);
if(diff < bdiff) bdiff = diff, n.bestsample = i; if(diff < bdiff) bdiff = diff, n.bestsample = p.second;
} }
} }
} }
@ -223,7 +226,8 @@ void distribute_neurons() {
for(neuron& n: net) n.drawn_samples = 0; for(neuron& n: net) n.drawn_samples = 0;
for(int s: samples_shown) { for(auto p: sample_vdata_id) {
int s = p.first;
auto& w = winner(s); auto& w = winner(s);
whowon[s] = &w; whowon[s] = &w;
w.drawn_samples++; w.drawn_samples++;
@ -231,8 +235,9 @@ void distribute_neurons() {
ld rad = .25 * scalef; ld rad = .25 * scalef;
for(int id=0; id<isize(samples_shown); id++) { for(auto p: sample_vdata_id) {
int s = samples_shown[id]; int id = p.second;
int s = p.first;
auto& w = *whowon[s]; auto& w = *whowon[s];
vdata[id].m->base = w.where; vdata[id].m->base = w.where;
vdata[id].m->at = vdata[id].m->at =
@ -567,31 +572,32 @@ vector<unsigned short> bids;
vector<double> bdiffn; vector<double> bdiffn;
int showsample(int id) { int showsample(int id) {
for(int i=0; i<isize(samples_shown); i++) if(sample_vdata_id.count(id))
if(samples_shown[i] == id) return sample_vdata_id[id];
return i;
if(bids.size()) { if(bids.size()) {
if(net[bids[id]].drawn_samples >= net[bids[id]].max_group_here) { if(net[bids[id]].drawn_samples >= net[bids[id]].max_group_here) {
ld bdist = 1e18; ld bdist = 1e18;
int whichid = -1; int whichid = -1;
for(int i=0; i<isize(samples_shown); i++) for(auto p: sample_vdata_id) {
if(bids[samples_shown[i]] == bids[id]) { int s = p.first;
ld cdist = vnorm(data[samples_shown[i]].val, data[id].val); if(bids[s] == bids[id]) {
if(cdist < bdist) bdist = cdist, whichid = i; ld cdist = vnorm(data[s].val, data[id].val);
if(cdist < bdist) bdist = cdist, whichid = p.second;
}
} }
return whichid; return whichid;
} }
net[bids[id]].drawn_samples++; net[bids[id]].drawn_samples++;
} }
int i = vdata.size(); int i = vdata.size();
samples_shown.push_back(id); sample_vdata_id[id] = i;
vdata.emplace_back(); vdata.emplace_back();
auto& v = vdata.back(); auto& v = vdata.back();
v.name = data[id].name; v.name = data[id].name;
v.cp = dftcolor; v.cp = dftcolor;
createViz(i, cwt.c, Id); createViz(i, bids.size() ? net[bids[id]].where : cwt.c, Id);
v.m->store(); v.m->store();
return isize(samples_shown) - 1; return i;
} }
int showsample(string s) { int showsample(string s) {
@ -656,18 +662,20 @@ void sominit(int initto) {
for(neuron& n: net) for(int d=BARLEV; d>=7; d--) setdist(n.where, d, NULL); for(neuron& n: net) for(int d=BARLEV; d>=7; d--) setdist(n.where, d, NULL);
printf("samples = %d (%d) cells = %d\n", samples, isize(samples_shown), cells); printf("samples = %d (%d) cells = %d\n", samples, isize(sample_vdata_id), cells);
if(!noshow) { if(!noshow) for(int s: samples_to_show) {
vdata.resize(isize(samples_shown)); int vdid = isize(vdata);
for(int i=0; i<isize(samples_shown); i++) { sample_vdata_id[s] = vdid;
vdata[i].name = data[samples_shown[i]].name; vdata.emplace_back();
vdata[i].cp = dftcolor; auto &vd = vdata.back();
createViz(i, cwt.c, Id); vd.name = data[s].name;
vd.cp = dftcolor;
createViz(vdid, cwt.c, Id);
storeall(vdid);
} }
storeall(); samples_to_show.clear();
}
analyze(); analyze();
} }
@ -1074,7 +1082,7 @@ void load_edges(const string& fname_edges, int pick = 0) {
edgedata2.emplace_back(showsample(p.first), showsample(p.second)); edgedata2.emplace_back(showsample(p.first), showsample(p.second));
distribute_neurons(); distribute_neurons();
for(auto p: edgedata2) for(auto p: edgedata2)
addedge(p.first, p.second, 0, false); addedge(p.first, p.second, 0, true);
} }
void klistsamples(const string& fname_samples, bool best, bool colorformat) { void klistsamples(const string& fname_samples, bool best, bool colorformat) {
@ -1102,8 +1110,10 @@ void klistsamples(const string& fname_samples, bool best, bool colorformat) {
klistsample(net[n].bestsample, n); klistsample(net[n].bestsample, n);
} }
else else
for(int i=0; i<isize(samples_shown); i++) for(auto p: sample_vdata_id) {
klistsample(samples_shown[i], neuronId(*(whowon[i]))); int id = p.first;
klistsample(id, neuronId(*(whowon[id])));
}
fclose(f); fclose(f);
} }
} }

View File

@ -191,8 +191,8 @@ void addedge(int i, int j, double wei, bool subdiv) {
else addedge0(i, j, ei); else addedge0(i, j, ei);
} }
void storeall() { void storeall(int from) {
for(int i=0; i<isize(vdata); i++) for(int i=from; i<isize(vdata); i++)
if(vdata[i].m) if(vdata[i].m)
vdata[i].m->store(); vdata[i].m->store();
} }

View File

@ -52,7 +52,7 @@ namespace rogueviz {
extern vector<vertexdata> vdata; extern vector<vertexdata> vdata;
void storeall(); void storeall(int from = 0);
namespace anygraph { namespace anygraph {
extern double R, alpha, T; extern double R, alpha, T;