{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE FunctionalDependencies #-}
module Numeric.Optimization.MIP.Solver.Base
(
IsSolver (..)
, SolveOptions (..)
, Default (..)
) where
import Data.Default.Class
import Data.Scientific (Scientific)
import Numeric.Optimization.MIP.Base as MIP
import qualified Data.Map as Map
data SolveOptions
= SolveOptions
{ SolveOptions -> Maybe Double
solveTimeLimit :: Maybe Double
, SolveOptions -> Maybe (Tol Scientific)
solveTol :: Maybe (MIP.Tol Scientific)
, SolveOptions -> String -> IO ()
solveLogger :: String -> IO ()
, SolveOptions -> String -> IO ()
solveErrorLogger :: String -> IO ()
, SolveOptions -> Bool
solveCondensedSolution :: Bool
}
instance Default SolveOptions where
def :: SolveOptions
def =
SolveOptions
{ solveTimeLimit :: Maybe Double
solveTimeLimit = Maybe Double
forall a. Maybe a
Nothing
, solveTol :: Maybe (Tol Scientific)
solveTol = Maybe (Tol Scientific)
forall a. Maybe a
Nothing
, solveLogger :: String -> IO ()
solveLogger = IO () -> String -> IO ()
forall a b. a -> b -> a
const (IO () -> String -> IO ()) -> IO () -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
, solveErrorLogger :: String -> IO ()
solveErrorLogger = IO () -> String -> IO ()
forall a b. a -> b -> a
const (IO () -> String -> IO ()) -> IO () -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
, solveCondensedSolution :: Bool
solveCondensedSolution = Bool
False
}
class Monad m => IsSolver s m | s -> m where
solve' :: s -> SolveOptions -> MIP.Problem Scientific -> m (MIP.Solution Scientific)
solve :: s -> SolveOptions -> MIP.Problem Scientific -> m (MIP.Solution Scientific)
solve s
s SolveOptions
opts Problem Scientific
problem = (if SolveOptions -> Bool
solveCondensedSolution SolveOptions
opts then Solution Scientific -> Solution Scientific
forall a. a -> a
id else Problem Scientific -> Solution Scientific -> Solution Scientific
addZeroes Problem Scientific
problem) (Solution Scientific -> Solution Scientific)
-> m (Solution Scientific) -> m (Solution Scientific)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> SolveOptions -> Problem Scientific -> m (Solution Scientific)
forall s (m :: * -> *).
IsSolver s m =>
s -> SolveOptions -> Problem Scientific -> m (Solution Scientific)
solve' s
s SolveOptions
opts Problem Scientific
problem
{-# MINIMAL solve' #-}
addZeroes :: MIP.Problem Scientific -> MIP.Solution Scientific -> MIP.Solution Scientific
addZeroes :: Problem Scientific -> Solution Scientific -> Solution Scientific
addZeroes Problem Scientific
problem (Solution Status
stat Maybe Scientific
obj Map Var Scientific
solmap) =
Status
-> Maybe Scientific -> Map Var Scientific -> Solution Scientific
forall r. Status -> Maybe r -> Map Var r -> Solution r
Solution Status
stat Maybe Scientific
obj (Map Var Scientific -> Solution Scientific)
-> Map Var Scientific -> Solution Scientific
forall a b. (a -> b) -> a -> b
$ Map Var Scientific -> Map Var Scientific -> Map Var Scientific
forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map Var Scientific
solmap ((Var -> Scientific) -> Set Var -> Map Var Scientific
forall k a. (k -> a) -> Set k -> Map k a
Map.fromSet (Scientific -> Var -> Scientific
forall a b. a -> b -> a
const Scientific
0) (Problem Scientific -> Set Var
forall a. Variables a => a -> Set Var
vars Problem Scientific
problem))