Skip to content

Commit d7b9649

Browse files
committed
compute different analysis for fitness function
1 parent 6532b9c commit d7b9649

9 files changed

+270
-105
lines changed

Diff for: GA/GA-stochastic.rkt

+6-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@
8888
(set! a (+ a (arithmetic-shift a -16)))
8989
(bitwise-and a #x3f))
9090

91-
(pop-count (bitwise-xor (bitwise-and x #x3ffff)
92-
(bitwise-and y #x3ffff))))
91+
;; (pop-count (bitwise-xor (bitwise-and x #x3ffff)
92+
;; (bitwise-and y #x3ffff))))
93+
94+
(/
95+
(log (add1 (abs (- (bitwise-and x #x3ffff) (bitwise-and y #x3ffff)))))
96+
(log 2)))
9397

9498
(define-syntax-rule (accum x) (set! correctness (+ correctness x)))
9599

Diff for: GA/main.rkt

+18-8
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@
4848
(generate-outputs-steps (send printer encode code) dir subdir
4949
machine printer simulator backward-stochastic))
5050

51-
#|(define (GA-calculate-cost dir name live-out)
52-
(calculate-cost dir name live-out machine backward-stochastic))|#
51+
(define (GA-calculate-cost dir name live-out)
52+
(calculate-cost dir name live-out machine backward-stochastic))
53+
54+
(define (GA-calculate-cost2 dir name live-out n)
55+
(calculate-cost2 dir name live-out n machine backward-stochastic))
56+
57+
(define (GA-generate-tree code dir subdir degree n)
58+
(generate-tree (send printer encode code) dir subdir degree n
59+
machine printer simulator backward-stochastic))
5360

5461
#|
5562
(GA-generate-inputs
@@ -61,12 +68,15 @@
6168
;; (GA-generate-outputs-steps
6269
;; (send (new GA-parser%) ast-from-string
6370
;; "2 b! @b 3 b! !b 1 b! @b 2 b! !b")
64-
;; "data-ex" "prog")
71+
;; "data-ex" "n100-s3")
6572

66-
#|
67-
(GA-calculate-cost "data-ex/spec-n100-1" "v1" '((data . 2) memory))
68-
(GA-calculate-cost "data-ex/spec-n100-2" "v1" '((data . 2) memory))
69-
(GA-calculate-cost "data-ex/spec-n100-3" "v1" '((data . 2) memory))|#
7073

74+
;(GA-calculate-cost "data-ex/n100-s3" "v1" '((data . 2) memory))
75+
76+
#|
77+
(GA-generate-tree
78+
(send (new GA-parser%) ast-from-string
79+
"2 b! @b 3 b! !b 1 b! @b 2 b! !b")
80+
"data-ex" "tree-n5000" 4 5000)|#
7181

72-
82+
(GA-calculate-cost2 "data-ex/tree-n5000" "v3" '((data . 2) memory) 5005)

Diff for: GA/run.rkt

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
;; #:assume '((<= . 65535) (<= . 65535) (<= . 65535)))
1212

1313
;; mem
14-
;; (optimize (send parser ast-from-string "2 b! @b 3 b! !b 1 b! @b 2 b! !b")
15-
;; '((data . 2) memory)
16-
;; #t #t 0 #:cores 8 #:time-limit 3600 #:size 8)
1714
(optimize (send parser ast-from-string "2 b! @b 3 b! !b 1 b! @b 2 b! !b")
1815
'((data . 2) memory)
19-
#t #t 0 #:cores 2 #:time-limit 3600 #:size 8
20-
#:input-file "data-ex/inputs"
21-
#:start-prog "2 b! @b 3 b! !b 1 !b @b 2 b! b!")
16+
#t #t 0 #:cores 4 #:time-limit 60 #:size 8
17+
#:input-file "data-ex/inputs")
18+
;; (optimize (send parser ast-from-string "2 b! @b 3 b! !b 1 b! @b 2 b! !b")
19+
;; '((data . 2) memory)
20+
;; #t #t 0 #:cores 2 #:time-limit 3600 #:size 8
21+
;; #:input-file "data-ex/inputs"
22+
;; #:start-prog "2 b! @b 3 b! !b 1 !b @b 2 b! b!")
2223

2324
;; has zero byte?
2425
;; (optimize (send parser ast-from-string

Diff for: fitness-learner.rkt

+101-87
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#lang racket
22

33
(require "ast.rkt")
4-
(provide generate-inputs generate-outputs-steps)
4+
(provide generate-inputs generate-outputs-steps calculate-cost
5+
generate-tree calculate-cost2)
56

6-
(define nsteps 10)
7-
(define repeat 10)
7+
(define nsteps 3)
8+
(define repeat 100)
89

910
(define (get-states-from-file machine file)
1011
(send machine get-states-from-file file))
@@ -27,65 +28,6 @@
2728
(send machine display-state-text (cons #t input))))))
2829

2930

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-
8931
(define (generate-outputs-steps code dir subdir
9032
machine printer simulator stochastic)
9133
(define (interpret code input)
@@ -113,9 +55,9 @@
11355
(define outputs (map (lambda (x) (interpret new-code (cdr x))) inputs))
11456
;; (pretty-display `(get-new-code ,(andmap car outputs)))
11557
;; (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))
11961
(values new-code outputs)
12062
(get-new-code code (add1 round))))
12163

@@ -132,29 +74,101 @@
13274
(for ([i (in-range 1 (add1 repeat))])
13375
(iter code 1 i)))
13476

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))))
137131

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))
140136

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))))
159158

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))
160174

Diff for: stat.rkt

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636

3737
(super-new)
3838

39-
(define/public (inc-iter)
39+
(define/public (inc-iter current-cost)
40+
(with-output-to-file #:exists 'append (format "~a.csv" name)
41+
(thunk
42+
(pretty-display (format "~a,~a" iter-count current-cost))))
4043
(set! iter-count (add1 iter-count))
4144
(when (= (modulo iter-count 1000) 0)
4245
(print-stat-to-file)
@@ -75,6 +78,9 @@
7578
;; (pretty-display (format "best-correct-time: ~a" best-correct-time))
7679
(send printer print-syntax (send printer decode program))))
7780
)
81+
82+
(define/public (update-best-correct-program program)
83+
(set! best-correct-program program))
7884

7985
(define/public (update-best-correct program cost)
8086
(set! best-correct-program program)

Diff for: stat/correlation.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from scipy.stats import spearmanr, kendalltau
2+
import numpy
3+
import commands
4+
5+
def correlation(dirname):
6+
status, files = commands.getstatusoutput('ls ' + dirname)
7+
files = files.split('\n')
8+
steps = []
9+
costs = []
10+
count = 0
11+
correlation = []
12+
for filename in files:
13+
f = open(dirname + '/' + filename,'r')
14+
last_cost = 0
15+
my_costs = []
16+
my_steps = []
17+
for line in f:
18+
tokens = line.split(',')
19+
step = int(tokens[0])
20+
cost = int(tokens[1])
21+
if step < 5:
22+
steps.append(step)
23+
costs.append(cost)
24+
my_steps.append(step)
25+
my_costs.append(cost)
26+
f.close()
27+
#corr, p = spearmanr(my_steps,my_costs)
28+
#correlation.append(corr)
29+
if my_costs[0] <= my_costs[1] and my_costs[1] <= my_costs[2]:
30+
count = count + 1
31+
32+
#print dirname, spearmanr(steps,costs)
33+
print dirname, count
34+
#print numpy.median(correlation), numpy.percentile(correlation, 25), numpy.mean(correlation)
35+
#print filename, kendalltau(steps,costs)
36+
37+
38+
#print spearmanr([1,2,3,4,5],[5,6,7,8,7])
39+
correlation("../GA/data-ex/n100-s3/v1")
40+
correlation("../GA/data-ex/n100-s3/v2")

0 commit comments

Comments
 (0)