remove extraneous

main
Matthew Butterick 10 years ago
parent d0516947bf
commit b53201480b

@ -28,19 +28,14 @@
(field [_forwardcheck forwardcheck]) (field [_forwardcheck forwardcheck])
(define/override (get-solution-iter domains constraints vconstraints) (define/override (get-solution-iter domains constraints vconstraints)
(define forwardcheck _forwardcheck)
(define assignments (make-hash)) (define assignments (make-hash))
(define queue null) (define queue null)
(define variable #f)
(define values null) (define values null)
(define pushdomains null) (define pushdomains null)
(define variable #f) (define work-list #f)
(define work-list null) (let/ec return-k
(define want-to-return #f) (let main-loop ()
(define return-k #f)
(let/ec break-loop1
(set! return-k break-loop1)
(let loop1 ()
;; 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 +48,7 @@
(set! variable last-item) (set! variable last-item)
(set! values ((hash-ref domains variable))) (set! values ((hash-ref domains variable)))
(set! pushdomains (set! pushdomains
(if forwardcheck (if _forwardcheck
(for/list ([x (in-hash-keys domains)] (for/list ([x (in-hash-keys domains)]
#:when (and (not (hash-has-key? assignments x)) #:when (and (not (hash-has-key? assignments x))
(not (equal? variable x)))) (not (equal? variable x))))
@ -69,8 +64,7 @@
[(not (null? queue)) [(not (null? queue))
(set!-values (variable values pushdomains) (pop-vvp-values! queue)) (set!-values (variable values pushdomains) (pop-vvp-values! queue))
(for-each-send pop-state pushdomains)] (for-each-send pop-state pushdomains)]
[else [else (return-k)]))
(set! want-to-return #t) (return-k)]))
(let constraint-checking-loop () (let constraint-checking-loop ()
;; We have a variable. Do we have any values left? ;; We have a variable. Do we have any values left?
@ -78,7 +72,7 @@
;; No. Go back to last variable, if there is one, otherwise solver is done. ;; No. Go back to last variable, if there is one, otherwise solver is done.
(for/or ([i (in-naturals)]) (for/or ([i (in-naturals)])
(hash-remove! assignments variable) (hash-remove! assignments variable)
(when (null? queue) (set! want-to-return #t) (return-k)) (when (null? queue) (return-k))
(set!-values (variable values pushdomains) (pop-vvp-values! queue)) (set!-values (variable values pushdomains) (pop-vvp-values! queue))
(for-each-send pop-state pushdomains) (for-each-send pop-state pushdomains)
(not (null? values)))) (not (null? values))))
@ -88,18 +82,18 @@
(for-each-send push-state pushdomains) (for-each-send push-state pushdomains)
(when (for/or ([cvpair (in-list (hash-ref vconstraints variable))]) (when (for/or ([cvpair (in-list (hash-ref vconstraints variable))])
(match-define (list constraint variables) cvpair) (match-define (list constraint variables) cvpair)
(not (send constraint is-true? variables domains assignments pushdomains))) (not (send constraint broken? variables domains assignments pushdomains)))
;; constraint failed, so try again ;; constraint failed, so try again
(for-each-send pop-state pushdomains) (for-each-send pop-state pushdomains)
(constraint-checking-loop))) (constraint-checking-loop)))
;; 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))
(loop1))) (main-loop))
(error 'get-solution-iter "Should never get here"))
(if want-to-return (void))
(void)
(error 'get-solution-iter "Whoops, broken solver")))
(define (call-solution-generator domains constraints vconstraints #:first-only [first-only #f]) (define (call-solution-generator domains constraints vconstraints #:first-only [first-only #f])

Loading…
Cancel
Save