From d68fb49cad1a5bba7e52c7ff464d15c867052d0f Mon Sep 17 00:00:00 2001 From: Justin Bedo Date: Tue, 28 Oct 2014 10:19:49 +1100 Subject: Add support for binary/integer contraints; Change to hash maps to speed up variable LUT for large number of variables. --- Math/LinProg/LPSolve.hs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'Math/LinProg/LPSolve.hs') diff --git a/Math/LinProg/LPSolve.hs b/Math/LinProg/LPSolve.hs index baa5d7e..5299d94 100644 --- a/Math/LinProg/LPSolve.hs +++ b/Math/LinProg/LPSolve.hs @@ -26,14 +26,15 @@ import Math.LinProg.LPSolve.FFI hiding (solve) import qualified Math.LinProg.LPSolve.FFI as F import Math.LinProg.LP import Math.LinProg.Types -import qualified Data.Map.Strict as M +import qualified Data.HashMap.Strict as M +import Data.Hashable import Prelude hiding (EQ) -solve :: (Eq v, Ord v) => LinProg Double v () -> IO (Maybe ResultCode, [(v, Double)]) +solve :: (Hashable v, Eq v, Ord v) => LinProg Double v () -> IO (Maybe ResultCode, [(v, Double)]) solve = solveWithTimeout 0 -- | Solves an LP using lp_solve. -solveWithTimeout :: (Eq v, Ord v) => Integer -> LinProg Double v () -> IO (Maybe ResultCode, [(v, Double)]) +solveWithTimeout :: (Hashable v, Eq v, Ord v) => Integer -> LinProg Double v () -> IO (Maybe ResultCode, [(v, Double)]) solveWithTimeout t (compile -> lp) = do model <- makeLP nconstr nvars case model of @@ -59,6 +60,14 @@ solveWithTimeout t (compile -> lp) = do setRHS m i c return () + -- Ints + forM_ (lp ^. ints) $ \v -> do + setInt m (varLUT M.! v) + + -- Bins + forM_ (lp ^. bins) $ \v -> do + setBin m (varLUT M.! v) + -- Objective forM_ (varTerms (lp ^. objective)) $ \(v, w) -> do void $ setMat m 0 (varLUT M.! v) w -- cgit v1.2.3