From c3dfa21d7dd002a23f26f250abea20fe6b95d28c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 16 Oct 2018 13:58:28 -0700 Subject: [PATCH] closer --- csp/aima.rkt | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/csp/aima.rkt b/csp/aima.rkt index c1ec2146..3b5c6b35 100644 --- a/csp/aima.rkt +++ b/csp/aima.rkt @@ -49,15 +49,15 @@ (define nchecks $csp-nchecks) (define/contract (check-constraint csp . varvals) - ($csp? variable? any/c variable? any/c . -> . any/c) - (define varval-hash (apply hasheq #R varvals)) + (($csp?) #:rest (listof any/c) . ->* . any/c) + (define varval-hash (apply hasheq varvals)) (define relevant-constraints (for/list ([constraint (in-list ($csp-constraints csp))] #:when (for/and ([cname (in-list ($constraint-names constraint))]) (memq cname (hash-keys varval-hash)))) constraint)) (begin0 - (for/and ([constraint (in-list #R relevant-constraints)]) + (for/and ([constraint (in-list relevant-constraints)]) (define vals (for/list ([cname (in-list ($constraint-names constraint))]) (hash-ref varval-hash cname))) (apply ($constraint-proc constraint) vals)) @@ -85,16 +85,22 @@ ($csp? assignment? . -> . boolean?) (= (length (hash-keys assignment)) (length ($csp-variables csp)))) + (define/contract (nconflicts csp var val assignment) ($csp? variable? any/c assignment? . -> . number?) ;; Return the number of conflicts var=val has with other variables.""" ;; Subclasses may implement this more efficiently (for/sum ([v (in-list (neighbors csp var))] #:when (assignment . assigns? . v)) - (if (check-constraint csp var val v (hash-ref assignment v)) + #;(define this (apply check-constraint csp (append (list var val) (flatten (for/list ([(k v) (in-hash assignment)] + #:unless (eq? var k)) + (list k v)))))) + (define that (check-constraint csp var val v (hash-ref assignment v))) + (if that 0 1))) + (define (display csp assignment) (displayln csp)) @@ -389,7 +395,7 @@ (require rackunit) -#;(begin +(begin (define vs '(wa nsw t q nt v sa)) (define vds (for/list ([k vs]) ($vd k '(red green blue))))