-
Notifications
You must be signed in to change notification settings - Fork 22
/
software-evolution-library.lisp
1295 lines (1139 loc) · 51.2 KB
/
software-evolution-library.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
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
;;; software-evolution-library.lisp --- Extant Software Evolution
(defpackage :software-evolution-library/software-evolution-library
(:nicknames :sel :software-evolution-library)
(:use
:gt/full
:software-evolution-library/utility/git)
(:import-from :atomics :atomic-incf)
(:export
:+software-evolution-library-dir+
:+software-evolution-library-major-version+
:+software-evolution-library-version+
:+software-evolution-library-branch+
;; software objects
:oid-object
:software
:define-software
:edits
:fitness
:fitness-extra-data
:mutation-stats
:*mutation-improvements*
:*crossover-stats*
:genome
:phenome
:interpreted-phenome
:phenome-p
:ignore-phenome-errors
:return-nil-for-bin
:retry-project-build
:evaluate
:copy
:lines
:line-breaks
:genome-string
:pick
:pick-good
:pick-bad
:mutation-targets
:mutate
:no-mutation-targets
:pick-mutation-type
:create-super
:*mutation-stats*
:build-op
:apply-mutation-ops
:apply-mutation
:apply-mutations
:apply-all-mutations
:apply-picked-mutations
:text
:*mutation-stats*
:*crossover-stats*
:analyze-mutation
:mutation-key
:summarize-mutation-stats
:classify
:crossover
:one-point-crossover
:two-point-crossover
:from-file
:from-file-exactly
:from-string
:to-file
:apply-path
:define-mutation
:compose-mutations
:sequence-mutations
:mutation
:object
:targeter
:picker
:targets
:get-targets
:at-targets
:pick-bad-good
:pick-bad-bad
:pick-bad-only
;; global variables
:*population*
:*generations*
:*max-population-size*
:*tournament-size*
:*tournament-eviction-size*
:*fitness-predicate*
:fitness-better-p
:fitness-equal-p
:*cross-chance*
:*mut-rate*
:*elitism*
:*fitness-evals*
:*running*
:*start-time*
:elapsed-time
:*target-fitness-p*
:*worst-fitness*
:*worst-fitness-p*
;; evolution functions
:incorporate
:evict
:default-select-best
:default-random-winner
:*tournament-selector*
:*tournament-tie-breaker*
:*tie-breaker-predicate*
:tournament
:mutant
:crossed
:new-individual
:mcmc
:mcmc-step
:*mcmc-fodder*
:evolve
:generational-evolve
:simple-reproduce
:simple-evaluate
:simple-select
:worst-numeric-fitness
:worst-numeric-fitness-p
:*fitness-scalar-fn*
:fitness-scalar
:ignore-failed-mutation
:try-another-mutation
;; logging functions
:log-new-individuals
:log-message))
(in-package :software-evolution-library/software-evolution-library)
(in-readtable :curry-compose-reader-macros)
;;; Software Object
(defvar +software-evolution-library-dir+
(pathname-directory
#.(or *compile-file-truename*
*load-truename*
*default-pathname-defaults*))
"Path to directory holding SOFTWARE-EVOLUTION-LIBRARY.")
(define-constant +software-evolution-library-major-version+ "v0.1.0"
:test #'equal
:documentation
"Current major version of the SOFTWARE-EVOLUTION-LIBRARY (without git hash).")
(defvar +software-evolution-library-version+
(eval-when (:compile-toplevel :load-toplevel :execute)
(concatenate 'string +software-evolution-library-major-version+ "-"
(handler-case
(current-git-commit +software-evolution-library-dir+)
(git-error (e) (declare (ignorable e)) "UNKNOWN"))))
"Current version of the SOFTWARE-EVOLUTION-LIBRARY.")
(defvar +software-evolution-library-branch+
(eval-when (:compile-toplevel :load-toplevel :execute)
(handler-case
(current-git-branch +software-evolution-library-dir+)
(git-error (e) (declare (ignorable e)) "UNKNOWN")))
"Current branch of the SOFTWARE-EVOLUTION-LIBRARY.")
;;; oid-counter must be a cons for portable atomic-incf.
(let ((oid-counter (list 0)))
(defun generate-oid ()
"Create a fresh, unique oid (object id) in range [1 ...]"
(atomic-incf (car oid-counter))))
(defclass oid-object (standard-object)
((oid :reader oid :initform (generate-oid)))
(:documentation
"Attaches a unique oid (object identifier) to each instance."))
(defclass software (oid-object)
((fitness :initarg :fitness :accessor fitness :initform nil))
(:documentation "Base class for all software objects."))
(defmacro define-software
(name direct-superclasses direct-slots &rest options)
"Define a new `software' class NAME including a deep `copy' method.
Arguments NAME DIRECT-SUPERCLASSES and OPTIONS are passed through to
`defclass' unmodified. Additional optional :COPIER property on each
slot in DIRECT-SLOTS may be one of the following:
* :NONE this slot is not copied and will be nil in the new object
* :DIRECT this slot is copied by direct reference to the slot value
skipping the accessor
* otherwise if the value of :COPIER is nil (default) then the slot is
copied through its accessor, otherwise the value is
assumed to be a function (e.g., `copy-tree') which is used
to copy the slot."
;; Ensure a child of software.
`(progn
;; Define the class
(defclass ,name ,(if (member 'software direct-superclasses)
direct-superclasses
`(,@direct-superclasses software))
,(mapcar «cons #'car [{plist-drop :copier} #'cdr]» direct-slots)
,@options)
;; Define the copy method
,(unless (null direct-slots)
(let ((direct-slot-names (mapcar #'car direct-slots)))
`(defmethod copy :around
((obj ,name)
&key ,@(mapcar (lambda (name)
`(,name nil ,(symbol-cat name 'supplied-p)))
direct-slot-names)
&allow-other-keys)
(let ((copy (call-next-method)))
,@(mapcar
(lambda (name keyword-supplied-p copier)
`(if ,keyword-supplied-p
(setf (slot-value copy ',name) ,name)
,(case copier
(:none nil)
(:direct
`(setf (slot-value copy ',name)
(with-slots (,name) obj
,name)))
(otherwise
`(setf (slot-value copy ',name)
,(if copier
`(funcall ,(if (symbolp copier)
`',copier
copier)
(,name obj))
`(,name obj)))))))
direct-slot-names
(mapcar {symbol-cat _ 'supplied-p} direct-slot-names)
(mapcar [{plist-get :copier} #'cdr] direct-slots))
copy))))
(find-class ',name)))
(defgeneric genome (software)
(:documentation
"The software genotype or ``code'', exposed as a simplified data structure.
For example an AST genome, e.g., of a `cil' or `lisp' software object
my have a tree structure while the genome of an `asm' or `llvm'
software object will be a vector."))
(defclass interpreted-phenome ()
()
(:documentation "Mixin for an interpreted phenome."))
(defgeneric phenome (software &key bin)
(:documentation
"Phenotype of the software.
This method will link, compile or serialize the software object as
necessary returning an executable version of the software suitable for
testing and evaluation. Returns multiple values holding in order; (1)
the binary path to which the executable was compiled, (2) the errno,
or a numeric indication of success, of the compilation process, (3)
STDERR of the compilation process, or a string holding error output
relevant to phenome generation, (4) STDOUT of the compilation process,
or a string holding non-error output relevant to phenome
generation, (5) the source file name used during compilation. ")
(:method ((software interpreted-phenome) &key bin)
(interpreted-phenome software bin)))
(defun interpreted-phenome (obj bin)
"Create a phenotype of the interpreted software OBJ."
(to-file obj bin)
(values bin 0 nil nil nil))
(defgeneric phenome-p (software)
(:documentation "Return non-nil if SOFTWARE has a phenotype."))
(defmacro ignore-phenome-errors (&rest body)
"Handle errors in `phenome' execution by returning nil as the
first value from the `phenome' method."
`(handler-bind ((phenome
(lambda (c)
(declare (ignorable c))
(invoke-restart 'return-nil-for-bin))))
(progn ,@body)))
(defmethod phenome-p ((obj software))
(ignore-phenome-errors
(with-temporary-file (:pathname bin)
(phenome obj :bin bin))))
(defgeneric evaluate (function software &rest extra-keys &key &allow-other-keys)
(:documentation "Evaluate the software returning a numerical fitness."))
(defmethod evaluate ((test symbol) (obj software)
&rest extra-keys &key &allow-other-keys)
(declare (ignorable extra-keys))
(evaluate (symbol-function (or test 'identity)) obj))
(defmethod evaluate ((test function) (obj software)
&rest extra-keys &key &allow-other-keys)
(declare (ignorable extra-keys))
(if (fitness obj)
(values (fitness obj) (fitness-extra-data obj))
(multiple-value-bind (fit extra) (funcall test obj)
(setf (fitness obj) fit)
(setf (fitness-extra-data obj) extra)
(values fit extra))))
(defgeneric fitness-extra-data (software)
(:documentation "Hold extra data returned by the fitness function."))
(defmethod fitness-extra-data ((obj software)) nil)
(defgeneric (setf fitness-extra-data) (extra-data software)
(:documentation "Pass extra data (optionally) returned by the fitness function
to the software object."))
(defmethod (setf fitness-extra-data) (extra-data (obj software))
(declare (ignorable extra-data)))
(defmethod copy ((obj software) &key (fitness (fitness obj)))
(make-instance (class-of obj) :fitness fitness))
(defmethod size ((software software))
"Return the size of the `genome' of SOFTWARE."
(size (genome software)))
(defgeneric genome-string (software &optional stream)
(:documentation "Return a string of the `genome' of SOFTWARE."))
(defgeneric pick (software key &optional func)
(:documentation "Pick an element of GENOME based on KEY of each element.
KEY is passed to `proportional-pick' to return an index. Optional
argument FUNC processes the index to return a result."))
(defmethod pick ((sw software) key &optional func)
(let ((pick (proportional-pick (genome sw) key)))
(if func (funcall func pick) pick)))
(defgeneric pick-good (software)
(:documentation "Pick a 'good' index into a software object.
Used to target mutation."))
(defmethod pick-good ((software software)) (random (size software)))
(defgeneric pick-bad (software)
(:documentation "Pick a 'bad' index into a software object.
Used to target mutation."))
(defmethod pick-bad ((software software)) (random (size software)))
(defgeneric pick-bad-good (software &key &allow-other-keys)
(:documentation "Pick a 'bad' and a 'good' indexes into a software object.
Used to target mutation."))
(defmethod pick-bad-good ((software software) &key)
(list (pick-bad software) (pick-good software)))
(defgeneric pick-bad-bad (software &key &allow-other-keys)
(:documentation "Pick two 'bad' indexes into a software object.
Used to target mutation.")
(:method ((software software) &key)
(list (pick-bad software) (pick-bad software))))
(defgeneric pick-bad-only (software &key &allow-other-keys)
(:documentation "Pick a single 'bad' index into a software object.
Used to target mutation."))
(defmethod pick-bad-only ((software software) &key)
(list (pick-bad software)))
(defgeneric mutate (software)
(:documentation "Mutate the software. May throw a `mutate' error."))
(defgeneric pick-mutation-type (software)
(:documentation "Select a type of mutation to apply to SOFTWARE."))
(defvar *mutation-stats* (make-hash-table
:test #'equal
#+sbcl :synchronized #+sbcl t
#+ccl :shared #+ccl :lock-free)
"Variable to hold mutation statistics.")
(defgeneric create-super (variant &optional rest-variants)
(:documentation
"Create a super-mutant populated with VARIANT . REST-VARIANTS"))
;;;
;;; Note that we can't method dispatch on the types in a list, so
;;; we dispatch on the first item of the list. This is a helper
;;; function to simplify the process of pulling out a variant to
;;; dispatch on. The list of variants should all contain the same
;;; type of software object, and presumably be related to each other
;;; such that they can be assigned to a common super-mutant.
;;;
(defun create-and-populate-super (variant-list)
"Create and populate a super-mutant with supplied list of variants."
(create-super (first variant-list) (rest variant-list)))
(defvar *max-saved-mutation-improvements* 24
"Maximum number of mutation improvements to hold on to.")
(defvar *mutation-improvements* nil
"List of recent mutation improvements cons'd with *fitness-evals*.")
(defvar *crossover-stats* (make-hash-table
:test #'equal
#+sbcl :synchronized #+sbcl t
#+ccl :shared #+ccl :lock-free)
"Variable to hold crossover statistics.")
(defvar *fitness-evals* 0
"Track the total number of fitness evaluations.")
(defvar *fitness-predicate* #'>
"Function to compare two fitness values to select which is preferred.")
(defun fitness-scalar (fitness)
(cond ((numberp fitness) fitness)
((or (listp fitness) (vectorp fitness))
(reduce #'+ fitness))
(:otherwise (error "Can't convert fitness ~a to a scalar"
fitness))))
(defvar *fitness-scalar-fn* #'fitness-scalar
"Function to convert fitness to a numeric value")
(defun worst-numeric-fitness ()
(cond ((equal #'< *fitness-predicate*) infinity)
((equal #'> *fitness-predicate*) 0)
(t (error "bad *fitness-predicate* ~a" *fitness-predicate*))))
(defvar *worst-fitness* (worst-numeric-fitness)
"Default worst fitness TODO.")
(defun worst-numeric-fitness-p (obj)
(= (fitness obj)
(worst-numeric-fitness)))
(defvar *worst-fitness-p* #'worst-numeric-fitness-p
"Predicate indicating whether an individual has the worst possible fitness.")
(defvar *target-fitness-p* nil
"Predicate indicating whether an individual has reached the target fitness.")
(defun fitness-better-p (fitness-a fitness-b)
"Check if FITNESS-A is strictly better than FITNESS-B."
(funcall *fitness-predicate*
(funcall *fitness-scalar-fn* fitness-a)
(funcall *fitness-scalar-fn* fitness-b)))
(defun fitness-equal-p (fitness-a fitness-b)
"Return true if FITNESS-A and FITNESS-B are equal"
(equalp fitness-a fitness-b))
(defun analyze-mutation (obj mutation-info test &aux result)
"Default function to collect statistics from an applied mutation.
This function will calculate the improvements to the fitness of SOFTWARE
as the result of crossover and mutation using `evaluate' and TEST.
Each crossover and mutation will be paired with one of the following tags;
:dead, :same, :worse, or :better. Calculated stats will be added to the
*crossover-stats* and *mutation-stats* variables for analysis."
;; Mutation info from new-individual
(destructuring-bind (mutation software-a cross-point-a
crossed software-b cross-point-b)
mutation-info
;; Evaluate software objects to ensure fitness
(when crossed (evaluate test crossed)) ; Evaluate for fitness
(when software-a (evaluate test software-a)) ; Safety - should have fitness
(when software-b (evaluate test software-b)) ; Safety - should have fitness
(when obj (evaluate test obj)) ; Safety - should have fitness
;; Add information on the mutation to `*mutation-stats*`.
(multiple-value-bind (effect fit old-fit)
(classify obj crossed)
(when (equal effect :better)
(push (cons (mutation-key crossed mutation)
*fitness-evals*)
*mutation-improvements*)
(when (length>= *mutation-improvements*
*max-saved-mutation-improvements*)
(setf *mutation-improvements*
(butlast *mutation-improvements*))))
(push (setf result (list effect *fitness-evals* fit old-fit))
(gethash (mutation-key crossed mutation) *mutation-stats*)))
;; Add information on the crossover to `*crossover-stats*`.
(when cross-point-a
(let ((effect (classify crossed software-a software-b)))
(push (list effect *fitness-evals*)
(gethash (mutation-key crossed mutation) *crossover-stats*))))
(values
obj mutation
software-a cross-point-a crossed software-b cross-point-b
(first result))))
(defun classify (new &rest old)
"Classify the fitness of NEW as :BETTER, :WORSE, :SAME, or :DEAD when
compared to OLD. NEW and OLD must have fitness populated."
(let ((fit (fitness new))
(old-fit (extremum (mapcar {fitness} old)
#'fitness-better-p)))
(values
(cond
((funcall *worst-fitness-p* new) :dead)
((and (not (fitness-better-p fit old-fit))
(not (fitness-better-p old-fit fit)))
:same)
((funcall (complement #'fitness-better-p) fit old-fit)
:worse)
((fitness-better-p fit old-fit)
:better))
fit old-fit)))
(defgeneric mutation-key (software mutation)
(:documentation "Key used to organize mutations in *mutation-stats*."))
(defmethod mutation-key ((obj software) mutation)
"DOCFIXME
* OBJ DOCFIXME
* MUTATION DOCFIXME
"
(declare (ignorable obj)) mutation)
(defun summarize-mutation-stats (&aux results)
"DOCFIXME
* RESULTS DOCFIXME
"
(maphash (lambda (key vals)
(mapc (lambda (result)
(if (aget result (aget (car key) results))
(incf (aget result (aget (car key) results)))
(setf (aget result (aget (car key) results)) 1)))
(mapcar #'first vals)))
*mutation-stats*)
results)
(defgeneric mcmc-step (software)
(:documentation "Change software in a way amenable to MCMC.
Specifically every step should be reversible, and the resulting walk
should be ergodic."))
(defvar *mcmc-fodder* nil
"Holds the genome elements which may be used by `mcmc-step'.
Should be initialized to a list of the unique possible genome
elements.")
(define-condition phenome (error)
((text :initarg :text :initform nil :reader text)
(obj :initarg :obj :initform nil :reader obj)
(loc :initarg :loc :initform nil :reader loc))
(:report (lambda (condition stream)
(format stream "Phenome error ~S on ~S~@[ in ~A~]."
(text condition) (obj condition) (loc condition))))
(:documentation "DOCFIXME"))
(define-condition mutate (error)
((text :initarg :text :initform nil :reader text)
(obj :initarg :obj :initform nil :reader obj)
(operation :initarg :operation :initform nil :reader operation))
(:report (lambda (condition stream)
(format stream "Mutation error, ~a, ~:[on~;~:*applying ~S to~] ~S"
(text condition) (operation condition) (obj condition))))
(:documentation
"Mutation errors are thrown when a mutation fails.
These may often be safely ignored. A common restart is
`ignore-failed-mutation'."))
(define-condition no-mutation-targets (mutate)
((text :initarg :text :initform nil :reader text)
(obj :initarg :obj :initform nil :reader obj)
(operation :initarg :operation :initform nil :reader operation))
(:report (lambda (condition stream)
(format stream "No targets error ~a ~:[on~;~:*applying ~S to~] ~S"
(text condition)
(operation condition)
(obj condition))))
(:documentation
"This is a particularly benign form of mutation error.
A common restart is `ignore-failed-mutation'."))
(defgeneric apply-mutation (software mutation)
(:documentation "Apply MUTATION to SOFTWARE, return the resulting software object.
Mutation application may destructively modify the software object, or it may return a
new instance with the mutation applied, and leave the original untouched. Any client
which calls apply-mutation should ensure that the result returned by apply-mutation is
captured, and should not make assumptions about the state of the original.
Example: (let ((mutated-software (apply-mutation (copy software) mutation)))
...
Define an :around method on this function to record mutations."))
(defgeneric apply-all-mutations (software mutation)
(:documentation "Apply MUTATION to every target in SOFTWARE.
Returns the resulting software objects. Returns a list of the applied
mutations as an optional second value."))
(defgeneric apply-mutations (software mutation n)
(:documentation "Apply MUTATION to the first N targets in SOFTWARE.
Returns the resulting software objects. Returns a list of the applied
mutations as an optional second value."))
(defgeneric apply-picked-mutations (software mutation n)
(:documentation "Apply MUTATION to N randomly selected targets in SOFTWARE.
Returns the resulting software objects. Returns a list of the applied
mutations as an optional second value."))
(defgeneric crossover (software-a software-b)
(:documentation "Crossover two software objects.
Define an :around method on this function to record crossovers."))
(defmethod crossover :around ((software-a software) (software-b software))
;; Mutation removes previously calculated fitness values.
(multiple-value-call (lambda (child &rest rest)
(setf (fitness child) nil)
(apply #'values child rest))
(call-next-method)))
(defgeneric one-point-crossover (software-a software-b)
(:documentation "Crossover at a single point."))
(defgeneric two-point-crossover (software-a software-b)
(:documentation "Crossover between two points."))
(defgeneric from-string (software string)
(:documentation "Initialize SOFTWARE with contents of STRING.")
(:method ((class symbol) string)
(from-string (make-instance class) string)))
(defgeneric from-file (software file)
(:documentation "Initialize SOFTWARE with contents of FILE.")
(:method ((class symbol) file)
(from-file (make-instance class) file)))
(defgeneric apply-config (software config-file)
(:documentation "Parse CONFIG-FILE and use to configure SOFTWARE."))
(defgeneric to-file (software file)
(:documentation "Write SOFTWARE to FILE."))
(defmethod to-file ((software software) file)
(string-to-file (genome-string software) file))
(defmethod to-file :before ((software software) file)
(declare (ignorable software))
(ensure-directories-exist (pathname-directory-pathname file)))
(defgeneric apply-path (software key PATH) ; TODO: is this used?
(:documentation "Apply the execution trace PATH behind KEY in SOFTWARE."))
(defgeneric expression (software what)
(:documentation "Return WHAT in SOFTWARE as a lisp expression."))
;;; Mutation object
(defmacro define-mutation (class-name superclasses slots &rest options)
"Like `defclass' but inherits TARGETER slot-options from MUTATION.
Also, ensures MUTATION is a member of superclasses"
`(defclass ,class-name ,(if (member 'mutation (cons class-name superclasses))
superclasses
(append superclasses (list 'mutation)))
((targeter
,@(plist-merge
(cdr (assoc 'targeter slots))
(list :initarg :targeter :reader 'targeter
:initform '(function pick-bad) :type 'function
:documentation "A function from software -> targets.")))
(picker
,@(plist-merge
(cdr (assoc 'picker slots))
(list :initarg :picker :reader 'picker
:initform '(compose #'random-elt #'pick-bad) :type 'function
:documentation "A function from software -> random target.")))
,@(remove-if {member _ '(targeter picker)} slots :key #'car))
,@options))
(defgeneric picker (x)
(:documentation "Reader for the PICKER slot of mutation objects"))
(defgeneric targeter (x)
(:documentation "Reader for the TARGETER slot of mutation objects"))
(defgeneric build-op (mutation software)
;; Returns a list of build-op objects
;; Each build-op object is a pair (<build-op-keyword> . <build-op-args>)
;; <build-op-args> is an alist of (<build-arg-keyword> . <build-arg-value>) pairs
(:documentation "Build operation on SOFTWARE from a MUTATION."))
(defmacro compose-mutations (class-name mutations &rest options)
"Define a new mutation named CLASS-NAME composing MUTATIONS.
MUTATIONS is a list of the names of mutation classes."
(with-gensyms ((args args)
(mut mut)
(software software)
(mutation mutation)
(target target))
(flet ((slot-initform (slot-name class)
(finalize-inheritance (find-class class))
(slot-definition-initform
(find-if [{eql slot-name} #'slot-definition-name]
(class-slots (find-class class))))))
`(prog1
(define-mutation ,class-name
,(remove-duplicates
(mappend
(lambda (obj)
(remove-if «or {eql 'standard-object} {eql 'mutation}»
(mapcar #'class-name
(class-direct-superclasses
(find-class obj)))))
mutations))
((targeter
:initform
(lambda (&rest ,args)
(list ,@(mapcar
(lambda (fun) `(apply ,fun ,args))
(mapcar {slot-initform 'targeter} mutations))))
:type function
:documentation
,(format nil "Targeters from ~a." mutations))
(picker
:initform
(lambda (targets)
(mapcar
(lambda (target picker) (funcall picker target))
targets
(list ,@(mapcar {slot-initform 'picker} mutations))))
:type function
:documentation
,(format nil "Pickers from ~a." mutations)))
;; NOTE: Should compose other slots as well.
,@options)
(defmethod build-op ((,mut ,class-name) ,software)
(mappend
(lambda (,mutation ,target)
(build-op (make-instance ,mutation :targets ,target)
,software))
',mutations
(targets ,mut)))))))
(defmacro sequence-mutations (class-name mut-a mut-b &rest options)
"Define a new mutation named CLASS-NAME sequencing MUT-A and MUT-B.
MUT-A and MUT-B are instances of mutations. Instead of collecting
targets for A and then targets for B and then applying A and B as done
by `compose-mutations', `sequence-mutations' first targets and applies A and then targets and applied B."
(declare (ignorable class-name mut-a mut-b options))
(error "TODO: Implement `sequence-mutations'."))
(defclass mutation (oid-object)
((object :initarg :object :accessor object :initform nil
:type (or software null)
:documentation "The software object to be mutated.")
(targets :initarg :targets :reader get-targets :initform nil
:documentation "A calculated target set."))
(:documentation "The base class of all software mutations."))
(defmethod print-object ((mut mutation) stream)
(print-unreadable-object (mut stream :type t)
(prin1 (object mut) stream)
(when (or (get-targets mut) (targeter mut))
(format stream " ")
(prin1 (or (get-targets mut)
(multiple-value-call [#'third #'list]
(function-lambda-expression (targeter mut)))) stream))))
(defgeneric targets (mutation)
(:documentation "Return all possible targets of MUTATION.")
(:method ((mut mutation))
(or (get-targets mut)
(when (object mut)
(restart-case
(setf (slot-value mut 'targets)
(funcall (targeter mut) (object mut)))
(ignore-failed-mutation ()
:report "Ignore failed mutation targeter and continue"
nil))))))
(defgeneric at-targets (mutation targets &key &allow-other-keys)
(:documentation "Return a copy of MUTATION with `targets' set to TARGETS."))
(defmethod at-targets ((mut mutation) targets &key (object (object mut)))
(make-instance (type-of mut) :object object :targets targets))
(defmethod mutation-key ((obj software) (mutation mutation))
(declare (ignorable obj)) (list (type-of mutation)))
(defmethod apply-mutation :before ((obj software) (mut mutation))
;; Mutation removes previously calculated fitness values.
(declare (ignorable mut))
(setf (fitness obj) nil))
(defmethod apply-all-mutations ((obj software) (mut mutation))
(apply-mutations obj mut (1- array-dimension-limit)))
(defmethod apply-mutations ((obj software) (mut mutation) n)
(setf (object mut) obj)
(iter (for targeted in (mapcar {at-targets mut} (targets mut)))
(for i below n)
(collect targeted into mutations)
(restart-case
(collect (apply-mutation (copy obj) targeted) into results)
(ignore-failed-mutation ()
:report "Ignore failed mutation application and continue"
(values nil nil)))
(finally (return (values results mutations)))))
(defmethod apply-picked-mutations ((obj software) (mut mutation) n)
(setf (object mut) obj)
(iter (for i below n)
(for picked = (funcall (picker mut) obj))
(while picked)
(let ((targeted (at-targets mut picked)))
(collect targeted into mutations)
(collect (apply-mutation (copy obj) targeted) into results))
(finally (return (values results mutations)))))
;;; Evolution
;;; TODO: *population* accesses are currently not thread-safe.
;;; A mutex should be created for it, and modifications should
;;; be wrapped in a with-mutex.
(defvar *population* nil
"Holds the variant programs to be evolved.
This variable may be read to inspect a running search process, or
written to as part of a running search process.")
(defvar *generations* 0
"Holds the running generation count.")
(defvar *max-population-size* nil
"Maximum allowable population size.")
(defvar *tournament-size* 2
"Number of individuals to participate in tournament selection.")
(defvar *tournament-eviction-size* 2
"Number of individuals to participate in eviction tournaments.")
(defvar *elitism* 0
"Number of individuals to automatically promote to next population.
Range: 0..(- (length *population*) 1)
When evolving super-mutants, or calling generational-evolve,
*ELITISM* specifies the number of individuals which are automatically
promoted prior to typical generational replacement or eviction
process. The selected individuals will be the ones with the
best fitness.
When using super-mutants, the *ELITISM* value will reduce the number of new
individuals created in each generation by the value of *ELITISM* (since this
number will automatically be promoted).")
(declaim (type (integer 0 *) *elitism*))
(defvar *cross-chance* 2/3
"Fraction of new individuals generated using crossover rather than mutation.")
(defvar *mut-rate* 1
"Chance to mutate a new individual.
* If <1, new individuals will be mutated once with change *MUT-RATE*.
* If =1, then every new individual will be mutated exactly once.
* If >1, then new individuals will be mutated from 1 to *MUT-RATE* times.")
(defvar *running* nil
"True when a search process is running, set to nil to stop evolution.")
(defvar *start-time* nil
"Holds the start time of evolutionary processes.")
(declaim (inline elapsed-time))
(defun elapsed-time () (/ (- (get-internal-real-time) *start-time*)
internal-time-units-per-second))
(defun incorporate (software)
"Incorporate SOFTWARE into POPULATION, keeping POPULATION size constant."
(push software *population*)
(loop :while (and *max-population-size*
(length> *population* *max-population-size*))
:do (evict)))
(defvar *tie-breaker-predicate* #'>
"Function to compare two tie breaker values to select which is preferred.")
(defun evict ()
(let ((loser (tournament :predicate (complement *fitness-predicate*)
:size *tournament-eviction-size*
:tie-breaker-predicate
(complement *tie-breaker-predicate*))))
(setf *population* (remove loser *population* :count 1))
loser))
(defun default-select-best (group &key (predicate *fitness-predicate*))
"Return the members of GROUP with most PREDICATE fitness.
Default selection function for `tournament'."
(remove-if-not [{equal (fitness (extremum group predicate :key #'fitness))}
#'fitness]
group))
(defun default-random-winner (group &key predicate)
"Choose a random winner from GROUP."
(declare (ignorable predicate))
(random-elt group))
(defvar *tournament-selector* #'default-select-best
"Function used to select winners of a tournament. Returns a list of
winners.")
(defvar *tournament-tie-breaker* #'default-random-winner
"Function used to break ties in a tournament. Returns a single winner.")
(defun tournament
(&key (predicate *fitness-predicate*)
(tie-breaker-predicate *tie-breaker-predicate*)
(size *tournament-size*))
"Select an individual from *POPULATION* with a tournament."
(flet ((verify (it)
(assert (typep it 'software) (it)
"Population member is not software object")
(assert (fitness it) (it)
"Population member with no fitness")
it))
(assert *population* (*population*) "Empty population.")
(funcall *tournament-tie-breaker*
(funcall *tournament-selector*
(iter (for i below size)
(collect (verify (random-elt *population*))))
:predicate predicate)
:predicate tie-breaker-predicate)))
(defun mutant (&optional (new (copy (tournament))))
"Generate a new mutant from a *POPULATION*."
(cond ((< *mut-rate* 1) (if (< (random 1.0) *mut-rate*) (mutate new) new))
((= *mut-rate* 1) (mutate new))
((> *mut-rate* 1) (dotimes (n (1+ (floor (random *mut-rate*))) new)
(mutate new)))))
(defun crossed (&optional (a (tournament)) (b (tournament)))
"Generate a new individual from *POPULATION* using crossover."
(if (< (random 1.0) *cross-chance*)
(crossover a b)
(values (copy a) nil nil)))
(defun new-individual (&optional (a (tournament)) (b (tournament)))
"Generate a new individual from *POPULATION*."
(multiple-value-bind (crossed a-point b-point) (crossed a b)
;; NOTE: This `copy' call is only needed for `analyze-mutation'.
;; If it appears to be adding significant overhead, consider two
;; alternate implementations of `new-individual' instead of the
;; current approach in which `analyze-mutate' "wraps"
;; `new-individual'.
(multiple-value-bind (mutant mutation) (mutant (copy crossed))
(values mutant
;; Mutation info for analyze-mutation
(list mutation a a-point crossed b b-point)))))
(defun new-individuals (count)
"Generate COUNT new individuals from *POPULATION*."
(labels ((safe-mutate ()
(restart-case
(new-individual)
(ignore-failed-mutation ()
:report "Ignore failed mutation and continue evolution"
(values nil nil))
(try-another-mutation ()
:report "Try another mutation"
(safe-mutate)))))
(iter (multiple-value-bind (variant mutation-info)
(safe-mutate)
(unless (and (null variant) (null mutation-info))
(log-new-individuals variant mutation-info)
(collect variant into variants)
(collect mutation-info into infos)))
(while (length< variants count))
(finally (return (values variants infos))))))
(defun validate-evolution-parameters ()
"Validate special variables that are used during the evolutionary loop."
(when *target-fitness-p*
(assert (functionp *target-fitness-p*) (*target-fitness-p*)
"`*target-fitness-p*' must be a function"))
(assert (typep *max-population-size* '(integer 0 *))
(*max-population-size*)
"*MAX-POPULATION-SIZE* should be an integer >= 0"))
(defun initialize-evolutionary-loop ()
"Initialize common special variables used by the evolutionary loop and
assert their validity."
;; NOTE: there are technically race conditions here, but they are likely
;; not much of an issue.
(unless *start-time*
(setf *start-time* (get-internal-real-time)))
(setf *running* t)
(validate-evolution-parameters))
(defun deinitialize-evolutionary-loop ()
"Deinitialize common special variables used by the evolutionary loop."
(setf *running* nil))
(defun continue-evolutionary-loop-p (&key max-time max-evals max-generations)
"Return T if all of the common conditions are met for continuing the
evolutionary loop:
- *running* is t.
- Maximum number of evaluations has not been reached.
- Maximum time has not been reached.
- Maximum generations has not been reached."
(labels ((below-maximum-p (current max)
"Return T if MAX exists and CURRENT is it."
(or (not max) (not current) (< current max))))
(and *running*
(below-maximum-p *fitness-evals* max-evals)
(below-maximum-p (elapsed-time) max-time)
(below-maximum-p *generations* max-generations))))
(defun remove-elite-individuals ()