ref: aae2af6ab3d4c6433ee7713994e846621f6781ab
parent: eca529658c0b091fd99ee770b6c2da21bcfcc596
author: qwx <qwx@sciops.net>
date: Tue Sep 13 22:27:19 EDT 2022
path: visualize loaded benchmark maps
--- a/app/path/client.c
+++ b/app/path/client.c
@@ -16,6 +16,14 @@
static Keyboardctl *kc;
static Mousectl *mc;
+int
+menter(char *label, char *buf, int bufsz)
+{
+ if(enter(label, buf, bufsz, mc, kc, nil) < 0)
+ return -1;
+ return 0;
+}
+
void
evloop(void)
{
@@ -52,7 +60,7 @@
}
void
-init(char *scen, Vertex v, int m, int a, int d)
+init(char *scen, char *res, Vertex v, int m, int a, int d)
{
fmtinstall('P', Pfmt);
fmtinstall('R', Rfmt);
@@ -59,10 +67,8 @@
fmtinstall('V', Vfmt);
fmtinstall('N', Nfmt);
initfs();
- if(initmap(scen, v, m, a, d) < 0)
+ if(initmap(scen, res, v, m, a, d) < 0)
sysfatal("init: %r");
- if(doprof)
- return;
initdrw();
if((kc = initkeyboard(nil)) == nil)
sysfatal("initkeyboard: %r");
--- a/app/path/dat.h
+++ b/app/path/dat.h
@@ -1,5 +1,5 @@
enum{
- Nodesz = 4,
+ Nodesz = 2,
};
extern Node *selected;
extern Node *start, *goal;
@@ -10,3 +10,5 @@
Pa∗,
};
extern int (*pathfn)(Node*, Node*);
+
+extern int nscen, scenid;
--- a/app/path/drw.c
+++ b/app/path/drw.c
@@ -117,7 +117,8 @@
continue;
p1 = addpt(n2s(n), Pt(Nodesz / 2, Nodesz / 2));
p0 = addpt(n2s(n->from), Pt(Nodesz / 2, Nodesz / 2));
- line(view, p0, p1, Endarrow, 0, 0, col[Cgrid], ZP);
+ //line(view, p0, p1, Endarrow, 0, 0, col[Cgrid], ZP);
+ line(view, p0, p1, 0, 0, 0, col[Cgrid], ZP);
}
}
--- a/app/path/fns.h
+++ b/app/path/fns.h
@@ -1,13 +1,16 @@
-void init(char*, Vertex, int, int, int);
+void init(char*, char*, Vertex, int, int, int);
Node* scrselect(Point);
void updatedrw(void);
+int menter(char*, char*, int);
void evloop(void);
+void showscen(int);
+void reloadscen(void);
void runscens(void);
-int readscen(char*, Vertex*, int*, int*, int*);
+int readscen(char*, char*, Vertex*, int*, int*, int*);
void initfs(void);
int Vfmt(Fmt*);
int Nfmt(Fmt*);
-int initmap(char*, Vertex, int, int, int);
+int initmap(char*, char*, Vertex, int, int, int);
void initdrw(void);
void resetdrw(void);
Vertex n2s(Node*);
--- a/app/path/fs.c
+++ b/app/path/fs.c
@@ -6,6 +6,8 @@
#include "dat.h"
#include "fns.h"
+int nscen, scenid;
+
/* https://bitbucket.org/dharabor/pathfinding/src/gppc/gppc-2014/scenarios/ */
typedef struct Sim Sim;
struct Sim{
@@ -12,11 +14,55 @@
Prof;
Vertex start;
Vertex goal;
- double dist;
};
static VArray *sims;
+static char *scenmap;
void
+showscen(int id)
+{
+ Sim *sp;
+
+ assert(id >= 0 && id < nscen);
+ sp = sims->p;
+ sp += id;
+ start = p2n(sp->start);
+ goal = p2n(sp->goal);
+ if(pathfn(start, goal) < 0)
+ fprint(2, "runscens: findpath from %N to %N: %r\n",
+ start, goal);
+}
+
+static void
+readresults(char *path)
+{
+ int n;
+ char *s, *arg[32];
+ Biobuf *bf;
+ Sim *sp, *se;
+
+ if((bf = Bopen(path, OREAD)) == nil)
+ sysfatal("readscen: %r");
+ sp = sims->p;
+ se = sp + sims->n;
+ while(sp < se){
+ if((s = Brdstr(bf, '\n', 1)) == nil)
+ sysfatal("readresults: %r");
+ if((n = getfields(s, arg, nelem(arg), 1, " \t")) != 8)
+ sysfatal("invalid record length %d not 8", n);
+ sp->cost = strtod(arg[7], nil);
+ sp->steps = atoi(arg[2]);
+ sp->touched = atoi(arg[3]);
+ sp->opened = atoi(arg[4]);
+ sp->updated = atoi(arg[5]);
+ sp->closed = atoi(arg[6]);
+ free(s);
+ sp++;
+ }
+ Bterm(bf);
+}
+
+void
runscens(void)
{
Sim *sp, *se;
@@ -23,7 +69,7 @@
sp = sims->p;
se = sp + sims->n;
- fprint(2, "id\tsteps\ttouched\texpanded\tupdated\topened\n");
+ fprint(2, "id\tsteps\ttouched\texpanded\tupdated\topened\tcost\tdist\n");
while(sp < se){
start = p2n(sp->start);
goal = p2n(sp->goal);
@@ -31,10 +77,10 @@
fprint(2, "runscens: findpath from %N to %N: %r\n",
start, goal);
memcpy(sp, &stats, sizeof stats);
- fprint(2, "%zd\t%d\t%d\t%d\t%d\t%d\n",
+ fprint(2, "%zd\t%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\n",
sp - (Sim*)sims->p,
stats.steps, stats.touched, stats.opened,
- stats.updated, stats.closed);
+ stats.updated, stats.closed, stats.cost, stats.dist);
sp++;
}
}
@@ -48,7 +94,7 @@
done = 0;
while((s = Brdstr(bf, '\n', 1)) != nil){
t = strtok(s, " ");
- if(strcmp(t, "type") == 0){
+ if(strcmp(t, "type") == 0 || strcmp(t, "version") == 0){
;
}else if(strcmp(t, "height") == 0){
if((t = strtok(nil, " ")) == nil)
@@ -82,7 +128,9 @@
sysfatal("readscenmap: %r");
if(readscenmaphdr(bf, v) < 0)
return -1;
- initgrid(v->x, v->y);
+ if(gridwidth == 0)
+ initgrid(v->x, v->y);
+ cleargrid();
for(u.y=0; u.y<gridheight; u.y++){
if((s = Brdstr(bf, '\n', 1)) == nil)
return -1;
@@ -98,7 +146,7 @@
}
}
if(u.x != gridwidth){
- werrstr("line %d: invalid length %d not %d", u.y+1, u.x, gridwidth);
+ werrstr("line %d: invalid width %d not %d", u.y+1, u.x, gridwidth);
return -1;
}
free(s);
@@ -107,6 +155,15 @@
return 0;
}
+void
+reloadscen(void)
+{
+ Vertex v;
+
+ if(readscenmap(scenmap, &v) < 0)
+ sysfatal("reloadscen: %r");
+}
+
static int
readscenhdr(Biobuf *bf, Vertex *v)
{
@@ -140,7 +197,7 @@
}
int
-readscen(char *path, Vertex *v, int *m, int *a, int *d)
+readscen(char *path, char *respath, Vertex *v, int *m, int *a, int *d)
{
int n;
char *s, *arg[32];
@@ -147,15 +204,18 @@
Biobuf *bf;
Sim sim;
- /* only supported benchmarking configuration so far */
- *d = Doctile;
- *a = Pa∗;
- *m = Move8;
+ if(path == nil)
+ return 0;
+ doprof = 1;
+ /* only supported benchmarking configurations so far */
+ if(*d != Doctile || *a != Pa∗ && *a != Pdijkstra || *m != Move8)
+ sysfatal("unimplemented profiling for parameter set");
if((s = strrchr(path, '.')) == nil){
werrstr("invalid path name");
return -1;
}
*s = 0;
+ scenmap = estrdup(path);
if(readscenmap(path, v) < 0)
return -1;
*s = '.';
@@ -169,14 +229,21 @@
return -1;
}
sim.start.x = atoi(arg[4]);
- sim.start.y = atoi(arg[5]);
+ sim.start.y = atoi(arg[5]);
sim.goal.x = atoi(arg[6]);
sim.goal.y = atoi(arg[7]);
sim.dist = strtod(arg[8], nil);
vinsert(sims, (char*)&sim);
+ nscen++;
free(s);
}
Bterm(bf);
+ if(nscen != 4100)
+ sysfatal("scen file -- phase error");
+ if(respath != nil){
+ readresults(respath);
+ showscen(0);
+ }
return 0;
}
--- a/app/path/map.c
+++ b/app/path/map.c
@@ -52,23 +52,13 @@
cleargrid();
}
-static void
-defaults(Vertex v)
-{
- initgrid(v.x, v.y);
- setparm(Move8, Pa∗, Doctile);
-}
-
int
-initmap(char *scen, Vertex v, int m, int a, int d)
+initmap(char *scen, char *res, Vertex v, int m, int a, int d)
{
- if(scen == nil){
- defaults(v);
- return 0;
- }
- if(readscen(scen, &v, &m, &a, &d) < 0)
- sysfatal("readscen: %r");
- doprof = 1;
setparm(m, a, d);
+ if(scen == nil)
+ initgrid(v.x, v.y);
+ else if(readscen(scen, res, &v, &m, &a, &d) < 0)
+ sysfatal("readscen: %r");
return 0;
}
--- a/app/path/path.c
+++ b/app/path/path.c
@@ -53,12 +53,41 @@
}
static int
+setscen(void)
+{
+ int n;
+ char buf[128];
+
+ snprint(buf, sizeof buf, "%d", scenid);
+ if(menter("Scenario id?", buf, sizeof buf) < 0){
+ fprint(2, "getscen: %r\n");
+ return -1;
+ }
+ if((n = strtol(buf, nil, 10)) < 0 || n > nscen){
+ fprint(2, "getscen: invalid id %s\n", buf);
+ return -1;
+ }
+ scenid = n;
+ showscen(n);
+ return 0;
+}
+
+static int
grkey(Rune r)
{
switch(r){
case Kdel:
case 'q': threadexitsall(nil);
- case 'r': cleargrid(); updatedrw(); break;
+ case 'r':
+ if(doprof){
+ reloadscen();
+ showscen(scenid);
+ }else
+ cleargrid();
+ updatedrw();
+ break;
+ case ' ':
+ case '\n': if(setscen() >= 0) updatedrw(); break;
}
return 0;
}
@@ -74,7 +103,7 @@
threadmain(int argc, char **argv)
{
int w, h, a, d, m;
- char *s, *scen;
+ char *s, *scenres, *scenmap;
w = 64;
h = 64;
@@ -81,7 +110,8 @@
a = -1;
d = -1;
m = Move8;
- scen = nil;
+ scenmap = nil;
+ scenres = nil;
ARGBEGIN{
case 'D':
if(++debuglevel >= Logparanoid)
@@ -118,9 +148,12 @@
}
break;
case 'm':
- scen = EARGF(usage());
+ scenmap = EARGF(usage());
doprof = 1;
break;
+ case 'r':
+ scenres = EARGF(usage());
+ break;
case 's':
w = strtol(EARGF(usage()), &s, 0);
if(w <= 0)
@@ -144,10 +177,9 @@
a = Pa∗;
keyfn = grkey;
mousefn = grmouse;
- init(scen, (Vertex){w,h}, m, a, d);
- if(doprof)
+ init(scenmap, scenres, (Vertex){w,h}, m, a, d);
+ if(doprof && scenres == nil)
runscens();
- else
- evloop();
+ evloop();
threadexitsall(nil);
}