seating_chart.rkt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #lang racket/base
  2. (require racket/list)
  3. (require csp)
  4. (define seating-chart (make-csp))
  5. (define Npeople 6)
  6. ; return a series of seat variables from [start, end)
  7. (define (chairs-subset start end) (make-var-names
  8. "seat"
  9. (range start end)))
  10. ; return all chairs
  11. (define chairs
  12. (chairs-subset 0 Npeople))
  13. (add-vars! seating-chart
  14. chairs
  15. (range Npeople))
  16. ; constraint for people wanting to sit next to each other
  17. ; p1 wants to sit next to p2
  18. (define (person-next-to p1 p2 a b c)
  19. (if (= b p1)
  20. (or (= a p2)
  21. (= c p2))
  22. #false))
  23. ; constraint for people avoiding each other
  24. ; p1 does not want to sit next to p2
  25. (define (person-avoiding p1 p2 a b c)
  26. (if (= b p1)
  27. (and (not (= a p2))
  28. (not (= c p2)))
  29. #false))
  30. ; Constraints------------------------------------------------------------------
  31. ; # General constraints #######################################################
  32. ; 1. people can't occupy more than one seat
  33. (add-pairwise-constraint! seating-chart
  34. (lambda (a b) (not (= a b)))
  35. chairs)
  36. ; # Solution-specific constraints (for a given problem) ######################
  37. ; These should be replaced with inputs, for a general user-submitted query
  38. ; person 2 wants to sit in seat 1, next to person 4
  39. ; TODO: this misses the ends (assuming the table is not just a line). person 2 in seat 0 and person 4 in seat 5 doesn't match, nor does person 2 in seat 5 and person 4 in seat 0
  40. (add-constraint! seating-chart
  41. (lambda (a b c) (person-next-to 2 4 a b c))
  42. (chairs-subset 0 3))
  43. ; person 2 wants to sit next to person 4
  44. (define (adjacent-constraint person1 person2)
  45. (map (add-constraint! seating-chart)))
  46. ; person 2 wants to sit in seat 1, but doesn't want to sit next to person 5
  47. ; TODO: this misses the ends (assuming the table is not just a line). person 2 in seat 0 and person 5 in seat 5 would incorrectly pass, as would person 2 in seat 5 and person 5 in seat 0
  48. (add-constraint! seating-chart
  49. (lambda (a b c) (person-avoiding 2 5 a b c))
  50. (chairs-subset 0 3))
  51. (printf "Size of state space: ~a.~n"
  52. (state-count seating-chart))
  53. (time (solve* seating-chart 1))