Skip to content

Instantly share code, notes, and snippets.

@banacorn
Created May 23, 2013 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/5637107 to your computer and use it in GitHub Desktop.
Save banacorn/5637107 to your computer and use it in GitHub Desktop.
生測資
import Control.Monad (mapM_)
import System.Random
import System.Cmd
import System.Environment (getArgs)
import Data.List (intercalate, nub)
type Size = Int
type Constraint = (Variable, Variable)
data Variable = Variable Int deriving (Eq)
data Map = Map Size Size [Constraint]
instance Show Variable where
show (Variable n) = "V" ++ show n
instance Show Map where
show (Map v c p) = "4 " ++ show v ++ " " ++ show c ++ "\n" ++ variables ++ "\n" ++ pairs
where variables = intercalate " " $ map (show . Variable) [0 .. pred v]
pairs = intercalate "\n" $ map (\(a, b) -> show a ++ " " ++ show b) p
color = 4
constraints :: Size -> Int -> [Constraint]
constraints numberOfVariables seed = nub . take numberOfConstraints . map pickConstraint $ randomRs (0, upperBound - 1) gen'
where gen = mkStdGen seed
(numberOfConstraints, gen') = randomR (0, upperBound) gen
upperBound = (numberOfVariables - 1) * numberOfVariables `div` 2
pickConstraint = (!!) (constraintPairs numberOfVariables)
constraintPairs n = [ (Variable a, Variable b) | a <- [0..pred n], b <- [0..pred n], a < b]
makeMap seed n = Map n (length pairs) pairs
where pairs = constraints n seed
genTest n = return $ map (makeMap 40) [0..n]
writeMap m = writeFile ("test" ++ show size ++ ".txt") (show m)
where (Map size _ _) = m
main = do
fmap (read . head) getArgs >>= genTest >>= mapM_ writeMap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment