forked from mapio/gpsr
/
cli.c
363 lines (314 loc) · 9.3 KB
/
cli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
GPSR: Genetic Programming for Symbolic Regression
Copyright (C) 2012 Massimo Santini
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "genetic.h"
#include "utils.h"
#include "expr.h"
#include "stats.h"
#include "signal.h"
#include "gpplots.h"
#define CMDARGSIZE 255
#define HISTORYFILE ".graphics_history"
static char historyfile[CMDARGSIZE];
static gp_port port;
static void help(const char *line);
static void quit(const char *line);
static void sndcmd(const char *line);
static void ploti(const char *line);
static void plotr(const char *line);
static void pprinti(const char *line);
static void padjfi(const char *line);
static void pprintsi(const char *line);
static void plotd(const char *line);
static void plotma(const char *line);
static void plotva(const char *line);
static void plotava(const char *line);
static void parser(char *line);
static struct command {
char *cmd, *scmd, *help;
void (*function)(const char *);
} commands[] = {
{ "help", "h", "this help", help },
{ "quit", "q", "quit the program", quit },
{ "ploti", "pi", "plot individual", ploti },
{ "plotr", "pr", "plot residuals", plotr },
{ "printsi", "ps", "print simplyfied individual", pprintsi },
{ "printi", "pri", "print individual", pprinti },
{ "adjfi", "a", "print adjusted fitness of an individual", padjfi },
{ "plotd", "pd", "plot data", plotd },
{ "plotmaxadjf", "pm", "plot max adjusted fitness over runs", plotma },
{ "plotaveadjf", "pa", "plot adjusted fitness average over runs", plotva },
{ "plotvaraveadjf", "pva", "plot adjusted fitness average and variance over runs", plotava },
{ "sndcmd", "s", "send a command directly to gplot engine", sndcmd },
{ NULL, NULL, NULL, NULL }
};
static struct {
char current_cmd[CMDARGSIZE];
char structure_present;
char plot_initialized;
} parserstatus = { "", 0, 0 };
/* EXTERN FUNCS */
void p_error( const char *str )
{
fprintf( stderr, "error: %s: %s\n", parserstatus.current_cmd, str );
}
void p_perror( const char *str )
{
char buf[CMDARGSIZE];
sprintf( buf, "%s: %s", parserstatus.current_cmd, str );
perror( buf );
}
/* STATIC FUNCS */
static void help(const char *line)
{
int i;
char arg[CMDARGSIZE], buf[CMDARGSIZE];
if ( sscanf( line, "%*s %s", arg ) == 1 ) {
for ( i=0; commands[i].cmd; i++ )
if ( !strcmp( arg, commands[i].scmd ) || !strcmp( arg, commands[i].cmd ) ) {
printf( "%s (%s): %s\n", commands[i].cmd, commands[i].scmd, commands[i].help );
break; }
if ( !commands[i].cmd ) {
sprintf( buf, "there is no \"%s\" command", arg );
p_error(buf);
}
} else {
printf( "available commands:\n" );
for ( i=0; commands[i].cmd; i++ )
printf( "%s (%s): %s\n", commands[i].cmd, commands[i].scmd, commands[i].help );
}
}
static void quit(const char *line)
{
gp_close( port );
if ( write_history(historyfile) )
perror( "writing history" );
exit(EXIT_SUCCESS);
}
static void sndcmd(const char *line )
{
char *buf;
if ( (buf=strstr( line, " " )) ) {
gp_tell( port, "%s\n", buf+1 );
} else
p_error( "no command given" );
}
/* DOMAIN SPECIFIC */
static void ploti(const char *line )
{
int t, tt, g, n, h, S, E;
plotv *y, *x, *dx, *dy;
forecast r;
if ( (n=sscanf( line, "%*s %d %d %d", &g, &S, &E ))!=EOF ) {
if ( n == 1 ) {
S = 0;
E = datalen( DATA_TARGET );
} else {
S = max( 0, S );
E = min( E, datalen( DATA_TARGET ) );
}
if ( 0 < g && g <= status.curgen ) {
g--;
n = E - S;
x = ymalloc( status.length * n * sizeof(plotv) );
y = ymalloc( status.length * n * sizeof(plotv) );
dx = ymalloc( n * sizeof(plotv) );
dy = ymalloc( n * sizeof(plotv) );
tt = 0;
for ( t = S; t < E; t++ ) {
dy[t-S] = getdata( DATA_TARGET, t );
dx[t-S] = t;
r = evali( run[g].besti, t );
for ( h = 0; h < status.length; h++ ) {
x[tt] = t + h + status.horizon;
y[tt] = r.v[h];
tt++;
}
}
gp_plotarray( port, GP_PLOT, GP_TITLE "\"data\"" GP_DOTS, dx, dy, n );
gp_plotarray( port, GP_REPLOT, GP_TITLE "\"forecast\"" GP_DOTS, x, y, n * status.length );
yfree( x );
yfree( y );
yfree( dx );
yfree( dy );
} else
fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
} else
p_error( "no individual" );
}
static void plotr(const char *line )
{
int t, tt, g, n, h, S, E;
plotv *y, *x;
forecast r;
datav d;
if ( (n=sscanf( line, "%*s %d %d %d", &g, &S, &E ))!=EOF ) {
if ( n == 1 ) {
S = 0;
E = datalen( DATA_TARGET ) - status.length - status.horizon;
} else {
S = max( 0, S );
E = min( E, datalen( DATA_TARGET ) - status.length - status.horizon );
}
if ( 0 < g && g <= status.curgen ) {
g--;
n = E - S;
x = ymalloc( status.length * n * sizeof(plotv) );
y = ymalloc( status.length * n * sizeof(plotv) );
tt = 0;
for ( t = S; t < E; t++ ) {
r = evali( run[g].besti, t );
for ( h = 0; h < status.length; h++ ) {
d = getdata( DATA_TARGET, t + h + status.horizon );
x[tt] = t + h + status.horizon;
y[tt] = ( d - r.v[h] ) / d ;
tt++;
}
}
gp_plotarray( port, GP_PLOT, GP_TITLE "\"residual\"" GP_DOTS, x, y, n * status.length );
yfree( x );
yfree( y );
} else
fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
} else
p_error( "no individual" );
}
static void pprinti(const char *line )
{
int g;
if ( sscanf( line, "%*s %d", &g )!=EOF ) {
if ( 0 < g && g <= status.curgen ) {
printi( run[g-1].besti );
} else
fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
} else
p_error( "no individual" );
}
static void padjfi(const char *line )
{
int g;
if ( sscanf( line, "%*s %d", &g )!=EOF ) {
if ( 0 < g && g <= status.curgen ) {
printf( "adjf[i] = %e\n", adjfitnessi( run[g-1].besti ) );
} else
fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
} else
p_error( "no individual" );
}
static void pprintsi(const char *line )
{
int g;
if ( sscanf( line, "%*s %d", &g )!=EOF ) {
if ( 0 < g && g <= status.curgen ) {
#ifdef HAS_SIMPLIFY
printsi( run[g-1].besti );
#else
printi( run[g-1].besti );
#endif
} else
fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
} else
p_error( "no individual" );
}
static void plotd(const char *line )
{
gp_plotfile( port, GP_PLOT, GP_LINES, status.dataf[DATA_TARGET] );
}
static void plotma(const char *line )
{
int i;
plotv *y;
y = ymalloc( status.curgen * sizeof(plotv) );
for ( i = 0; i < status.curgen; i++ ) y[i] = run[i].maxadjf;
gp_plotarray( port, GP_PLOT, GP_TITLE "\"max adjf\"" GP_DOTS, NULL, y, status.curgen );
yfree( y );
}
static void plotva(const char *line )
{
int i;
plotv *y1, *y2;
y1 = ymalloc( status.curgen * sizeof(plotv) );
y2 = ymalloc( status.curgen * sizeof(plotv) );
for ( i = 0; i < status.curgen; i++ ) y1[i] = run[i].aveadjf;
gp_plotarray( port, GP_PLOT, GP_TITLE "\"average adjf\"" GP_DOTS, NULL, y1, status.curgen );
yfree( y1 );
}
static void plotava(const char *line )
{
int i;
plotv *y1, *y2;
y1 = ymalloc( status.curgen * sizeof(plotv) );
y2 = ymalloc( status.curgen * sizeof(plotv) );
for ( i = 0; i < status.curgen; i++ ) {
y1[i] = run[i].aveadjf;
y2[i] = run[i].varadjf;
}
gp_plotarray( port, GP_PLOT, GP_TITLE "\"average adjf\"" GP_DOTS, NULL, y1, status.curgen );
gp_plotarray( port, GP_REPLOT, GP_TITLE "\"variance adjf\"" GP_DOTS, NULL, y2, status.curgen );
yfree( y1 );
yfree( y2 );
}
/* PARSER */
static void parser( char *line )
{
char cmd[CMDARGSIZE];
int i;
if ( line != NULL ) {
sscanf( line, "%s", cmd );
for ( i=0; commands[i].cmd; i++ ) {
if ( !strcmp( cmd, commands[i].scmd ) || !strcmp( cmd, commands[i].cmd ) ) {
strcpy( parserstatus.current_cmd, commands[i].cmd );
commands[i].function( line );
break; }
}
if ( !commands[i].cmd && line[0] )
printf( "error: there is no \"%s\" command\n", cmd );
}
}
int main( int argc, char **argv )
{
char file[MAX_FILENAME_LEN + 1];
char *line, *prompt="> ";
int i;
if ( argc == 2 )
ystrncpy( file, argv[1], MAX_FILENAME_LEN );
else
yerror( "missing filename\n" );
restores( file );
prints();
restorer( file );
for( i = 0; i < status.datan; i++ )
readdata( status.dataf[i], i );
port = gp_open( "400x400" );
using_history();
stifle_history(1000);
sprintf( historyfile, "%s/%s", getenv("HOME"), HISTORYFILE );
if ( !access (historyfile, R_OK) )
if ( read_history(historyfile) )
perror( "reading history" );
while (1) {
line=readline( prompt );
if ( line ) {
add_history( line );
parser( line );
} else printf( "\n" );
}
return 0;
}