-
Notifications
You must be signed in to change notification settings - Fork 10
/
OvertonFD.hs
392 lines (340 loc) · 12.8 KB
/
OvertonFD.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
{-
- Origin:
- Constraint Programming in Haskell
- http://overtond.blogspot.com/2008/07/pre.html
- author: David Overton, Melbourne Australia
-
- Modifications:
- Monadic Constraint Programming
- http://www.cs.kuleuven.be/~toms/Haskell/
- Tom Schrijvers
-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Control.CP.FD.OvertonFD.OvertonFD (
OvertonFD,
fd_objective,
fd_domain,
FDVar,
OConstraint(..),
lookup,
) where
import Prelude hiding (lookup)
import Data.Maybe (fromJust,isJust)
import Control.Monad.State.Lazy
import Control.Monad.Trans
import qualified Data.Map as Map
import Data.Map ((!), Map)
import Control.Monad (liftM,(<=<))
import Control.CP.FD.OvertonFD.Domain as Domain
import Control.CP.FD.FD hiding ((!))
import Control.CP.Solver
import Control.CP.SearchTree
import Control.CP.EnumTerm
import Control.CP.Debug
--------------------------------------------------------------------------------
-- Solver instance -------------------------------------------------------------
--------------------------------------------------------------------------------
data OConstraint =
OHasValue FDVar Int
| OSame FDVar FDVar
| ODiff FDVar FDVar
| OLess FDVar FDVar
| OLessEq FDVar FDVar
| OAdd FDVar FDVar FDVar
| OSub FDVar FDVar FDVar
| OMult FDVar FDVar FDVar
| OAbs FDVar FDVar
deriving (Show,Eq)
instance Solver OvertonFD where
type Constraint OvertonFD = OConstraint
type Label OvertonFD = FDState
add c = debug ("addOverton("++(show c)++")") $ addOverton c
run p = debug ("runOverton") $ runOverton p
mark = get
goto = put
instance Term OvertonFD FDVar where
newvar = newVar ()
type Help OvertonFD FDVar = ()
help _ _ = ()
instance EnumTerm OvertonFD FDVar where
type TermBaseType OvertonFD FDVar = Int
getDomain = fd_domain
setValue var val = return [var `OHasValue` val]
--------------------------------------------------------------------------------
-- Constraints -----------------------------------------------------------------
--------------------------------------------------------------------------------
addOverton (OHasValue v i) = v `hasValue` i
addOverton (OSame a b) = a `same` b
addOverton (ODiff a b) = a `different` b
addOverton (OLess a b) = a .<. b
addOverton (OLessEq a b) = a .<=. b
addOverton (OAdd a b c) = addSum a b c
addOverton (OSub a b c) = addSub a b c
addOverton (OMult a b c) = addMult a b c
addOverton (OAbs a b) = addAbs a b
fd_domain :: FDVar -> OvertonFD [Int]
fd_domain v = do d <- lookup v
return $ elems d
fd_objective :: OvertonFD FDVar
fd_objective =
do s <- get
return $ objective s
--------------------------------------------------------------------------------
-- The FD monad
newtype OvertonFD a = OvertonFD { unFD :: State FDState a }
deriving (Monad, Applicative, Functor, MonadState FDState)
-- FD variables
newtype FDVar = FDVar { unFDVar :: Int } deriving (Ord, Eq, Show)
type VarSupply = FDVar
data VarInfo = VarInfo
{ delayedConstraints :: OvertonFD Bool, domain :: Domain }
instance Show VarInfo where
show x = show $ domain x
type VarMap = Map FDVar VarInfo
data FDState = FDState
{ varSupply :: VarSupply, varMap :: VarMap, objective :: FDVar }
deriving Show
instance Eq FDState where
s1 == s2 = f s1 == f s2
where f s = head $ elems $ domain $ varMap s ! (objective s)
instance Ord FDState where
compare s1 s2 = compare (f s1) (f s2)
where f s = head $ elems $ domain $ varMap s ! (objective s)
-- TOM: inconsistency is not observable within the OvertonFD monad
consistentFD :: OvertonFD Bool
consistentFD = return True
-- Run the FD monad and produce a lazy list of possible solutions.
runOverton :: OvertonFD a -> a
runOverton fd =
let j = evalState (unFD fd) initState
in j
initState :: FDState
initState = FDState { varSupply = FDVar 0, varMap = Map.empty, objective = FDVar 0 }
-- Get a new FDVar
newVar :: ToDomain a => a -> OvertonFD FDVar
newVar d = do
s <- get
let v = varSupply s
put $ s { varSupply = FDVar (unFDVar v + 1) }
modify $ \s ->
let vm = varMap s
vi = VarInfo {
delayedConstraints = return True,
domain = toDomain d}
in
s { varMap = Map.insert v vi vm }
return v
newVars :: ToDomain a => Int -> a -> OvertonFD [FDVar]
newVars n d = replicateM n (newVar d)
-- Lookup the current domain of a variable.
lookup :: FDVar -> OvertonFD Domain
lookup x = do
s <- get
return . domain $ varMap s ! x
-- Update the domain of a variable and fire all delayed constraints
-- associated with that variable.
update :: FDVar -> Domain -> OvertonFD Bool
update x i = do
debug (show x ++ " <- " ++ show i) (return ())
s <- get
let vm = varMap s
let vi = vm ! x
debug ("where old domain = " ++ show (domain vi)) (return ())
put $ s { varMap = Map.insert x (vi { domain = i}) vm }
delayedConstraints vi
-- Add a new constraint for a variable to the constraint store.
addConstraint :: FDVar -> OvertonFD Bool -> OvertonFD ()
addConstraint x constraint = do
s <- get
let vm = varMap s
let vi = vm ! x
let cs = delayedConstraints vi
put $ s { varMap =
Map.insert x (vi { delayedConstraints = do b <- cs
if b then constraint
else return False}) vm }
-- Useful helper function for adding binary constraints between FDVars.
type BinaryConstraint = FDVar -> FDVar -> OvertonFD Bool
addBinaryConstraint :: BinaryConstraint -> BinaryConstraint
addBinaryConstraint f x y = do
let constraint = f x y
b <- constraint
when b $ (do addConstraint x constraint
addConstraint y constraint)
return b
-- Constrain a variable to a particular value.
hasValue :: FDVar -> Int -> OvertonFD Bool
var `hasValue` val = do
vals <- lookup var
if val `member` vals
then do let i = singleton val
if (i /= vals)
then update var i
else return True
else return False
-- Constrain two variables to have the same value.
same :: FDVar -> FDVar -> OvertonFD Bool
same = addBinaryConstraint $ \x y -> do
debug "inside same" $ return ()
xv <- lookup x
yv <- lookup y
debug (show xv ++ " same " ++ show yv) $ return ()
let i = xv `intersection` yv
if not $ Domain.null i
then whenwhen (i /= xv) (i /= yv) (update x i) (update y i)
else return False
whenwhen c1 c2 a1 a2 =
if c1
then do b1 <- a1
if b1
then if c2
then a2
else return True
else return False
else if c2
then a2
else return True
-- Constrain two variables to have different values.
different :: FDVar -> FDVar -> OvertonFD Bool
different = addBinaryConstraint $ \x y -> do
xv <- lookup x
yv <- lookup y
if not (isSingleton xv) || not (isSingleton yv) || xv /= yv
then whenwhen (isSingleton xv && xv `isSubsetOf` yv)
(isSingleton yv && yv `isSubsetOf` xv)
(update y (yv `difference` xv))
(update x (xv `difference` yv))
else return False
-- Constrain one variable to have a value less than the value of another
-- variable.
infix 4 .<.
(.<.) :: FDVar -> FDVar -> OvertonFD Bool
(.<.) = addBinaryConstraint $ \x y -> do
xv <- lookup x
yv <- lookup y
let xv' = filterLessThan (findMax yv) xv
let yv' = filterGreaterThan (findMin xv) yv
if not $ Domain.null xv'
then if not $ Domain.null yv'
then whenwhen (xv /= xv') (yv /= yv') (update x xv') (update y yv')
else return False
else return False
-- Constrain one variable to have a value less than or equal to the value of another
-- variable.
infix 4 .<=.
(.<=.) :: FDVar -> FDVar -> OvertonFD Bool
(.<=.) = addBinaryConstraint $ \x y -> do
xv <- lookup x
yv <- lookup y
let xv' = filterLessThan (1 + findMax yv) xv
let yv' = filterGreaterThan ((findMin xv) - 1) yv
if not $ Domain.null xv'
then if not $ Domain.null yv'
then whenwhen (xv /= xv') (yv /= yv') (update x xv') (update y yv')
else return False
else return False
{-
-- Get all solutions for a constraint without actually updating the
-- constraint store.
solutions :: OvertonFD s a -> OvertonFD s [a]
solutions constraint = do
s <- get
return $ evalStateT (unFD constraint) s
-- Label variables using a depth-first left-to-right search.
labelling :: [FDVar s] -> OvertonFD s [Int]
labelling = mapM label where
label var = do
vals <- lookup var
val <- OvertonFD . lift $ elems vals
var `hasValue` val
return val
-}
dump :: [FDVar] -> OvertonFD [Domain]
dump = mapM lookup
-- Add constraint (z = x `op` y) for var z
addArithmeticConstraint ::
(Domain -> Domain -> Domain) ->
(Domain -> Domain -> Domain) ->
(Domain -> Domain -> Domain) ->
FDVar -> FDVar -> FDVar -> OvertonFD Bool
addArithmeticConstraint getZDomain getXDomain getYDomain x y z = do
xv <- lookup x
yv <- lookup y
let constraint z x y getDomain = do
xv <- lookup x
yv <- lookup y
zv <- lookup z
let znew = debug "binaryArith:intersection" $ (debug "binaryArith:zv" $ zv) `intersection` (debug "binaryArith:getDomain" $ getDomain xv yv)
debug ("binaryArith:" ++ show z ++ " before: " ++ show zv ++ show "; after: " ++ show znew) (return ())
if debug "binaryArith:null?" $ not $ Domain.null (debug "binaryArith:null?:znew" $ znew)
then if (znew /= zv)
then debug ("binaryArith:update") $ update z znew
else return True
else return False
let zConstraint = debug "binaryArith: zConstraint" $ constraint z x y getZDomain
xConstraint = debug "binaryArith: xConstraint" $ constraint x z y getXDomain
yConstraint = debug "binaryArith: yConstraint" $ constraint y z x getYDomain
debug ("addBinaryArith: z x") (return ())
addConstraint z xConstraint
debug ("addBinaryArith: z y") (return ())
addConstraint z yConstraint
debug ("addBinaryArith: x z") (return ())
addConstraint x zConstraint
debug ("addBinaryArith: x y") (return ())
addConstraint x yConstraint
debug ("addBinaryArith: y z") (return ())
addConstraint y zConstraint
debug ("addBinaryArith: y x") (return ())
addConstraint y xConstraint
debug ("addBinaryArith: done") (return ())
return True
-- Add constraint (z = op x) for var z
addUnaryArithmeticConstraint :: (Domain -> Domain) -> (Domain -> Domain) -> FDVar -> FDVar -> OvertonFD Bool
addUnaryArithmeticConstraint getZDomain getXDomain x z = do
xv <- lookup x
let constraint z x getDomain = do
xv <- lookup x
zv <- lookup z
let znew = zv `intersection` (getDomain xv)
debug ("unaryArith:" ++ show z ++ " before: " ++ show zv ++ show "; after: " ++ show znew) (return ())
if not $ Domain.null znew
then if (znew /= zv)
then update z znew
else return True
else return False
let zConstraint = constraint z x getZDomain
xConstraint = constraint x z getXDomain
addConstraint z xConstraint
addConstraint x zConstraint
return True
addSum = addArithmeticConstraint getDomainPlus getDomainMinus getDomainMinus
addSub = addArithmeticConstraint getDomainMinus getDomainPlus (flip getDomainMinus)
addMult = addArithmeticConstraint getDomainMult getDomainDiv getDomainDiv
addAbs = addUnaryArithmeticConstraint absDomain (\z -> mapDomain z (\i -> [i,-i]))
getDomainPlus :: Domain -> Domain -> Domain
getDomainPlus xs ys = toDomain (zl, zh) where
zl = findMin xs + findMin ys
zh = findMax xs + findMax ys
getDomainMinus :: Domain -> Domain -> Domain
getDomainMinus xs ys = toDomain (zl, zh) where
zl = findMin xs - findMax ys
zh = findMax xs - findMin ys
getDomainMult :: Domain -> Domain -> Domain
getDomainMult xs ys = (\d -> debug ("multDomain" ++ show d ++ "=" ++ show xs ++ "*" ++ show ys ) d) $ toDomain (zl, zh) where
zl = minimum products
zh = maximum products
products = [x * y |
x <- [findMin xs, findMax xs],
y <- [findMin ys, findMax ys]]
getDomainDiv :: Domain -> Domain -> Domain
getDomainDiv xs ys = toDomain (zl, zh) where
zl = minimum quotientsl
zh = maximum quotientsh
quotientsl = [if y /= 0 then x `div` y else minBound |
x <- [findMin xs, findMax xs],
y <- [findMin ys, findMax ys]]
quotientsh = [if y /= 0 then x `div` y else maxBound |
x <- [findMin xs, findMax xs],
y <- [findMin ys, findMax ys]]