1
1
#lang racket
2
2
3
3
(require "ast.rkt " )
4
- (provide generate-inputs generate-outputs-steps)
4
+ (provide generate-inputs generate-outputs-steps calculate-cost
5
+ generate-tree calculate-cost2)
5
6
6
- (define nsteps 10 )
7
- (define repeat 10 )
7
+ (define nsteps 3 )
8
+ (define repeat 100 )
8
9
9
10
(define (get-states-from-file machine file )
10
11
(send machine get-states-from-file file ))
27
28
(send machine display-state-text (cons #t input))))))
28
29
29
30
30
- ;; (define (generate-outputs-steps code live-out dir subdir
31
- ;; machine printer simulator stochastic)
32
- ;; (define (interpret code input)
33
- ;; (with-handlers*
34
- ;; ([exn:state? (lambda (e)
35
- ;; (cons #f (exn:state-state e)))])
36
- ;; (cons #t (send simulator interpret code input))))
37
-
38
- ;; (define inputs (get-states-from-file machine (format "~a/inputs" dir)))
39
- ;; (define correct-outputs (map (lambda (x) (interpret code (cdr x))) inputs))
40
- ;; (define constraint (send machine output-constraint live-out))
41
- ;; (system (format "rm -r ~a/~a" dir subdir))
42
- ;; (system (format "mkdir ~a/~a" dir subdir))
43
-
44
- ;; (define (compare states)
45
- ;; (min
46
- ;; 10000
47
- ;; (for/sum ([state1 correct-outputs]
48
- ;; [state2 states])
49
- ;; (if (car state2)
50
- ;; (send stochastic correctness-cost (cdr state1) (cdr state2) constraint)
51
- ;; 10000))))
52
-
53
- ;; (define (print-state-step code outputs cost step count round)
54
- ;; (when (>= step 0)
55
- ;; (with-output-to-file #:exists 'append (format "~a/~a/cost_~a" dir subdir count)
56
- ;; (thunk (pretty-display (format "~a,~a,~a" step cost round)))))
57
- ;; (with-output-to-file #:exists 'append (format "~a/~a/program_~a" dir subdir count)
58
- ;; (thunk (send printer print-syntax (send printer decode code))
59
- ;; (newline)))
60
- ;; (with-output-to-file (format "~a/~a/outputs_~a_~a" dir subdir step count)
61
- ;; (thunk
62
- ;; (for ([output outputs])
63
- ;; (send machine display-state-text output)))))
64
-
65
- ;; (define (get-new-code code cost [round 0])
66
- ;; (define new-code (send stochastic mutate code))
67
- ;; (define outputs (map (lambda (x) (interpret new-code (cdr x))) inputs))
68
- ;; (define new-cost (compare outputs))
69
- ;; (pretty-display `(get-new-code ,new-cost ,(andmap car outputs)))
70
- ;; (send printer print-syntax (send printer decode new-code)) (newline)
71
- ;; (if (or (and (>= new-cost cost) (andmap car outputs)) (> round 10))
72
- ;; (values new-code outputs new-cost round)
73
- ;; (get-new-code code cost (add1 round))))
74
-
75
- ;; (define (iter code cost step count)
76
- ;; (pretty-display `(iter ,count ,step))
77
- ;; (define-values (new-code outputs new-cost round) (get-new-code code cost))
78
- ;; (print-state-step new-code outputs new-cost step count round)
79
- ;; (when (< step nsteps)
80
- ;; (iter new-code new-cost (add1 step) count)))
81
-
82
- ;; (send printer print-syntax (send printer decode code)) (newline)
83
- ;; (pretty-display ">>> Phase 2: generate output states")
84
- ;; (print-state-step code correct-outputs 0 -1 0 0)
85
- ;; (for ([i (in-range repeat)])
86
- ;; (iter code 0 1 i)))
87
-
88
-
89
31
(define (generate-outputs-steps code dir subdir
90
32
machine printer simulator stochastic)
91
33
(define (interpret code input)
113
55
(define outputs (map (lambda (x) (interpret new-code (cdr x))) inputs))
114
56
;; (pretty-display `(get-new-code ,(andmap car outputs)))
115
57
;; (send printer print-syntax (send printer decode new-code)) (newline)
116
- (if (or (not (send machine syntax-equal? code new-code))
117
- (> round 10 ))
118
- ;; (or (andmap car outputs) (> round 10))
58
+ (if ;; (or (not (send machine syntax-equal? code new-code))
59
+ ;; (> round 10))
60
+ (or (andmap car outputs) (> round 10 ))
119
61
(values new-code outputs)
120
62
(get-new-code code (add1 round))))
121
63
132
74
(for ([i (in-range 1 (add1 repeat))])
133
75
(iter code 1 i)))
134
76
135
- ;; (define (calculate-cost dir name live-out
136
- ;; machine stochastic)
77
+ (define (calculate-cost dir name live-out
78
+ machine stochastic)
79
+
80
+ (define constraint (send machine output-constraint live-out))
81
+ (define ref-states (get-states-from-file machine (format "~a/outputs_~a_~a " dir 0 0 )))
82
+
83
+ (define (compare states)
84
+ (min
85
+ 10000
86
+ (for/sum ([state1 ref-states]
87
+ [state2 states])
88
+ (if (car state2)
89
+ (send stochastic correctness-cost (cdr state1) (cdr state2) constraint)
90
+ 10000 ))))
91
+
92
+ (system (format "mkdir ~a/~a " dir name))
93
+ (for ([count (in-range 1 (add1 repeat))])
94
+ (with-output-to-file (format "~a/~a/cost-~a.csv " dir name count)
95
+ (thunk
96
+ (for* ([step (range 1 (add1 nsteps))])
97
+ (let ([states (get-states-from-file
98
+ machine
99
+ (format "~a/outputs_~a_~a " dir step count))])
100
+ (pretty-display (format "~a,~a " step (compare states)))))))))
101
+
102
+ (define (calculate-cost2 dir name live-out n
103
+ machine stochastic)
104
+
105
+ (define constraint (send machine output-constraint live-out))
106
+ (define ref-states (get-states-from-file machine (format "~a/outputs_0 " dir)))
107
+
108
+ (define (compare states)
109
+ (min
110
+ 10000
111
+ (for/sum ([state1 ref-states]
112
+ [state2 states])
113
+ (if (car state2)
114
+ (send stochastic correctness-cost (cdr state1) (cdr state2) constraint)
115
+ 10000 ))))
116
+
117
+ (with-output-to-file (format "~a/costs-~a " dir name)
118
+ (thunk
119
+ (for* ([id n])
120
+ (let ([states (get-states-from-file machine (format "~a/outputs_~a " dir id))])
121
+ (pretty-display (compare states)))))))
122
+
123
+
124
+ (define (generate-tree code dir subdir degree n
125
+ machine printer simulator stochastic)
126
+ (define (interpret code input)
127
+ (with-handlers*
128
+ ([exn:state? (lambda (e)
129
+ (cons #f (exn:state-state e)))])
130
+ (cons #t (send simulator interpret code input))))
137
131
138
- ;; (define constraint (send machine output-constraint live-out))
139
- ;; (define ref-states (get-states-from-file machine (format "~a/outputs_~a_~a" dir 0 0)))
132
+ (system (format "rm -r ~a/~a " dir subdir))
133
+ (system (format "mkdir ~a/~a " dir subdir))
134
+ (define inputs (get-states-from-file machine (format "~a/inputs " dir)))
135
+ (define correct-outputs (map (lambda (x) (interpret code (cdr x))) inputs))
140
136
141
- ;; (define (compare states)
142
- ;; (min
143
- ;; 10000
144
- ;; (for/sum ([state1 ref-states]
145
- ;; [state2 states])
146
- ;; (if (car state2)
147
- ;; (send stochastic correctness-cost (cdr state1) (cdr state2) constraint)
148
- ;; 10000))))
149
-
150
- ;; (system (format "mkdir ~a/~a" dir name))
151
- ;; (for ([count (in-range 1 (add1 repeat))])
152
- ;; (with-output-to-file (format "~a/~a/cost-~a.csv" dir name count)
153
- ;; (thunk
154
- ;; (for* ([step (range 1 (add1 nsteps))])
155
- ;; (let ([states (get-states-from-file
156
- ;; machine
157
- ;; (format "~a/outputs_~a_~a" dir step count))])
158
- ;; (pretty-display (format "~a,~a" step (compare states)))))))))
137
+ (define (print-state code outputs id)
138
+ (with-output-to-file #:exists 'append (format "~a/~a/programs " dir subdir)
139
+ (thunk (send printer print-syntax (send printer decode code))
140
+ (newline)))
141
+ (with-output-to-file (format "~a/~a/outputs_~a " dir subdir id)
142
+ (thunk
143
+ (for ([output outputs])
144
+ (send machine display-state-text output)))))
145
+
146
+ (define (get-new-code code id [round 0 ])
147
+ (define new-code (send stochastic mutate code))
148
+ (define outputs (map (lambda (x) (interpret new-code (cdr x))) inputs))
149
+ ;; (pretty-display `(get-new-code ,(andmap car outputs)))
150
+ ;; (send printer print-syntax (send printer decode new-code)) (newline)
151
+ (if ;;(or (not (send machine syntax-equal? code new-code))
152
+ ;; (> round 10))
153
+ (or (andmap car outputs) (> round 10 ))
154
+ (begin
155
+ (print-state new-code outputs id)
156
+ new-code)
157
+ (get-new-code code id (add1 round))))
159
158
159
+ (define (loop code-list id-list count)
160
+ (define my-code (car code-list))
161
+ (define my-id (car id-list))
162
+ (define new-codes (for/list ([i degree]) (get-new-code my-code (+ count i))))
163
+ (define new-ids (for/list ([i degree]) (+ count i)))
164
+ (with-output-to-file #:exists 'append (format "~a/~a/tree " dir subdir)
165
+ (thunk
166
+ (for ([i degree]) (pretty-display (format "~a ~a " my-id (+ count i))))))
167
+ (when (< count n)
168
+ (loop (append (cdr code-list) new-codes)
169
+ (append (cdr id-list) new-ids)
170
+ (+ count degree))))
171
+
172
+ (print-state code correct-outputs 0 )
173
+ (loop (list code) (list 0 ) 1 ))
160
174
0 commit comments