refactory

main
Matthew Butterick 10 years ago
parent bad6780d66
commit b6503c03e9

@ -13,10 +13,10 @@
(define solver%? (is-a?/c solver%)) (define solver%? (is-a?/c solver%))
(struct vvp (variable values pushdomains)) (struct vvp (variable values pushdomains))
(define-syntax-rule (pop-vvp! vvps) (define-syntax-rule (pop-vvp-values! vvps)
(let ([vvp (car vvps)]) (let ([vvp (car vvps)])
(set! vvps (cdr vvps)) (set! vvps (cdr vvps))
vvp)) (values (vvp-variable vvp) (vvp-values vvp) (vvp-pushdomains vvp))))
(define backtracking-solver% (define backtracking-solver%
;; Problem solver with backtracking capabilities ;; Problem solver with backtracking capabilities
@ -39,9 +39,6 @@
(let/ec break-loop1 (let/ec break-loop1
(set! return-k break-loop1) (set! return-k break-loop1)
(let loop1 () (let loop1 ()
;(displayln "starting while loop 1")
;; Mix the Degree and Minimum Remaing Values (MRV) heuristics ;; Mix the Degree and Minimum Remaing Values (MRV) heuristics
(set! work-list (sort (for/list ([variable (in-hash-keys domains)]) (set! work-list (sort (for/list ([variable (in-hash-keys domains)])
(list (* -1 (length (hash-ref vconstraints variable))) (list (* -1 (length (hash-ref vconstraints variable)))
@ -53,7 +50,6 @@
#:when (not (hash-has-key? assignments last-item))) #:when (not (hash-has-key? assignments last-item)))
; Found unassigned variable ; Found unassigned variable
(set! variable last-item) (set! variable last-item)
;(report variable unassigned-variable)
(set! values ((hash-ref domains variable))) (set! values ((hash-ref domains variable)))
(set! pushdomains (set! pushdomains
(if forwardcheck (if forwardcheck
@ -71,33 +67,20 @@
(when (null? queue) (begin (when (null? queue) (begin
(set! want-to-return #t) (set! want-to-return #t)
(return-k))) (return-k)))
(define vvp (pop-vvp! queue)) (set!-values (variable values pushdomains) (pop-vvp-values! queue))
(set! variable (vvp-variable vvp))
(set! values (vvp-values vvp))
(set! pushdomains (vvp-pushdomains vvp))
(for-each-send pop-state pushdomains)) (for-each-send pop-state pushdomains))
;(report variable variable-preloop-2)
;(report assignments assignments-preloop-2)
(let/ec break-loop2 (let/ec break-loop2
(let loop2 () (let loop2 ()
;(displayln "starting while loop 2")
;; We have a variable. Do we have any values left? ;; We have a variable. Do we have any values left?
;(report values values-tested) (when (null? values)
(when (null? values)
;; No. Go back to last variable, if there's one. ;; No. Go back to last variable, if there's one.
(hash-remove! assignments variable) (hash-remove! assignments variable)
(let/ec break-loop3 (let/ec break-loop3
(let loop3 () (let loop3 ()
(if (not (null? queue)) (if (not (null? queue))
(let () (let ()
(define vvp (pop-vvp! queue)) (set!-values (variable values pushdomains) (pop-vvp-values! queue))
(set! variable (vvp-variable vvp))
(set! values (vvp-values vvp))
(set! pushdomains (vvp-pushdomains vvp))
(for-each-send pop-state pushdomains) (for-each-send pop-state pushdomains)
(when (not (null? values)) (break-loop3)) (when (not (null? values)) (break-loop3))
(hash-remove! assignments variable) (hash-remove! assignments variable)
@ -108,31 +91,20 @@
;; Got a value. Check it. ;; Got a value. Check it.
(hash-set! assignments variable (py-pop! values)) (hash-set! assignments variable (py-pop! values))
(for-each-send push-state pushdomains) (for-each-send push-state pushdomains)
;(report pushdomains pushdomains1)
;(report domains domains1)
(let/ec break-for-loop (let/ec break-for-loop
(for ([cvpair (in-list (hash-ref vconstraints variable))]) (for ([cvpair (in-list (hash-ref vconstraints variable))])
(match-define (list constraint variables) cvpair) (match-define (list constraint variables) cvpair)
(define the_result (send constraint call variables domains assignments pushdomains)) (define the_result (send constraint call variables domains assignments pushdomains))
;(report pushdomains pushdomains2) (when (not the_result) ; Value is not good.
;(report domains domains2)
;(report the_result)
(when (not the_result)
;; Value is not good.
(break-for-loop))) (break-for-loop)))
(begin ;(displayln "now breaking loop 2") (break-loop2))
(break-loop2)))
(for-each-send pop-state pushdomains) (for-each-send pop-state pushdomains)
(loop2))) (loop2)))
;; Push state before looking for next variable. ;; Push state before looking for next variable.
(set! queue (cons (vvp variable values pushdomains) queue)) (set! queue (cons (vvp variable values pushdomains) queue))
;(report queue new-queue)
(loop1))) (loop1)))
(if want-to-return (if want-to-return

Loading…
Cancel
Save