summaryrefslogtreecommitdiff
path: root/packages/glpk/src
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2015-02-24 20:30:00 +0100
committerAlberto Ruiz <aruiz@um.es>2015-02-24 20:30:00 +0100
commit29887ff45b07b42095b994d7b57a2d8bb5363956 (patch)
treecadcf9bd0c457943bbb18cdabb13842620906af9 /packages/glpk/src
parent1a5cfd2c50600c60e23bcf44f6e9a45996c699a2 (diff)
parente57907c22e8a16d2a9b62b70dd04ffcfd0d96b6a (diff)
Merge pull request #114 from piotrm0/master
added handling of general sparse constraints
Diffstat (limited to 'packages/glpk/src')
-rw-r--r--packages/glpk/src/Numeric/LinearProgramming.hs40
1 files changed, 35 insertions, 5 deletions
diff --git a/packages/glpk/src/Numeric/LinearProgramming.hs b/packages/glpk/src/Numeric/LinearProgramming.hs
index b0537cc..a54eb59 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
49 ] 49 ]
50@ 50@
51 51
52Note 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.
53
54@
55constr3 = General [ [1\#1, 1\#1, 1\#2] :<=: 10
56 , [1\#2, 5\#3] :<=: 20
57 ]
58@
59
52By default all variables are bounded as @x_i >= 0@, but this can be 60By default all variables are bounded as @x_i >= 0@, but this can be
53changed: 61changed:
54 62
@@ -67,6 +75,7 @@ Multiple bounds for a variable are not allowed, instead of
67 75
68module Numeric.LinearProgramming( 76module Numeric.LinearProgramming(
69 simplex, 77 simplex,
78 sparseOfGeneral,
70 Optimization(..), 79 Optimization(..),
71 Constraints(..), 80 Constraints(..),
72 Bounds, 81 Bounds,
@@ -82,13 +91,14 @@ import System.IO.Unsafe(unsafePerformIO)
82import Foreign.C.Types 91import Foreign.C.Types
83import Data.List((\\),sortBy,nub) 92import Data.List((\\),sortBy,nub)
84import Data.Function(on) 93import Data.Function(on)
94import qualified Data.Map.Strict as Map
85 95
86--import Debug.Trace 96--import Debug.Trace
87--debug x = trace (show x) x 97--debug x = trace (show x) x
88 98
89----------------------------------------------------- 99-----------------------------------------------------
90 100
91-- | Coefficient of a variable for a sparse representation of constraints. 101-- | Coefficient of a variable for a sparse and general representations of constraints.
92(#) :: Double -> Int -> (Double,Int) 102(#) :: Double -> Int -> (Double,Int)
93infixl 5 # 103infixl 5 #
94(#) = (,) 104(#) = (,)
@@ -108,18 +118,29 @@ data Solution = Undefined
108 | Unbounded 118 | Unbounded
109 deriving Show 119 deriving Show
110 120
111data Constraints = Dense [ Bound [Double] ] 121data Constraints = Dense [ Bound [Double] ]
112 | Sparse [ Bound [(Double,Int)] ] 122 | Sparse [ Bound [(Double,Int)] ]
123 | General [ Bound [(Double,Int)] ]
113 124
114data Optimization = Maximize [Double] 125data Optimization = Maximize [Double]
115 | Minimize [Double] 126 | Minimize [Double]
116 127
117type Bounds = [Bound Int] 128type Bounds = [Bound Int]
118 129
130-- | Convert a system of General constraints to one with unique coefficients.
131sparseOfGeneral :: Constraints -> Constraints
132sparseOfGeneral (General cs) =
133 Sparse $ map (\bl ->
134 let cl = obj bl in
135 let m = foldr (\(c,t) m -> Map.insertWith (+) t c m) Map.empty cl in
136 withObj bl (Map.foldrWithKey' (\t c l -> (c#t) : l) [] m)) cs
137sparseOfGeneral _ = error "sparseOfGeneral: a general system of constraints was expected"
138
119simplex :: Optimization -> Constraints -> Bounds -> Solution 139simplex :: Optimization -> Constraints -> Bounds -> Solution
120 140
121simplex opt (Dense []) bnds = simplex opt (Sparse []) bnds 141simplex opt (Dense []) bnds = simplex opt (Sparse []) bnds
122simplex opt (Sparse []) bnds = simplex opt (Sparse [Free [0#1]]) bnds 142simplex opt (Sparse []) bnds = simplex opt (Sparse [Free [0#1]]) bnds
143simplex opt (General []) bnds = simplex opt (Sparse [Free [0#1]]) bnds
123 144
124simplex opt (Dense constr) bnds = extract sg sol where 145simplex opt (Dense constr) bnds = extract sg sol where
125 sol = simplexSparse m n (mkConstrD sz objfun constr) (mkBounds sz constr bnds) 146 sol = simplexSparse m n (mkConstrD sz objfun constr) (mkBounds sz constr bnds)
@@ -133,6 +154,8 @@ simplex opt (Sparse constr) bnds = extract sg sol where
133 m = length constr 154 m = length constr
134 (sz, sg, objfun) = adapt opt 155 (sz, sg, objfun) = adapt opt
135 156
157simplex opt constr@(General _) bnds = simplex opt (sparseOfGeneral constr) bnds
158
136adapt :: Optimization -> (Int, Double, [Double]) 159adapt :: Optimization -> (Int, Double, [Double])
137adapt opt = case opt of 160adapt opt = case opt of
138 Maximize x -> (size x, 1 ,x) 161 Maximize x -> (size x, 1 ,x)
@@ -162,6 +185,13 @@ obj (x :&: _) = x
162obj (x :==: _) = x 185obj (x :==: _) = x
163obj (Free x) = x 186obj (Free x) = x
164 187
188withObj :: Bound t -> t -> Bound t
189withObj (_ :<=: b) x = (x :<=: b)
190withObj (_ :>=: b) x = (x :>=: b)
191withObj (_ :&: b) x = (x :&: b)
192withObj (_ :==: b) x = (x :==: b)
193withObj (Free _) x = Free x
194
165tb :: Bound t -> Double 195tb :: Bound t -> Double
166tb (_ :<=: _) = glpUP 196tb (_ :<=: _) = glpUP
167tb (_ :>=: _) = glpLO 197tb (_ :>=: _) = glpLO