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

msakai / haskell-MIP / 246

28 Jan 2025 11:58PM UTC coverage: 76.66%. Remained the same
246

push

github

web-flow
Merge 843abba85 into 96285dc20

1501 of 1958 relevant lines covered (76.66%)

0.77 hits per line

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

66.67
/MIP/src/Numeric/Optimization/MIP/Solver/Base.hs
1
{-# OPTIONS_GHC -Wall #-}
2
{-# OPTIONS_HADDOCK show-extensions #-}
3
{-# LANGUAGE FunctionalDependencies #-}
4
-----------------------------------------------------------------------------
5
-- |
6
-- Module      :  Numeric.Optimization.MIP.Solver.Base
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.Base
16
  (
17
  -- * Solver type
18
    IsSolver (..)
19
  , SolveOptions (..)
20
  -- * Utilities
21
  , Default (..)
22
  ) where
23

24
import Data.Default.Class
25
import Data.Scientific (Scientific)
26
import Numeric.Optimization.MIP.Base as MIP
27
import qualified Data.Map as Map
28

29
-- | Options for 'solve' function
30
data SolveOptions
31
  = SolveOptions
32
  { solveTimeLimit :: Maybe Double
1✔
33
    -- ^ time limit in seconds
34
  , solveTol :: Maybe (MIP.Tol Scientific)
1✔
35
    -- ^ tolerance
36
  , solveLogger :: String -> IO ()
1✔
37
    -- ^ invoked when a solver output a line
38
  , solveErrorLogger :: String -> IO ()
×
39
    -- ^ invoked when a solver output a line to stderr
40
  , solveCondensedSolution :: Bool
1✔
41
    -- ^ potentially omit variables set to zero from the solution
42
  }
43

44
instance Default SolveOptions where
45
  def =
1✔
46
    SolveOptions
1✔
47
    { solveTimeLimit = Nothing
1✔
48
    , solveTol = Nothing
1✔
49
    , solveLogger = const $ return ()
×
50
    , solveErrorLogger = const $ return ()
×
51
    , solveCondensedSolution = False
1✔
52
    }
53

54

55
-- | Type class for solvers
56
--
57
-- 
58
class Monad m => IsSolver s m | s -> m where
59
  -- | Low level version of 'solve'' that allows omission of variables with a value 0.
60
  --
61
  -- Implementor of the type class must implement this method.
62
  solve' :: s -> SolveOptions -> MIP.Problem Scientific -> m (MIP.Solution Scientific)
63

64
  -- | A method for solving 'MIP.Problem'
65
  --
66
  -- This method is a bit higher level than 'solve'' in that it does not omit variables
67
  -- with a value @0@ unless 'solveCondensedSolution' is set to @True@.
68
  -- Implementor of the type class can override this method as @solve = solve'@ if the
69
  -- solver always returns all variables.
70
  solve  :: s -> SolveOptions -> MIP.Problem Scientific -> m (MIP.Solution Scientific)
71
  solve s opts problem = (if solveCondensedSolution opts then id else addZeroes problem) <$> solve' s opts problem
×
72
  {-# MINIMAL solve' #-}
73

74
-- Several solvers (at least CBC) do not include any variables set to 0 in their solution.
75
-- TODO: for solvers that do return all variables, add @solve = solve'@
76
-- for a minor performance improvement.
77
addZeroes :: MIP.Problem Scientific -> MIP.Solution Scientific -> MIP.Solution Scientific
78
addZeroes problem (Solution stat obj solmap) = 
1✔
79
  -- Map.union is left-biased: only values not present in the solution are added.
80
  Solution stat obj $ Map.union solmap (Map.fromSet (const 0) (vars problem))
×
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