-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsexp.cpp
More file actions
545 lines (441 loc) · 9.81 KB
/
sexp.cpp
File metadata and controls
545 lines (441 loc) · 9.81 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
////////////////////////////////////////////////////////////
// sexp.c -- class implementation for S-Expression in C++
//
// written by Larry I. Gritz, 1993
// The George Washington University, Dept. of EE&CS
// Computer Graphics and Animation Lab
////////////////////////////////////////////////////////////
#include <iostream>
#include <malloc.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "gp.h"
#include "random.h"
using namespace std;
static S_Expression *free_list = NULL;
// Constructor
S_Expression::S_Expression (void)
{
type = STnone;
val = 0;
which = 0;
for (int i = 0; i < MAX_SEXP_ARGS; ++i)
args[i] = NULL;
}
void* S_Expression::operator new (size_t size)
{
static long retrieved = 0;
static long numalloced = 0;
S_Expression *s;
if (free_list)
{
s = free_list;
free_list = s->args[0];
++retrieved;
}
else
{
s = (S_Expression *) malloc (sizeof (S_Expression));
++numalloced;
if (! (numalloced % 100000))
cout << "\nAllocated " << numalloced << " sexp cells (" << retrieved << ")\n";
}
return s;
}
void S_Expression::operator delete (void *s)
{
S_Expression *se = (S_Expression *) s;
for (int i = 0; i < MAX_SEXP_ARGS; ++i)
if (se->args[i])
delete se->args[i];
((S_Expression *)s)->args[0] = free_list;
free_list = (S_Expression *)s;
}
// Execute an encapsulated program, specified by index.
// This takes the recursion out of S_Expression::eval(),
// so that we can inline it.
float run_encapsulated_program(int ind)
{
S_Expression *s = Fset.lookup_encapsulation (ind);
return s->eval();
}
// Perform editing operation on this S_Expression,
// then return new expr
S_Expression *edit (S_Expression *s)
{
if (s && (s->type == STfunction))
{
for (int i = 0; i < Fset.nargs (s->which); ++i)
s->args[i] = edit (s->args[i]);
editfunc e = Fset.editop (s->which);
return e ? (*e)(s) : s;
}
return s;
}
// Perform permutation on a random node in this S-expression
void S_Expression::permute (void)
{
cout << "Permuting:\nold = " << this << "\nnew = ";
cout.flush();
if (type == STfunction && Fset.nargs(which) > 1)
{
for (int i = Fset.nargs(which)-1; i; --i)
{
float u = random();
int k = (int) floor (i * u);
S_Expression *temp = args[k];
args[k] = args[i];
args[i] = temp;
}
}
cout << this << "\n";
cout.flush();
}
// Return a duplicate copy of the given S-Expression
S_Expression * S_Expression::copy (void)
{
S_Expression *se = new S_Expression;
se->type = type;
se->val = val;
se->which = which;
for (int i = 0; i < MAX_SEXP_ARGS; ++i)
if (args[i])
se->args[i] = args[i]->copy();
else se->args[i] = NULL;
return se;
}
// Test for equivalence, return 1 if S-expressions are same
int equiv (S_Expression *s1, S_Expression *s2)
{
if (! s1 && ! s2)
return 1;
if (!s1 || !s2)
return 0;
if (s1->type != s2->type)
return 0;
switch (s1->type)
{
case STnone:
cout << "Error: bad case in S_Expression::==\n";
return 0;
case STconstant:
return (s1->val == s2->val);
case STterminal:
return (s1->which == s2->which);
case STfunction:
if (s1->which != s2->which)
return 0;
for (int i = 0; i < MAX_SEXP_ARGS; ++i)
if (!equiv (s1->args[i], s2->args[i]))
return 0;
return 1;
}
}
// If the S_Expression is a numerical constant, put it in f.
int S_Expression::is_numerical (float& f)
{
if (type == STconstant)
{
f = val;
return 1;
}
else if (type == STterminal)
{
char *name = Tset.getname (which);
if ((name[0] >= '0' && name[0] <= '9') || name[0] == '-')
{
f = Tset.lookup (which);
return 1;
}
}
return 0;
}
// Characterize the tree, returning its depth, the total
// number of nodesin the tree, the total number of internal
// nodes (those with children), and the total number of
// external nodes (those without children).
void S_Expression::characterize (int *depth, int *totalnodes, int *internal, int *external)
{
*depth = 1; *totalnodes = 1;
if (type == STfunction && Fset.nargs(which) >= 1)
{
int child_depth, child_total, child_internal;
int child_external;
*internal = 1;
*external = 0;
int nargs = Fset.nargs (which);
int max_child_depth = 0;
for (int i = 0; i < nargs; ++i)
{
args[i]->characterize (&child_depth, &child_total, &child_internal, &child_external);
*totalnodes += child_total;
*internal += child_internal;
*external += child_external;
if (child_depth > max_child_depth)
max_child_depth = child_depth;
}
*depth += max_child_depth;
}
else
{
*internal = 0;
*external = 1;
}
}
// Return if the tree contains functions with side effects
int S_Expression::side_effects (void)
{
if (type == STfunction)
{
if (Fset.has_sideeffects (which))
return 1;
for (int i = 0; i < Fset.nargs (which); ++i)
if (args[i]->side_effects())
return 1;
}
return 0;
}
S_Expression * S_Expression::selectany (int m, int *n, S_Expression ***ptr)
{
(*n)++;
if (*n == m)
return (this);
if (type == STfunction)
{
int nargs = Fset.nargs (which);
for (int i = 0; i < nargs; ++i)
{
S_Expression *s = args[i]->selectany (m, n, ptr);
if (s)
{
if (args[i] == s)
*ptr = &(args[i]);
return s;
}
}
}
return NULL;
}
S_Expression* S_Expression::selectexternal (int m, int *n, S_Expression ***ptr)
{
if (type == STfunction && Fset.nargs(which) >= 1)
{
int nargs = Fset.nargs (which);
for (int i = 0; i < nargs; ++i)
{
S_Expression *s=args[i]->selectexternal (m,n,ptr);
if (s)
{
if (args[i] == s)
*ptr = &(args[i]);
return s;
}
}
return NULL;
}
else
{
(*n)++;
if (*n == m)
return (this);
else
return NULL;
}
}
S_Expression* S_Expression::selectinternal (int m, int *n, S_Expression ***ptr)
{
if (type == STfunction && Fset.nargs(which) >= 1)
{
(*n)++;
if (*n == m)
return (this);
int nargs = Fset.nargs (which);
for (int i = 0; i < nargs; ++i)
{
S_Expression *s=args[i]->selectinternal (m,n,ptr);
if (s)
{
if (args[i] == s)
*ptr = &(args[i]);
return s;
}
}
}
return NULL;
}
S_Expression * S_Expression::select (float pip, S_Expression ***ptr)
{
int depth, total, internal, external;
int n = -1;
int which;
*ptr = NULL;
characterize (&depth, &total, &internal, &external);
if (random() < pip && internal >= 1)
{
which = (int) floor (random() * internal);
return selectinternal (which, &n, ptr);
}
else if (external >= 1)
{
which = (int) floor (random() * external);
return selectexternal (which, &n, ptr);
}
}
// Perform the crossover between these two S-Expressions
void crossover(S_Expression **s1, S_Expression **s2, float pip)
{
S_Expression **parent1ptr = NULL, **parent2ptr = NULL;
S_Expression *fragment1, *fragment2;
//char buffer[1024];
fragment1 = (*s1)->select (pip, &parent1ptr);
if (! parent1ptr)
parent1ptr = s1;
fragment2 = (*s2)->select (pip, &parent2ptr);
if (! parent2ptr)
parent2ptr = s2;
cout.flush();
*parent1ptr = fragment2;
*parent2ptr = fragment1;
}
// Choose a random terminal (possibly including the
// ephemeral constant
static void random_terminal (S_Expression *s)
{
int i = Tset.n + (ephemeral_constant ? 1 : 0);
s->which = (int) floor (i * random());
if (s->which >= Tset.n)
{
s->type = STconstant;
s->val = ephemeral_constant();
}
else
s->type = STterminal;
}
// Choose a random function
static void random_function (S_Expression *s)
{
s->type = STfunction;
s->which = (int) floor (Fset.n * random());
}
// Choose a random terminal or function
static void random_terminal_or_function (S_Expression *s)
{
int i = Tset.n + Fset.n + (ephemeral_constant ? 1 : 0);
s->which = (int) floor (i * random());
if (s->which < Fset.n)
s->type = STfunction;
else
{
s->which -= Fset.n;
if (s->which >= Tset.n)
{
s->type = STconstant;
s->val = ephemeral_constant();
}
else s->type = STterminal;
}
}
// Create a random S-Expression
S_Expression *random_sexpression(GenerativeMethod strategy, int maxdepth, int depth)
{
S_Expression *s = new S_Expression;
if(!depth)
{
maxdepth = 2 + (int) floor ((maxdepth - 1) * random());
if (strategy == RAMPED_HALF_AND_HALF)
{
if (random() < 0.5)
strategy = GROW;
else
strategy = FULL;
}
}
++depth;
switch (strategy)
{
case GROW :
if (depth == maxdepth)
random_terminal (s);
else
random_terminal_or_function (s);
break;
case FULL :
if (depth == maxdepth)
random_terminal (s);
else
random_function (s);
break;
case RAMPED_HALF_AND_HALF :
cout << "random_sexpression: bad case\n";
break;
}
if (s->type == STfunction)
for (int i = 0; i < Fset.nargs(s->which); ++i)
s->args[i] = random_sexpression (strategy, maxdepth, depth);
return s;
}
// Chop off everything below a given depth
//
void S_Expression::restrict_depth (int maxdepth, int depth)
{
++depth;
if (type == STfunction)
{
if (depth == maxdepth-1)
{
for (int i = 0; i < Fset.nargs(which); ++i)
if (args[i]->type == STfunction)
{
delete args[i];
args[i]=random_sexpression(GROW,maxdepth-depth);
}
}
}
else
{
for (int i = 0; i < Fset.nargs(which); ++i)
args[i]->restrict_depth (maxdepth, depth);
}
}
// Put an ASCII representation of the S-expression into the
// given string
void S_Expression::write(string* s, int level)
{
string buffer;
//if(!level)
//s[0] = 0;
switch (type)
{
case STnone :
cout << "Error: bad case in S_Expression::write\n";
break;
case STconstant:
buffer += "%g";
buffer += val;
*s += buffer;
break;
case STterminal:
*s += Tset.getname(which);
break;
case STfunction:
*s += "(";
*s += Fset.getname(which);
for (int i = 0; i < Fset.nargs(which); ++i)
{
*s += " ";
args[i]->write(s, level+1);
}
*s += ")";
break;
}
}
// Stream output of S-expressions
ostream& operator<< (ostream& out, S_Expression *s)
{
string buffer;
s->write(&buffer);
return (out << buffer);
}
EPHEMERAL ephemeral_constant = NULL;