summaryrefslogtreecommitdiff
path: root/packages/glpk/src/Numeric/LinearProgramming.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/glpk/src/Numeric/LinearProgramming.hs')
-rw-r--r--packages/glpk/src/Numeric/LinearProgramming.hs30
1 files changed, 18 insertions, 12 deletions
diff --git a/packages/glpk/src/Numeric/LinearProgramming.hs b/packages/glpk/src/Numeric/LinearProgramming.hs
index 6a0c47d..0a776fa 100644
--- a/packages/glpk/src/Numeric/LinearProgramming.hs
+++ b/packages/glpk/src/Numeric/LinearProgramming.hs
@@ -85,8 +85,8 @@ module Numeric.LinearProgramming(
85 Solution(..) 85 Solution(..)
86) where 86) where
87 87
88import Data.Packed 88import Numeric.LinearAlgebra.HMatrix
89import Data.Packed.Development 89import Numeric.LinearAlgebra.Devel hiding (Dense)
90import Foreign(Ptr) 90import Foreign(Ptr)
91import System.IO.Unsafe(unsafePerformIO) 91import System.IO.Unsafe(unsafePerformIO)
92import Foreign.C.Types 92import Foreign.C.Types
@@ -180,16 +180,17 @@ exact opt constr@(General _) bnds = exact opt (sparseOfGeneral constr) bnds
180 180
181adapt :: Optimization -> (Int, Double, [Double]) 181adapt :: Optimization -> (Int, Double, [Double])
182adapt opt = case opt of 182adapt opt = case opt of
183 Maximize x -> (size x, 1 ,x) 183 Maximize x -> (sz x, 1 ,x)
184 Minimize x -> (size x, -1, (map negate x)) 184 Minimize x -> (sz x, -1, (map negate x))
185 where size x | null x = error "simplex: objective function with zero variables" 185 where
186 | otherwise = length x 186 sz x | null x = error "simplex: objective function with zero variables"
187 | otherwise = length x
187 188
188extract :: Double -> Vector Double -> Solution 189extract :: Double -> Vector Double -> Solution
189extract sg sol = r where 190extract sg sol = r where
190 z = sg * (sol@>1) 191 z = sg * (sol!1)
191 v = toList $ subVector 2 (dim sol -2) sol 192 v = toList $ subVector 2 (size sol -2) sol
192 r = case round(sol@>0)::Int of 193 r = case round(sol!0)::Int of
193 1 -> Undefined 194 1 -> Undefined
194 2 -> Feasible (z,v) 195 2 -> Feasible (z,v)
195 3 -> Infeasible (z,v) 196 3 -> Infeasible (z,v)
@@ -261,7 +262,7 @@ mkConstrD n f b1 | ok = fromLists (ob ++ co)
261 ok = all (==n) ls 262 ok = all (==n) ls
262 den = fromLists cs 263 den = fromLists cs
263 ob = map (([0,0]++).return) f 264 ob = map (([0,0]++).return) f
264 co = [[fromIntegral i, fromIntegral j,den@@>(i-1,j-1)]| i<-[1 ..rows den], j<-[1 .. cols den]] 265 co = [[fromIntegral i, fromIntegral j,den `atIndex` (i-1,j-1)]| i<-[1 ..rows den], j<-[1 .. cols den]]
265 266
266mkConstrS :: Int -> [Double] -> [Bound [(Double, Int)]] -> Matrix Double 267mkConstrS :: Int -> [Double] -> [Bound [(Double, Int)]] -> Matrix Double
267mkConstrS n objfun b1 = fromLists (ob ++ co) where 268mkConstrS n objfun b1 = fromLists (ob ++ co) where
@@ -274,6 +275,11 @@ mkConstrS n objfun b1 = fromLists (ob ++ co) where
274 275
275----------------------------------------------------- 276-----------------------------------------------------
276 277
278(##) :: TransArray c => TransRaw c b -> c -> b
279infixl 1 ##
280a ## b = applyRaw a b
281{-# INLINE (##) #-}
282
277foreign import ccall unsafe "c_simplex_sparse" c_simplex_sparse 283foreign import ccall unsafe "c_simplex_sparse" c_simplex_sparse
278 :: CInt -> CInt -- rows and cols 284 :: CInt -> CInt -- rows and cols
279 -> CInt -> CInt -> Ptr Double -- coeffs 285 -> CInt -> CInt -> Ptr Double -- coeffs
@@ -284,7 +290,7 @@ foreign import ccall unsafe "c_simplex_sparse" c_simplex_sparse
284simplexSparse :: Int -> Int -> Matrix Double -> Matrix Double -> Vector Double 290simplexSparse :: Int -> Int -> Matrix Double -> Matrix Double -> Vector Double
285simplexSparse m n c b = unsafePerformIO $ do 291simplexSparse m n c b = unsafePerformIO $ do
286 s <- createVector (2+n) 292 s <- createVector (2+n)
287 app3 (c_simplex_sparse (fi m) (fi n)) mat (cmat c) mat (cmat b) vec s "c_simplex_sparse" 293 c_simplex_sparse (fi m) (fi n) ## (cmat c) ## (cmat b) ## s #|"c_simplex_sparse"
288 return s 294 return s
289 295
290foreign import ccall unsafe "c_exact_sparse" c_exact_sparse 296foreign import ccall unsafe "c_exact_sparse" c_exact_sparse
@@ -297,7 +303,7 @@ foreign import ccall unsafe "c_exact_sparse" c_exact_sparse
297exactSparse :: Int -> Int -> Matrix Double -> Matrix Double -> Vector Double 303exactSparse :: Int -> Int -> Matrix Double -> Matrix Double -> Vector Double
298exactSparse m n c b = unsafePerformIO $ do 304exactSparse m n c b = unsafePerformIO $ do
299 s <- createVector (2+n) 305 s <- createVector (2+n)
300 app3 (c_exact_sparse (fi m) (fi n)) mat (cmat c) mat (cmat b) vec s "c_exact_sparse" 306 c_exact_sparse (fi m) (fi n) ## (cmat c) ## (cmat b) ## s #|"c_exact_sparse"
301 return s 307 return s
302 308
303glpFR, glpLO, glpUP, glpDB, glpFX :: Double 309glpFR, glpLO, glpUP, glpDB, glpFX :: Double