From d61a87258b24437da207e53c9cd78060c543517c Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 11 Oct 2018 16:58:50 -0700 Subject: [PATCH] notes --- csp/csp.rkt | 14 +++++++++----- csp/test.rkt | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/csp/csp.rkt b/csp/csp.rkt index d8108a77..1ddb3638 100644 --- a/csp/csp.rkt +++ b/csp/csp.rkt @@ -67,7 +67,7 @@ (for/or ([var (in-list ($csp-vars csp))]) (empty? ($var-vals var)))) -(struct $csp-inconsistent () #:transparent) +(struct inconsistency-error () #:transparent) (define/contract (apply-unary-constraint csp constraint) ($csp? unary-constraint? . -> . $csp?) @@ -75,13 +75,17 @@ (define new-csp ($csp (for/list ([var (in-list ($csp-vars csp))]) (match-define ($var name vals) var) (if (eq? name constraint-name) + ;; special rule: use promise for a constant value + ;; to skip the filtering ($var name (if (promise? proc) (force proc) (filter proc vals))) var)) ;; once the constraint is applied, it can go away + ;; ps this is not the same as an "assigned" constraint + ;; because the var may still have multiple values (remove constraint ($csp-constraints csp)))) - (when (no-solutions? new-csp) (raise ($csp-inconsistent))) + (when (no-solutions? new-csp) (raise (inconsistency-error))) new-csp) (define/contract (make-nodes-consistent csp) @@ -211,7 +215,7 @@ ([constraint (in-list ($csp-constraints csp))] #:when (and (constraint-has-name? constraint name) (constraint-assigned? csp constraint))) - (unless (constraint csp) (raise ($csp-inconsistent))) + (unless (constraint csp) (raise (inconsistency-error))) (remove-assigned-constraints csp))) (define/contract (backtracking-solver csp) @@ -223,7 +227,7 @@ [else ;; we have at least 1 unassigned var (match-define ($var name vals) (select-unassigned-var csp)) (for ([val (in-list (order-domain-values vals))]) - (with-handlers ([$csp-inconsistent? (const #f)]) + (with-handlers ([inconsistency-error? void]) (backtrack (infer (assign-val csp name val)))))])))) (define/contract (solve* csp [finish-proc values][solution-limit +inf.0]) @@ -232,7 +236,7 @@ (for/list ([solution (in-producer (backtracking-solver csp) (void))] [idx (in-range solution-limit)]) (finish-proc solution))) - (unless (pair? solutions) (raise ($csp-inconsistent))) + (unless (pair? solutions) (raise (inconsistency-error))) solutions) (define/contract (solve csp [finish-proc values]) diff --git a/csp/test.rkt b/csp/test.rkt index 114d585f..b2992f58 100644 --- a/csp/test.rkt +++ b/csp/test.rkt @@ -108,7 +108,7 @@ A collection of 33 coins, consisting of nickels, dimes, and quarters, has a valu |# (define xsum-problem (make-csp)) -(add-vars! xsum-problem '(l1 l2 l3 l4 r1 r2 r3 r4 x) (λ () (shuffle (range 1 10))) +(add-vars! xsum-problem '(l1 l2 l3 l4 r1 r2 r3 r4 x) (λ () (shuffle (range 1 10)))) (add-constraint! xsum-problem < '(l1 l2 l3 l4)) (add-constraint! xsum-problem < '(r1 r2 r3 r4)) (add-constraint! xsum-problem (λ (l1 l2 l3 l4 x) (= 27 (+ l1 l2 l3 l4 x))) '(l1 l2 l3 l4 x))