2017-09-01 20:14:02 +00:00
|
|
|
// Hyperbolic Rogue
|
2018-02-08 23:40:26 +00:00
|
|
|
// Copyright (C) 2011-2018 Zeno and Tehora Rogue, see 'hyper.cpp' for details
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
// Kohonen's self-organizing maps.
|
2017-09-01 20:14:02 +00:00
|
|
|
// This is a part of RogueViz, not a part of HyperRogue.
|
|
|
|
|
2021-07-18 09:38:45 +00:00
|
|
|
#include "kohonen.h"
|
2020-03-29 14:03:31 +00:00
|
|
|
|
2018-06-11 16:00:40 +00:00
|
|
|
namespace rogueviz { namespace kohonen {
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-09-04 17:53:42 +00:00
|
|
|
int columns;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
vector<sample> data;
|
|
|
|
|
2018-07-05 07:55:11 +00:00
|
|
|
map<int, int> sample_vdata_id;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
int whattodraw[3] = {-2,-2,-2};
|
|
|
|
|
2018-07-05 05:30:15 +00:00
|
|
|
int min_group = 10, max_group = 10;
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
vector<string> colnames;
|
|
|
|
|
|
|
|
kohvec weights;
|
|
|
|
|
|
|
|
vector<neuron> net;
|
|
|
|
|
|
|
|
int neuronId(neuron& n) { return &n - &(net[0]); }
|
|
|
|
|
|
|
|
bool neurons_indexed = false;
|
|
|
|
|
|
|
|
int samples;
|
|
|
|
|
|
|
|
template<class T> T sqr(T x) { return x*x; }
|
|
|
|
|
|
|
|
vector<neuron*> whowon;
|
|
|
|
|
|
|
|
void normalize() {
|
|
|
|
alloc(weights);
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int k=0; k<columns; k++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
double sum = 0, sqsum = 0;
|
|
|
|
for(sample& s: data)
|
|
|
|
sum += s.val[k],
|
|
|
|
sqsum += s.val[k] * s.val[k];
|
|
|
|
double variance = sqsum/samples - sqr(sum/samples);
|
|
|
|
weights[k] = 1 / sqrt(variance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:42:51 +00:00
|
|
|
double vdot(const kohvec& a, const kohvec& b) {
|
|
|
|
double diff = 0;
|
|
|
|
for(int k=0; k<columns; k++) diff += a[k] * b[k] * weights[k];
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vshift(kohvec& a, const kohvec& b, ld i) {
|
|
|
|
for(int k=0; k<columns; k++) a[k] += b[k] * i;
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
double vnorm(kohvec& a, kohvec& b) {
|
|
|
|
double diff = 0;
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int k=0; k<columns; k++) diff += sqr((a[k]-b[k]) * weights[k]);
|
2017-09-01 20:14:02 +00:00
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2018-07-05 07:55:11 +00:00
|
|
|
bool noshow = false;
|
|
|
|
|
|
|
|
vector<int> samples_to_show;
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void loadsamples(const string& fname) {
|
2021-03-30 22:41:13 +00:00
|
|
|
data.clear();
|
|
|
|
samples_to_show.clear();
|
|
|
|
clear();
|
2019-05-21 22:05:58 +00:00
|
|
|
fhstream f(fname, "rt");
|
|
|
|
if(!f.f) {
|
2018-07-06 17:28:20 +00:00
|
|
|
fprintf(stderr, "Could not load samples: %s\n", fname.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-21 22:05:58 +00:00
|
|
|
if(!scan(f, columns)) {
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Bad format: %s\n", fname.c_str());
|
2019-05-21 22:05:58 +00:00
|
|
|
return;
|
2018-07-06 17:28:20 +00:00
|
|
|
}
|
|
|
|
printf("Loading samples: %s\n", fname.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
while(true) {
|
|
|
|
sample s;
|
|
|
|
bool shown = false;
|
|
|
|
alloc(s.val);
|
2019-05-21 22:05:58 +00:00
|
|
|
if(feof(f.f)) break;
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int i=0; i<columns; i++)
|
2019-05-21 22:05:58 +00:00
|
|
|
if(!scan(f, s.val[i])) { goto bigbreak; }
|
|
|
|
fgetc(f.f);
|
2017-09-01 20:14:02 +00:00
|
|
|
while(true) {
|
2019-05-21 22:05:58 +00:00
|
|
|
int c = fgetc(f.f);
|
2017-09-01 20:14:02 +00:00
|
|
|
if(c == -1 || c == 10 || c == 13) break;
|
|
|
|
if(c == '!' && s.name == "") shown = true;
|
2021-06-25 12:00:01 +00:00
|
|
|
else if(!rv_ignore(c)) s.name += c;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
data.push_back(move(s));
|
2018-07-05 07:55:11 +00:00
|
|
|
if(shown)
|
|
|
|
samples_to_show.push_back(isize(data)-1);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
bigbreak:
|
2018-06-22 12:47:24 +00:00
|
|
|
samples = isize(data);
|
2017-09-01 20:14:02 +00:00
|
|
|
normalize();
|
2018-09-04 17:53:42 +00:00
|
|
|
colnames.resize(columns);
|
|
|
|
for(int i=0; i<columns; i++) colnames[i] = "Column " + its(i);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int tmax = 30000;
|
|
|
|
double distmul = 1;
|
|
|
|
double learning_factor = .1;
|
|
|
|
|
|
|
|
int qpct = 100;
|
|
|
|
|
|
|
|
int t, lpct, cells;
|
|
|
|
double maxdist;
|
|
|
|
|
|
|
|
neuron& winner(int id) {
|
2018-07-09 16:55:43 +00:00
|
|
|
double bdiff = HUGE_VAL;
|
2017-09-01 20:14:02 +00:00
|
|
|
neuron *bcell = NULL;
|
|
|
|
for(neuron& n: net) {
|
|
|
|
double diff = vnorm(n.net, data[id].val);
|
|
|
|
if(diff < bdiff) bdiff = diff, bcell = &n;
|
|
|
|
}
|
|
|
|
return *bcell;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setindex(bool b) {
|
|
|
|
if(b == neurons_indexed) return;
|
|
|
|
neurons_indexed = b;
|
|
|
|
if(b) {
|
|
|
|
for(neuron& n: net) n.lpbak = n.where->landparam, n.where->landparam = neuronId(n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(neuron& n: net) n.where->landparam = n.lpbak;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
neuron *getNeuron(cell *c) {
|
|
|
|
if(!c) return NULL;
|
|
|
|
setindex(true);
|
|
|
|
if(c->landparam < 0 || c->landparam >= cells) return NULL;
|
|
|
|
neuron& ret = net[c->landparam];
|
|
|
|
if(ret.where != c) return NULL;
|
|
|
|
return &ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
neuron *getNeuronSlow(cell *c) {
|
|
|
|
if(neurons_indexed) return getNeuron(c);
|
|
|
|
for(neuron& n: net) if(n.where == c) return &n;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
double maxudist;
|
|
|
|
|
|
|
|
neuron *distfrom;
|
|
|
|
|
|
|
|
void coloring() {
|
|
|
|
if(noshow) return;
|
|
|
|
setindex(false);
|
|
|
|
|
|
|
|
bool besttofind = true;
|
|
|
|
|
|
|
|
for(int pid=0; pid<3; pid++) {
|
|
|
|
int c = whattodraw[pid];
|
|
|
|
|
|
|
|
if(c == -5) {
|
|
|
|
if(besttofind) {
|
|
|
|
besttofind = false;
|
|
|
|
for(neuron& n: net) {
|
|
|
|
double bdiff = 1e20;
|
2021-03-30 22:43:56 +00:00
|
|
|
n.bestsample = -1;
|
2018-07-05 07:55:11 +00:00
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
double diff = vnorm(n.net, data[p.first].val);
|
|
|
|
if(diff < bdiff) bdiff = diff, n.bestsample = p.second;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int i=0; i<cells; i++) {
|
2021-03-30 22:43:56 +00:00
|
|
|
if(net[i].bestsample >= 0)
|
|
|
|
part(net[i].where->landparam_color, pid) = part(vdata[net[i].bestsample].cp.color1, pid+1);
|
|
|
|
else
|
|
|
|
part(net[i].where->landparam_color, pid) = 128;
|
2018-09-04 17:53:42 +00:00
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
vector<double> listing;
|
|
|
|
for(neuron& n: net) switch(c) {
|
|
|
|
case -4:
|
2018-07-04 12:37:33 +00:00
|
|
|
listing.push_back(log(5+n.allsamples));
|
2017-09-01 20:14:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case -3:
|
|
|
|
if(distfrom)
|
|
|
|
listing.push_back(vnorm(n.net, distfrom->net));
|
|
|
|
else
|
|
|
|
listing.push_back(0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -2:
|
|
|
|
listing.push_back(n.udist);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
listing.push_back(-n.udist);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
listing.push_back(n.net[c]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
double minl = listing[0], maxl = listing[0];
|
|
|
|
for(double& d: listing) minl = min(minl, d), maxl = max(maxl, d);
|
|
|
|
if(maxl-minl < 1e-3) maxl = minl+1e-3;
|
|
|
|
|
|
|
|
for(int i=0; i<cells; i++)
|
2021-03-30 22:42:51 +00:00
|
|
|
part(net[i].where->landparam_color, pid) = 32 + (191 * (listing[i] - minl)) / (maxl - minl);
|
|
|
|
|
|
|
|
for(int i=0; i<cells; i++)
|
|
|
|
net[i].where->wall = waNone;
|
|
|
|
|
|
|
|
vid.wallmode = 2;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:42:51 +00:00
|
|
|
ld precise_placement = 1.6;
|
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
bool neighbor_dir(cell *c, int a, int b) {
|
|
|
|
if(a == b) return false;
|
|
|
|
if(WDIM == 2)
|
|
|
|
return (a+1 == b) || (a-1 == b) || (a == 0 && b == c->type-1) || (b == 0 && a == c->type-1);
|
|
|
|
return currentmap->get_cellshape(c).dirdist[a][b] == 1;
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:42:51 +00:00
|
|
|
bool triangulate(kohvec d, neuron& w, map<cell*, neuron*>& find, transmatrix& res) {
|
|
|
|
|
|
|
|
if(precise_placement < 1) return false;
|
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
vector<int> dirs;
|
|
|
|
vector<neuron*> other;
|
|
|
|
vector<kohvec> kv;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
for(int i=0; i<WDIM; i++) {
|
|
|
|
|
|
|
|
ld bdiff = HUGE_VAL;
|
|
|
|
|
|
|
|
/* find the second neuron */
|
|
|
|
neuron *candidate = nullptr;
|
|
|
|
int cdir = -1;
|
|
|
|
|
|
|
|
forCellIdEx(c2, i, w.where) {
|
|
|
|
if(!find.count(c2)) continue;
|
|
|
|
auto w2 = find[c2];
|
|
|
|
double diff = vnorm(w2->net, d);
|
|
|
|
if(1) {
|
|
|
|
bool valid = true;
|
|
|
|
for(int d: dirs)
|
|
|
|
if(!neighbor_dir(w.where, d, i))
|
|
|
|
valid = false;
|
|
|
|
if(!valid) continue;
|
|
|
|
}
|
|
|
|
if(diff < bdiff) bdiff = diff, candidate = w2, cdir = i;
|
|
|
|
}
|
2022-03-01 07:42:20 +00:00
|
|
|
if(cdir == -1) break;
|
2021-07-18 09:40:03 +00:00
|
|
|
dirs.push_back(cdir);
|
|
|
|
other.push_back(candidate);
|
|
|
|
kv.push_back(candidate->net);
|
2021-03-30 22:42:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
int q = isize(dirs);
|
2021-03-30 22:42:51 +00:00
|
|
|
const kohvec& a = w.net;
|
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
auto orig_d = d;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
/* center at a */
|
|
|
|
for(int i=0; i<q; i++)
|
|
|
|
vshift(kv[i], a, -1);
|
|
|
|
vshift(d, a, -1);
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
transmatrix R;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
hyperpoint coeff;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
/* orthonormalize */
|
|
|
|
for(int i=0; i<q; i++) {
|
|
|
|
R[i][i] = vdot(kv[i], kv[i]);
|
|
|
|
if(R[i][i] < 1e-12) {
|
2022-03-01 07:42:20 +00:00
|
|
|
/*
|
2021-07-18 09:40:03 +00:00
|
|
|
auto head = [] (const vector<ld>& v) { vector<ld> res; for(int i=0; i<10; i++) res.push_back(v[i]); return res; };
|
|
|
|
println(hlog, "dot too small, i=", i,", dirs=", dirs);
|
|
|
|
println(hlog, "a = ", head(a));
|
|
|
|
println(hlog, "orig d = ", head(d));
|
|
|
|
for(auto z: other)
|
|
|
|
println(hlog, "orig kv: ", head(z->net), " @ ", z->where);
|
|
|
|
for(auto z: kv)
|
|
|
|
println(hlog, "curr kv: ", head(z));
|
2022-03-01 07:42:20 +00:00
|
|
|
*/
|
2021-07-18 09:40:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(int j=i+1; j<q; j++) {
|
|
|
|
R[i][j] = vdot(kv[i], kv[j]) / R[i][i];
|
|
|
|
vshift(kv[j], kv[i], -R[i][j]);
|
|
|
|
}
|
|
|
|
coeff[i] = vdot(d, kv[i]) / R[i][i];
|
|
|
|
}
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
for(int i=q-1; i>=0; i--) {
|
|
|
|
for(int j=0; j<i; j++)
|
|
|
|
coeff[j] -= coeff[i] * R[j][i];
|
|
|
|
}
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
/* rescale if out of the simplex */
|
|
|
|
for(int i=0; i<q; i++)
|
|
|
|
if(coeff[i] < 0) {
|
|
|
|
coeff /= (1-coeff[i]);
|
|
|
|
coeff[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ld total = 0;
|
|
|
|
for(int i=0; i<q; i++) total += coeff[i];
|
|
|
|
if(total > 1) coeff /= total, total = 1;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
coeff /= precise_placement;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
2021-07-18 09:40:03 +00:00
|
|
|
hyperpoint h = (1-total) * C0;
|
|
|
|
for(int i=0; i<q; i++) h += coeff[i] * tC0(currentmap->adj(w.where, dirs[i]));
|
2021-03-30 22:42:51 +00:00
|
|
|
h = normalize(h);
|
|
|
|
|
|
|
|
res = rgpushxto0(h);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
void distribute_neurons() {
|
|
|
|
whowon.resize(samples);
|
|
|
|
|
2018-07-05 07:55:33 +00:00
|
|
|
for(neuron& n: net) n.drawn_samples = 0, n.csample = 0;
|
2018-07-04 12:37:33 +00:00
|
|
|
|
2018-07-05 07:55:11 +00:00
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
int s = p.first;
|
2018-07-04 12:37:33 +00:00
|
|
|
auto& w = winner(s);
|
|
|
|
whowon[s] = &w;
|
|
|
|
w.drawn_samples++;
|
|
|
|
}
|
2021-03-30 22:42:51 +00:00
|
|
|
|
|
|
|
map<cell*, neuron*> find;
|
|
|
|
if(precise_placement >= 1)
|
|
|
|
for(auto& w: net) find[w.where] = &w;
|
|
|
|
|
2019-05-27 05:18:18 +00:00
|
|
|
ld rad = .25 * cgi.scalefactor;
|
2018-07-05 07:55:11 +00:00
|
|
|
|
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
int id = p.second;
|
|
|
|
int s = p.first;
|
2018-07-04 12:37:33 +00:00
|
|
|
auto& w = *whowon[s];
|
|
|
|
vdata[id].m->base = w.where;
|
2021-03-30 22:42:51 +00:00
|
|
|
|
|
|
|
if(!triangulate(data[s].val, w, find, vdata[id].m->at))
|
|
|
|
vdata[id].m->at =
|
|
|
|
spin(2*M_PI*w.csample / w.drawn_samples) * xpush(rad * (w.drawn_samples-1) / w.drawn_samples);
|
2018-07-04 12:37:33 +00:00
|
|
|
w.csample++;
|
2021-03-30 22:42:51 +00:00
|
|
|
for(auto& e: vdata[id].edges) e.second->orig = nullptr;
|
2018-07-04 12:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shmup::fixStorage();
|
|
|
|
setindex(false);
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
void analyze() {
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_neurons();
|
|
|
|
initialize_samples_to_show();
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
setindex(true);
|
|
|
|
|
|
|
|
maxudist = 0;
|
|
|
|
for(neuron& n: net) {
|
|
|
|
int qty = 0;
|
|
|
|
double total = 0;
|
|
|
|
forCellEx(c2, n.where) {
|
|
|
|
neuron *n2 = getNeuron(c2);
|
|
|
|
if(!n2) continue;
|
|
|
|
qty++;
|
|
|
|
total += sqrt(vnorm(n.net, n2->net));
|
|
|
|
}
|
|
|
|
n.udist = total / qty;
|
|
|
|
maxudist = max(maxudist, n.udist);
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
if(!noshow) distribute_neurons();
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
coloring();
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:46:37 +00:00
|
|
|
bool show_rings = true;
|
|
|
|
|
2020-07-29 21:34:00 +00:00
|
|
|
bool coloring_3d(cell *c, const shiftmatrix& V) {
|
2021-03-30 22:58:56 +00:00
|
|
|
if(WDIM == 3 && show_rings)
|
2019-11-09 10:21:13 +00:00
|
|
|
queuepoly(face_the_player(V), cgi.shRing, darkena(c->landparam_color, 0, 0xFF));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
// traditionally Gaussian blur is used in the Kohonen algoritm
|
|
|
|
// but it does not seem to make much sense in hyperbolic geometry
|
|
|
|
// especially wrapped one.
|
|
|
|
// GAUSSIAN==1: use the Gaussian blur, on celldistance
|
|
|
|
// GAUSSIAN==2: use the Gaussian blur, on true distance
|
|
|
|
// GAUSSIAN==0: simulate the dispersion on our network
|
|
|
|
|
|
|
|
int gaussian = 0;
|
|
|
|
|
|
|
|
double mydistance(cell *c1, cell *c2) {
|
2018-08-17 22:46:45 +00:00
|
|
|
if(gaussian == 2) return hdist(tC0(ggmatrix(c1)), tC0(ggmatrix(c2)));
|
2017-09-01 20:14:02 +00:00
|
|
|
else return celldistance(c1, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cellcrawler {
|
|
|
|
|
|
|
|
struct cellcrawlerdata {
|
|
|
|
cellwalker orig;
|
|
|
|
int from, spin, dist;
|
|
|
|
cellwalker target;
|
|
|
|
cellcrawlerdata(const cellwalker& o, int fr, int sp) : orig(o), from(fr), spin(sp) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<cellcrawlerdata> data;
|
|
|
|
|
2018-06-28 12:56:41 +00:00
|
|
|
void store(const cellwalker& o, int from, int spin, manual_celllister& cl) {
|
2018-08-17 22:46:45 +00:00
|
|
|
if(!cl.add(o.at)) return;
|
2017-09-01 20:14:02 +00:00
|
|
|
data.emplace_back(o, from, spin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void build(const cellwalker& start) {
|
|
|
|
data.clear();
|
2018-06-28 12:56:41 +00:00
|
|
|
manual_celllister cl;
|
2018-06-29 10:16:35 +00:00
|
|
|
store(start, 0, 0, cl);
|
2018-06-22 12:47:24 +00:00
|
|
|
for(int i=0; i<isize(data); i++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
cellwalker cw0 = data[i].orig;
|
2018-08-17 22:46:45 +00:00
|
|
|
for(int j=0; j<cw0.at->type; j++) {
|
2018-03-24 11:59:01 +00:00
|
|
|
cellwalker cw = cw0 + j + wstep;
|
2018-08-17 22:46:45 +00:00
|
|
|
if(!getNeuron(cw.at)) continue;
|
2018-06-29 10:16:35 +00:00
|
|
|
store(cw, i, j, cl);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-01 07:42:20 +00:00
|
|
|
if(gaussian || true) for(cellcrawlerdata& s: data)
|
2018-08-17 22:46:45 +00:00
|
|
|
s.dist = mydistance(s.orig.at, start.at);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sprawl(const cellwalker& start) {
|
|
|
|
data[0].target = start;
|
|
|
|
|
2018-06-22 12:47:24 +00:00
|
|
|
for(int i=1; i<isize(data); i++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
cellcrawlerdata& s = data[i];
|
|
|
|
s.target = data[s.from].target;
|
2018-08-17 22:46:45 +00:00
|
|
|
if(!s.target.at) continue;
|
2018-03-24 11:59:01 +00:00
|
|
|
s.target += s.spin;
|
2018-08-17 22:46:45 +00:00
|
|
|
if(!s.target.peek()) s.target.at = NULL;
|
2018-03-24 11:59:01 +00:00
|
|
|
else s.target += wstep;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
vector<vector<float>> dispersion;
|
2018-04-21 14:19:19 +00:00
|
|
|
};
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:48:54 +00:00
|
|
|
double dispersion_end_at = 1.6;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
bool dispersion_long;
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
double dispersion_precision = .0001;
|
|
|
|
int dispersion_each = 1;
|
|
|
|
|
|
|
|
int dispersion_count;
|
|
|
|
|
2018-06-22 12:26:24 +00:00
|
|
|
void buildcellcrawler(cell *c, cellcrawler& cr, int dir) {
|
|
|
|
cr.build(cellwalker(c,dir));
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
if(!gaussian) {
|
2022-03-01 07:42:20 +00:00
|
|
|
vector<float> curtemp;
|
|
|
|
vector<float> newtemp;
|
2017-09-01 20:14:02 +00:00
|
|
|
vector<int> qty;
|
2022-03-01 07:42:20 +00:00
|
|
|
vector<pair<float*, float*> > pairs;
|
2018-06-22 12:47:24 +00:00
|
|
|
int N = isize(net);
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
curtemp.resize(N, 0);
|
|
|
|
newtemp.resize(N, 0);
|
|
|
|
qty.resize(N, 0);
|
|
|
|
|
|
|
|
for(int i=0; i<N; i++)
|
|
|
|
forCellEx(c2, net[i].where) {
|
|
|
|
neuron *nj = getNeuron(c2);
|
|
|
|
if(nj) {
|
|
|
|
pairs.emplace_back(&curtemp[i], &newtemp[neuronId(*nj)]);
|
|
|
|
qty[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curtemp[neuronId(*getNeuron(c))] = 1;
|
|
|
|
|
|
|
|
ld vmin = 0, vmax = 1;
|
|
|
|
int iter;
|
|
|
|
|
2018-04-21 14:19:19 +00:00
|
|
|
auto &d = cr.dispersion;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
d.clear();
|
2022-03-01 07:42:20 +00:00
|
|
|
|
|
|
|
// DEBBI(DF_LOG, ("Building dispersion, precision = ", dispersion_precision, " end_at = ", dispersion_end_at, "...\n"));
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
for(iter=0; dispersion_count ? true : vmax > vmin * dispersion_end_at; iter++) {
|
|
|
|
if(iter % dispersion_each == 0) {
|
|
|
|
d.emplace_back(N);
|
|
|
|
auto& dispvec = d.back();
|
2018-08-17 22:46:45 +00:00
|
|
|
for(int i=0; i<N; i++) dispvec[i] = curtemp[neuronId(*getNeuron(cr.data[i].orig.at))] / vmax;
|
2018-06-22 12:47:24 +00:00
|
|
|
if(isize(d) == dispersion_count) break;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
double df = dispersion_precision * (iter+1);
|
|
|
|
double df0 = df / ceil(df);
|
|
|
|
for(int i=0; i<df; i++) {
|
|
|
|
for(auto& p: pairs)
|
|
|
|
*p.second += *p.first;
|
|
|
|
for(int i=0; i<N; i++) {
|
|
|
|
curtemp[i] += (newtemp[i] / qty[i] - curtemp[i]) * df0;
|
|
|
|
newtemp[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vmin = vmax = curtemp[0];
|
|
|
|
for(int i=0; i<N; i++)
|
|
|
|
if(curtemp[i] < vmin) vmin = curtemp[i];
|
|
|
|
else if(curtemp[i] > vmax) vmax = curtemp[i];
|
2022-03-01 07:42:20 +00:00
|
|
|
// if(iter % 50 == 0) println(hlog, "iter=", iter, " vmin=", vmin, " vmax=", vmax, " pairs=", isize(pairs));
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
if(!dispersion_count) {
|
|
|
|
if(!dispersion_long) dispersion_count = isize(d);
|
|
|
|
DEBB(DF_LOG, ("Dispersion count = ", isize(d), " celldist = ", celldist(c)));
|
|
|
|
}
|
2021-03-30 22:48:54 +00:00
|
|
|
/*
|
|
|
|
println(hlog, "dlast = ", d.back());
|
|
|
|
println(hlog, "dlast2 = ", d[d.size()-2]);
|
|
|
|
println(hlog, "vmin=", vmin, " vmax=",vmax, " end_at=", dispersion_end_at);
|
|
|
|
*/
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:19:19 +00:00
|
|
|
map<int, cellcrawler> scc;
|
|
|
|
|
2018-06-22 12:26:24 +00:00
|
|
|
pair<int, int> get_cellcrawler_id(cell *c) {
|
2022-03-01 07:42:20 +00:00
|
|
|
if(!bounded)
|
|
|
|
return make_pair(neuronId(*getNeuronSlow(c)), 0);
|
|
|
|
if(among(geometry, gZebraQuotient, gMinimal, gArnoldCat, gField435, gField534) || (euclid && quotient && !bounded) || IRREGULAR || (GDIM == 3 && sphere) || (hyperbolic && GDIM == 3)
|
2021-04-23 19:00:15 +00:00
|
|
|
|| (euclid && nonorientable)) {
|
2018-06-22 12:26:24 +00:00
|
|
|
// Zebra Quotient does exhibit some symmetries,
|
|
|
|
// but these are so small anyway that it is safer to just build
|
|
|
|
// a crawler for every neuron
|
|
|
|
return make_pair(neuronId(*getNeuronSlow(c)), 0);
|
2018-11-27 01:32:11 +00:00
|
|
|
// not yet implemented for cylinder
|
2018-06-22 12:26:24 +00:00
|
|
|
}
|
2019-11-30 17:52:24 +00:00
|
|
|
if(euclid && bounded && PURE && nonorientable)
|
2019-11-30 18:02:43 +00:00
|
|
|
return make_pair(euc2_coordinates(c).second * 2 + ctof(c), 0);
|
2018-06-22 12:26:24 +00:00
|
|
|
int id = 0, dir = 0;
|
2019-05-06 23:08:49 +00:00
|
|
|
#if CAP_GP
|
2018-08-28 15:17:34 +00:00
|
|
|
if(GOLDBERG) {
|
2018-06-22 12:26:24 +00:00
|
|
|
gp::local_info li = gp::get_local_info(c);
|
2019-08-09 12:12:33 +00:00
|
|
|
id = (li.relative.first & 15) + (li.relative.second & 15) * 16 + gmod(li.total_dir, S6) * 256;
|
2018-06-22 12:26:24 +00:00
|
|
|
// ld = li.last_dir;
|
|
|
|
}
|
2019-05-06 23:08:49 +00:00
|
|
|
#else
|
|
|
|
if(0) ;
|
|
|
|
#endif
|
2018-04-21 14:19:19 +00:00
|
|
|
else {
|
2018-06-22 12:26:24 +00:00
|
|
|
id = c->type == S7;
|
2018-08-17 22:46:45 +00:00
|
|
|
// if(id == 0) ld = c->c.spin(0);
|
2018-06-22 12:26:24 +00:00
|
|
|
}
|
|
|
|
/* if(geometry == gZebraQuotient) {
|
|
|
|
id = 8*id + ld;
|
|
|
|
id = 64 * id + c->master->zebraval;
|
|
|
|
return make_pair(id, 0);
|
|
|
|
} */
|
|
|
|
return make_pair(id, dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unit test: do the crawlers work correctly? */
|
|
|
|
|
|
|
|
bool verify_crawler(cellcrawler& cc, cellwalker cw) {
|
|
|
|
cc.sprawl(cw);
|
2018-08-17 22:46:45 +00:00
|
|
|
for(auto& d: cc.data) if(celldistance(cw.at, d.target.at) != d.dist)
|
2018-06-22 12:26:24 +00:00
|
|
|
return false;
|
|
|
|
vector<int> cellcounter(cells, 0);
|
2018-08-17 22:46:45 +00:00
|
|
|
for(auto& d: cc.data) cellcounter[d.target.at->landparam]++;
|
2018-06-22 12:26:24 +00:00
|
|
|
for(int i=0; i<cells; i++) if(cellcounter[i] != 1) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void verify_crawlers() {
|
|
|
|
|
|
|
|
setindex(false);
|
|
|
|
gaussian = 1;
|
|
|
|
auto& allcells = currentmap->allcells();
|
2018-06-22 12:47:24 +00:00
|
|
|
cells = isize(allcells);
|
2018-06-22 12:26:24 +00:00
|
|
|
net.resize(cells);
|
|
|
|
for(int i=0; i<cells; i++) net[i].where = allcells[i];
|
|
|
|
setindex(true);
|
|
|
|
map<int, cellcrawler> allcrawlers;
|
|
|
|
|
|
|
|
int uniq = 0, failures = 0;
|
|
|
|
|
|
|
|
printf("Verifying crawlers...\n");
|
|
|
|
for(cell *c: allcells) {
|
|
|
|
auto id = get_cellcrawler_id(c);
|
|
|
|
if(allcrawlers.count(id.first)) {
|
|
|
|
bool b = verify_crawler(allcrawlers[id.first], cellwalker(c, id.second));
|
|
|
|
if(!b) {
|
2020-02-23 02:48:46 +00:00
|
|
|
printf("cell %p: type = %d id = %d dir = %d / earlier crawler failed\n", hr::voidp(c), c->type, id.first, id.second);
|
2018-06-22 12:26:24 +00:00
|
|
|
failures++;
|
|
|
|
}
|
2018-04-21 14:19:19 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-06-22 12:26:24 +00:00
|
|
|
for(int i=0; i<c->type; i++)
|
|
|
|
for(auto& cc: allcrawlers) if(verify_crawler(cc.second, cellwalker(c, i))) {
|
2020-02-23 02:48:46 +00:00
|
|
|
printf("cell %p: type = %d id = %d dir = %d / also works id %d in direction %d\n", hr::voidp(c), c->type, id.first, id.second, cc.first, i);
|
2018-06-22 12:26:24 +00:00
|
|
|
uniq--;
|
|
|
|
goto breakcheck;
|
|
|
|
}
|
|
|
|
breakcheck:
|
|
|
|
cellcrawler cr;
|
|
|
|
cr.build(cellwalker(c, id.second));
|
|
|
|
allcrawlers[id.first] = move(cr);
|
|
|
|
uniq++;
|
2018-04-21 14:19:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-22 12:47:24 +00:00
|
|
|
printf("Crawlers constructed: %d (%d unique, %d failures)\n", isize(allcrawlers), uniq, failures);
|
2018-06-22 12:26:24 +00:00
|
|
|
setindex(false);
|
|
|
|
if(failures) exit(1);
|
2018-04-21 14:19:19 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
bool finished() { return t == 0; }
|
|
|
|
|
2021-03-30 22:47:44 +00:00
|
|
|
int krad, kqty;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
double ttpower = 1;
|
|
|
|
|
|
|
|
void step() {
|
|
|
|
|
|
|
|
if(t == 0) return;
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_dispersion();
|
|
|
|
initialize_neurons_initial();
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:50:37 +00:00
|
|
|
double tt = (t-.5) / tmax;
|
2017-09-01 20:14:02 +00:00
|
|
|
tt = pow(tt, ttpower);
|
|
|
|
|
|
|
|
double sigma = maxdist * tt;
|
|
|
|
|
|
|
|
if(qpct) {
|
|
|
|
int pct = (int) ((qpct * (t+.0)) / tmax);
|
|
|
|
if(pct != lpct) {
|
|
|
|
lpct = pct;
|
|
|
|
analyze();
|
2022-03-01 08:32:56 +00:00
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2022-03-01 07:44:59 +00:00
|
|
|
// if(gaussian)
|
|
|
|
// println(hlog, format("t = %6d/%6d %3d%% sigma=%10.7lf maxudist=%10.7lf\n", t, tmax, pct, sigma, maxudist));
|
|
|
|
// else
|
|
|
|
// println(hlog, format("t = %6d/%6d %3d%% dispid=%5d maxudist=%10.7lf\n", t, tmax, pct, dispid, maxudist));
|
|
|
|
// }
|
|
|
|
} //
|
2017-09-01 20:14:02 +00:00
|
|
|
int id = hrand(samples);
|
|
|
|
neuron& n = winner(id);
|
|
|
|
whowon.resize(samples);
|
|
|
|
whowon[id] = &n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
for(neuron& n2: net) {
|
|
|
|
int d = celldistance(n.where, n2.where);
|
|
|
|
double nu = learning_factor;
|
|
|
|
// nu *= exp(-t*(double)maxdist/perdist);
|
|
|
|
// nu *= exp(-t/t2);
|
|
|
|
nu *= exp(-sqr(d/sigma));
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int k=0; k<columns; k++)
|
2017-09-01 20:14:02 +00:00
|
|
|
n2.net[k] += nu * (irisdata[id][k] - n2.net[k]);
|
|
|
|
} */
|
|
|
|
|
2018-06-22 12:26:24 +00:00
|
|
|
auto cid = get_cellcrawler_id(n.where);
|
|
|
|
cellcrawler& s = scc[cid.first];
|
|
|
|
s.sprawl(cellwalker(n.where, cid.second));
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2022-03-01 07:43:42 +00:00
|
|
|
vector<float> fake(0,0);
|
|
|
|
/* for(auto& sd: s.data)
|
|
|
|
fake.push_back(exp(-sqr(sd.dist/sigma))); */
|
|
|
|
|
|
|
|
int dispersion_count = isize(s.dispersion);
|
|
|
|
int dispid = int(dispersion_count * tt);
|
|
|
|
|
2018-04-21 14:19:19 +00:00
|
|
|
auto it = gaussian ? fake.begin() : s.dispersion[dispid].begin();
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
for(auto& sd: s.data) {
|
2018-08-17 22:46:45 +00:00
|
|
|
neuron *n2 = getNeuron(sd.target.at);
|
2022-03-01 07:43:42 +00:00
|
|
|
if(!n2) { it++; continue; }
|
2021-04-23 19:00:15 +00:00
|
|
|
n2->debug++;
|
2017-09-01 20:14:02 +00:00
|
|
|
double nu = learning_factor;
|
|
|
|
|
2021-03-30 22:50:37 +00:00
|
|
|
if(gaussian) {
|
2017-09-01 20:14:02 +00:00
|
|
|
nu *= exp(-sqr(sd.dist/sigma));
|
2021-03-30 22:50:37 +00:00
|
|
|
if(isnan(nu))
|
|
|
|
throw hr_exception(lalign(0, "obtained nan, ", sd.dist, " / ", sigma));
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
else
|
|
|
|
nu *= *(it++);
|
2021-03-30 22:50:37 +00:00
|
|
|
|
|
|
|
for(int k=0; k<columns; k++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
n2->net[k] += nu * (data[id].val[k] - n2->net[k]);
|
2022-03-01 07:43:42 +00:00
|
|
|
/* if(isnan(n2->net[k]))
|
|
|
|
throw hr_exception("obtained nan somehow, nu = " + lalign(0, nu)); */
|
2021-03-30 22:50:37 +00:00
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
2021-04-23 19:00:15 +00:00
|
|
|
|
2022-03-01 07:43:42 +00:00
|
|
|
/* for(auto& n2: net) {
|
2021-04-23 19:00:15 +00:00
|
|
|
if(n2.debug > 1) throw hr_exception("sprawler error");
|
|
|
|
n2.debug = 0;
|
2022-03-01 07:43:42 +00:00
|
|
|
} */
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
t--;
|
|
|
|
if(t == 0) analyze();
|
|
|
|
}
|
|
|
|
|
|
|
|
int initdiv = 1;
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
flagtype state = 0;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
vector<double> bdiffs;
|
|
|
|
vector<unsigned short> bids;
|
|
|
|
vector<double> bdiffn;
|
|
|
|
|
|
|
|
int showsample(int id) {
|
2018-07-05 07:55:11 +00:00
|
|
|
if(sample_vdata_id.count(id))
|
|
|
|
return sample_vdata_id[id];
|
2018-07-04 12:37:33 +00:00
|
|
|
if(bids.size()) {
|
2018-07-05 05:30:15 +00:00
|
|
|
if(net[bids[id]].drawn_samples >= net[bids[id]].max_group_here) {
|
2018-07-04 12:37:33 +00:00
|
|
|
ld bdist = 1e18;
|
|
|
|
int whichid = -1;
|
2018-07-05 07:55:11 +00:00
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
int s = p.first;
|
|
|
|
if(bids[s] == bids[id]) {
|
|
|
|
ld cdist = vnorm(data[s].val, data[id].val);
|
|
|
|
if(cdist < bdist) bdist = cdist, whichid = p.second;
|
2018-07-04 12:37:33 +00:00
|
|
|
}
|
2018-07-05 07:55:11 +00:00
|
|
|
}
|
2018-07-04 12:37:33 +00:00
|
|
|
return whichid;
|
|
|
|
}
|
|
|
|
net[bids[id]].drawn_samples++;
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
int i = vdata.size();
|
2018-07-05 07:55:11 +00:00
|
|
|
sample_vdata_id[id] = i;
|
2017-09-01 20:14:02 +00:00
|
|
|
vdata.emplace_back();
|
|
|
|
auto& v = vdata.back();
|
|
|
|
v.name = data[id].name;
|
|
|
|
v.cp = dftcolor;
|
2018-08-17 22:46:45 +00:00
|
|
|
createViz(i, bids.size() ? net[bids[id]].where : cwt.at, Id);
|
2017-09-01 20:14:02 +00:00
|
|
|
v.m->store();
|
2018-07-05 07:55:11 +00:00
|
|
|
return i;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 05:31:27 +00:00
|
|
|
int showsample(string s) {
|
|
|
|
if(s == "") return -1;
|
|
|
|
int ret = -1;
|
2017-09-01 20:14:02 +00:00
|
|
|
for(int i=0; i<samples; i++) {
|
|
|
|
if(s[0] != '*' && data[i].name == s)
|
2018-07-05 05:31:27 +00:00
|
|
|
ret = showsample(i);
|
2017-09-01 20:14:02 +00:00
|
|
|
if(s[0] == '*' && data[i].name.find(s.substr(1)) != string::npos)
|
2018-07-05 05:31:27 +00:00
|
|
|
ret = showsample(i);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
2018-07-05 05:31:27 +00:00
|
|
|
return ret;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void showbestsamples() {
|
|
|
|
vector<int> samplesbak;
|
|
|
|
for(auto& n: net)
|
2018-07-04 12:37:33 +00:00
|
|
|
if(n.allsamples)
|
2017-09-01 20:14:02 +00:00
|
|
|
showsample(n.bestsample);
|
|
|
|
analyze();
|
|
|
|
}
|
2018-06-28 08:27:35 +00:00
|
|
|
|
|
|
|
int kohrestrict = 1000000;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:58:56 +00:00
|
|
|
void initialize_rv();
|
2021-03-30 22:41:13 +00:00
|
|
|
|
|
|
|
void initialize_neurons() {
|
|
|
|
if(state & KS_NEURONS) return;
|
|
|
|
create_neurons();
|
|
|
|
state |= KS_NEURONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<cell*> gen_neuron_cells() {
|
|
|
|
vector<cell*> allcells;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
if(krad) {
|
|
|
|
celllister cl(cwt.at, krad, 1000000, NULL);
|
|
|
|
allcells = cl.lst;
|
|
|
|
}
|
|
|
|
else if(kqty) {
|
|
|
|
celllister cl(cwt.at, 999, kqty, NULL);
|
|
|
|
allcells = cl.lst;
|
|
|
|
allcells.resize(kqty);
|
|
|
|
}
|
|
|
|
else allcells = currentmap->allcells();
|
|
|
|
|
|
|
|
if(isize(allcells) > kohrestrict) {
|
|
|
|
map<cell*, int> clindex;
|
|
|
|
for(int i=0; i<isize(allcells); i++) clindex[allcells[i]] = i;
|
|
|
|
sort(allcells.begin(), allcells.end(), [&clindex] (cell *c1, cell *c2) {
|
2021-04-23 19:00:15 +00:00
|
|
|
ld d1 = hdist0(tC0(ggmatrix(c1)));
|
|
|
|
ld d2 = hdist0(tC0(ggmatrix(c2)));
|
|
|
|
if(d1 < d2 - 1e-6)
|
|
|
|
return true;
|
|
|
|
if(d2 < d1 - 1e-6)
|
|
|
|
return false;
|
|
|
|
return clindex[c1] < clindex[c2];
|
2021-03-30 22:41:13 +00:00
|
|
|
});
|
|
|
|
int at = kohrestrict;
|
|
|
|
ld dist = hdist0(tC0(ggmatrix(allcells[at-1])));
|
|
|
|
while(at < isize(allcells) && hdist0(tC0(ggmatrix(allcells[at]))) < dist + 1e-6) at++;
|
|
|
|
int at1 = kohrestrict;
|
|
|
|
while(at1 > 0 && hdist0(tC0(ggmatrix(allcells[at1-1]))) > dist - 1e-6) at1--;
|
|
|
|
printf("Cells numbered [%d,%d) are in the same distance\n", at1, at);
|
|
|
|
allcells.resize(kohrestrict);
|
|
|
|
for(int i=kohrestrict; i<isize(allcells); i++) {
|
|
|
|
setdist(allcells[i], 0, nullptr);
|
|
|
|
allcells[i]->wall = waInvisibleFloor;
|
2018-06-28 08:27:35 +00:00
|
|
|
}
|
2021-03-30 22:41:13 +00:00
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
return allcells;
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_neurons() {
|
|
|
|
initialize_rv();
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
if(!samples) {
|
|
|
|
fprintf(stderr, "Error: SOM without samples\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
weight_label = "quantity";
|
|
|
|
|
|
|
|
DEBBI(DF_LOG, ("Creating neurons"));
|
|
|
|
|
|
|
|
auto allcells = gen_neuron_cells();
|
|
|
|
|
|
|
|
|
|
|
|
cells = isize(allcells);
|
|
|
|
net.resize(cells);
|
|
|
|
for(int i=0; i<cells; i++) {
|
|
|
|
net[i].where = allcells[i];
|
|
|
|
allcells[i]->landparam = i;
|
|
|
|
net[i].where->land = laCanvas;
|
|
|
|
}
|
2018-07-05 07:55:11 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
for(neuron& n: net) for(int d=BARLEV; d>=7; d--) setdist(n.where, d, NULL);
|
|
|
|
DEBB(DF_LOG, ("number of neurons = ", cells));
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_neuron_initial() {
|
|
|
|
initialize_neurons();
|
|
|
|
DEBBI(DF_LOG, ("Setting initial neuron values"));
|
|
|
|
for(int i=0; i<cells; i++) {
|
|
|
|
alloc(net[i].net);
|
|
|
|
for(int k=0; k<columns; k++)
|
|
|
|
net[i].net[k] = 0;
|
|
|
|
for(int k=0; k<columns; k++)
|
|
|
|
for(int z=0; z<initdiv; z++)
|
|
|
|
net[i].net[k] += data[hrand(samples)].val[k] / initdiv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_neurons_initial() {
|
|
|
|
if(state & KS_NEURONS_INI) return;
|
|
|
|
set_neuron_initial();
|
|
|
|
state |= KS_NEURONS_INI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_samples_to_show() {
|
|
|
|
if(state & KS_SAMPLES) return;
|
|
|
|
if(noshow) return;
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
DEBBI(DF_LOG, ("Initializing samples-to-show (", isize(samples_to_show), " samples", ")"));
|
|
|
|
if(!noshow) for(int s: samples_to_show) {
|
|
|
|
int vdid = isize(vdata);
|
|
|
|
sample_vdata_id[s] = vdid;
|
|
|
|
vdata.emplace_back();
|
|
|
|
auto &vd = vdata.back();
|
|
|
|
vd.name = data[s].name;
|
|
|
|
vd.cp = dftcolor;
|
|
|
|
createViz(vdid, cwt.at, Id);
|
|
|
|
storeall(vdid);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
samples_to_show.clear();
|
|
|
|
state |= KS_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_dispersion() {
|
|
|
|
if(state & KS_DISPERSION) return;
|
|
|
|
|
|
|
|
initialize_neurons();
|
|
|
|
|
|
|
|
DEBBI(DF_LOG, ("Initializing dispersion"));
|
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
if(gaussian || true) {
|
2021-03-30 22:41:13 +00:00
|
|
|
DEBB(DF_LOG, ("dist = ", fts(mydistance(net[0].where, net[1].where))));
|
|
|
|
cell *c1 = net[cells/2].where;
|
|
|
|
vector<double> mapdist;
|
|
|
|
for(neuron &n2: net) mapdist.push_back(mydistance(c1,n2.where));
|
|
|
|
sort(mapdist.begin(), mapdist.end());
|
|
|
|
maxdist = mapdist[isize(mapdist)*5/6] * distmul;
|
|
|
|
DEBB(DF_LOG, ("maxdist = ", fts(maxdist)));
|
|
|
|
}
|
2022-03-01 07:42:20 +00:00
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
dispersion_count = 0;
|
2022-03-01 07:42:20 +00:00
|
|
|
|
|
|
|
if(!gaussian)
|
|
|
|
DEBB(DF_LOG, ("dispersion precision = ", dispersion_precision, " end_at = ", dispersion_end_at, "...\n"));
|
|
|
|
|
|
|
|
DEBB(DF_LOG, ("building crawlers...\n"));
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
scc.clear();
|
|
|
|
for(int i=0; i<cells; i++) {
|
|
|
|
cell *c = net[i].where;
|
|
|
|
auto cid = get_cellcrawler_id(c);
|
|
|
|
if(!scc.count(cid.first)) {
|
2022-03-01 07:42:20 +00:00
|
|
|
// DEBB(DF_LOG, ("Building cellcrawler id = ", itsh(cid.first)));
|
2021-03-30 22:41:13 +00:00
|
|
|
buildcellcrawler(c, scc[cid.first], cid.second);
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
DEBB(DF_LOG, ("crawlers constructed = ", isize(scc), "\n"));
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
lpct = -46130;
|
|
|
|
state |= KS_DISPERSION;
|
|
|
|
}
|
|
|
|
|
2018-07-09 18:09:56 +00:00
|
|
|
void describe_cell(cell *c) {
|
2017-09-01 20:14:02 +00:00
|
|
|
if(cmode & sm::HELP) return;
|
|
|
|
neuron *n = getNeuronSlow(c);
|
|
|
|
if(!n) return;
|
2018-09-07 13:15:53 +00:00
|
|
|
string h;
|
|
|
|
h += "cell number: " + its(neuronId(*n)) + " (" + its(n->allsamples) + ")\n";
|
|
|
|
h += "parameters:"; for(int k=0; k<columns; k++) h += " " + fts(n->net[k]);
|
|
|
|
h += ", u-matrix = " + fts(n->udist);
|
|
|
|
h += "\n";
|
2017-09-01 20:14:02 +00:00
|
|
|
vector<pair<double, int>> v;
|
|
|
|
for(int s=0; s<samples; s++) if(whowon[s] == n) v.emplace_back(vnorm(n->net, data[s].val), s);
|
2018-06-30 14:29:37 +00:00
|
|
|
for(int i=1; i<isize(v); i++) swap(v[i], v[rand() % (i+1)]);
|
2017-09-30 09:47:00 +00:00
|
|
|
sort(v.begin(), v.end(), [] (pair<double,int> a, pair<double,int> b) { return a.first < b.first; });
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-06-22 12:47:24 +00:00
|
|
|
for(int i=0; i<isize(v) && i<20; i++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
int s = v[i].second;
|
2018-09-07 13:15:53 +00:00
|
|
|
h += "sample "+its(s)+":";
|
|
|
|
for(int k=0; k<columns; k++) h += " " + fts(data[s].val[k]);
|
|
|
|
h += " "; h += data[s].name; h += "\n";
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
2018-09-07 13:15:53 +00:00
|
|
|
appendHelp(h);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace levelline {
|
|
|
|
|
|
|
|
struct levelline {
|
|
|
|
int column, qty;
|
2018-09-04 17:53:42 +00:00
|
|
|
color_t color;
|
2017-09-01 20:14:02 +00:00
|
|
|
vector<double> values;
|
|
|
|
bool modified;
|
|
|
|
};
|
|
|
|
|
|
|
|
vector<levelline> levellines;
|
|
|
|
|
|
|
|
bool on;
|
|
|
|
|
|
|
|
void create() {
|
2018-07-09 16:17:40 +00:00
|
|
|
int xlalpha = part(default_edgetype.color, 0);
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int i=0; i<columns; i++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
levellines.emplace_back();
|
|
|
|
levelline& lv = levellines.back();
|
|
|
|
lv.column = i;
|
|
|
|
lv.color = ((hrandpos() & 0xFFFFFF) << 8) | xlalpha;
|
|
|
|
lv.qty = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void build() {
|
|
|
|
if(levellines.size() == 0) create();
|
|
|
|
on = false;
|
|
|
|
for(auto& lv: levellines) {
|
2021-03-30 22:52:43 +00:00
|
|
|
if(!lv.qty || lv.qty < 0) { lv.values.clear(); continue; }
|
2017-09-01 20:14:02 +00:00
|
|
|
on = true;
|
|
|
|
if(!lv.modified) continue;
|
|
|
|
lv.modified = false;
|
|
|
|
vector<double> sample;
|
|
|
|
for(int j=0; j<=1024; j++) sample.push_back(data[hrand(samples)].val[lv.column]);
|
|
|
|
sort(sample.begin(), sample.end());
|
|
|
|
lv.values.clear();
|
|
|
|
lv.values.push_back(-1e10);
|
|
|
|
for(int j=0; j<=1024; j+=1024 >> (lv.qty)) lv.values.push_back(sample[j]);
|
|
|
|
lv.values.push_back(1e10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw() {
|
|
|
|
if(!on) return;
|
|
|
|
for(auto& g: gmatrix) {
|
|
|
|
cell *c1 = g.first;
|
2020-07-29 21:34:00 +00:00
|
|
|
shiftmatrix T = g.second;
|
2017-09-01 20:14:02 +00:00
|
|
|
neuron *n1 = getNeuron(c1);
|
|
|
|
if(!n1) continue;
|
|
|
|
for(int i=0; i<c1->type; i++) {
|
2018-08-17 22:46:45 +00:00
|
|
|
cell *c2 = c1->move(i);
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!c2) continue;
|
2018-08-17 22:46:45 +00:00
|
|
|
cell *c3 = c1->modmove(i-1);
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!c3) continue;
|
|
|
|
|
|
|
|
if(!gmatrix.count(c2)) continue;
|
|
|
|
if(!gmatrix.count(c3)) continue;
|
|
|
|
double d2 = hdist(tC0(T), tC0(gmatrix[c2]));
|
|
|
|
double d3 = hdist(tC0(T), tC0(gmatrix[c3]));
|
|
|
|
|
|
|
|
neuron *n2 = getNeuron(c2);
|
|
|
|
if(!n2) continue;
|
|
|
|
neuron *n3 = getNeuron(c3);
|
|
|
|
if(!n3) continue;
|
|
|
|
|
|
|
|
for(auto& l: levellines) {
|
|
|
|
auto val1 = n1->net[l.column];
|
|
|
|
auto val2 = n2->net[l.column];
|
|
|
|
auto val3 = n3->net[l.column];
|
|
|
|
auto v1 = lower_bound(l.values.begin(), l.values.end(), val1);
|
|
|
|
auto v2 = lower_bound(l.values.begin(), l.values.end(), val2);
|
|
|
|
auto v3 = lower_bound(l.values.begin(), l.values.end(), val3);
|
|
|
|
auto draw = [&] () {
|
|
|
|
auto vmid = *v1;
|
|
|
|
queueline(
|
2018-08-19 14:28:36 +00:00
|
|
|
(T * ddspin(c1,i) * xpush0(d2 * (vmid-val1) / (val2-val1))),
|
|
|
|
(T * ddspin(c1,i-1) * xpush0(d3 * (vmid-val1) / (val3-val1))),
|
2018-08-01 09:07:22 +00:00
|
|
|
l.color, vid.linequality);
|
2017-09-01 20:14:02 +00:00
|
|
|
};
|
|
|
|
while(v1 < v2 && v1 < v3) {
|
|
|
|
draw();
|
|
|
|
v1++;
|
|
|
|
}
|
|
|
|
while(v1 > v2 && v1 > v3) {
|
|
|
|
v1--;
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setindex(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void show() {
|
|
|
|
if(levellines.size() == 0) create();
|
|
|
|
gamescreen(0);
|
2018-12-13 16:02:10 +00:00
|
|
|
cmode = sm::SIDE | sm::MAYDARK;
|
2017-09-01 20:14:02 +00:00
|
|
|
dialog::init("level lines");
|
|
|
|
char nx = 'a';
|
|
|
|
for(auto &l : levellines) {
|
|
|
|
dialog::addSelItem(colnames[l.column], its(l.qty), nx++);
|
|
|
|
dialog::lastItem().colorv = l.color >> 8;
|
|
|
|
}
|
|
|
|
dialog::addItem("exit menu", '0');
|
|
|
|
dialog::addItem("shift+letter to change color", 0);
|
|
|
|
dialog::display();
|
|
|
|
keyhandler = [] (int sym, int uni) {
|
|
|
|
dialog::handleNavigation(sym, uni);
|
2018-06-22 12:47:24 +00:00
|
|
|
if(uni >= 'a' && uni - 'a' + isize(levellines)) {
|
2017-09-01 20:14:02 +00:00
|
|
|
auto& l = levellines[uni - 'a'];
|
|
|
|
dialog::editNumber(l.qty, 0, 10, 1, 0, colnames[l.column],
|
|
|
|
XLAT("Controls the number of level lines."));
|
|
|
|
dialog::reaction = [&l] () {
|
|
|
|
l.modified = true;
|
|
|
|
build();
|
|
|
|
};
|
|
|
|
}
|
2018-06-22 12:47:24 +00:00
|
|
|
else if(uni >= 'A' && uni - 'A' + isize(levellines)) {
|
2017-09-01 20:14:02 +00:00
|
|
|
auto& l = levellines[uni - 'A'];
|
|
|
|
dialog::openColorDialog(l.color, NULL);
|
2018-07-09 16:17:40 +00:00
|
|
|
dialog::dialogflags |= sm::MAYDARK | sm::SIDE;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(doexiton(sym, uni)) popScreen();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void ksave(const string& fname) {
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_neurons_initial();
|
2018-04-21 10:17:22 +00:00
|
|
|
FILE *f = fopen(fname.c_str(), "wt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
|
|
|
fprintf(stderr, "Could not save the network\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(f, "%d %d\n", cells, t);
|
|
|
|
for(neuron& n: net) {
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int k=0; k<columns; k++)
|
2017-09-01 20:14:02 +00:00
|
|
|
fprintf(f, "%.9lf ", n.net[k]);
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void kload(const string& fname) {
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_neurons();
|
2017-09-01 20:14:02 +00:00
|
|
|
int xcells;
|
2019-05-21 22:05:58 +00:00
|
|
|
fhstream f(fname.c_str(), "rt");
|
|
|
|
if(!f.f) {
|
2018-07-06 17:28:20 +00:00
|
|
|
fprintf(stderr, "Could not load the network: %s\n", fname.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2019-05-21 22:05:58 +00:00
|
|
|
if(!scan(f, xcells, t)) {
|
2018-07-06 17:28:20 +00:00
|
|
|
fprintf(stderr, "Bad network format: %s\n", fname.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Loading the network %s...\n", fname.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
if(xcells != cells) {
|
2018-05-18 11:57:55 +00:00
|
|
|
fprintf(stderr, "Error: bad number of cells (x=%d c=%d)\n", xcells, cells);
|
2017-09-01 20:14:02 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for(neuron& n: net) {
|
2019-05-21 22:05:58 +00:00
|
|
|
for(int k=0; k<columns; k++) if(!scan(f, n.net[k])) return;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
analyze();
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void ksavew(const string& fname) {
|
|
|
|
FILE *f = fopen(fname.c_str(), "wt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
2018-07-06 17:28:20 +00:00
|
|
|
fprintf(stderr, "Could not save the weights: %s\n", fname.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Saving the network to %s...\n", fname.c_str());
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int i=0; i<columns; i++)
|
2017-09-01 20:14:02 +00:00
|
|
|
fprintf(f, "%s=%.9lf\n", colnames[i].c_str(), weights[i]);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void kloadw(const string& fname) {
|
|
|
|
FILE *f = fopen(fname.c_str(), "rt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
|
|
|
fprintf(stderr, "Could not load the weights\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int i=0; i<columns; i++) {
|
2017-09-01 20:14:02 +00:00
|
|
|
string s1, s2;
|
|
|
|
char kind = 0;
|
|
|
|
while(true) {
|
|
|
|
int c = fgetc(f);
|
|
|
|
if(c == 10 || c == 13 || c == -1) {
|
|
|
|
if(s1 == "" && !kind && c != -1) continue;
|
|
|
|
if(s1 != "") colnames[i] = s1;
|
|
|
|
if(kind == '=') weights[i] = atof(s2.c_str());
|
|
|
|
if(kind == '*') weights[i] *= atof(s2.c_str());
|
|
|
|
if(kind == '/') weights[i] /= atof(s2.c_str());
|
|
|
|
if(c == -1) break;
|
|
|
|
goto nexti;
|
|
|
|
}
|
|
|
|
else if(c == '=' || c == '/' || c == '*') kind = c;
|
2021-06-25 12:00:01 +00:00
|
|
|
else if(rv_ignore(c)) ;
|
2017-09-01 20:14:02 +00:00
|
|
|
else (kind?s2:s1) += c;
|
|
|
|
}
|
|
|
|
nexti: ;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
analyze();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned lastprogress;
|
|
|
|
void progress(string s) {
|
|
|
|
if(SDL_GetTicks() >= lastprogress + (noGUI ? 500 : 100)) {
|
|
|
|
if(noGUI)
|
|
|
|
printf("%s\n", s.c_str());
|
|
|
|
else {
|
|
|
|
clearMessages();
|
|
|
|
addMessage(s);
|
|
|
|
mainloopiter();
|
|
|
|
}
|
|
|
|
lastprogress = SDL_GetTicks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
template<class T> void save_raw(string fname, const vector<T>& v) {
|
|
|
|
FILE *f = fopen(fname.c_str(), "wb");
|
|
|
|
fwrite(&v[0], sizeof(v[0]), v.size(), f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
template<class T> void load_raw(string fname, vector<T>& v) {
|
|
|
|
FILE *f = fopen(fname.c_str(), "rb");
|
|
|
|
if(!f) { fprintf(stderr, "file does not exist: %s\n", fname.c_str()); exit(1); }
|
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
auto s = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
v.resize(s / sizeof(v[0]));
|
2018-07-14 06:45:14 +00:00
|
|
|
hr::ignore(fread(&v[0], sizeof(v[0]), v.size(), f));
|
2018-07-04 12:37:33 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-05 05:30:15 +00:00
|
|
|
bool groupsizes_known = false;
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
void do_classify() {
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_neurons_initial();
|
2018-07-04 12:37:33 +00:00
|
|
|
if(bids.empty()) {
|
|
|
|
printf("Classifying...\n");
|
|
|
|
bids.resize(samples, 0);
|
|
|
|
bdiffs.resize(samples, 1e20);
|
|
|
|
for(int s=0; s<samples; s++) {
|
|
|
|
for(int n=0; n<cells; n++) {
|
|
|
|
double diff = vnorm(net[n].net, data[s].val);
|
|
|
|
if(diff < bdiffs[s]) bdiffs[s] = diff, bids[s] = n, whowon[s] = &net[n];
|
|
|
|
}
|
|
|
|
if(!(s % 128))
|
|
|
|
progress("Classifying: " + its(s) + "/" + its(samples));
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-04 12:37:33 +00:00
|
|
|
if(bdiffs.empty()) {
|
|
|
|
printf("Computing distances...\n");
|
|
|
|
bdiffs.resize(samples, 1e20);
|
|
|
|
for(int s=0; s<samples; s++)
|
|
|
|
bdiffs[s] = vnorm(net[bids[s]].net, data[s].val);
|
|
|
|
}
|
|
|
|
if(bdiffn.empty()) {
|
|
|
|
printf("Finding samples...\n");
|
|
|
|
bdiffn.resize(cells, 1e20);
|
2021-03-30 22:43:56 +00:00
|
|
|
for(int i=0; i<cells; i++) net[i].bestsample = -1;
|
2018-07-04 12:37:33 +00:00
|
|
|
for(int s=0; s<samples; s++) {
|
|
|
|
int n = bids[s];
|
|
|
|
double diff = bdiffs[s];
|
|
|
|
if(diff < bdiffn[n]) bdiffn[n] = diff, net[n].bestsample = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
whowon.resize(samples);
|
|
|
|
for(int i=0; i<samples; i++) whowon[i] = &net[bids[i]];
|
|
|
|
for(neuron& n: net) n.allsamples = 0;
|
|
|
|
for(int sn: bids) net[sn].allsamples++;
|
2018-07-05 05:30:15 +00:00
|
|
|
|
|
|
|
if(!groupsizes_known) {
|
|
|
|
groupsizes_known = true;
|
|
|
|
|
|
|
|
vector<int> neurons_to_sort;
|
|
|
|
for(int i=0; i<cells; i++) neurons_to_sort.push_back(i);
|
|
|
|
sort(neurons_to_sort.begin(), neurons_to_sort.end(), [] (int i, int j) { return net[i].allsamples < net[j].allsamples; });
|
|
|
|
int last = 0;
|
|
|
|
int lastfirst = 0, lastlast = 0;
|
|
|
|
for(int i=0; i<cells; i++) {
|
|
|
|
int ngroup = min_group + ((max_group - min_group) * i + (cells/2)) / (cells-1);
|
|
|
|
int as = net[neurons_to_sort[i]].allsamples;
|
|
|
|
if(ngroup != last) {
|
|
|
|
if(last) printf("%d: %d - %d\n", last, lastfirst, lastlast);
|
|
|
|
last = ngroup; lastfirst = as;
|
|
|
|
}
|
|
|
|
net[neurons_to_sort[i]].max_group_here = ngroup;
|
|
|
|
lastlast = as;
|
|
|
|
}
|
|
|
|
if(last) printf("%d: %d - %d\n", last, lastfirst, lastlast);
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
coloring();
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-05 05:30:15 +00:00
|
|
|
void fillgroups() {
|
|
|
|
do_classify();
|
|
|
|
vector<int> samples_to_sort;
|
|
|
|
for(int i=0; i<samples; i++) samples_to_sort.push_back(i);
|
2021-07-14 07:22:33 +00:00
|
|
|
hrandom_shuffle(samples_to_sort);
|
2018-07-05 05:30:15 +00:00
|
|
|
for(int i=0; i<samples; i++) if(net[bids[i]].drawn_samples < net[bids[i]].max_group_here)
|
|
|
|
showsample(i);
|
|
|
|
distribute_neurons();
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
void kclassify(const string& fname_classify) {
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-07-04 12:37:33 +00:00
|
|
|
do_classify();
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
if(fname_classify != "") {
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Listing classification to %s...\n", fname_classify.c_str());
|
2018-04-21 10:17:22 +00:00
|
|
|
FILE *f = fopen(fname_classify.c_str(), "wt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
|
|
|
printf("Failed to open file\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for(int s=0; s<samples; s++)
|
|
|
|
fprintf(f, "%s;%d\n", data[s].name.c_str(), bids[s]);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 12:37:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kclassify_save_raw(const string& fname_classify) {
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Saving raw classify to %s...\n", fname_classify.c_str());
|
2018-07-04 12:37:33 +00:00
|
|
|
do_classify();
|
|
|
|
save_raw(fname_classify, bids);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kclassify_load_raw(const string& fname_classify) {
|
2018-07-06 17:28:20 +00:00
|
|
|
printf("Loading raw classify from %s...\n", fname_classify.c_str());
|
2018-07-04 12:37:33 +00:00
|
|
|
load_raw(fname_classify, bids);
|
|
|
|
do_classify();
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:17:40 +00:00
|
|
|
void load_edges(const string& fname_edges, string edgename, int pick = 0) {
|
2018-07-04 12:37:33 +00:00
|
|
|
do_classify();
|
2018-07-09 16:17:40 +00:00
|
|
|
auto t = add_edgetype(edgename);
|
2018-07-04 12:37:33 +00:00
|
|
|
vector<pair<int, int>> edgedata;
|
|
|
|
load_raw(fname_edges, edgedata);
|
|
|
|
int N = isize(edgedata);
|
|
|
|
if(pick > 0 && pick < N) {
|
|
|
|
for(int i=1; i<N; i++) swap(edgedata[i], edgedata[hrand(i+1)]);
|
|
|
|
edgedata.resize(N = pick);
|
|
|
|
}
|
2018-07-09 16:17:40 +00:00
|
|
|
t->visible_from = 1. / N;
|
2018-07-04 12:37:33 +00:00
|
|
|
vector<pair<int, int>> edgedata2;
|
|
|
|
for(auto p: edgedata)
|
|
|
|
edgedata2.emplace_back(showsample(p.first), showsample(p.second));
|
|
|
|
distribute_neurons();
|
2018-07-09 16:17:40 +00:00
|
|
|
int i = 0;
|
2018-07-04 12:37:33 +00:00
|
|
|
for(auto p: edgedata2)
|
2018-11-24 00:31:15 +00:00
|
|
|
if(p.first >= 0 && p.second >= 0)
|
|
|
|
addedge(p.first, p.second, 1 / (i+++.5), true, t);
|
|
|
|
else {
|
|
|
|
printf("error reading graph\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 07:56:09 +00:00
|
|
|
void random_edges(int q) {
|
2018-07-09 16:17:40 +00:00
|
|
|
auto t = add_edgetype("random");
|
2018-07-05 07:56:09 +00:00
|
|
|
vector<int> ssamp;
|
|
|
|
for(auto p: sample_vdata_id) ssamp.push_back(p.second);
|
2018-07-09 16:17:40 +00:00
|
|
|
for(int i=0; i<q; i++)
|
|
|
|
addedge(ssamp[hrand(isize(ssamp))], ssamp[hrand(isize(ssamp))], 0, true, t);
|
2018-07-05 07:56:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void klistsamples(const string& fname_samples, bool best, bool colorformat) {
|
|
|
|
if(fname_samples != "") {
|
2017-09-01 20:14:02 +00:00
|
|
|
printf("Listing samples...\n");
|
2018-04-21 10:17:22 +00:00
|
|
|
FILE *f = fopen(fname_samples.c_str(), "wt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
|
|
|
printf("Failed to open file\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto klistsample = [f, colorformat] (int id, int neu) {
|
|
|
|
if(colorformat) {
|
|
|
|
fprintf(f, "%s;+#%d\n", data[id].name.c_str(), neu);
|
|
|
|
}
|
|
|
|
else {
|
2018-09-04 17:53:42 +00:00
|
|
|
for(int k=0; k<columns; k++)
|
2017-09-01 20:14:02 +00:00
|
|
|
fprintf(f, "%.4lf ", data[id].val[k]);
|
|
|
|
fprintf(f, "!%s\n", data[id].name.c_str());
|
|
|
|
}
|
|
|
|
};
|
2018-09-04 17:53:42 +00:00
|
|
|
if(!colorformat) fprintf(f, "%d\n", columns);
|
2017-09-01 20:14:02 +00:00
|
|
|
if(best)
|
|
|
|
for(int n=0; n<cells; n++) {
|
2018-07-04 12:37:33 +00:00
|
|
|
if(!net[n].allsamples && !net[n].drawn_samples) { if(!colorformat) fprintf(f, "\n"); continue; }
|
2021-03-30 22:43:56 +00:00
|
|
|
if(net[n].bestsample >= 0)
|
|
|
|
klistsample(net[n].bestsample, n);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else
|
2018-07-05 07:55:11 +00:00
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
int id = p.first;
|
|
|
|
klistsample(id, neuronId(*(whowon[id])));
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-21 10:17:22 +00:00
|
|
|
void neurondisttable(const string &name) {
|
2018-04-21 14:19:29 +00:00
|
|
|
FILE *f = fopen(name.c_str(), "wt");
|
2017-09-01 20:14:02 +00:00
|
|
|
if(!f) {
|
2018-04-21 14:19:29 +00:00
|
|
|
printf("Could not open file: %s\n", name.c_str());
|
2017-09-01 20:14:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-22 12:47:24 +00:00
|
|
|
int neurons = isize(net);
|
2017-09-01 20:14:02 +00:00
|
|
|
fprintf(f, "%d\n", neurons);
|
|
|
|
for(int i=0; i<neurons; i++) {
|
|
|
|
for(int j=0; j<neurons; j++) fprintf(f, "%3d", celldistance(net[i].where, net[j].where));
|
|
|
|
// todo: build the table correctly for gaussian=2
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2021-03-30 22:47:25 +00:00
|
|
|
bool animate = true;
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
void steps() {
|
2021-03-30 22:47:25 +00:00
|
|
|
if(kohonen::animate && !kohonen::finished()) {
|
2017-09-01 20:14:02 +00:00
|
|
|
unsigned int t = SDL_GetTicks();
|
|
|
|
while(SDL_GetTicks() < t+20) kohonen::step();
|
|
|
|
setindex(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 11:09:10 +00:00
|
|
|
void shift_color(int i) {
|
|
|
|
whattodraw[i]++;
|
|
|
|
if(whattodraw[i] == columns) whattodraw[i] = -5;
|
|
|
|
coloring();
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
void showMenu() {
|
|
|
|
string parts[3] = {"red", "green", "blue"};
|
|
|
|
for(int i=0; i<3; i++) {
|
|
|
|
string c;
|
|
|
|
if(whattodraw[i] == -1) c = "u-matrix";
|
|
|
|
else if(whattodraw[i] == -2) c = "u-matrix reversed";
|
|
|
|
else if(whattodraw[i] == -3) c = "distance from marked ('m')";
|
|
|
|
else if(whattodraw[i] == -4) c = "number of samples";
|
|
|
|
else if(whattodraw[i] == -5) c = "best sample's color";
|
|
|
|
else if(whattodraw[i] == -6) c = "sample names to colors";
|
|
|
|
else c = colnames[whattodraw[i]];
|
|
|
|
dialog::addSelItem(XLAT("coloring (%1)", parts[i]), c, '1'+i);
|
2020-03-29 11:09:10 +00:00
|
|
|
dialog::add_action([i] { shift_color(i); });
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
dialog::addItem("coloring (all)", '0');
|
2020-03-29 11:09:10 +00:00
|
|
|
dialog::add_action([] {
|
|
|
|
shift_color(0); shift_color(1); shift_color(2);
|
|
|
|
});
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
dialog::addItem("level lines", '4');
|
2020-03-29 11:09:10 +00:00
|
|
|
dialog::add_action_push(levelline::show);
|
2021-03-30 22:42:51 +00:00
|
|
|
|
|
|
|
add_edit(precise_placement);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 13:15:00 +00:00
|
|
|
void save_compressed(string name) {
|
|
|
|
// save everything in compressed form
|
2018-11-24 16:01:49 +00:00
|
|
|
fhstream f(name, "wb");
|
|
|
|
if(!f.f) {
|
2018-09-07 13:15:00 +00:00
|
|
|
printf("failed to open for save_compressed: %s\n", name.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// save columns
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write(columns);
|
|
|
|
for(int i=0; i<columns; i++) f.write(colnames[i]);
|
|
|
|
for(int i=0; i<columns; i++) hwrite_raw<float>(f, weights[i]);
|
2018-09-07 13:15:00 +00:00
|
|
|
// save neurons
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write<int>(isize(net));
|
|
|
|
for(int i=0; i<isize(net); i++)
|
|
|
|
for(int j=0; j<columns; j++) hwrite_raw<float>(f, net[i].net[j]);
|
2018-09-07 13:15:00 +00:00
|
|
|
// save shown samples
|
|
|
|
map<int, int> saved_id;
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write<int>(isize(sample_vdata_id));
|
2018-09-07 13:15:00 +00:00
|
|
|
int index = 0;
|
|
|
|
for(auto p: sample_vdata_id) {
|
|
|
|
int i = p.first;
|
2018-11-24 16:01:49 +00:00
|
|
|
for(int j=0; j<columns; j++) hwrite_raw<float>(f, data[i].val[j]);
|
|
|
|
f.write(data[i].name);
|
2018-09-07 13:15:00 +00:00
|
|
|
int id = p.second;
|
|
|
|
saved_id[id] = index++;
|
|
|
|
auto& vd = vdata[id];
|
2019-10-06 22:10:25 +00:00
|
|
|
struct colorpair_old { color_t color1, color2; char shade; } cpo;
|
|
|
|
cpo.color1 = vd.cp.color1;
|
|
|
|
cpo.color2 = vd.cp.color2;
|
|
|
|
cpo.shade = vd.cp.shade;
|
|
|
|
hwrite_raw(f, cpo);
|
2018-09-07 13:15:00 +00:00
|
|
|
}
|
|
|
|
// save edge types
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write<int>(isize(edgetypes));
|
2018-09-07 13:15:00 +00:00
|
|
|
for(auto&et: edgetypes) {
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write(et->name);
|
|
|
|
hwrite_raw<float>(f, et->visible_from);
|
|
|
|
f.write(et->color);
|
2018-09-07 13:15:00 +00:00
|
|
|
}
|
|
|
|
// save edge infos
|
2018-11-24 16:01:49 +00:00
|
|
|
f.write<int>(isize(edgeinfos));
|
2018-09-07 13:15:00 +00:00
|
|
|
for(auto& ei: edgeinfos) {
|
|
|
|
for(int x=0; x<isize(edgetypes); x++)
|
2018-11-24 16:01:49 +00:00
|
|
|
if(ei->type == &*edgetypes[x]) f.write_char(x);
|
|
|
|
f.write(saved_id[ei->i]);
|
|
|
|
f.write(saved_id[ei->j]);
|
|
|
|
hwrite_raw<float>(f, ei->weight);
|
2018-09-07 13:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_compressed(string name) {
|
|
|
|
// save everything in compressed form
|
2018-11-24 16:01:49 +00:00
|
|
|
fhstream f(name, "rb");
|
|
|
|
if(!f.f) {
|
2018-09-07 13:15:00 +00:00
|
|
|
printf("failed to open for load_compressed: %s\n", name.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// load columns
|
2018-11-24 16:01:49 +00:00
|
|
|
f.read(columns);
|
2018-09-07 13:15:00 +00:00
|
|
|
colnames.resize(columns);
|
2018-11-24 16:01:49 +00:00
|
|
|
for(int i=0; i<columns; i++) f.read(colnames[i]);
|
2018-09-07 13:15:00 +00:00
|
|
|
alloc(weights);
|
2018-11-24 16:01:49 +00:00
|
|
|
for(int i=0; i<columns; i++) weights[i] = f.get_raw<float>();
|
2021-03-30 22:41:13 +00:00
|
|
|
samples = 0;
|
|
|
|
initialize_neurons_initial();
|
2018-09-07 13:15:00 +00:00
|
|
|
// load neurons
|
2018-11-24 16:01:49 +00:00
|
|
|
int N = f.get<int>();
|
2018-09-07 13:15:00 +00:00
|
|
|
if(cells != N) {
|
|
|
|
fprintf(stderr, "Error: bad number of cells (N=%d c=%d)\n", N, cells);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for(neuron& n: net)
|
|
|
|
for(int k=0; k<columns; k++)
|
2018-11-24 16:01:49 +00:00
|
|
|
n.net[k] = f.get_raw<float>();
|
2018-09-07 13:15:00 +00:00
|
|
|
// load data
|
2018-11-24 16:01:49 +00:00
|
|
|
samples = f.get<int>();
|
2018-09-07 13:15:00 +00:00
|
|
|
data.resize(samples);
|
|
|
|
int id = 0;
|
|
|
|
for(auto& d: data) {
|
|
|
|
alloc(d.val);
|
2018-11-24 16:01:49 +00:00
|
|
|
for(int j=0; j<columns; j++)
|
|
|
|
d.val[j] = f.get_raw<float>();
|
|
|
|
f.read(d.name);
|
2018-09-07 13:15:00 +00:00
|
|
|
int i = vdata.size();
|
|
|
|
sample_vdata_id[id] = i;
|
|
|
|
vdata.emplace_back();
|
|
|
|
auto& v = vdata.back();
|
|
|
|
v.name = data[i].name;
|
2019-10-06 22:10:25 +00:00
|
|
|
struct colorpair_old { color_t color1, color2; char shade; } cpo;
|
|
|
|
hread_raw(f, cpo);
|
|
|
|
v.cp.color1 = cpo.color1;
|
|
|
|
v.cp.color2 = cpo.color2;
|
|
|
|
v.cp.shade = cpo.shade;
|
2018-09-07 13:15:00 +00:00
|
|
|
createViz(i, cwt.at, Id);
|
|
|
|
v.m->store();
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
// load edge types
|
2018-11-24 16:01:49 +00:00
|
|
|
int qet = f.get<int>();
|
2018-09-07 13:15:00 +00:00
|
|
|
for(int i=0; i<qet; i++) {
|
2018-11-24 16:01:49 +00:00
|
|
|
auto et = add_edgetype(f.get<string>());
|
|
|
|
et->visible_from = f.get_raw<float>();
|
|
|
|
f.read(et->color);
|
2018-09-07 13:15:00 +00:00
|
|
|
}
|
|
|
|
// load edge infos
|
2018-11-24 16:01:49 +00:00
|
|
|
int qei = f.get<int>();
|
2018-09-07 13:15:00 +00:00
|
|
|
for(int i=0; i<qei; i++) {
|
2018-11-24 16:01:49 +00:00
|
|
|
auto t = edgetypes[f.read_char()];
|
|
|
|
int ei = f.get<int>();
|
|
|
|
int ej = f.get<int>();
|
|
|
|
float w = f.get_raw<float>();
|
2018-09-07 13:15:00 +00:00
|
|
|
addedge(ei, ej, w, true, &*t);
|
|
|
|
}
|
|
|
|
analyze();
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:27:16 +00:00
|
|
|
#if CAP_COMMANDLINE
|
2017-09-01 20:14:02 +00:00
|
|
|
int readArgs() {
|
|
|
|
using namespace arg;
|
|
|
|
|
|
|
|
// #1: load the samples
|
|
|
|
|
|
|
|
if(argis("-som")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::loadsamples(args());
|
|
|
|
}
|
|
|
|
|
|
|
|
// #2: set parameters
|
|
|
|
|
2019-04-03 18:35:12 +00:00
|
|
|
else if(argis("-somskrad")) {
|
|
|
|
shift(); krad = argi();
|
2021-03-30 22:41:13 +00:00
|
|
|
state &=~ (KS_NEURONS | KS_NEURONS_INI | KS_DISPERSION);
|
|
|
|
}
|
|
|
|
else if(argis("-somskqty")) {
|
|
|
|
shift(); kqty = argi();
|
|
|
|
state &=~ (KS_NEURONS | KS_NEURONS_INI | KS_DISPERSION);
|
2019-04-03 18:35:12 +00:00
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
else if(argis("-somsim")) {
|
2021-03-30 22:41:13 +00:00
|
|
|
gaussian = 0;
|
|
|
|
state &=~ KS_DISPERSION;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
2022-03-01 07:42:20 +00:00
|
|
|
else if(argis("-somcgauss") || argis("-cgauss")) {
|
2021-03-30 22:41:13 +00:00
|
|
|
gaussian = 1;
|
|
|
|
state &=~ KS_DISPERSION;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(argis("-somggauss")) {
|
2021-03-30 22:41:13 +00:00
|
|
|
gaussian = 2;
|
|
|
|
state &=~ KS_DISPERSION;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(argis("-sompct")) {
|
|
|
|
shift(); qpct = argi();
|
|
|
|
}
|
|
|
|
else if(argis("-sompower")) {
|
2018-11-09 13:26:31 +00:00
|
|
|
shift_arg_formula(ttpower);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(argis("-somparam")) {
|
2018-11-09 13:26:31 +00:00
|
|
|
shift_arg_formula((gaussian ? distmul : dispersion_end_at));
|
2017-09-01 20:14:02 +00:00
|
|
|
if(dispersion_end_at <= 1) {
|
|
|
|
fprintf(stderr, "Dispersion parameter illegal\n");
|
|
|
|
dispersion_end_at = 1.5;
|
|
|
|
}
|
2021-03-30 22:41:13 +00:00
|
|
|
state &=~ KS_DISPERSION;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(argis("-sominitdiv")) {
|
2021-03-30 22:41:13 +00:00
|
|
|
shift(); initdiv = argi();
|
|
|
|
state &=~ KS_NEURONS_INI;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
else if(argis("-somtmax")) {
|
|
|
|
shift(); t = (t*1./tmax) * argi();
|
|
|
|
tmax = argi();
|
|
|
|
}
|
2022-03-01 07:42:20 +00:00
|
|
|
else if(argis("-somlong")) {
|
|
|
|
shift(); dispersion_long = argi();
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
else if(argis("-somlearn")) {
|
|
|
|
// this one can be changed at any moment
|
2018-11-09 13:26:31 +00:00
|
|
|
shift_arg_formula(learning_factor);
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 07:42:20 +00:00
|
|
|
else if(argis("-som-analyze")) {
|
|
|
|
analyze();
|
|
|
|
}
|
|
|
|
|
2017-09-01 20:14:02 +00:00
|
|
|
else if(argis("-somrun")) {
|
2021-03-30 22:41:13 +00:00
|
|
|
initialize_rv();
|
|
|
|
set_neuron_initial();
|
|
|
|
t = tmax;
|
2017-09-01 20:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// #3: load the neuron data (usually without #2)
|
|
|
|
else if(argis("-somload")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::kload(args());
|
|
|
|
}
|
|
|
|
|
|
|
|
// #4: run, stop etc.
|
|
|
|
else if(argis("-somrunto")) {
|
|
|
|
int i = argi();
|
|
|
|
shift(); while(t > i) {
|
|
|
|
if(t % 128 == 0) progress("Steps left: " + its(t));
|
|
|
|
kohonen::step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(argis("-somstop")) {
|
|
|
|
t = 0;
|
|
|
|
}
|
|
|
|
else if(argis("-somnoshow")) {
|
|
|
|
noshow = true;
|
|
|
|
}
|
|
|
|
else if(argis("-somfinish")) {
|
|
|
|
while(!finished()) {
|
|
|
|
kohonen::step();
|
|
|
|
if(t % 128 == 0) progress("Steps left: " + its(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// #5 save data, classify etc.
|
|
|
|
else if(argis("-somsave")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::ksave(args());
|
|
|
|
}
|
|
|
|
else if(argis("-somsavew")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::ksavew(args());
|
|
|
|
}
|
|
|
|
else if(argis("-somloadw")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::kloadw(args());
|
|
|
|
}
|
2018-07-04 12:37:33 +00:00
|
|
|
else if(argis("-somclassify0")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::do_classify();
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
else if(argis("-somclassify")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::kclassify(args());
|
|
|
|
}
|
2018-07-04 12:37:33 +00:00
|
|
|
else if(argis("-somclassify-sr")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::kclassify_save_raw(args());
|
|
|
|
}
|
|
|
|
else if(argis("-somclassify-lr")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::kclassify_load_raw(args());
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
else if(argis("-somlistshown")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::klistsamples(args(), false, false);
|
|
|
|
}
|
|
|
|
else if(argis("-somlistbest")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::klistsamples(args(), true, false);
|
|
|
|
}
|
|
|
|
else if(argis("-somlistbestc")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::klistsamples(args(), true, true);
|
|
|
|
}
|
|
|
|
else if(argis("-somndist")) {
|
|
|
|
PHASE(3);
|
|
|
|
shift(); kohonen::neurondisttable(args());
|
|
|
|
}
|
|
|
|
else if(argis("-somshowbest")) {
|
|
|
|
showbestsamples();
|
|
|
|
}
|
2018-06-22 12:26:24 +00:00
|
|
|
else if(argis("-somverify")) {
|
|
|
|
start_game();
|
|
|
|
verify_crawlers();
|
|
|
|
}
|
2018-06-28 08:27:35 +00:00
|
|
|
else if(argis("-somrestrict")) {
|
|
|
|
shift(); kohrestrict = argi();
|
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-maxgroup")) {
|
2018-07-04 12:37:33 +00:00
|
|
|
shift(); max_group = argi();
|
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-mingroup")) {
|
2018-07-05 05:30:15 +00:00
|
|
|
shift(); min_group = argi();
|
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-fillgroups")) {
|
2018-07-05 05:30:15 +00:00
|
|
|
fillgroups();
|
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-load-edges")) {
|
2018-07-09 16:17:40 +00:00
|
|
|
shift(); string edgename = args();
|
|
|
|
shift(); kohonen::load_edges(args(), edgename, 0);
|
2018-07-04 12:37:33 +00:00
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-random-edges")) {
|
|
|
|
shift();
|
|
|
|
random_edges(argi());
|
|
|
|
}
|
2021-03-30 22:51:16 +00:00
|
|
|
else if(argis("-som-wtd")) {
|
|
|
|
for(int i=0; i<3; i++) {
|
|
|
|
shift();
|
|
|
|
whattodraw[i] = argi();
|
|
|
|
}
|
|
|
|
coloring();
|
|
|
|
}
|
2018-07-05 07:56:09 +00:00
|
|
|
else if(argis("-som-load-n-edges")) {
|
2018-07-09 16:17:40 +00:00
|
|
|
shift(); string edgename = args();
|
2018-07-04 12:37:33 +00:00
|
|
|
shift(); int n = argi();
|
2018-07-09 16:17:40 +00:00
|
|
|
shift(); kohonen::load_edges(args(), edgename, n);
|
|
|
|
}
|
|
|
|
else if(argis("-less-edges")) {
|
|
|
|
shift(); double d = argf();
|
|
|
|
for(auto t: edgetypes) t->visible_from *= d;
|
2018-07-04 12:37:33 +00:00
|
|
|
}
|
2018-09-07 13:15:00 +00:00
|
|
|
else if(argis("-som-save-compressed")) {
|
|
|
|
shift();
|
|
|
|
save_compressed(args());
|
|
|
|
}
|
|
|
|
else if(argis("-som-load-compressed")) {
|
|
|
|
shift();
|
|
|
|
load_compressed(args());
|
|
|
|
}
|
2017-09-01 20:14:02 +00:00
|
|
|
|
|
|
|
else return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-01 23:27:16 +00:00
|
|
|
auto hooks = addHook(hooks_args, 100, readArgs);
|
|
|
|
#endif
|
|
|
|
|
2020-03-29 11:09:10 +00:00
|
|
|
bool turn(int delta) {
|
2021-03-30 22:58:56 +00:00
|
|
|
kohonen::steps(), timetowait = 0;
|
2020-03-29 11:09:10 +00:00
|
|
|
return false;
|
|
|
|
// shmup::pc[0]->rebase();
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:37:04 +00:00
|
|
|
bool kohonen_color(int& c2, string& lab, FILE *f) {
|
|
|
|
if(c2 == '+') {
|
|
|
|
int known_id = kohonen::showsample(lab);
|
|
|
|
c2 = fgetc(f);
|
|
|
|
if(c2 == '@') {
|
|
|
|
legend.push_back(known_id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:56:27 +00:00
|
|
|
void clear() {
|
2021-03-30 22:41:13 +00:00
|
|
|
if(data.empty()) return;
|
2018-07-09 16:56:27 +00:00
|
|
|
printf("clearing Kohonen...\n");
|
|
|
|
sample_vdata_id.clear();
|
|
|
|
colnames.clear();
|
|
|
|
weights.clear();
|
|
|
|
net.clear();
|
|
|
|
whowon.clear();
|
|
|
|
scc.clear();
|
|
|
|
bdiffs.clear();
|
|
|
|
bids.clear();
|
|
|
|
bdiffn.clear();
|
2021-03-30 22:41:13 +00:00
|
|
|
state = 0;
|
2018-07-09 16:56:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 22:41:13 +00:00
|
|
|
auto hooks4 = addHook(hooks_clearmemory, 100, clear)
|
|
|
|
+ addHook(hooks_configfile, 100, [] {
|
|
|
|
param_f(precise_placement, "koh_placement")
|
|
|
|
-> editable(0, 2, .2, "precise placement", "0 = make all visible, 1 = place ideally, n = place 1/n of the distance from center to ideal placement", 'p')
|
|
|
|
-> set_reaction([] { if((state & KS_NEURONS) && (state & KS_SAMPLES)) distribute_neurons(); });
|
2021-03-30 22:46:37 +00:00
|
|
|
param_b(show_rings, "som_show_rings");
|
2021-03-30 22:47:25 +00:00
|
|
|
param_b(animate, "animate");
|
2021-03-30 22:52:07 +00:00
|
|
|
param_f(dispersion_precision, "som_dispersion")
|
|
|
|
-> set_reaction([] { state &=~ KS_DISPERSION; });
|
2021-03-30 22:41:13 +00:00
|
|
|
});
|
2017-09-01 20:14:02 +00:00
|
|
|
|
2021-03-30 22:58:56 +00:00
|
|
|
bool mark(cell *c) {
|
|
|
|
initialize_neurons();
|
|
|
|
distfrom = getNeuronSlow(c);
|
|
|
|
coloring();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void initialize_rv() {
|
|
|
|
if(state & KS_ROGUEVIZ) return;
|
2021-03-30 23:10:45 +00:00
|
|
|
init(RV_GRAPH | RV_HAVE_WEIGHT);
|
2021-03-30 22:58:56 +00:00
|
|
|
state |= KS_ROGUEVIZ;
|
|
|
|
|
|
|
|
rv_hook(hooks_frame, 50, levelline::draw);
|
|
|
|
rv_hook(hooks_mouseover, 100, describe_cell);
|
|
|
|
rv_hook(shmup::hooks_turn, 100, turn);
|
|
|
|
rv_hook(rogueviz::hooks_rvmenu, 100, showMenu);
|
|
|
|
rv_hook(hooks_readcolor, 100, kohonen_color);
|
2021-03-31 10:25:25 +00:00
|
|
|
rv_hook(hooks_drawcell, 100, coloring_3d);
|
2021-03-30 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|