when to unless, etc

main
Matthew Butterick 10 years ago
parent 02aae96912
commit 05a763b9b5

@ -31,7 +31,7 @@
(set-field! _list domain (set-field! _list domain
(for/fold ([domain-values (domain)]) (for/fold ([domain-values (domain)])
([value (in-list (domain))] ([value (in-list (domain))]
#:when (not (broken? variables domains (make-hash (list (cons variable value)))))) #:unless (broken? variables domains (make-hash (list (cons variable value)))))
(remove value domain-values))) (remove value domain-values)))
(set! constraints (remove (list this variables) constraints)) (set! constraints (remove (list this variables) constraints))
(hash-update! vconstraints variable (λ(val) (remove (list this variables) val))))) (hash-update! vconstraints variable (λ(val) (remove (list this variables) val)))))

@ -29,10 +29,11 @@
(field [_forwardcheck forwardcheck]) (field [_forwardcheck forwardcheck])
(define/override (get-solution-iter domains constraints vconstraints) (define/override (get-solution-iter domains constraints vconstraints)
(define work-list (sort (for/list ([variable (in-hash-keys domains)]) (define sorted-variables
(list (* -1 (length (hash-ref vconstraints variable))) (map last (sort (for/list ([(variable domain) (in-hash domains)])
(length ((hash-ref domains variable))) (list (- (length (hash-ref vconstraints variable))) ; first two elements used for sorting
variable)) list-comparator)) (length (domain))
variable)) list-comparator)))
;; state-retention variables ;; state-retention variables
(define possible-solution (make-hash)) (define possible-solution (make-hash))
(define variable-queue null) (define variable-queue null)
@ -41,14 +42,14 @@
(define pushdomains null) (define pushdomains null)
(define (get-next-unassigned-variable) (define (get-next-unassigned-variable)
(for/first ([last-item (in-list (map last work-list))] (for/first ([sorted-variable (in-list sorted-variables)]
#:when (not (hash-has-key? possible-solution last-item))) #:unless (hash-has-key? possible-solution sorted-variable))
(set! variable last-item) (set! variable sorted-variable)
(set! values ((hash-ref domains variable))) (set! values ((hash-ref domains variable)))
(set! pushdomains (set! pushdomains
(if _forwardcheck (if _forwardcheck
(for/list ([(var domain) (in-hash domains)] (for/list ([(var domain) (in-hash domains)]
#:when (nor (hash-has-key? possible-solution var) #:unless (and (hash-has-key? possible-solution var)
(equal? variable var))) (equal? variable var)))
domain) domain)
null)) null))

Loading…
Cancel
Save