Skip to content

Commit

Permalink
extend Numeric.Optimization.MIP.Solver.* to pass arguments to solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Jan 23, 2021
1 parent 8fb7543 commit ac50698
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
6 changes: 4 additions & 2 deletions MIP/src/Numeric/Optimization/MIP/Solver/CBC.hs
Expand Up @@ -31,13 +31,14 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data CBC
= CBC
{ cbcPath :: String
, cbcArgs :: [String]
}

instance Default CBC where
def = cbc

cbc :: CBC
cbc = CBC "cbc"
cbc = CBC "cbc" []

instance IsSolver CBC IO where
solve solver opt prob = do
Expand All @@ -49,7 +50,8 @@ instance IsSolver CBC IO where
hClose h1
withSystemTempFile "cbc.sol" $ \fname2 h2 -> do
hClose h2
let args = [fname1]
let args = cbcArgs solver
++ [fname1]
++ (case solveTimeLimit opt of
Nothing -> []
Just sec -> ["sec", show sec])
Expand Down
14 changes: 8 additions & 6 deletions MIP/src/Numeric/Optimization/MIP/Solver/CPLEX.hs
Expand Up @@ -33,13 +33,15 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data CPLEX
= CPLEX
{ cplexPath :: String
, cplexArgs :: [String]
, cplexCommands :: [String]
}

instance Default CPLEX where
def = cplex

cplex :: CPLEX
cplex = CPLEX "cplex"
cplex = CPLEX "cplex" [] []

instance IsSolver CPLEX IO where
solve solver opt prob = do
Expand All @@ -52,13 +54,13 @@ instance IsSolver CPLEX IO where
withSystemTempFile "cplex.sol" $ \fname2 h2 -> do
hClose h2
isInfeasibleRef <- newIORef False
let args = []
input = unlines $
let input = unlines $
(case solveTimeLimit opt of
Nothing -> []
Just sec -> ["set timelimit ", show sec]) ++
[ "read " ++ show fname1
, "optimize"
[ "read " ++ show fname1 ] ++
cplexCommands solver ++
[ "optimize"
, "write " ++ show fname2
, "y"
, "quit"
Expand All @@ -68,7 +70,7 @@ instance IsSolver CPLEX IO where
writeIORef isInfeasibleRef True
solveLogger opt s
onGetErrorLine = solveErrorLogger opt
exitcode <- runProcessWithOutputCallback (cplexPath solver) args input onGetLine onGetErrorLine
exitcode <- runProcessWithOutputCallback (cplexPath solver) (cplexArgs solver) input onGetLine onGetErrorLine
case exitcode of
ExitFailure n -> ioError $ userError $ "exit with " ++ show n
ExitSuccess -> do
Expand Down
6 changes: 4 additions & 2 deletions MIP/src/Numeric/Optimization/MIP/Solver/Glpsol.hs
Expand Up @@ -33,13 +33,14 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data Glpsol
= Glpsol
{ glpsolPath :: String
, glpsolArgs :: [String]
}

instance Default Glpsol where
def = glpsol

glpsol :: Glpsol
glpsol = Glpsol "glpsol"
glpsol = Glpsol "glpsol" []

instance IsSolver Glpsol IO where
solve solver opt prob = do
Expand All @@ -53,7 +54,8 @@ instance IsSolver Glpsol IO where
hClose h2
isUnboundedRef <- newIORef False
isInfeasibleRef <- newIORef False
let args = ["--lp", fname1, "-o", fname2] ++
let args = glpsolArgs solver ++
["--lp", fname1, "-o", fname2] ++
(case solveTimeLimit opt of
Nothing -> []
Just sec -> ["--tmlim", show (max 1 (floor sec) :: Int)])
Expand Down
6 changes: 4 additions & 2 deletions MIP/src/Numeric/Optimization/MIP/Solver/GurobiCl.hs
Expand Up @@ -33,13 +33,14 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data GurobiCl
= GurobiCl
{ gurobiClPath :: String
, gurobiClArgs :: [String]
}

instance Default GurobiCl where
def = gurobiCl

gurobiCl :: GurobiCl
gurobiCl = GurobiCl "gurobi_cl"
gurobiCl = GurobiCl "gurobi_cl" []

instance IsSolver GurobiCl IO where
solve solver opt prob = do
Expand All @@ -52,7 +53,8 @@ instance IsSolver GurobiCl IO where
withSystemTempFile "gurobi.sol" $ \fname2 h2 -> do
hClose h2
statusRef <- newIORef MIP.StatusUnknown
let args = ["ResultFile=" ++ fname2]
let args = gurobiClArgs solver
++ ["ResultFile=" ++ fname2]
++ (case solveTimeLimit opt of
Nothing -> []
Just sec -> ["TimeLimit=" ++ show sec])
Expand Down
6 changes: 4 additions & 2 deletions MIP/src/Numeric/Optimization/MIP/Solver/LPSolve.hs
Expand Up @@ -35,13 +35,14 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data LPSolve
= LPSolve
{ lpSolvePath :: String
, lpSolveArgs :: [String]
}

instance Default LPSolve where
def = lpSolve

lpSolve :: LPSolve
lpSolve = LPSolve "lp_solve"
lpSolve = LPSolve "lp_solve" []

instance IsSolver LPSolve IO where
solve solver opt prob = do
Expand All @@ -54,7 +55,8 @@ instance IsSolver LPSolve IO where
objRef <- newIORef Nothing
solRef <- newIORef []
flagRef <- newIORef False
let args = (case solveTimeLimit opt of
let args = lpSolveArgs solver
++ (case solveTimeLimit opt of
Nothing -> []
Just sec -> ["-timeout", show sec])
++ ["-fmps", fname1]
Expand Down
23 changes: 14 additions & 9 deletions MIP/src/Numeric/Optimization/MIP/Solver/SCIP.hs
Expand Up @@ -30,13 +30,15 @@ import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallba
data SCIP
= SCIP
{ scipPath :: String
, scipArgs :: [String]
, scipCommands :: [String]
}

instance Default SCIP where
def = scip

scip :: SCIP
scip = SCIP "scip"
scip = SCIP "scip" [] []

instance IsSolver SCIP IO where
solve solver opt prob = do
Expand All @@ -48,14 +50,17 @@ instance IsSolver SCIP IO where
hClose h1
withSystemTempFile "scip.sol" $ \fname2 h2 -> do
hClose h2
let args = [ "-c", "read " ++ show fname1 ]
++ (case solveTimeLimit opt of
Nothing -> []
Just sec -> ["-c", "set limits time " ++ show sec])
++ [ "-c", "optimize"
, "-c", "write solution " ++ show fname2
, "-c", "quit"
]
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
Expand Down

0 comments on commit ac50698

Please sign in to comment.