From e499467d2c67f214b0871376b068c5b6fc7896a1 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 10 Apr 2015 09:53:23 +0200 Subject: merge recent --- packages/glpk/src/Numeric/LinearProgramming.hs | 75 ++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 5 deletions(-) (limited to 'packages/glpk/src/Numeric') diff --git a/packages/glpk/src/Numeric/LinearProgramming.hs b/packages/glpk/src/Numeric/LinearProgramming.hs index d2e9f3c..7bf4279 100644 --- a/packages/glpk/src/Numeric/LinearProgramming.hs +++ b/packages/glpk/src/Numeric/LinearProgramming.hs @@ -49,6 +49,14 @@ constr2 = Dense [ [2,1,0] :<=: 10 ] @ +Note that when using sparse constraints, coefficients cannot appear more than once in each constraint. You can alternatively use General which will automatically sum any duplicate coefficients when necessary. + +@ +constr3 = General [ [1\#1, 1\#1, 1\#2] :<=: 10 + , [1\#2, 5\#3] :<=: 20 + ] +@ + By default all variables are bounded as @x_i >= 0@, but this can be changed: @@ -67,6 +75,8 @@ Multiple bounds for a variable are not allowed, instead of module Numeric.LinearProgramming( simplex, + exact, + sparseOfGeneral, Optimization(..), Constraints(..), Bounds, @@ -82,13 +92,14 @@ import System.IO.Unsafe(unsafePerformIO) import Foreign.C.Types import Data.List((\\),sortBy,nub) import Data.Function(on) +import qualified Data.Map.Strict as Map --import Debug.Trace --debug x = trace (show x) x ----------------------------------------------------- --- | Coefficient of a variable for a sparse representation of constraints. +-- | Coefficient of a variable for a sparse and general representations of constraints. (#) :: Double -> Int -> (Double,Int) infixl 5 # (#) = (,) @@ -108,18 +119,29 @@ data Solution = Undefined | Unbounded deriving Show -data Constraints = Dense [ Bound [Double] ] - | Sparse [ Bound [(Double,Int)] ] +data Constraints = Dense [ Bound [Double] ] + | Sparse [ Bound [(Double,Int)] ] + | General [ Bound [(Double,Int)] ] data Optimization = Maximize [Double] | Minimize [Double] type Bounds = [Bound Int] +-- | Convert a system of General constraints to one with unique coefficients. +sparseOfGeneral :: Constraints -> Constraints +sparseOfGeneral (General cs) = + Sparse $ map (\bl -> + let cl = obj bl in + let cl_unique = foldr (\(c,t) m -> Map.insertWith (+) t c m) Map.empty cl in + withObj bl (Map.foldrWithKey' (\t c l -> (c#t) : l) [] cl_unique)) cs +sparseOfGeneral _ = error "sparseOfGeneral: a general system of constraints was expected" + simplex :: Optimization -> Constraints -> Bounds -> Solution -simplex opt (Dense []) bnds = simplex opt (Sparse []) bnds -simplex opt (Sparse []) bnds = simplex opt (Sparse [Free [0#1]]) bnds +simplex opt (Dense []) bnds = simplex opt (Sparse []) bnds +simplex opt (Sparse []) bnds = simplex opt (Sparse [Free [0#1]]) bnds +simplex opt (General []) bnds = simplex opt (Sparse [Free [0#1]]) bnds simplex opt (Dense constr) bnds = extract sg sol where sol = simplexSparse m n (mkConstrD sz objfun constr) (mkBounds sz constr bnds) @@ -133,6 +155,29 @@ simplex opt (Sparse constr) bnds = extract sg sol where m = length constr (sz, sg, objfun) = adapt opt +simplex opt constr@(General _) bnds = simplex opt (sparseOfGeneral constr) bnds + +-- | Simplex method with exact internal arithmetic. See glp_exact in glpk documentation for more information. +exact :: Optimization -> Constraints -> Bounds -> Solution + +exact opt (Dense []) bnds = exact opt (Sparse []) bnds +exact opt (Sparse []) bnds = exact opt (Sparse [Free [0#1]]) bnds +exact opt (General []) bnds = exact opt (Sparse [Free [0#1]]) bnds + +exact opt (Dense constr) bnds = extract sg sol where + sol = exactSparse m n (mkConstrD sz objfun constr) (mkBounds sz constr bnds) + n = length objfun + m = length constr + (sz, sg, objfun) = adapt opt + +exact opt (Sparse constr) bnds = extract sg sol where + sol = exactSparse m n (mkConstrS sz objfun constr) (mkBounds sz constr bnds) + n = length objfun + m = length constr + (sz, sg, objfun) = adapt opt + +exact opt constr@(General _) bnds = exact opt (sparseOfGeneral constr) bnds + adapt :: Optimization -> (Int, Double, [Double]) adapt opt = case opt of Maximize x -> (sz x, 1 ,x) @@ -163,6 +208,13 @@ obj (x :&: _) = x obj (x :==: _) = x obj (Free x) = x +withObj :: Bound t -> t -> Bound t +withObj (_ :<=: b) x = (x :<=: b) +withObj (_ :>=: b) x = (x :>=: b) +withObj (_ :&: b) x = (x :&: b) +withObj (_ :==: b) x = (x :==: b) +withObj (Free _) x = Free x + tb :: Bound t -> Double tb (_ :<=: _) = glpUP tb (_ :>=: _) = glpLO @@ -236,6 +288,19 @@ simplexSparse m n c b = unsafePerformIO $ do app3 (c_simplex_sparse (fi m) (fi n)) mat (cmat c) mat (cmat b) vec s "c_simplex_sparse" return s +foreign import ccall unsafe "c_exact_sparse" c_exact_sparse + :: CInt -> CInt -- rows and cols + -> CInt -> CInt -> Ptr Double -- coeffs + -> CInt -> CInt -> Ptr Double -- bounds + -> CInt -> Ptr Double -- result + -> IO CInt -- exit code + +exactSparse :: Int -> Int -> Matrix Double -> Matrix Double -> Vector Double +exactSparse m n c b = unsafePerformIO $ do + s <- createVector (2+n) + app3 (c_exact_sparse (fi m) (fi n)) mat (cmat c) mat (cmat b) vec s "c_exact_sparse" + return s + glpFR, glpLO, glpUP, glpDB, glpFX :: Double glpFR = 0 glpLO = 1 -- cgit v1.2.3