-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcopystat.lisp
357 lines (292 loc) · 13.5 KB
/
copystat.lisp
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
;
; COPYSTAT: This program collects and stores statistical information
; from runs of the Copycat program.
;---------------------------------------------
(in-package 'user)
(proclaim '(special *run-number* *data-file* *verbose-data-file*
*random-state-file*))
;---------------------------------------------
(defun copystat (initial-string modified-string target-string n
&optional (directory "~/")
&aux universal-time hour minute day month year file-string
random-state-list old-num-of-runs)
(format t "~%In Copystat.~&")
(setq file-string
(string-append initial-string "-" modified-string "-" target-string))
(setq *data-file*
(string-append directory file-string ".data"))
(setq *verbose-data-file*
(string-append directory file-string ".verbose-data"))
(setq *random-state-file*
(string-append directory file-string ".random-state"))
(if* (null (probe-file *verbose-data-file*))
then (with-open-file (ostream1 *data-file* :direction :output
:if-does-not-exist :create)
(with-open-file (ostream2 *verbose-data-file* :direction :output
:if-exists :append :if-does-not-exist :create)
(format ostream2 "PROBLEM: If ~a => ~a then ~a => ?~%~%"
initial-string modified-string target-string)
(format ostream1 "(") ; Start list.
(format ostream2 "------------------------------------~&"))))
; Figure out how many runs are stored in the files.
(if* (probe-file *random-state-file*)
then (with-open-file (istream *random-state-file* :direction :input)
(setq random-state-list (read istream))))
(setq old-num-of-runs (length random-state-list))
(loop for i from 1 to n do
(format t "Starting run ~a.~&" i)
(with-open-file (ostream1 *data-file* :direction :output
:if-exists :append :if-does-not-exist :create)
(with-open-file (ostream2 *verbose-data-file* :direction :output
:if-exists :append :if-does-not-exist :create)
(setq universal-time
(multiple-value-list
(decode-universal-time (get-universal-time))))
(setq hour (fix-to-string (nth 2 universal-time)))
(setq minute (fix-to-string (nth 1 universal-time)))
(if* (= (length minute) 1)
then (setq minute (string-append "0" minute)))
(setq day (fix-to-string (nth 3 universal-time)))
(setq month (fix-to-string (nth 4 universal-time)))
(setq year (subseq (fix-to-string (nth 5 universal-time)) 2))
(format ostream2 "Date: ~a/~a/~a ~%Time: ~a:~a~&"
month day year hour minute)
(setq *run-number* (1- (+ i old-num-of-runs)))
(init-ccat initial-string modified-string target-string
:no-graphics t)
(format ostream1 "(~a ~a ~a ~a ~a ~a ~a ~a)~%"
(send *answer-string* :pstring) *temperature*
*codelet-count* *snag-count*
*single-letter-group-count*
*single-letter-group-at-end-count*
*length-description-count*
*length-relevant-at-end*)
(format ostream2 "~a. Answer: ~a~&" *run-number*
(send *answer-string* :pstring))
(format ostream2 " Temperature: ~a~&" *temperature*)
(format ostream2 " Codelets run: ~a~&" *codelet-count*)
(format ostream2 " Snags hit: ~a~&" *snag-count*)
(format ostream2 " Single-letter-groups: ~a~&"
*single-letter-group-count*)
(format ostream2 " Single-letter-groups at end: ~a~&"
*single-letter-group-at-end-count*)
(format ostream2 " Length descriptions made: ~a~&"
*length-description-count*)
(format ostream2 " Length relevant at end?: ~a~&"
*length-relevant-at-end*)
(format ostream2 "------------------------------------~&")))
; Write out the random-state.
(if* (probe-file *random-state-file*)
then (with-open-file (istream *random-state-file* :direction :input)
(setq random-state-list
(endcons *random-state-this-run* (read istream))))
else (setq random-state-list (list *random-state-this-run*)))
(with-open-file (ostream *random-state-file* :direction :output
:if-does-not-exist :create)
(print random-state-list ostream))))
;---------------------------------------------
(defflavor answer-summary
(answer-string (frequency 0) (temperature-sum 0) (temperature-sqrs-sum 0)
(codelet-sum 0) (codelet-sqrs-sum 0)
; (snag-sum 0) (no-snag-runs 0)
; (single-letter-group-runs 0) (single-letter-groups-at-end-runs 0)
; (length-description-runs 0) (length-relevant-at-end-runs 0)
)
()
:gettable-instance-variables
:settable-instance-variables
:initable-instance-variables
)
;---------------------------------------------
(defun copystat-summary
(input-file &optional (directory "~/")
&aux data-file temp-data-file all-answers
current-answer-summary
answer-summary-list
answer-list hyphen-pos sorted-answer-summary-list
initial-string modified-string target-string new-string
summary-file-list summary-file verbose-summary-file
(overall-temperature-sum 0)
(overall-temperature-sqrs-sum 0)
(overall-codelet-sum 0) (overall-codelet-sqrs-sum 0))
(block nil
(setq data-file (string-append directory input-file ".data"))
(setq temp-data-file (string-append data-file ".tmp"))
(if* (null (probe-file data-file))
then (format t "Error: ~a: No such file.~&" data-file)
(return))
(run-program "cp" :arguments (list data-file temp-data-file))
; End list in temporary data-file.
(with-open-file
(ostream temp-data-file :direction :output :if-exists :append)
(format ostream ")"))
(with-open-file (istream temp-data-file :direction :input)
(setq answer-list (read istream)))
(delete-file temp-data-file)
(loop for answer in answer-list do
(if* (not (member (car answer) all-answers))
then (setq all-answers (endcons (car answer) all-answers))
(setq current-answer-summary
(make-instance 'answer-summary
:answer-string (car answer)))
(push current-answer-summary answer-summary-list)
else (setq current-answer-summary
(loop for answer-summary in answer-summary-list
when (eq (car answer)
(send answer-summary :answer-string))
return answer-summary)))
(send current-answer-summary :set-frequency
(1+ (send current-answer-summary :frequency)))
(send current-answer-summary :set-temperature-sum
(+ (send current-answer-summary :temperature-sum)
(nth 1 answer)))
(send current-answer-summary :set-temperature-sqrs-sum
(+ (send current-answer-summary :temperature-sqrs-sum)
(sqr (nth 1 answer))))
(incf overall-temperature-sum (nth 1 answer))
(incf overall-temperature-sqrs-sum (sqr (nth 1 answer)))
(send current-answer-summary :set-codelet-sum
(+ (send current-answer-summary :codelet-sum) (nth 2 answer)))
(send current-answer-summary :set-codelet-sqrs-sum
(+ (send current-answer-summary :codelet-sqrs-sum)
(sqr (nth 2 answer))))
(incf overall-codelet-sum (nth 2 answer))
(incf overall-codelet-sqrs-sum (sqr (nth 2 answer))))
; Now sort the answer-summary-list by frequency.
(setq sorted-answer-summary-list
(sort answer-summary-list #'> :key '(lambda (x) (send x :frequency))))
; Now get initial-string, modified-string, and target-string.
(setq hyphen-pos (position '#\- input-file))
(setq initial-string (subseq input-file 0 hyphen-pos))
(setq new-string (subseq input-file (1+ hyphen-pos)))
(setq hyphen-pos (position '#\- new-string))
(setq modified-string (subseq new-string 0 hyphen-pos))
(setq target-string (subseq new-string (1+ hyphen-pos)))
(setq summary-file (string-append directory input-file ".summary"))
(setq verbose-summary-file
(string-append directory input-file ".verbose-summary"))
(if* (probe-file summary-file) then (delete-file summary-file))
(if* (probe-file verbose-summary-file)
then (delete-file verbose-summary-file))
; Make the summary-file list.
(setq summary-file-list
(list (format nil "Problem: ~a --> ~a, ~a --> ?~&"
(string-downcase initial-string)
(string-downcase modified-string)
(string-downcase target-string))
(list-sum (send-method-to-list answer-summary-list :frequency))
; overall temp mean
(round (/ overall-temperature-sum (length answer-list)))
(if* (= (length answer-list) 1) ; overall temp std err
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- overall-temperature-sqrs-sum
(/ (sqr overall-temperature-sum)
(length answer-list))))
(1- (length answer-list))))
(sqrt (length answer-list))) 1))
; overall codelet mean
(round (/ overall-codelet-sum (length answer-list)))
(if* (= (length answer-list) 1) ; overall codelet std-err
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- overall-codelet-sqrs-sum
(/ (sqr overall-codelet-sum)
(length answer-list))))
(1- (length answer-list))))
(sqrt (length answer-list))) 1))
; Stats for individual answers
(loop for a in sorted-answer-summary-list
collect
(list (string-downcase (send a :answer-string))
(send a :frequency)
; Temperature mean
(round (/ (send a :temperature-sum)
(send a :frequency)))
; Temperature standard error
(if* (= (send a :frequency) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- (send a :temperature-sqrs-sum)
(/ (sqr (send a :temperature-sum))
(send a :frequency))))
(1- (send a :frequency))))
(sqrt (send a :frequency))) 1))
; Codelet mean
(round (/ (send a :codelet-sum)
(send a :frequency)))
; Codelet standard error
(if* (= (send a :frequency) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- (send a :codelet-sqrs-sum)
(/ (sqr (send a :codelet-sum))
(send a :frequency))))
(1- (send a :frequency))))
(sqrt (send a :frequency))) 1))))))
; Make the summary file
(with-open-file (ostream summary-file :direction :output
:if-exists :append :if-does-not-exist :create)
(print summary-file-list ostream))
; Make the verbose summary file.
(with-open-file (verbose-ostream verbose-summary-file :direction :output
:if-exists :append :if-does-not-exist :create)
(format verbose-ostream "Problem: ~a --> ~a, ~a --> ?~&"
(string-downcase initial-string)
(string-downcase modified-string)
(string-downcase target-string))
(format verbose-ostream "Number of runs: ~a~&"
(list-sum (send-method-to-list answer-summary-list :frequency)))
(format verbose-ostream "Overall temperature average: ~5,2f~&"
(round (/ overall-temperature-sum (length answer-list))))
(format verbose-ostream "Overall temperature average standard error: ~5,2f~&"
(if* (= (length answer-list) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- overall-temperature-sqrs-sum
(/ (sqr overall-temperature-sum)
(length answer-list))))
(1- (length answer-list))))
(sqrt (length answer-list))) 1)))
(format verbose-ostream "Overall codelet average: ~5,2f~&"
(round (/ overall-codelet-sum (length answer-list))))
(format verbose-ostream "Overall codelet average standard error: ~5,2f~&"
(if* (= (length answer-list) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- overall-codelet-sqrs-sum
(/ (sqr overall-codelet-sum)
(length answer-list))))
(1- (length answer-list))))
(sqrt (length answer-list))) 1)))
(format verbose-ostream "------------------------------------~&~%")
(loop for a in sorted-answer-summary-list do
(format verbose-ostream "Answer: ~a~&"
(string-downcase (send a :answer-string)))
(format verbose-ostream "Frequency: ~a~&"
(send a :frequency))
(format verbose-ostream "Average temperature: ~a~&~%"
(round (/ (send a :temperature-sum)
(send a :frequency))))
(format verbose-ostream "Average temperature standard error: ~a~&~%"
(if* (= (send a :frequency) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- (send a :temperature-sqrs-sum)
(/ (sqr (send a :temperature-sum))
(send a :frequency))))
(1- (send a :frequency))))
(sqrt (send a :frequency))) 1)))
(format verbose-ostream "Average number of codelets: ~a~&"
(round (/ (send a :codelet-sum) (send a :frequency))))
(format verbose-ostream "Average number of codelets standard error: ~a~&"
(if* (= (send a :frequency) 1)
then 0.0
else (reduce-decimal
(/ (sqrt (/ (abs (- (send a :codelet-sqrs-sum)
(/ (sqr (send a :codelet-sum))
(send a :frequency))))
(1- (send a :frequency))))
(sqrt (send a :frequency))) 1)))
(format verbose-ostream "------------------------------------~&~%")))))
;---------------------------------------------