You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/csp/solver.rkt

142 lines
5.6 KiB
Racket

10 years ago
#lang racket/base
(require racket/class sugar/container racket/list racket/generator racket/match "helper.rkt")
(provide (all-defined-out))
(define solver%
10 years ago
;; Abstract base class for solvers
(class object%
(super-new)
(abstract get-solution)
(abstract get-solutions)
(abstract get-solution-iter)))
10 years ago
(define solver%? (is-a?/c solver%))
10 years ago
10 years ago
(struct vvp (variable values pushdomains))
10 years ago
(define-syntax-rule (pop-vvp-values! vvps)
10 years ago
(if (null? vvps)
(error 'pop-vvp-values! (format "~a is null" vvps))
(let ([vvp (car vvps)])
(set! vvps (cdr vvps))
(values (vvp-variable vvp) (vvp-values vvp) (vvp-pushdomains vvp)))))
10 years ago
(define backtracking-solver%
10 years ago
;; Problem solver with backtracking capabilities
(class solver%
10 years ago
(super-new)
(init-field [forwardcheck #t])
(field [_forwardcheck forwardcheck])
(define/override (get-solution-iter domains constraints vconstraints)
10 years ago
(define forwardcheck _forwardcheck)
(define assignments (make-hash))
(define queue null)
(define values null)
(define pushdomains null)
(define variable #f)
10 years ago
(define work-list null)
10 years ago
(define want-to-return #f)
(define return-k #f)
(let/ec break-loop1
(set! return-k break-loop1)
(let loop1 ()
;; Mix the Degree and Minimum Remaing Values (MRV) heuristics
10 years ago
(set! work-list (sort (for/list ([variable (in-hash-keys domains)])
10 years ago
(list (* -1 (length (hash-ref vconstraints variable)))
(length ((hash-ref domains variable)))
variable)) list-comparator))
10 years ago
;(report lst)
(let/ec break-for-loop
10 years ago
(for ([last-item (in-list (map last work-list))]
#:when (not (hash-has-key? assignments last-item)))
; Found unassigned variable
(set! variable last-item)
(set! values ((hash-ref domains variable)))
(set! pushdomains
(if forwardcheck
(for/list ([x (in-hash-keys domains)]
#:when (and (not (hash-has-key? assignments x))
(not (equal? variable x))))
(hash-ref domains x))
null))
(break-for-loop))
10 years ago
;; if it makes it through the loop without breaking, then there are
10 years ago
;; no unassigned variables. We've got a solution.
10 years ago
(yield (hash-copy assignments))
10 years ago
;; Return to previous variable in queue if possible, otherwise all done
(cond
[(not (null? queue))
(set!-values (variable values pushdomains) (pop-vvp-values! queue))
(for-each-send pop-state pushdomains)]
[else
(set! want-to-return #t) (return-k)]))
10 years ago
(let/ec break-loop2
(let loop2 ()
;; We have a variable. Do we have any values left?
10 years ago
(when (null? values)
10 years ago
;; No. Go back to last variable, if there's one.
(hash-remove! assignments variable)
10 years ago
(if #f
(for/or ([i (in-naturals)])
(when (null? queue) (set! want-to-return #t) (return-k))
(set!-values (variable values pushdomains) (pop-vvp-values! queue))
(for-each-send pop-state pushdomains)
(if (not (null? values))
#t
(hash-remove! assignments variable)))
10 years ago
(let/ec break-loop3
10 years ago
(let loop ()
(when (null? queue)
(set! want-to-return #t)
(return-k))
(set!-values (variable values pushdomains) (pop-vvp-values! queue))
(for-each-send pop-state pushdomains)
(when (not (null? values)) (break-loop3))
(hash-remove! assignments variable)
(loop)))
)
)
10 years ago
;; Got a value. Check it.
10 years ago
(hash-set! assignments variable (car-pop! values))
10 years ago
(for-each-send push-state pushdomains)
10 years ago
(let/ec break-for-loop
(for ([cvpair (in-list (hash-ref vconstraints variable))])
(match-define (list constraint variables) cvpair)
(define the_result (send constraint call variables domains assignments pushdomains))
10 years ago
(when (not the_result) ; Value is not good.
10 years ago
(break-for-loop)))
10 years ago
(break-loop2))
10 years ago
10 years ago
(for-each-send pop-state pushdomains)
10 years ago
(loop2)))
;; Push state before looking for next variable.
10 years ago
(set! queue (cons (vvp variable values pushdomains) queue))
10 years ago
(loop1)))
(if want-to-return
(void)
(error 'get-solution-iter "Whoops, broken solver")))
10 years ago
(define (call-solution-generator domains constraints vconstraints #:first-only [first-only #f])
(for/list ([solution (in-generator (get-solution-iter domains constraints vconstraints))] #:final first-only)
10 years ago
solution))
(define/override (get-solution . args)
10 years ago
(car (apply call-solution-generator #:first-only #t args)))
(define/override (get-solutions . args)
(apply call-solution-generator args))))
(define backtracking-solver%? (is-a?/c backtracking-solver%))