Skip to content

Commit

Permalink
generate '#product=' and 'sizeproduct=' in header line of OPB/WBO files
Browse files Browse the repository at this point in the history
  • Loading branch information
msakai committed Jun 20, 2015
1 parent 1a7558b commit a41a776
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
0.1.1.0
-------
* parse* functions fails if the parser does not consume all of the inputs
* generate '#product=' and 'sizeproduct=' in header line of OPB/WBO files
17 changes: 15 additions & 2 deletions src/Data/PseudoBoolean/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ module Data.PseudoBoolean.Builder
, toWBOString
) where

import qualified Prelude
import Prelude hiding (sum)
import qualified Data.DList as DList
import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import Data.Monoid hiding (Sum (..))
import Data.String
import Text.Printf
Expand All @@ -34,7 +37,12 @@ opbBuilder opb = (size <> part1 <> part2)
where
nv = pbNumVars opb
nc = pbNumConstraints opb
size = fromString (printf "* #variable= %d #constraint= %d\n" nv nc)
p = pbProducts opb
np = Set.size p
sp = Prelude.sum [IntSet.size tm | tm <- Set.toList p]
size = fromString (printf "* #variable= %d #constraint= %d" nv nc)
<> (if np >= 1 then fromString (printf " #product= %d sizeproduct= %d" np sp) else mempty)
<> fromString "\n"
part1 =
case pbObjectiveFunction opb of
Nothing -> mempty
Expand All @@ -47,7 +55,12 @@ wboBuilder wbo = size <> part1 <> part2
where
nv = wboNumVars wbo
nc = wboNumConstraints wbo
size = fromString (printf "* #variable= %d #constraint= %d\n" nv nc)
p = wboProducts wbo
np = Set.size p
sp = Prelude.sum [IntSet.size tm | tm <- Set.toList p]
size = fromString (printf "* #variable= %d #constraint= %d" nv nc)
<> (if np >= 1 then fromString (printf " #product= %d sizeproduct= %d" np sp) else mempty)
<> fromString "\n"
part1 =
case wboTopCost wbo of
Nothing -> fromString "soft: ;\n"
Expand Down
17 changes: 15 additions & 2 deletions src/Data/PseudoBoolean/ByteStringBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ module Data.PseudoBoolean.ByteStringBuilder
, hPutWBO
) where

import qualified Prelude
import Prelude hiding (sum)
import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import Data.Monoid hiding (Sum (..))
import qualified Data.ByteString.Lazy as BS
import Data.ByteString.Builder (Builder, intDec, integerDec, char7, string7, hPutBuilder, toLazyByteString)
Expand All @@ -40,7 +43,12 @@ opbBuilder opb = (size <> part1 <> part2)
where
nv = pbNumVars opb
nc = pbNumConstraints opb
size = string7 "* #variable= " <> intDec nv <> string7 " #constraint= " <> intDec nc <> char7 '\n'
p = pbProducts opb
np = Set.size p
sp = Prelude.sum [IntSet.size tm | tm <- Set.toList p]
size = string7 "* #variable= " <> intDec nv <> string7 " #constraint= " <> intDec nc
<> (if np >= 1 then string7 " #product= " <> intDec np <> string7 " sizeproduct= " <> intDec sp else mempty)
<> char7 '\n'
part1 =
case pbObjectiveFunction opb of
Nothing -> mempty
Expand All @@ -53,7 +61,12 @@ wboBuilder wbo = size <> part1 <> part2
where
nv = wboNumVars wbo
nc = wboNumConstraints wbo
size = string7 "* #variable= " <> intDec nv <> string7 " #constraint= " <> intDec nc <> char7 '\n'
p = wboProducts wbo
np = Set.size p
sp = Prelude.sum [IntSet.size tm | tm <- Set.toList p]
size = string7 "* #variable= " <> intDec nv <> string7 " #constraint= " <> intDec nc
<> (if np >= 1 then string7 " #product= " <> intDec np <> string7 " sizeproduct= " <> intDec sp else mempty)
<> char7 '\n'
part1 =
case wboTopCost wbo of
Nothing -> string7 "soft: ;\n"
Expand Down
23 changes: 23 additions & 0 deletions src/Data/PseudoBoolean/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ module Data.PseudoBoolean.Types

-- * Internal utilities
, pbComputeNumVars
, pbProducts
, wboComputeNumVars
, wboProducts
) where

import GHC.Generics (Generic)
import Control.Monad
import Control.DeepSeq
import Data.Data
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.Hashable
import Data.Maybe

Expand Down Expand Up @@ -117,3 +124,19 @@ wboComputeNumVars cs = maximum (0 : vs)
(_, tm) <- s
lit <- tm
return $ abs lit

pbProducts :: Formula -> Set IntSet
pbProducts formula = Set.fromList $ do
s <- maybeToList (pbObjectiveFunction formula) ++ [s | (s,_,_) <- pbConstraints formula]
(_, tm) <- s
let tm2 = IntSet.fromList tm
guard $ IntSet.size tm2 > 1
return tm2

wboProducts :: SoftFormula -> Set IntSet
wboProducts softformula = Set.fromList $ do
(_,(s,_,_)) <- wboConstraints softformula
(_, tm) <- s
let tm2 = IntSet.fromList tm
guard $ IntSet.size tm2 > 1
return tm2

0 comments on commit a41a776

Please sign in to comment.