-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueens-Constraints.hs
58 lines (44 loc) · 1.45 KB
/
Queens-Constraints.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{-# LANGUAGE FlexibleContexts, ConstraintKinds #-}
import Constraints
import Text.Printf
type Position = Int
mkDom :: Int -> [Int]
mkDom n = [1..n]
type Pos = (Int,Int)
queensNet :: Int -> Net
queensNet n =
let nodes f = map f [1..n]
in Net
(nodes (\i -> var (show i) (mkDom n) ) )
(
allDiff (nodes show)
++ allDistinctPairs notDiag (nodes show)
)
allDistinctPairs :: (Pos -> Pos -> Bool) -> [String] -> [Constraint]
allDistinctPairs f xs =
let pairs g = [g a b | a <- xs, b <- xs, a /= b]
in pairs
(\x1 y1 ->
mkConstraint' x1
(\x2 y2 ->
f (read x1 :: Int,x2) (read y1 :: Int,y2)
) y1 (x1 ++ " notDiag " ++ y1)
)
notDiag :: Pos -> Pos -> Bool
notDiag (x1,x2) (y1,y2) = abs (x1 - y1) /= abs (x2 - y2)
mkConstraint' :: On Int
mkConstraint' = mkConstraint
allDiff :: [String] -> [Constraint]
allDiff ls = [f a b | a <- ls, b <- ls, a /= b]
where
f :: String -> String -> Constraint
f a b = mkConstraint' a (/=) b (a ++ " /= " ++ b)
main = do
let net = queensNet 7
(sols, acs, infs, log) =
solve net $ defaultConfig
-- print net
printf "%s" log
putStrLn "Solutions:"
mapM_ print sols
printf "%d ACs, %d inferences.\n" acs infs