main
Matthew Butterick 6 years ago
parent 19ca1054e4
commit d61a87258b

@ -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])

@ -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))

Loading…
Cancel
Save