• 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

66.67
/MIP/src/Numeric/Optimization/MIP.hs
1
{-# OPTIONS_GHC -Wall #-}
2
{-# OPTIONS_HADDOCK show-extensions #-}
3
{-# LANGUAGE FlexibleContexts #-}
4
{-# LANGUAGE TypeFamilies #-}
5
{-# LANGUAGE TypeOperators #-}
6
-----------------------------------------------------------------------------
7
-- |
8
-- Module      :  Numeric.Optimization.MIP
9
-- Copyright   :  (c) Masahiro Sakai 2011-2014
10
-- License     :  BSD-style
11
--
12
-- Maintainer  :  masahiro.sakai@gmail.com
13
-- Stability   :  provisional
14
-- Portability :  non-portable
15
--
16
-- Mixed-Integer Programming Problems with some commmonly used extensions
17
--
18
-----------------------------------------------------------------------------
19
module Numeric.Optimization.MIP
20
  (
21
  -- * Mixed-Integer Programming (MIP) problem specification
22

23
  -- ** MIP problems
24
    Problem (..)
25

26
  -- *** Set of variables
27
  , variables
28
  , continuousVariables
29
  , integerVariables
30
  , binaryVariables
31
  , semiContinuousVariables
32
  , semiIntegerVariables
33

34
  -- *** Variable's attributes
35
  , varTypes
36
  , varType
37
  , getVarType
38
  , varBounds
39
  , getBounds
40

41
  -- ** Variables
42
  , Var (Var)
43
  , varName
44
  , toVar
45
  , fromVar
46

47
  -- *** Variable domain
48
  , Domain
49

50
  -- *** Variable types
51
  , VarType (..)
52

53
  -- *** Variable bounds
54
  , BoundExpr
55
  , Extended (..)
56
  , Bounds
57
  , defaultBounds
58
  , defaultLB
59
  , defaultUB
60

61
  -- ** Labels
62
  , Label
63

64
  -- ** Expressions
65
  , Expr (Expr)
66
  , varExpr
67
  , constExpr
68
  , terms
69
  , Term (..)
70

71
  -- ** Objective function
72
  , OptDir (..)
73
  , ObjectiveFunction (..)
74

75
  -- ** Constraints
76

77
  -- *** Linear (or Quadratic or Polynomial) constraints
78
  , Constraint (..)
79
  , constrBounds
80
  , (.==.)
81
  , (.<=.)
82
  , (.>=.)
83
  , RelOp (..)
84

85
  -- *** SOS constraints
86
  , SOSType (..)
87
  , SOSConstraint (..)
88

89
  -- * Solutions
90
  , Solution (..)
91
  , Status (..)
92
  , meetStatus
93

94
  -- * Evaluation
95
  , Tol (..)
96
  , zeroTol
97
  , Eval (..)
98
  , isInDomain
99
  , isIntegral
100
  , isInBounds
101

102
  -- * File I/O
103
  -- $IO
104
  , FileOptions (..)
105
  , WriteSetting (..)
106
  , ParseError
107
  , isGZipSupported
108

109
  -- ** Reading problem files
110
  , readFile
111
  , readLPFile
112
  , readMPSFile
113
  , parseLPString
114
  , parseMPSString
115

116
  -- ** Generating problem files
117
  , writeFile
118
  , writeLPFile
119
  , writeMPSFile
120
  , toLPString
121
  , toMPSString
122

123
  -- * Utilities
124
  , Default (..)
125
  , Variables (..)
126
  , intersectBounds
127
  ) where
128

129
import Prelude hiding (readFile, writeFile)
130
import Control.Applicative
131
import Control.Exception
132
import Data.Scientific (Scientific)
133
import Data.String
134
import qualified Data.Text.Lazy as TL
135
import System.IO hiding (readFile, writeFile)
136
import Text.Megaparsec (Stream (..))
137

138
import Numeric.Optimization.MIP.Base
139
import Numeric.Optimization.MIP.FileUtils
140
import qualified Numeric.Optimization.MIP.LPFile as LPFile
141
import qualified Numeric.Optimization.MIP.MPSFile as MPSFile
142

143
-- | Parse LP or MPS file based on file extension.
144
readFile :: FileOptions -> FilePath -> IO (Problem Scientific)
145
readFile opt fname =
1✔
146
  case getBaseExtension fname of
1✔
147
    ".lp"  -> readLPFile opt fname
1✔
148
    ".mps" -> readMPSFile opt fname
1✔
149
    ext -> ioError $ userError $ "unknown extension: " ++ ext
×
150

151
-- | Parse a file containing LP file data.
152
readLPFile :: FileOptions -> FilePath -> IO (Problem Scientific)
153
readLPFile = LPFile.parseFile
1✔
154

155
-- | Parse a file containing MPS file data.
156
readMPSFile :: FileOptions -> FilePath -> IO (Problem Scientific)
157
readMPSFile = MPSFile.parseFile
1✔
158

159
-- | Parse a string containing LP file data.
160
parseLPString :: (Stream s, Token s ~ Char, IsString (Tokens s)) => FileOptions -> String -> s -> Either (ParseError s) (Problem Scientific)
161
parseLPString = LPFile.parseString
×
162

163
-- | Parse a string containing MPS file data.
164
parseMPSString :: (Stream s, Token s ~ Char, IsString (Tokens s)) => FileOptions -> String -> s -> Either (ParseError s) (Problem Scientific)
165
parseMPSString = MPSFile.parseString
×
166

167
-- | Generate LP file or MPS file based on file extension.
168
writeFile :: FileOptions -> FilePath -> Problem Scientific -> IO ()
169
writeFile opt fname prob =
1✔
170
  case getBaseExtension fname of
1✔
171
    ".lp"  -> writeLPFile opt fname prob
1✔
172
    ".mps" -> writeMPSFile opt fname prob
1✔
173
    ext -> ioError $ userError $ "unknown extension: " ++ ext
×
174

175
-- | Generate LP file.
176
writeLPFile :: FileOptions -> FilePath -> Problem Scientific -> IO ()
177
writeLPFile opt fname prob =
1✔
178
  case LPFile.render opt{ optNewline = optNewline opt <|> Just nativeNewline } prob of
1✔
179
    Left err -> ioError $ userError err
×
180
    Right s -> writeTextFile opt fname (isAscii prob) s
×
181

182
-- | Generate MPS file.
183
writeMPSFile :: FileOptions -> FilePath -> Problem Scientific -> IO ()
184
writeMPSFile opt fname prob =
1✔
185
  case MPSFile.render opt{ optNewline = optNewline opt <|> Just nativeNewline } prob of
1✔
186
    Left err -> ioError $ userError err
×
187
    Right s -> writeTextFile opt fname (isAscii prob) s
×
188

189
-- | Generate a 'TL.Text' containing LP file data.
190
toLPString :: FileOptions -> Problem Scientific -> Either String TL.Text
191
toLPString opt = LPFile.render opt{ optNewline = optNewline opt <|> Just LF }
1✔
192

193
-- | Generate a 'TL.Text' containing MPS file data.
194
toMPSString :: FileOptions -> Problem Scientific -> Either String TL.Text
195
toMPSString opt = MPSFile.render opt{ optNewline = optNewline opt <|> Just LF }
1✔
196

197
-- $IO
198
-- If this library is built with @WithZlib@ flag (enabled by default), 
199
-- reading/writing gzipped file (@.gz@) are also supported.
200
-- Availability of gzipped file support can ben checked using 'isGZipSupported'.
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