-
Notifications
You must be signed in to change notification settings - Fork 2
/
SCIP.hs
69 lines (64 loc) · 2.27 KB
/
SCIP.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE MultiParamTypeClasses #-}
-----------------------------------------------------------------------------
-- |
-- Module : Numeric.Optimization.MIP.Solver.SCIP
-- Copyright : (c) Masahiro Sakai 2017
-- License : BSD-style
--
-- Maintainer : masahiro.sakai@gmail.com
-- Stability : provisional
-- Portability : non-portable
--
-----------------------------------------------------------------------------
module Numeric.Optimization.MIP.Solver.SCIP
( SCIP (..)
, scip
) where
import Data.Default.Class
import qualified Data.Text.Lazy.IO as TLIO
import System.Exit
import System.IO
import System.IO.Temp
import qualified Numeric.Optimization.MIP.LPFile as LPFile
import Numeric.Optimization.MIP.Solver.Base
import qualified Numeric.Optimization.MIP.Solution.SCIP as ScipSol
import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallback)
data SCIP
= SCIP
{ scipPath :: String
, scipArgs :: [String]
, scipCommands :: [String]
}
instance Default SCIP where
def = scip
scip :: SCIP
scip = SCIP "scip" [] []
instance IsSolver SCIP IO where
solve solver opt prob = do
case LPFile.render def prob of
Left err -> ioError $ userError err
Right lp -> do
withSystemTempFile "scip.lp" $ \fname1 h1 -> do
TLIO.hPutStr h1 lp
hClose h1
withSystemTempFile "scip.sol" $ \fname2 h2 -> do
hClose h2
let commands =
[ "read " ++ show fname1 ] ++
(case solveTimeLimit opt of
Nothing -> []
Just sec -> ["set limits time " ++ show sec]) ++
scipCommands solver ++
[ "optimize"
, "write solution " ++ show fname2
, "quit"
]
args = scipArgs solver ++ concat [["-c", cmd] | cmd <- commands]
onGetLine = solveLogger opt
onGetErrorLine = solveErrorLogger opt
exitcode <- runProcessWithOutputCallback (scipPath solver) args "" onGetLine onGetErrorLine
case exitcode of
ExitFailure n -> ioError $ userError $ "exit with " ++ show n
ExitSuccess -> ScipSol.readFile fname2