Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mip2pb test cases #139

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions test/Test/Converter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,22 @@ prop_mip2pb_backward =
===
evalMIP (transformBackward info m) ip

prop_mip2pb_backward' :: Property
prop_mip2pb_backward' =
forAll arbitraryBoundedIP $ \ip ->
case mip2pb ip of
Left err -> counterexample err $ property False
Right ret@(pb, info) ->
counterexample (show ret) $
QM.monadicIO $ do
solver <- arbitrarySolver
method <- QM.pick arbitrary
ret <- QM.run $ optimizePBFormula solver method pb
case ret of
Nothing -> return ()
Just (m, val) -> do
QM.assert $ Just (transformObjValueBackward info val) == evalMIP (transformBackward info m) ip

prop_mip2pb_json :: Property
prop_mip2pb_json =
forAll arbitraryBoundedIP $ \ip ->
Expand All @@ -930,16 +946,16 @@ arbitraryBoundedIP = do
pure (v, (MIP.Finite 0, MIP.Finite 1))
else do
lb <- arbitrary
Positive w <- arbitrary
let ub = lb + 1 + w
NonNegative w <- arbitrary
let ub = fromInteger (ceiling lb) + w
return (v, (MIP.Finite lb, MIP.Finite ub))
let vs = Map.keys bs
vs_bin = [v | (v, (MIP.Finite 0, MIP.Finite 1)) <- Map.toList bs]

dir <- elements [MIP.OptMin, MIP.OptMax]
obj <- arbitraryMIPExpr vs

nc <- choose (0,10)
nc <- choose (0,5)
cs <- replicateM nc $ do
ind <-
if null vs_bin then
Expand All @@ -954,7 +970,7 @@ arbitraryBoundedIP = do
pure $ Just (v, rhs)
e <- arbitraryMIPExpr vs
lb <- oneof [pure MIP.NegInf, MIP.Finite <$> arbitrary, pure MIP.PosInf]
ub <- oneof [pure MIP.NegInf, MIP.Finite <$> arbitrary, pure MIP.PosInf]
ub <- oneof $ [pure MIP.NegInf, MIP.Finite <$> arbitrary, pure MIP.PosInf] ++ [pure lb | case lb of{ MIP.Finite _ -> True; _ -> False }]
isLazy <- arbitrary
return $ MIP.def
{ MIP.constrIndicator = ind
Expand All @@ -964,11 +980,24 @@ arbitraryBoundedIP = do
, MIP.constrIsLazy = isLazy
}

sos <-
if length vs_bin == 0 then
pure []
else do
n <- choose (0, 1)
replicateM n $ do
t <- elements [MIP.S1, MIP.S2]
m <- choose (0, length vs_bin `div` 2)
xs <- liftM (take m) $ shuffle vs_bin
ns <- shuffle (map fromIntegral [0 .. length xs - 1])
pure (MIP.SOSConstraint{ MIP.sosLabel = Nothing, MIP.sosType = t, MIP.sosBody = zip xs ns })

return $ MIP.def
{ MIP.objectiveFunction = MIP.def{ MIP.objDir = dir, MIP.objExpr = obj }
, MIP.varType = fmap (\_ -> MIP.IntegerVariable) bs
, MIP.varBounds = bs
, MIP.constraints = cs
, MIP.sosConstraints = sos
}

arbitraryMIPExpr :: [MIP.Var] -> Gen (MIP.Expr Rational)
Expand All @@ -980,7 +1009,7 @@ arbitraryMIPExpr vs = do
if nv==0
then return []
else do
m <- choose (0,nv)
m <- choose (0,2)
replicateM m (elements vs)
c <- arbitrary
return $ MIP.Term c ls
Expand Down
Loading