Skip to content

Commit 79a5a6f

Browse files
committed
Test findHighestVar
+ And fix bug with findHighestVar
1 parent d1f2fc6 commit 79a5a6f

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

src/Linear/Simplex/Types.hs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,17 @@ type SimpleSystem = [SimpleConstraint]
341341
simplifySimpleSystem :: SimpleSystem -> SimpleSystem
342342
simplifySimpleSystem = map simplifySimpleConstraint
343343

344+
findHighestVar :: SimpleSystem -> Var
345+
findHighestVar simpleSystem =
346+
let
347+
vars = [v | gc <- simpleSystem,
348+
term <- exprToList $ getGenericConstraintLHS gc,
349+
v <- case term of
350+
VarTerm v -> [v]
351+
CoeffTerm _ v -> [v]
352+
_ -> []]
353+
in maximum vars
354+
344355
data StandardFormRow = StandardFormRow
345356
{ lhs :: Expr
346357
, rhs :: SimplexNum
@@ -406,9 +417,6 @@ removeUselessSystemBounds constraints bounds =
406417
)
407418
constraints
408419

409-
findHighestVar :: SimpleSystem -> Var
410-
findHighestVar = maximum . map (maximum . map (\case (CoeffTerm _ v) -> v; _ -> 0) . exprToList . getGenericConstraintLHS)
411-
412420
-- | Eliminate negative lower bounds via substitution
413421
-- Return the system with the eliminated variables and a map of the eliminated variables to their equivalent expressions
414422
-- First step here https://en.wikipedia.org/wiki/Simplex_algorithm#Standard_form

test/Linear/Simplex/TypesSpec.hs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,32 @@ spec = do
539539
<> show simplifiedSimpleSystemEval
540540
) $
541541
simpleSystemEval == simplifiedSimpleSystemEval
542+
it "findHighestVar finds the highest variable in a simple system" $ do
543+
let simpleSystem1 =
544+
[ Expr (VarTerm 0) :>= 0
545+
, Expr (VarTerm 0) :<= 1
546+
, Expr (VarTerm 1) :>= 0
547+
, Expr (VarTerm 1) :<= 1
548+
]
549+
simpleSystem100 =
550+
[ Expr (VarTerm 0) :<= 1
551+
, Expr (VarTerm 50) :<= 1
552+
, Expr (VarTerm 100) :<= 1
553+
]
554+
simpleSystem10 =
555+
[ Expr (VarTerm (-10)) :<= 1
556+
, Expr (VarTerm 0) :<= 1
557+
, Expr (VarTerm 10) :<= 1
558+
]
559+
simpleSystemMinus10 =
560+
[ Expr (VarTerm (-10)) :<= 1
561+
, Expr (VarTerm (-20)) :<= 1
562+
]
563+
564+
findHighestVar simpleSystem1 `shouldBe` 1
565+
findHighestVar simpleSystem100 `shouldBe` 100
566+
findHighestVar simpleSystem10 `shouldBe` 10
567+
findHighestVar simpleSystemMinus10 `shouldBe` (-10)
542568
describe "Bounds" $ do
543569
it "validateBounds finds that deriving bounds for a system where -1 <= x <= 1 has valid bounds" $ do
544570
let simpleSystem =
@@ -691,4 +717,3 @@ spec = do
691717
simplifiedSimpleSystem = removeUselessSystemBounds simpleSystem bounds
692718
expectedSimpleSystem = [Expr (VarTerm 0) :<= 2, Expr (CoeffTerm 2 0) :<= 6]
693719
simplifiedSimpleSystem `shouldBe` expectedSimpleSystem
694-

0 commit comments

Comments
 (0)