Hungarian-Munkres 0.1.4 → 0.1.5
raw patch · 3 files changed
+33/−12 lines, 3 filesdep ~base
Dependency ranges changed: base
Files
- Hungarian-Munkres.cabal +4/−4
- benchmarks/bench.hs +1/−1
- src/Algorithms/Hungarian.hs +28/−7
Hungarian-Munkres.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: Hungarian-Munkres-version: 0.1.4+version: 0.1.5 synopsis: A Linear Sum Assignment Problem (LSAP) solver description: This library provide a Haskell binding to the libhungarian, a solver for Linear Sum Assignment Problem (LSAP) implemented@@ -25,7 +25,7 @@ exposed-modules: Algorithms.Hungarian -- other-modules: -- other-extensions: - build-depends: base >=4.7 && <4.8+ build-depends: base >=4.0 && <5.0 hs-source-dirs: src c-sources: cbits/hungarian.c include-dirs: include@@ -38,7 +38,7 @@ main-is: bench.hs default-language: Haskell2010 build-depends:- base >=4.7 && <4.8+ base >=4.0 && <5.0 , array , Hungarian-Munkres , Munkres@@ -51,7 +51,7 @@ main-is: tests.hs default-language: Haskell2010 build-depends:- base >=4.7 && <4.8+ base >=4.0 && <5.0 , array , Hungarian-Munkres , Munkres
benchmarks/bench.hs view
@@ -5,7 +5,7 @@ import System.Random sample :: [Double]-sample = take 4000 $ randomRs (-100, 100) (mkStdGen 4)+sample = take 2000 $ randomRs (-100, 100) (mkStdGen 4) sample' :: [Double] sample' = take 40000 $ randomRs (-1000, 1000) (mkStdGen 24)
src/Algorithms/Hungarian.hs view
@@ -1,9 +1,10 @@ {-# LANGUAGE ForeignFunctionInterface #-}-{-# LANGUAGE EmptyDataDecls #-} module Algorithms.Hungarian ( hungarian , hungarianScore+ , unsafeHungarian+ , unsafeHungarianScore ) where import Data.List@@ -14,12 +15,32 @@ foreign import ccall "hungarian" c_hungarian :: Ptr CDouble -> CInt -> CInt -> Ptr CSize -> Ptr CSize -> IO Double --- | solve the LSAP by hungarian algorithm, return assignment and score+-- | solve the LSAP by hungarian algorithm, return assignment and score. hungarian :: [Double] -- ^ row majored flat matrix -> Int -- ^ number of rows -> Int -- ^ number of columns -> ([(Int, Int)], Double)-hungarian costMatrix rows cols = unsafePerformIO $ do+hungarian costMatrix rows cols+ | length costMatrix /= rows * cols = error "Algorithms.Hungarian.hungarian: incorrect size"+ | otherwise = unsafeHungarian costMatrix rows cols+{-# INLINE hungarian #-}++-- | solve the LSAP by hungarian algorithm, return score only+hungarianScore :: [Double] -> Int -> Int -> Double+hungarianScore costMatrix rows cols+ | length costMatrix /= rows * cols = error "Algorithms.Hungarian.hungarian: incorrect size"+ | otherwise = unsafePerformIO $ do+ withArray (map realToFrac costMatrix) $ \input -> do+ fmap realToFrac $ c_hungarian input (fromIntegral rows)+ (fromIntegral cols) nullPtr nullPtr+{-# INLINE hungarianScore #-}++-- | doesn't check if the input is a valid matrix+unsafeHungarian :: [Double] -- ^ row majored flat matrix+ -> Int -- ^ number of rows+ -> Int -- ^ number of columns+ -> ([(Int, Int)], Double)+unsafeHungarian costMatrix rows cols = unsafePerformIO $ do withArray (map realToFrac costMatrix) $ \input -> allocaArray n $ \from -> allocaArray n $ \to -> do cost <- c_hungarian input (fromIntegral rows) (fromIntegral cols)@@ -30,12 +51,12 @@ where f x y = (fromIntegral x, fromIntegral y) n = min rows cols-{-# INLINE hungarian #-}+{-# INLINE unsafeHungarian #-} -- | solve the LSAP by hungarian algorithm, return score only-hungarianScore :: [Double] -> Int -> Int -> Double-hungarianScore costMatrix rows cols = unsafePerformIO $ do+unsafeHungarianScore :: [Double] -> Int -> Int -> Double+unsafeHungarianScore costMatrix rows cols = unsafePerformIO $ do withArray (map realToFrac costMatrix) $ \input -> do fmap realToFrac $ c_hungarian input (fromIntegral rows) (fromIntegral cols) nullPtr nullPtr-{-# INLINE hungarianScore #-}+{-# INLINE unsafeHungarianScore #-}