-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmickey.lisp
executable file
·209 lines (175 loc) · 5.76 KB
/
mickey.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
(in-package :cl-user)
;;;Mickeys Riddle, see generic.lisp
;;;Mickey Mouse loves Gouda
;;;Mighty Mouse's favorite TV show is Emergency Room
;;;The mouse that lives in the left hole never misses an episode of Seinfeld
;;;Mickey Mouse and Mighty Mouse have one mouse hole between them
;;;The Simpsons fan does not live on the left of the Brie lover
;;;To get a solution, try (test-mickey)
;;;Run tests at least twice to get clos prepared
#-fast
(eval-when
#-:gcl (:compile-toplevel :execute :load-toplevel)
#+:gcl (compile eval load)
(proclaim '(optimize (speed 0) (safety 3) (space 0)(debug 3)(compilation-speed 0)))
)
#+fast
(eval-when
#-:gcl (:compile-toplevel :execute :load-toplevel)
#+:gcl (compile eval load)
(proclaim '(optimize (speed 3) (safety 0) (space 0)(debug 0)(compilation-speed 0)))
)
(defclass mickey-riddle-problem (SYMBOLIC-PROBLEM-SPECIFICATION)
()
)
(defmethod all-domains-extended ((me mickey-riddle-problem))
'((:name (:mickey :mighty :minny))
(:tv (:emergency-room :seinfield :simpsons))
(:cheese (:gouda :brie :emmental))
)
)
(defclass mickey-house (solution-element)
(
(name :accessor riddle-house-name :initform nil)
(tv :accessor riddle-house-tv :initform nil)
(cheese :accessor riddle-house-cheese :initform nil)
)
)
(defclass mickey-partial-solution (RIDDLE-PARTIAL-SOLUTION)
()
)
(defmethod read-value-for-key ((me mickey-house) key)
(ecase key
(:name (riddle-house-name me))
(:tv (riddle-house-tv me))
(:cheese (riddle-house-cheese me))))
(defmethod EXPAND-PARTIAL-SOLUTION ((me mickey-house)
index value)
(ecase index
(0 (setf (riddle-house-name me) value))
(1 (setf (riddle-house-tv me) value))
(2 (setf (riddle-house-cheese me) value))))
#-:gcl
(def-closures
+mickey-setf-mapper+
(list 0 #'(setf riddle-house-name)
1 #'(setf riddle-house-tv)
2 #'(setf riddle-house-cheese)
)
)
#+:gcl
(defparameter +mickey-setf-mapper+
(list 0 #'pcl::|SETF COMMON-LISP-USER RIDDLE-HOUSE-NAME|
1 #'pcl::|SETF COMMON-LISP-USER RIDDLE-HOUSE-TV|
2 #'pcl::|SETF COMMON-LISP-USER RIDDLE-HOUSE-CHEESE|
)
)
(defmethod element-mapper ((me mickey-partial-solution))
(error "element-mapper")
+mickey-setf-mapper+)
(def-closures
+mickey-property-mapper+
(list :name #'riddle-house-name
:tv #'riddle-house-tv
:cheese #'riddle-house-cheese))
(defmethod element-property-mapper ((me mickey-partial-solution))
(error "element-property-mapper")
+mickey-property-mapper+)
(defmethod partial-solution-class ((solver mickey-riddle-problem))
(find-class 'mickey-partial-solution))
(defmethod solution-element-class ((me mickey-riddle-problem))
(find-class 'mickey-house))
(defmethod show-house-result ((me mickey-house))
(format t "A house with ")
(format t "Name ~10a " (riddle-house-name me))
(format t "Soap ~10a " (riddle-house-tv me))
(format t "Cheese ~10a~%"(riddle-house-cheese me))
)
(defmethod initialize-instance :after ((me mickey-riddle-problem) &rest initargs)
(declare (ignore initargs))
(setf (my-constraints me)
(list
(make-instance 'TWO-VALUES-IN-HOUSE-CONSTRAINT
:SELECTOR-ONE :name
:value-one :mickey
:SELECTOR-two :cheese
:value-two :gouda)
(make-instance 'TWO-VALUES-IN-HOUSE-CONSTRAINT
:SELECTOR-ONE :name
:value-one :MIGHTY
:SELECTOR-two :tv
:value-two :emergency-room)
(make-instance 'POSITION-AND-PROPERTY-CONSTRAINT
:SELECTOR-ONE :tv
:VALUE-ONE :seinfield
:POSITION 0)
(make-instance 'ABS-DISTANCE-NEIGHBOUR-CONSTRAINT
:SELECTOR-ONE :NAME
:VALUE-ONE :MICKEY
:SELECTOR-TWO :NAME
:VALUE-TWO :MIGHTY
:DISTANCE 2)
(make-instance 'NEGATED-DIRECTED-DISTANCE-NEIGHBOUR-CONSTRAINT
:SELECTOR-ONE :tv
:VALUE-ONE :SIMPSONS
:SELECTOR-TWO :CHEESE
:VALUE-TWO :brie
:DISTANCE 1)
)))
(defclass MICKEY-RIDDLE-SOLVER (riddle-solver)
()
(:default-initargs
:specification (make-instance 'mickey-riddle-problem)))
;;; Test for Mickeys Riddle
(defun test-mickey (&optional (print t))
(test-backtracking
"Mickey Backtracking"
(make-instance
'MICKEY-RIDDLE-SOLVER
)
print)
)
#|
(test-mickey)
; cpu time (non-gc) 0 msec user, 0 msec system
; cpu time (gc) 0 msec user, 0 msec system
; cpu time (total) 0 msec user, 0 msec system
; real time 0 msec
; space allocation:
; 183 cons cells, 232 other bytes, 0 static bytes
The solution in 10 tries is:
A house with Name MICKEY Soap SEINFIELD Cheese GOUDA
A house with Name MINNY Soap SIMPSONS Cheese BRIE
A house with Name MIGHTY Soap EMERGENCY-ROOM Cheese EMMENTAL
|#
#|
(defparameter *solver* (make-instance 'mickey-riddle-solver))
(defparameter *test* (GENERATE-EMPTY-SOLUTION *solver*))
(PARTIAL-SOLUTION-CORRECT *solver* *test*)
|#
#|
(#(MICKEY GOUDA SEINFELD) #(MINNY BRIE SIMPSONS) #(MIGHTY EMMENTAL ER))
(defparameter *solver* (make-instance 'mickey-riddle-solver))
(defparameter *test* (GENERATE-EMPTY-SOLUTION *solver*))
(EXPAND-PARTIAL-SOLUTION *test* '(:mickey :MINNY :mighty) 0)
(PARTIAL-SOLUTION-CORRECT *solver* *test*)
(EXPAND-PARTIAL-SOLUTION *test* '(:SEINFIELD :SIMPSONS :EMERGENCY-ROOM) 1)
(PARTIAL-SOLUTION-CORRECT *solver* *test*)
(EXPAND-PARTIAL-SOLUTION *test* '(:gouda :brie :emmental) 2)
(PARTIAL-SOLUTION-CORRECT *solver* *test*)
|#
(defclass mickey-solver-gsat (gsat-riddle-solver)
()
(:default-initargs
:specification (make-instance 'MICKEY-RIDDLE-PROBLEM)))
(defun test-gsat-mickey (&optional (print t))
(TEST-GSAT
"Mickey Gsat"
(make-instance 'MICKEY-SOLVER-GSAT
:MAX-FLIPS 500
:max-tries 50)
print)
)
#|
(TEST-GSAT-MICKEY)
|#