-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrushgp.rkt
1371 lines (1194 loc) · 57.1 KB
/
rushgp.rkt
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#|
rushgp.rkt
This file implements a version of the Push programming language (http://hampshire.edu/lspector/push.html)
and the PushGP genetic programming system in Typed Racket (http://racket-lang.org/).
LICENSE:
Copyright (c) 2011 Luke Vilnis
This work is based on (versions as of 10/2/2011):
Schush/SchushGP (http://hampshire.edu/lspector/schush.ss), copyright (c) 2009, 2010 Lee Spector
Clojush/ClojushGP (https://github.com/lspector/Clojush), copyright (c) 2010, 2011 Lee Spector
both distributed under version 3 of the GNU General Public License as published by the
Free Software Foundation, available from http://www.gnu.org/licenses/gpl.txt.
This program is free software: you can redistribute it and/or modify it under
the terms of version 3 of the GNU General Public License as published by the
Free Software Foundation, available from http://www.gnu.org/licenses/gpl.txt.
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 (http://www.gnu.org/licenses/)
for more details.
HOW TO USE:
Look at the example1, example2, or example3 functions at the end of the source for a demonstration
of how to use this implementation of PushGP.
HISTORY:
8/10/2011: Started work.
8/21/2011: First version released.
9/5/2011: Cleaned up some instruction definitions, added some annotations and helpers to get RushGP building in Racket 5.1.3.
9/6/2011: Made sure RushGP builds in both Racket 5.1.2 and 5.1.3 for comparison, small type-based optimizations.
10/2/2011: Added error-scaling, small cleanup/fixes.
|#
#lang typed/racket
;; Push types
(define-type
PushType
(U 'exec 'integer 'float 'code 'boolean 'auxiliary 'tag 'zip))
(define-type Instruction Symbol)
(define-type
Literal
(U Integer Float Boolean))
(define-type
Program
(Rec Prog (U Instruction Literal (Listof Prog))))
(define-type (Stack a) (Listof a))
(struct:
ProgramState
([Exec : (Stack Program)]
[Integer : (Stack Integer)]
[Float : (Stack Float)]
[Code : (Stack Program)]
[Boolean : (Stack Boolean)]
[Auxiliary : (Stack Program)]
[Tag : (Stack Program)]
[Zip : (Stack Program)]))
(struct:
Instruction-Definition
([Type : PushType]
[Name : Symbol]
[Definition : (ProgramState -> ProgramState)]))
(define-predicate PushType? PushType)
(define-predicate Literal? Literal)
(define-predicate Boolean? Boolean)
(define-predicate Float? Float)
(define-predicate Integer? Integer)
(define-predicate Program-Sequence? (Listof Program))
(define-predicate Program? Program)
(define-predicate Instruction? Instruction)
(define-predicate Exact-Positive-Integer? Exact-Positive-Integer)
;; pushgp parameters
(: max-number-magnitude Integer)
(define max-number-magnitude 1000000000000)
(: min-number-magnitude Float)
(define min-number-magnitude 1.0E-10)
(: top-level-push-code Boolean)
(define top-level-push-code #t)
(: top-level-pop-code Boolean)
(define top-level-pop-code #f)
(: evalpush-limit Integer)
(define evalpush-limit 150)
(: save-traces Boolean)
(define save-traces #t)
;; utilities
(define-syntax pλ:
(syntax-rules ()
((_ args ...)
(plambda: args ...))))
(: real->float ((U Real Inexact-Real) -> Float))
(define (real->float ir)
(match (exact->inexact ir)
[(? Float? flt) flt]
[_ (error "Inexact real isn't float?")]))
(: sum-floats ((Listof Float) -> Float))
(define (sum-floats flts)
(foldl (λ: ([el : Float] [acc : Float]) (+ el acc)) (ann 0.0 Float) flts))
(: rand-int (Integer -> Integer))
(define (rand-int seed)
(cond
[(>= seed 1) (random seed)]
[else 0]))
(: copy-tree (Program -> Program))
(define (copy-tree tree)
(match tree
['() '()]
[(? Program-Sequence? ps) (apply map copy-tree ps '())]
[_ tree]))
(: keep-number-reasonable (Float -> Float))
(define (keep-number-reasonable flt)
(cond [(> flt max-number-magnitude) (exact->inexact max-number-magnitude)]
[(< flt (- max-number-magnitude)) (exact->inexact (- max-number-magnitude))]
[(and (< flt min-number-magnitude)
(> flt (- min-number-magnitude)))
(ann 0.0 Float)]
[else flt]))
(: count-points (Program -> Integer))
(define (count-points prog)
(match prog
[(? Program-Sequence? ps) (+ 1 (apply + (map count-points ps)))]
[_ 1]))
(: code-at-point-recursive (Program Integer Integer -> Program))
(define (code-at-point-recursive tree point-index points-so-far)
(match point-index
[0 tree]
[_ (match tree
['() '()]
[(cons first-subtree rest)
(define points-in-first-subtree (count-points first-subtree))
(define new-points-so-far (+ points-so-far points-in-first-subtree))
(if (<= point-index new-points-so-far)
(code-at-point-recursive first-subtree (- point-index points-so-far 1) 0)
(code-at-point-recursive rest point-index new-points-so-far))]
[_ tree])]))
(: code-at-point (Program Integer -> Program))
(define (code-at-point tree point-index)
(code-at-point-recursive
tree
(abs (modulo (abs point-index) (max 1 (count-points tree))))
0))
(: trunc (Float -> Integer))
(define (trunc n)
(ann (truncate (inexact->exact n)) Integer))
(: insert-code-at-point-recursive (Program Program Integer Integer -> Program))
(define (insert-code-at-point-recursive tree new-subtree point-index points-so-far)
(match point-index
[0 new-subtree]
[_ (match tree
['() '()]
[(cons first-subtree rest)
(define points-in-first-subtree (count-points first-subtree))
(define new-points-so-far (+ points-so-far points-in-first-subtree))
(if (<= point-index new-points-so-far)
(cons (insert-code-at-point-recursive first-subtree new-subtree (- point-index points-so-far 1) 0) rest)
(cons first-subtree
(match (insert-code-at-point-recursive rest new-subtree point-index new-points-so-far)
[(? Program-Sequence? replaced-rest) replaced-rest]
[_ '()])))]
[_ new-subtree])]))
(: insert-code-at-point (Program Integer Program -> Program))
(define (insert-code-at-point tree point-index new-subtree)
(insert-code-at-point-recursive tree
new-subtree
(abs (modulo (abs point-index) (count-points tree)))
0))
(: remove-code-at-point (Program Integer -> Program))
(define (remove-code-at-point tree point-index)
(define normed-point-index (abs (modulo point-index (count-points tree))))
(: remove-sillymarker24 (Program -> Program))
(define (remove-sillymarker24 prog)
(match prog
[(? Program-Sequence? ps)
(define no-marker (filter (λ: ([p : Program])
(match p
['sillymarker24 false]
[_ true])) ps))
((inst map Program Program) remove-sillymarker24 no-marker)]
[_ prog]))
(match* (tree point-index)
[('() _) '()]
[(_ 0) '()]
[(_ _)
(remove-sillymarker24
(insert-code-at-point-recursive tree 'sillymarker24 point-index 0))]))
(: prog->string (Program -> String))
(define (prog->string prog)
(match prog
[(? Program-Sequence? ps) (string-append "(" (string-join (map prog->string ps) " ") ")")]
[(? Instruction? instr) (string-downcase (symbol->string instr))]
[lit (format "~A" lit)]))
(: state-pretty-print (ProgramState -> Void))
(define (state-pretty-print state)
(printf "- Exec: ~A~%" (map prog->string (ProgramState-Exec state)))
(printf "- Integer: ~A~%" (ProgramState-Integer state))
(printf "- Float: ~A~%" (ProgramState-Float state))
(printf "- Code: ~A~%" (map prog->string (ProgramState-Code state)))
(printf "- Boolean: ~A~%" (ProgramState-Boolean state))
(printf "- Auxiliary: ~A~%" (ProgramState-Auxiliary state))
(printf "- Tag: ~A~%" (ProgramState-Tag state))
(printf "- Zip: ~A~%" (ProgramState-Zip state)))
(: flatten-program (Program -> Program))
(define (flatten-program prog)
(match prog
[(? Program-Sequence? ps)
(apply append (map (compose ensure-list flatten-program) ps))]
[_ prog]))
(: repeat (All (a) (a Integer -> (Listof a))))
(define (repeat item num-times)
(match num-times
[1 `(,item)]
[_ (cons item (repeat item (- num-times 1)))]))
(: symbol-downcase (Symbol -> Symbol))
(define (symbol-downcase sym)
(string->symbol (string-downcase (symbol->string sym))))
;; Macros and functions for registering push instructions and related fancy business
(: instructions (Listof Instruction-Definition))
(define instructions '())
(: instruction-table (HashTable String (ProgramState -> ProgramState)))
(define instruction-table (make-hash))
(: definition->instr (Instruction-Definition -> Instruction))
(define (definition->instr id)
(string->symbol (format "~A.~A" (Instruction-Definition-Type id) (Instruction-Definition-Name id))))
(: definition->string (Instruction-Definition -> String))
(define (definition->string id)
(string-downcase (symbol->string (definition->instr id))))
(: make-instr (PushType Symbol -> Instruction))
(define (make-instr type name)
(string->symbol (string-downcase (format "~A.~A" type name))))
(: register (Instruction-Definition (ProgramState -> ProgramState) -> Void))
(define (register instruction definition)
(set! instructions (cons instruction instructions))
((inst hash-set! String (ProgramState -> ProgramState)) instruction-table (definition->string instruction) definition))
(: registered-for-type (PushType -> (Listof Instruction)))
(define (registered-for-type pt)
(define has-same-type (λ: ([id : Instruction-Definition]) (equal? pt (Instruction-Definition-Type id))))
(map definition->instr (filter has-same-type instructions)))
(define-syntax define-instruction
(syntax-rules ()
((_ type instruction definition)
(register (Instruction-Definition 'type (symbol-downcase 'instruction) definition) definition))))
(define-syntax with-stacks
(syntax-rules ()
((_ ps args ...)
(struct-copy ProgramState ps args ...))))
(: get-stack-inst-list (Symbol (All (a) ((Stack a) -> (Stack a))) -> (Listof Instruction-Definition)))
(define (get-stack-inst-list sym definition)
(list
(Instruction-Definition 'exec sym (λ (ps) (with-stacks ps [Exec (definition (ProgramState-Exec ps))])))
(Instruction-Definition 'integer sym (λ (ps) (with-stacks ps [Integer (definition (ProgramState-Integer ps))])))
(Instruction-Definition 'float sym (λ (ps) (with-stacks ps [Float (definition (ProgramState-Float ps))])))
(Instruction-Definition 'code sym (λ (ps) (with-stacks ps [Code (definition (ProgramState-Code ps))])))
(Instruction-Definition 'boolean sym (λ (ps) (with-stacks ps [Boolean (definition (ProgramState-Boolean ps))])))))
(: define-stack-instructions-function (Symbol (All (a) ((Stack a) -> (Stack a))) -> Void))
(define (define-stack-instructions-function sym definition)
(for ((instr (get-stack-inst-list sym definition)))
(set! instructions (cons instr instructions))
((inst hash-set! String (ProgramState -> ProgramState)) instruction-table (definition->string instr) (Instruction-Definition-Definition instr))))
(define-syntax define-stack-instructions
(syntax-rules ()
((_ instruction definition)
(define-stack-instructions-function 'instruction definition))))
(: get-inst-list (Symbol (All (a) (ProgramState -> (Stack a)) (ProgramState (Stack a) -> ProgramState) -> (ProgramState -> ProgramState)) -> (Listof Instruction-Definition)))
(define (get-inst-list sym definition)
(list
(Instruction-Definition 'exec sym
((inst definition Program) (λ: ([ps : ProgramState]) (ProgramState-Exec ps))
(λ: ([ps : ProgramState] [stk : (Stack Program)]) (with-stacks ps [Exec stk]))))
(Instruction-Definition 'integer sym
((inst definition Integer) (λ: ([ps : ProgramState]) (ProgramState-Integer ps))
(λ: ([ps : ProgramState] [stk : (Stack Integer)]) (with-stacks ps [Integer stk]))))
(Instruction-Definition 'float sym
((inst definition Float) (λ: ([ps : ProgramState]) (ProgramState-Float ps))
(λ: ([ps : ProgramState] [stk : (Stack Float)]) (with-stacks ps [Float stk]))))
(Instruction-Definition 'code sym
((inst definition Program) (λ: ([ps : ProgramState]) (ProgramState-Code ps))
(λ: ([ps : ProgramState] [stk : (Stack Program)]) (with-stacks ps [Code stk]))))
(Instruction-Definition 'boolean sym
((inst definition Boolean) (λ: ([ps : ProgramState]) (ProgramState-Boolean ps))
(λ: ([ps : ProgramState] [stk : (Stack Boolean)]) (with-stacks ps [Boolean stk]))))))
(: define-instructions-function (Symbol (All (a) (ProgramState -> (Stack a)) (ProgramState (Stack a) -> ProgramState) -> (ProgramState -> ProgramState)) -> Void))
(define (define-instructions-function sym definition)
(for ((instr (get-inst-list sym definition)))
(set! instructions (cons instr instructions))
((inst hash-set! String (ProgramState -> ProgramState)) instruction-table (definition->string instr) (Instruction-Definition-Definition instr))))
(define-syntax define-instructions
(syntax-rules ()
((_ instruction definition)
(define-instructions-function 'instruction definition))))
;; Integer and float instructions
(: int-binop ((Integer Integer -> Integer) -> (ProgramState -> ProgramState)))
(define ((int-binop op) ps)
(match (ProgramState-Integer ps)
[(list-rest a b rest) (with-stacks ps [Integer (cons (op a b) rest)])]
[_ ps]))
(: float-binop ((Float Float -> Float) -> (ProgramState -> ProgramState)))
(define ((float-binop op) ps)
(match (ProgramState-Float ps)
[(list-rest a b rest) (with-stacks ps [Float (cons (op a b) rest)])]
[_ ps]))
(: float-unop ((Float -> Float) -> (ProgramState -> ProgramState)))
(define ((float-unop op) ps)
(match (ProgramState-Float ps)
[(cons a rest) (with-stacks ps [Float (cons (op a) rest)])]
[_ ps]))
(define-instruction integer ADD (int-binop +))
(define-instruction float ADD (float-binop +))
(define-instruction integer SUB (int-binop -))
(define-instruction float SUB (float-binop -))
(define-instruction integer MULT (int-binop *))
(define-instruction float MULT (float-binop *))
(define-instruction integer MIN (int-binop min))
(define-instruction float MIN (float-binop min))
(define-instruction integer MAX (int-binop max))
(define-instruction float MAX (float-binop max))
(define-instruction integer MOD (int-binop (λ: ([a : Integer] [b : Integer]) (modulo a (max 1 b)))))
(define-instruction float DIV (float-binop /))
(define-instruction float SIN (float-unop sin))
(define-instruction float COS (float-unop cos))
(define-instruction float TAN (float-unop tan))
(define-instruction float <
(λ: ([ps : ProgramState])
(match (ProgramState-Float ps)
[(list-rest a b rest)
(with-stacks ps [Float rest] [Boolean (cons (< a b) (ProgramState-Boolean ps))])]
[_ ps])))
(define-instruction integer <
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
[(list-rest a b rest)
(with-stacks ps [Integer rest] [Boolean (cons (< a b) (ProgramState-Boolean ps))])]
[_ ps])))
(define-instruction float >
(λ: ([ps : ProgramState])
(match (ProgramState-Float ps)
[(list-rest a b rest)
(with-stacks ps [Float rest] [Boolean (cons (> a b) (ProgramState-Boolean ps))])]
[_ ps])))
(define-instruction integer >
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
[(list-rest a b rest)
(with-stacks ps [Integer rest] [Boolean (cons (> a b) (ProgramState-Boolean ps))])]
[_ ps])))
(define-instruction float FROMBOOLEAN
(λ: ([ps : ProgramState])
(match (ProgramState-Boolean ps)
[(list-rest a rest)
(define flt (if a 1.0 (ann 0.0 Float)))
(with-stacks ps [Float (cons flt (ProgramState-Float ps))] [Boolean rest])]
[_ ps])))
(define-instruction integer FROMBOOLEAN
(λ: ([ps : ProgramState])
(match (ProgramState-Boolean ps)
[(cons a rest)
(define int (if a 1 0))
(with-stacks ps [Integer (cons int (ProgramState-Integer ps))] [Boolean rest])]
[_ ps])))
(define-instruction float FROMINTEGER
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
[(cons int rest)
(define flt (ann (exact->inexact int) Float))
(with-stacks ps [Float (cons flt (ProgramState-Float ps))] [Integer rest])]
[_ ps])))
(define-instruction integer FROMFLOAT
(λ: ([ps : ProgramState])
(match (ProgramState-Float ps)
[(cons flt rest)
(define int (trunc flt))
(with-stacks ps [Integer (cons int (ProgramState-Integer ps))] [Float rest])]
[_ ps])))
;; Boolean instructions
(: bool-binop ((Boolean Boolean -> Boolean) -> (ProgramState -> ProgramState)))
(define ((bool-binop op) ps)
(match (ProgramState-Boolean ps)
[`(,a ,b . ,rest) (with-stacks ps [Boolean (cons (op a b) rest)])]
[_ ps]))
(: bool-unop ((Boolean -> Boolean) -> (ProgramState -> ProgramState)))
(define ((bool-unop op) ps)
(match (ProgramState-Boolean ps)
[(cons a rest) (with-stacks ps [Boolean (cons (op a) rest)])]
[_ ps]))
(define-instruction boolean AND (bool-binop (λ: ([a : Boolean] [b : Boolean]) (and a b))))
(define-instruction boolean OR (bool-binop (λ: ([a : Boolean] [b : Boolean]) (or a b))))
(define-instruction boolean NOT (bool-unop (λ: ([a : Boolean]) (not a))))
(define-instruction boolean FROMFLOAT
(λ: ([ps : ProgramState])
(match (ProgramState-Float ps)
[(cons flt rest)
(define bool (= 0.0 flt))
(with-stacks ps [Boolean (cons bool (ProgramState-Boolean ps))] [Float rest])]
[_ ps])))
(define-instruction boolean FROMINTEGER
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
[(cons int rest)
(define bool (= 0 int))
(with-stacks ps [Boolean (cons bool (ProgramState-Boolean ps))] [Integer rest])]
[_ ps])))
;; Code and exec instructions
(: ensure-list (Program -> (Listof Program)))
(define (ensure-list thing)
(match thing
[(? Program-Sequence? thing) thing]
[_ `(,thing)]))
(define-instruction code APPEND
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[`(,a ,b . ,rest)
(with-stacks ps [Code (cons (append (ensure-list a) (ensure-list b)) rest)])]
[_ ps])))
(define-instruction code ATOM
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons a rest)
(with-stacks ps [Code (cons (not (pair? a)) rest)])]
[_ ps])))
(define-instruction code CAR
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[`((,hd . ,tl) . ,rest)
(with-stacks ps [Code (cons hd rest)])]
[_ ps])))
(define-instruction code CDR
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons (app ensure-list (cons hd tl)) rest)
(with-stacks ps [Code (cons tl rest)])]
[_ ps])))
(define-instruction code CONS
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[`(,(app ensure-list top-item) ,second-item . ,rest)
(with-stacks ps [Code (cons (cons second-item top-item) rest)])]
[_ ps])))
(define-instruction code DO
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons prog rest)
(with-stacks ps [Exec `(,prog code.pop . ,(ProgramState-Exec ps))])]
[_ ps])))
(define-instruction code DO*
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons prog rest)
(with-stacks ps [Code rest] [Exec (cons prog (ProgramState-Exec ps))])]
[_ ps])))
(define-instruction code DO*RANGE
(λ: ([ps : ProgramState])
(match* ((ProgramState-Code ps) (ProgramState-Integer ps))
[((cons to-do code-rest) `(,destination-index ,current-index . ,int-rest))
(define increment
(cond [(< current-index destination-index) 1]
[(> current-index destination-index) -1]
[else 0]))
(define new-state (with-stacks ps [Integer (cons current-index int-rest)] [Code code-rest]))
(define new-instruction
(match increment
[0 (copy-tree to-do)]
[_
(list (+ current-index increment)
destination-index
'code.quote
(copy-tree to-do)
'code.do*range)]))
(with-stacks new-state [Exec (cons new-instruction (ProgramState-Exec new-state))])]
[(_ _) ps])))
(define-instruction exec DO*RANGE
(λ: ([ps : ProgramState])
(match* ((ProgramState-Exec ps) (ProgramState-Integer ps))
[((cons to-do code-rest) (list-rest destination-index current-index int-rest))
(define increment
(cond [(< current-index destination-index) 1]
[(> current-index destination-index) -1]
[else 0]))
(define new-state (with-stacks ps [Integer (cons current-index int-rest)] [Exec code-rest]))
(define new-instruction
(match increment
[0 (copy-tree to-do)]
[_
(list (+ current-index increment)
destination-index
'exec.do*range
(copy-tree to-do))]))
(with-stacks new-state [Exec (cons new-instruction (ProgramState-Exec new-state))])]
[(_ _) ps])))
(define-instruction code DO*COUNT
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons to-do code-rest)
(match (ProgramState-Integer ps)
[(cons 0 int-rest) ps]
[(cons num-times int-rest)
(define new-instruction
(list 0 (- num-times 1) 'code.quote (copy-tree to-do) 'code.do*range))
(with-stacks ps [Exec (cons new-instruction (ProgramState-Exec ps))] [Code code-rest] [Integer int-rest])]
[_ ps])]
[_ ps])))
(define-instruction exec DO*COUNT
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(cons to-do exec-rest)
(match (ProgramState-Integer ps)
[(cons 0 int-rest) ps]
[(cons num-times int-rest)
(define new-instruction
(list 0 (- num-times 1) 'exec.do*range (copy-tree to-do)))
(with-stacks ps [Exec (cons new-instruction exec-rest)] [Integer int-rest])]
[_ ps])]
[_ ps])))
(define-instruction code DO*TIMES
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons to-do code-rest)
(match (ProgramState-Integer ps)
[(cons 0 int-rest) ps]
[(cons num-times int-rest)
(define new-instruction
(list 0
(- num-times 1)
'code.quote
(cons 'integer.pop (ensure-list (copy-tree to-do)))
'code.do*range))
(with-stacks ps [Exec (cons new-instruction (ProgramState-Exec ps))] [Code code-rest] [Integer int-rest])]
[_ ps])]
[_ ps])))
(define-instruction exec DO*TIMES
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(cons to-do exec-rest)
(match (ProgramState-Integer ps)
[(cons 0 int-rest) ps]
[(cons num-times int-rest)
(define new-instruction
(list 0
(- num-times 1)
'exec.do*range
(cons 'integer.pop (ensure-list (copy-tree to-do)))))
(with-stacks ps [Exec (cons new-instruction exec-rest)] [Integer int-rest])]
[_ ps])]
[_ ps])))
(define-instruction code FROMBOOLEAN
(λ: ([ps : ProgramState])
(match (ProgramState-Boolean ps)
[(cons hd tl)
(with-stacks ps [Code (cons hd (ProgramState-Code ps))] [Boolean tl])]
[_ ps])))
(define-instruction code FROMINTEGER
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
[(cons hd tl)
(with-stacks ps [Code (cons hd (ProgramState-Code ps))] [Integer tl])]
[_ ps])))
(define-instruction code FROMFLOAT
(λ: ([ps : ProgramState])
(match (ProgramState-Float ps)
[(cons hd tl)
(with-stacks ps [Code (cons hd (ProgramState-Code ps))] [Float tl])]
[_ ps])))
(define-instruction code QUOTE
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(cons hd tl)
(with-stacks ps [Code (cons hd (ProgramState-Code ps))] [Exec tl])]
[_ ps])))
(define-instruction code IF
(λ: ([ps : ProgramState])
(match* ((ProgramState-Code ps) (ProgramState-Boolean ps))
[((list-rest fst snd code-rest) (cons cond bool-rest))
(define to-do (if cond fst snd))
(with-stacks ps [Exec (cons (copy-tree to-do) (ProgramState-Exec ps))] [Boolean bool-rest] [Code code-rest])]
[(_ _) ps])))
(define-instruction exec IF
(λ: ([ps : ProgramState])
(match* ((ProgramState-Exec ps) (ProgramState-Boolean ps))
[(`(,fst ,snd . ,exec-rest) (cons cond bool-rest))
(define to-do (if cond snd fst))
(with-stacks ps [Exec (cons (copy-tree to-do) exec-rest)] [Boolean bool-rest])]
[(_ _) ps])))
(define-instruction code LENGTH
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons (app ensure-list first-code) code-rest)
(with-stacks ps [Integer (cons (length first-code) (ProgramState-Integer ps))] [Code code-rest])]
[_ ps])))
(define-instruction code LIST
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(list-rest top-item second-item code-rest)
(with-stacks ps [Code `((,second-item ,top-item) . ,code-rest)])]
[_ ps])))
(define-instruction code NOOP (λ (ps) ps))
(define-instruction exec NOOP (λ (ps) ps))
(define-instruction code MEMBER
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[`(,top-item ,second-item . ,code-rest)
(define new-bool-stack
(cons (not (not (member second-item (ensure-list top-item)))) (ProgramState-Boolean ps)))
(with-stacks ps [Boolean new-bool-stack] [Code code-rest])]
[_ ps])))
(define-instruction code NTH
(λ: ([ps : ProgramState])
(match* ((ProgramState-Code ps) (ProgramState-Integer ps))
[((cons (app ensure-list (and the-list (cons _ _))) code-rest) (cons n int-rest))
(define new-item (list-ref the-list (modulo (abs n) (length the-list))))
(with-stacks ps [Integer int-rest] [Code (cons new-item code-rest)])]
[(_ _) ps])))
(define-instruction code NTHCDR
(λ: ([ps : ProgramState])
(match* ((ProgramState-Code ps) (ProgramState-Integer ps))
[((cons (app ensure-list (and the-list (cons _ _))) code-rest) (cons n int-rest))
(define new-item (list-tail the-list (modulo (abs n) (length the-list))))
(with-stacks ps [Integer int-rest] [Code (cons new-item code-rest)])]
[(_ _) ps])))
(define-instruction code NULL
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons item code-rest)
(with-stacks ps [Boolean (cons (null? item) (ProgramState-Boolean ps))] [Code code-rest])]
[_ ps])))
(define-instruction code SIZE
(λ: ([ps : ProgramState])
(match (ProgramState-Code ps)
[(cons item code-rest)
(with-stacks ps [Integer (cons (count-points item) (ProgramState-Integer ps))] [Code code-rest])]
[_ ps])))
(define-instruction exec K
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(list-rest x _ exec-rest)
(with-stacks ps [Exec (cons x exec-rest)])]
[_ ps])))
(define-instruction exec S
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(list-rest x y z exec-rest)
(define new-exec (append `(,x ,z (,y ,z)) exec-rest))
(with-stacks ps [Exec new-exec])]
[_ ps])))
(define-instruction exec Y
(λ: ([ps : ProgramState])
(match (ProgramState-Exec ps)
[(cons x exec-rest)
(define new-exec `(,x exec.y . ,exec-rest))
(with-stacks ps [Exec new-exec])]
[_ ps])))
;; Instructions for all types
(define-stack-instructions DUP
(pλ: (a) ([stack : (Stack a)])
(match stack
[(cons hd tl) `(,hd ,hd . ,tl)]
[_ stack])))
(define-stack-instructions POP
(pλ: (a) ([stack : (Stack a)])
(match stack
[(cons hd tl) tl]
[_ stack])))
(define-stack-instructions SWAP
(pλ: (a) ([stack : (Stack a)])
(match stack
[`(,a ,b . ,tl) `(,b ,a . ,tl)]
[_ stack])))
(define-stack-instructions ROT
(pλ: (a) ([stack : (Stack a)])
(match stack
[`(,a ,b ,c . ,tl) `(,c ,a ,b . ,tl)]
[_ stack])))
(define-stack-instructions FLUSH
(pλ: (a) ([stack : (Stack a)])
'()))
(: id (All (a) (a -> a)))
(define (id x) x)
(define-instructions EQUAL
(pλ: (a) ([get-stack : (ProgramState -> (Stack a))]
[set-stack : (ProgramState (Stack a) -> ProgramState)])
(λ: ([ps : ProgramState])
(match (get-stack ps)
[(list-rest a b rest)
(define new-state (set-stack ps rest))
(define new-bool-stack (cons (equal? a b) (ProgramState-Boolean new-state)))
(with-stacks new-state [Boolean new-bool-stack])]
[_ ps]))))
(define-instructions STACKDEPTH
(pλ: (a) ([get-stack : (ProgramState -> (Stack a))]
[set-stack : (ProgramState (Stack a) -> ProgramState)])
(λ: ([ps : ProgramState])
(define stack-depth (length (get-stack ps)))
(with-stacks ps [Integer (cons stack-depth (ProgramState-Integer ps))]))))
(define-instructions YANK
(pλ: (a) ([get-stack : (ProgramState -> (Stack a))]
[set-stack : (ProgramState (Stack a) -> ProgramState)])
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
['() ps]
[(cons raw-yank-index rest)
(define new-state (with-stacks ps [Integer rest]))
(match (get-stack new-state)
['() ps]
[stk
(define yank-index (max 0 (min raw-yank-index (- (length stk) 1))))
(define item (list-ref stk yank-index))
(define stk-without-item (append (take stk yank-index) (drop stk (+ yank-index 1))))
(set-stack new-state (cons item stk-without-item))])]))))
(define-instructions YANKDUP
(pλ: (a) ([get-stack : (ProgramState -> (Stack a))]
[set-stack : (ProgramState (Stack a) -> ProgramState)])
(λ: ([ps : ProgramState])
(match (ProgramState-Integer ps)
['() ps]
[(cons raw-yank-index rest)
(define new-state (with-stacks ps [Integer rest]))
(match (get-stack new-state)
['() ps]
[stk
(define yank-index (max 0 (min raw-yank-index (- (length stk) 1))))
(define item (list-ref stk yank-index))
(set-stack new-state (cons item stk))])]))))
;; Random code generation
(define-type Atom-Generator (U (-> Program) Program))
(: random-element (All (a) ((Listof a) -> a)))
(define (random-element xs)
(list-ref xs (rand-int (length xs))))
(: shuffle (All (a) ((Listof a) -> (Listof a))))
(define (shuffle xs)
(match xs
[(cons _ _)
(define item (random-element xs))
(cons item (shuffle (remove item xs)))]
[_ xs]))
(: decompose (Integer Integer -> (Listof Integer)))
(define (decompose number max-parts)
(cond
[(or (<= max-parts 1) (<= number 1))
`(,number)]
[else
(define this-part (+ 1 (rand-int (- number 1))))
(cons this-part (decompose (- number this-part) (- max-parts 1)))]))
(: random-code-with-size (Integer (Listof Atom-Generator) -> Program))
(define (random-code-with-size points atom-generators)
(cond
[(< points 2)
(define element (random-element atom-generators))
(if (Program? element) element (element))]
[else
(define elements-this-level (shuffle (decompose (- points 1) (- points 1))))
(ann ((inst map Program Integer) (λ: ([size : Integer]) (random-code-with-size size atom-generators)) elements-this-level) Program)]))
(: random-code (Integer (Listof Atom-Generator) -> Program))
(define (random-code max-points atom-generators)
(random-code-with-size (+ 1 (rand-int max-points)) atom-generators))
;; Interpreter
;; all these push-foo routines feel pretty ugly... should I use a macro and parametrize with PushType?
(: push-integer (Integer ProgramState -> ProgramState))
(define (push-integer int ps)
(with-stacks ps [Integer (cons int (ProgramState-Integer ps))]))
(: push-float (Float ProgramState -> ProgramState))
(define (push-float flt ps)
(with-stacks ps [Float (cons flt (ProgramState-Float ps))]))
(: push-boolean (Boolean ProgramState -> ProgramState))
(define (push-boolean bool ps)
(with-stacks ps [Boolean (cons bool (ProgramState-Boolean ps))]))
(: push-code (Program ProgramState -> ProgramState))
(define (push-code code ps)
(with-stacks ps [Code (cons code (ProgramState-Code ps))]))
(: push-exec (Program ProgramState -> ProgramState))
(define (push-exec code ps)
(with-stacks ps [Exec (cons code (ProgramState-Exec ps))]))
(: lookup-instruction (Instruction -> (ProgramState -> ProgramState)))
(define (lookup-instruction instr)
;;(printf "Executing instruction: ~A" instr)
(hash-ref instruction-table (string-downcase (symbol->string instr))))
(: execute (ProgramState Integer (Listof Program) Boolean -> ProgramState))
(define (execute current-state execution-count traces print)
(match (ProgramState-Exec current-state)
['() current-state]
[(cons exec-top new-exec)
(define next-state
(match exec-top
[(? Instruction? instr)
(with-handlers
([(λ (_) #t)
(λ (a)
(printf "Failed during instruction: ~A" instr)
(error (format "~A" a)))])
((lookup-instruction instr) (with-stacks current-state [Exec new-exec])))]
[(? Literal? lit)
(define new-state (with-stacks current-state [Exec new-exec]))
(match lit
[(? Integer? int) (push-integer int new-state)]
[(? Float? flt) (push-float flt new-state)]
[(? Boolean? b) (push-boolean b new-state)])]
[(? Program-Sequence? ps)
(with-stacks current-state [Exec (append ps new-exec)])]))
(define new-execution-count (+ 1 execution-count))
(define new-traces (if save-traces (cons exec-top traces) traces))
(when print
(printf "~%State after ~A steps (last step: ~A):~%"
execution-count (if (list? exec-top) "(...)" (prog->string exec-top)))
(state-pretty-print next-state))
(if (> new-execution-count evalpush-limit)
next-state
(execute next-state new-execution-count new-traces print))]))
(: make-push-state (-> ProgramState))
(define (make-push-state) (ProgramState '() '() '() '() '() '() '() '()))
(: execute-toplevel (Program -> ProgramState))
(define (execute-toplevel prog)
(define starting-state (ProgramState `(,prog) '() '() `(,prog) '() '() '() '()))
(execute starting-state 0 '() #t))
(: run-rush (Program ProgramState Boolean -> ProgramState))
(define (run-rush code state print)
(define state-with-pushed (if top-level-push-code (push-code code state) state))
(define state-with-exec (push-exec code state-with-pushed))
(when print
(printf "~%State after 0 steps:~%")
(state-pretty-print state-with-exec))
(define state-evaluated (execute state-with-exec 0 '() print))
(define state-with-popped (if top-level-pop-code
(with-stacks state-evaluated
[Code (match (ProgramState-Code state-evaluated)
[(cons hd tl) tl]
['() '()])])
state-evaluated))
state-with-popped)
;; Rush-GP
(struct:
Individual
([Program : Program]
[Errors : (Listof Float)]
[Total-Error : (U 'undefined Float)]
[Scaled-Error : (U 'undefined Float)]))
(: auto-simplify (Program (Program -> (Listof Float)) Integer Boolean Integer -> Individual))
(define (auto-simplify program error-function steps print progress-interval)
(when print (printf "~%Auto-simplifying with starting size: ~A" (count-points program)))
(define: (randomly-remove [program : Program]) : Program
(for/fold ([removed-prog program])
([i (in-range (+ 1 (random 2)))])
(remove-code-at-point removed-prog (rand-int (count-points removed-prog)))))
(define: (randomly-flatten [program : Program]) : Program
(define point-index (rand-int (count-points program)))
(define point (code-at-point program point-index))
(if (list? point)
(insert-code-at-point program point-index (flatten-program point))
program))
(define: (print-simplification-report [i : Integer] [errors : (Listof Float)] [program : Program]) : Void
(when print (printf "~%step: ~A~%program: ~A~%errors: ~A~%total: ~A~%size: ~A~%"
i program errors (apply + errors) (count-points program))))
(define-values (simplified-errors simplified-program)
(for/fold: : (values (Listof Float) Program)
([errors (error-function program)]
[program program])
([step (in-range steps)])