• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

msakai / haskell-MIP / 344

04 Jan 2026 03:39PM UTC coverage: 76.98% (+0.3%) from 76.66%
344

push

github

web-flow
Merge cc7d6067e into 9f1c6930b

1565 of 2033 relevant lines covered (76.98%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.21
/MIP/src/Numeric/Optimization/MIP/Solver/CBC.hs
1
{-# OPTIONS_GHC -Wall #-}
2
{-# OPTIONS_HADDOCK show-extensions #-}
3
{-# LANGUAGE MultiParamTypeClasses #-}
4
-----------------------------------------------------------------------------
5
-- |
6
-- Module      :  Numeric.Optimization.MIP.Solver.CBC
7
-- Copyright   :  (c) Masahiro Sakai 2017
8
-- License     :  BSD-style
9
--
10
-- Maintainer  :  masahiro.sakai@gmail.com
11
-- Stability   :  provisional
12
-- Portability :  non-portable
13
--
14
-----------------------------------------------------------------------------
15
module Numeric.Optimization.MIP.Solver.CBC
16
  ( CBC (..)
17
  , cbc
18
  ) where
19

20
import Data.Default.Class
21
import qualified Data.Text.Lazy.IO as TLIO
22
import System.Exit
23
import System.IO
24
import System.IO.Temp
25
import qualified Numeric.Optimization.MIP.Base as MIP
26
import qualified Numeric.Optimization.MIP.LPFile as LPFile
27
import Numeric.Optimization.MIP.Solver.Base
28
import qualified Numeric.Optimization.MIP.Solution.CBC as CBCSol
29
import Numeric.Optimization.MIP.Internal.ProcessUtil (runProcessWithOutputCallback)
30

31
-- | A solver instance for calling @cbc@ command from [CBC (COIN-OR Branch-and-Cut solver)](https://github.com/coin-or/Cbc).
32
--
33
-- Use 'cbc' and record update syntax to modify its field.
34
data CBC
35
  = CBC
36
  { cbcPath :: String
1✔
37
  , cbcArgs :: [String]
1✔
38
  }
39

40
instance Default CBC where
41
  def = cbc
×
42

43
-- | Default value of t'CBC'
44
cbc :: CBC
45
cbc = CBC "cbc" []
1✔
46

47
instance IsSolver CBC IO where
1✔
48
  solve' solver opt prob = do
1✔
49
    case LPFile.render def prob{ MIP.objectiveFunction = obj' } of
1✔
50
      Left err -> ioError $ userError err
×
51
      Right lp -> do
1✔
52
        withSystemTempFile "cbc.lp" $ \fname1 h1 -> do
1✔
53
          TLIO.hPutStr h1 lp
1✔
54
          hClose h1
1✔
55
          withSystemTempFile "cbc.sol" $ \fname2 h2 -> do
1✔
56
            hClose h2
1✔
57
            let args = cbcArgs solver
1✔
58
                    ++ [fname1]
1✔
59
                    ++ (case solveTimeLimit opt of
1✔
60
                          Nothing -> []
1✔
61
                          Just sec -> ["sec", show sec])
×
62
                    ++ (case solveTol opt of
1✔
63
                          Nothing -> []
1✔
64
                          Just tol ->
65
                            [ "integerTolerance", show (MIP.integralityTol tol)
1✔
66
                            , "primalTolerance", show (MIP.feasibilityTol tol)
1✔
67
                            , "dualTolerance", show (MIP.optimalityTol tol)
1✔
68
                            ])
69
                    ++ ["solve", "solu", fname2]
1✔
70
                onGetLine = solveLogger opt
1✔
71
                onGetErrorLine = solveErrorLogger opt
×
72
            exitcode <- runProcessWithOutputCallback (cbcPath solver) args Nothing "" onGetLine onGetErrorLine
×
73
            case exitcode of
1✔
74
              ExitFailure n -> ioError $ userError $ "exit with " ++ show n
×
75
              ExitSuccess -> do
1✔
76
                sol <- CBCSol.readFile fname2
1✔
77
                if isMax then
1✔
78
                  return $ sol{ MIP.solObjectiveValue = fmap negate (MIP.solObjectiveValue sol) }
1✔
79
                else
80
                  return sol
1✔
81
    where
82
      obj = MIP.objectiveFunction prob
1✔
83
      isMax = MIP.objDir obj == MIP.OptMax
1✔
84
      obj' = if isMax then obj{ MIP.objDir = MIP.OptMin, MIP.objExpr = - MIP.objExpr obj } else obj
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc