-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtracking.lisp
executable file
·78 lines (69 loc) · 2.41 KB
/
backtracking.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
(in-package :cl-user)
#-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 backtracking-solver (combinatoric-solver)
()
)
(defmethod solve-it ((solver BACKTRACKING-SOLVER))
(let ((perm-array (generate-perm-array solver))
(index -1)
(limit (problem-size solver))
(stack-array (make-array (problem-size solver)))
(partial (generate-empty-solution solver))
)
#-gcl (setf (solution-tried solver) 0)
(unless (PARTIAL-SOLUTION-CORRECT solver partial)
(error "Empty solution not valid"))
(loop
#-gcl (incf (solution-tried solver))
#+clasp
(when (zerop (mod (solution-tried solver) 100))
(princ "."))
(cond ((PARTIAL-SOLUTION-CORRECT solver partial)
(incf index)
(when (= index limit)
;hurra
(return-from solve-it partial)
)
;extend the solution
(setf (svref stack-array index)(svref perm-array index))
(expand-partial-solution partial (first (svref stack-array index)) index))
(t
(loop
#+no (break "Backtracking")
(cond ((svref stack-array index)
(let ((new (pop (svref stack-array index))))
(change-partial-solution partial new index)
(return)))
(t
; no alternatives in current level, backtrack
(forget-partial-solution partial index)
(decf index)
(when (minusp index)
(break "Failed")))))
)))
)
)
(defmethod generate-perm-array ((solver BACKTRACKING-SOLVER))
(let ((array (make-array (problem-size solver)))
(index 0)
)
(dolist (domain (all-domains solver))
(setf (aref array index) (my-permutation solver domain))
(incf index))
array))
(defmethod my-permutation ((solver BACKTRACKING-SOLVER) domain)
(permutation domain))
(defclass RIDDLE-SOLVER (BACKTRACKING-SOLVER)
()
)