Commit e34df728 authored by Michael Hanus 's avatar Michael Hanus
Browse files

Libraries updated and CLP.FD examples added

parent 1463e760
......@@ -19,7 +19,7 @@ MAJORVERSION=1
# The minor version number:
MINORVERSION=13
# The revision version number:
REVISIONVERSION=0
REVISIONVERSION=1
# The build version number:
BUILDVERSION=1
# Complete version:
......
PAKCS: Release Notes
====================
Release notes for PAKCS Version 1.13.1 (September 2, 2015)
----------------------------------------------------------
Changes to version 1.13.0:
* Library `CLP.FD` added: its functionality is similar to the old
library `CLPFD`, but the interface is different so that other
FD constraint solvers are easier to connect.
Release notes for PAKCS Version 1.13.0 (August 24, 2015)
--------------------------------------------------------
......
----------------------------------------------------------------------------
--- Computing magic series.
--- A series [a_0,a_1,....,a_(n-1)] is called magic iff there are
--- a_i occurrences of i in this series, for all i=1,...,n-1
---
--- Adapted from an example of the TOY(FD) distribution.
----------------------------------------------------------------------------
import CLP.FD
-- Compute a magic series of length n:
magic :: Int -> [Int]
magic n =
let vs = take n (domain 0 (n-1)) -- FD variables
is = map fd (take n [0..]) -- FD constants: indices of elements
in solveFD [FirstFail] vs $
constrain vs vs is /\
sum vs Equ (fd n) /\
scalarProduct is vs Equ (fd n)
constrain :: [FDExpr] -> [FDExpr] -> [FDExpr] -> FDConstr
constrain [] _ _ = true
constrain (x:xs) vs (i:is) = count i vs Equ x /\ constrain xs vs is
magicfrom :: Int -> [[Int]]
magicfrom n = magic n : magicfrom (n+1)
main = take 3 (magicfrom 7)
--> [[3,2,1,1,0,0,0],[4,2,1,0,1,0,0,0],[5,2,1,0,0,1,0,0,0]]
import CLP.FD
-- solving the n-queens problem in Curry with FD constraints:
queens :: [Option] -> Int -> [Int]
queens options n =
let qs = take n (domain 1 n)
in solveFD options qs (allSafe qs)
allSafe :: [FDExpr] -> FDConstr
allSafe [] = true
allSafe (q:qs) = safe q qs (fd 1) /\ allSafe qs
safe :: FDExpr -> [FDExpr] -> FDExpr -> FDConstr
safe _ [] _ = true
safe q (q1:qs) p = no_attack q q1 p /\ safe q qs (p +# fd 1)
no_attack :: FDExpr -> FDExpr -> FDExpr -> FDConstr
no_attack q1 q2 p = q1 /=# q2 /\ q1 /=# q2+#p /\ q1 /=# q2-#p
-- queens [] 8
-- queens [FirstFail] 16
import CLP.FD
-- send more money puzzle in Curry with FD constraints:
smm :: [Int]
smm =
let xs@[s,e,n,d,m,o,r,y] = take 8 (domain 0 9)
constraints =
s ># fd 0 /\
m ># fd 0 /\
allDifferent xs /\
fd 1000 *# s +# fd 100 *# e +# fd 10 *# n +# d
+# fd 1000 *# m +# fd 100 *# o +# fd 10 *# r +# e
=# fd 10000 *# m +# fd 1000 *# o +# fd 100 *# n +# fd 10 *# e +# y
in solveFD [] xs constraints
-- smm --> [9,5,6,7,1,0,8,2]
-----------------------------------------------------------------------------
--- Solving Su Doku puzzles in Curry with FD constraints
---
--- @author Michael Hanus
--- @version September 2015
-----------------------------------------------------------------------------
{-# OPTIONS_CYMAKE -Wno-incomplete-patterns #-}
import CLP.FD
import List(transpose)
-- Solving a Su Doku puzzle represented as a matrix of numbers (possibly free
-- variables):
sudoku :: [[FDExpr]] -> [Int]
sudoku m = solveFD [FirstFail] (concat m) $
allC allDifferent m /\ -- all rows contain different digits
allC allDifferent (transpose m) /\ -- all columns have different digits
allC allDifferent (squares m) -- all 3x3 squares are different
where
-- translate a matrix into a list of small 3x3 squares
squares :: [[a]] -> [[a]]
squares [] = []
squares (l1:l2:l3:ls) = group3Rows [l1,l2,l3] ++ squares ls
group3Rows l123 = if null (head l123) then [] else
concatMap (take 3) l123 : group3Rows (map (drop 3) l123)
-- read a Su Doku specification written as a list of strings containing digits
-- and spaces
readSudoku :: [String] -> [[FDExpr]]
readSudoku = map (map (\c -> if c==' ' then head (domain 1 9)
else fd (ord c - ord '0')))
-- show a solved Su Doku matrix
showSudoku :: [[Int]] -> String
showSudoku = unlines . map (concatMap (\i->[chr (i + ord '0'),' ']))
-- the main function, e.g., evaluate (main s1):
main :: [[Char]] -> IO ()
main s = putStrLn (showSudoku (toMatrix m (sudoku m)))
where m = readSudoku s
toMatrix [] xs = [xs]
toMatrix (r:rs) xs = let rn = length r
in take rn xs : toMatrix rs (drop rn xs)
s1 :: [[Char]]
s1 = ["9 2 5 ",
" 4 6 3 ",
" 3 6",
" 9 2 ",
" 5 8 ",
" 7 4 3",
"7 1 ",
" 5 2 4 ",
" 1 6 9"]
s2 :: [[Char]]
s2 = ["819 5 ",
" 2 75 ",
" 371 4 6 ",
"4 59 1 ",
"7 3 8 2",
" 3 62 7",
" 5 7 921 ",
" 64 9 ",
" 2 438"]
s3 :: [[Char]]
s3 = [" 63 8 ",
" 1 ",
"327 1 ",
"9 2 3 ",
" 6 4 ",
" 3 4 9",
" 8 627",
" 6 ",
" 4 51 "]
......@@ -329,6 +329,9 @@ mortgage 1.0E+05 180.0 0.01 r 0.0
Loading program "smm"...
smm l
{l=[9,5,6,7,1,0,8,2]} success
Loading program "smm2"...
smm
[9,5,6,7,1,0,8,2]
Loading program "accountport"...
goal1 b
{b=250} success
......
......@@ -329,6 +329,9 @@ mortgage 100000.0 180.0 0.01 r 0.0
Loading program "smm"...
smm l
{l=[9,5,6,7,1,0,8,2]} success
Loading program "smm2"...
smm
[9,5,6,7,1,0,8,2]
Loading program "accountport"...
goal1 b
{b=250} success
......
......@@ -115,6 +115,8 @@ pali5
mortgage 100000.0 180.0 0.01 r 0.0 where r free
:l smm
smm l where l free
:l smm2
smm
:cd ..
:cd distcurry
:l accountport
......
lib-trunk @ 07d7fbe2
Subproject commit 5c85f41932e31b7a8e441ed5ceecf7f2135ec19b
Subproject commit 07d7fbe2c2deb55decf941da8771ade6fa704292
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment