From b5ceb26f8bff066520dc8a61279652c70aacb875 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 2 Oct 2014 19:10:26 -0700 Subject: [PATCH] check --- csp/constraint.rkt | 6 +++--- csp/domain.rkt | 8 ++++---- csp/problem.rkt | 19 ++++++++++--------- csp/solver.rkt | 8 ++++---- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/csp/constraint.rkt b/csp/constraint.rkt index ed64eb4b..2975965b 100644 --- a/csp/constraint.rkt +++ b/csp/constraint.rkt @@ -59,7 +59,7 @@ (for ([value (in-list (get-field _list domain))]) (hash-set! assignments unassignedvariable value) (when (not (send this call variables domains assignments)) - (send domain hideValue value))) + (send domain hide-value value))) (hash-remove! assignments unassignedvariable)) (when (null? (get-field _list domain)) (set! return-result #f) @@ -125,7 +125,7 @@ (set! domain (hash-ref domains variable)) (for ([value (in-hash-keys seen)]) (when (value . in? . (get-field _list (hash-ref domains variable))) - (send domain hideValue value) + (send domain hide-value value) (when (null? (get-field _list (hash-ref domains variable))) (set! return-value #f) (return-k))))))) @@ -166,7 +166,7 @@ (return-k)) (for ([value (in-list (get-field _list domain))]) (when (not (equal? value singlevalue)) - (send domain hideValue value)))))) + (send domain hide-value value)))))) (set! return-value #t) (return-k)) return-value))) diff --git a/csp/domain.rkt b/csp/domain.rkt index f86b9f7d..54a7c3d5 100644 --- a/csp/domain.rkt +++ b/csp/domain.rkt @@ -17,19 +17,19 @@ (define/public (custom-display out) (displayln (repr) out)) (define/public (custom-write out) (write (repr) out)) - (define/public (resetState) + (define/public (reset-state) ;; Reset to the original domain state, including all possible values (py-extend! _list _hidden) (set! _hidden null) (set! _states null)) - (define/public (pushState) + (define/public (push-state) ;; Save current domain state ;; Variables hidden after that call are restored when that state ;; is popped from the stack. (py-append! _states (length _list))) - (define/public (popState) + (define/public (pop-state) ;; Restore domain state from the top of the stack ;; Variables hidden since the last popped state are then available @@ -39,7 +39,7 @@ (py-extend! _list (take-right _hidden diff)) (set! _hidden (take _hidden (- (length _hidden) diff))))) - (define/public (hideValue value) + (define/public (hide-value value) ;; Hide the given value from the domain ;; After that call the given value won't be seen as a possible value diff --git a/csp/problem.rkt b/csp/problem.rkt index 37dc5f64..1aaac58e 100644 --- a/csp/problem.rkt +++ b/csp/problem.rkt @@ -110,18 +110,19 @@ (for ([(constraint variables) (in-parallel (map first constraints) (map second constraints))]) (for ([variable (in-list variables)]) - (hash-update! vconstraints variable (λ(val) (append val (list (list constraint variables))))))) - + (hash-update! vconstraints variable (λ(val) (cons (list constraint variables) val))))) + ;;(hash-update! vconstraints variable (λ(val) (append val (list (list constraint variables))))))) + (for ([(constraint variables) (in-parallel (map first constraints) (map second constraints))]) (send constraint preprocess variables variable-domains constraints vconstraints)) - - (define result #f) - (let/ec done - (for ([domain (in-list (hash-values variable-domains))]) - (send domain resetState) - (when (not domain) + + (define result (void)) + (let/ec break + (for/last ([domain (in-hash-values variable-domains)]) + (send domain reset-state) + (when (null? (get-field _list domain)) (set! result (list null null null)) - (done))) + (break))) (set! result (list variable-domains constraints vconstraints))) (apply values result)) diff --git a/csp/solver.rkt b/csp/solver.rkt index 357e2fb8..984354ef 100644 --- a/csp/solver.rkt +++ b/csp/solver.rkt @@ -71,7 +71,7 @@ (set-field! _list values (second variable-values-pushdomains)) (set! pushdomains (third variable-values-pushdomains)) (for ([domain (in-list pushdomains)]) - (send domain popState))) + (send domain pop-state))) ;(report variable variable-preloop-2) ;(report assignments assignments-preloop-2) @@ -96,7 +96,7 @@ (set! pushdomains (third variable-values-pushdomains)) (when (not (null? pushdomains)) (for ([domain (in-list pushdomains)]) - (send domain popState))) + (send domain pop-state))) (when (not (null? (get-field _list values))) (break-loop3)) (hash-remove! assignments variable) (loop3)) @@ -108,7 +108,7 @@ (hash-set! assignments variable (send values domain-pop!)) (for ([domain (in-list pushdomains)]) - (send domain pushState)) + (send domain push-state)) ;(report pushdomains pushdomains1) ;(report domains domains1) @@ -126,7 +126,7 @@ (break-loop2))) (for ([domain (in-list pushdomains)]) - (send domain popState)) + (send domain pop-state)) (loop2)))