Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased changes

- Correct fractional coefficients in pretty-printed expressions and remove trailing plus separators. ([#19](https://github.com/rasheedja/simplex-method/pull/19))
- Share shift and split coefficient-map transformations between objectives and constraints, and simplify access to their fields. ([#23](https://github.com/rasheedja/simplex-method/pull/23))
- Avoid ambiguous constraint record updates using the existing field lenses, group the substitution helpers together, and test them directly.
- Use standard Data.Map selection and deletion operations for solver variable filtering. ([#20](https://github.com/rasheedja/simplex-method/pull/20))
Expand Down
19 changes: 10 additions & 9 deletions src/Linear/Simplex/Prettify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
module Linear.Simplex.Prettify where

import Data.Generics.Labels ()
import Data.List (intercalate)
import Data.Map qualified as M
import Data.Ratio (denominator, numerator)
import Linear.Simplex.Types (ObjectiveFunction (..), PolyConstraint (..), VarLitMapSum)

-- | Convert a 'VarLitMapSum' into a human-readable 'String'
prettyShowVarLitMapSum :: VarLitMapSum -> String
prettyShowVarLitMapSum = aux . M.toList
prettyShowVarLitMapSum = intercalate " + " . map prettyShowTerm . M.toList
where
aux [] = ""
aux ((vName, vCoeff) : vs) = prettyShowRational vCoeff ++ " * " ++ show vName ++ " + " ++ aux vs
prettyShowTerm (vName, vCoeff) = prettyShowRational vCoeff ++ " * " ++ show vName

prettyShowRational r =
if r < 0 then "(" ++ rendered ++ ")" else rendered
where
prettyShowRational r =
if r < 0
then "(" ++ r' ++ ")"
else r'
where
r' = if denominator r == 1 then show (numerator r) else show (numerator r) ++ " / " ++ show (numerator r)
rendered =
if denominator r == 1
then show (numerator r)
else show (numerator r) ++ " / " ++ show (denominator r)

-- | Convert a 'PolyConstraint' into a human-readable 'String'
prettyShowPolyConstraint :: PolyConstraint -> String
Expand Down
20 changes: 12 additions & 8 deletions test/Linear/Simplex/Solver/TwoPhaseSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2677,7 +2677,7 @@
domainMap = VarDomainMap $ M.fromList [(1, lowerBoundOnly (-5))]
let ([newObj], newConstraints, transforms) = preprocess [obj] domainMap constraints
length transforms `shouldBe` 1
case head transforms of

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on macos-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on ubuntu-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on ubuntu-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on windows-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on macos-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on windows-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on windows-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on ubuntu-latest

In the use of ‘head’

Check warning on line 2680 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on macos-latest

In the use of ‘head’
Shift {..} -> do
originalVar `shouldBe` 1
shiftBy `shouldBe` (-5)
Expand All @@ -2689,7 +2689,7 @@
domainMap = VarDomainMap $ M.fromList [(1, unbounded)]
let (_, _, transforms) = preprocess [obj] domainMap constraints
length transforms `shouldBe` 1
case head transforms of

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on macos-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on ubuntu-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on ubuntu-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on windows-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on macos-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on windows-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.10 on windows-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.8 on ubuntu-latest

In the use of ‘head’

Check warning on line 2692 in test/Linear/Simplex/Solver/TwoPhaseSpec.hs

View workflow job for this annotation

GitHub Actions / GHC 9.12 on macos-latest

In the use of ‘head’
Split {..} -> originalVar `shouldBe` 1
_ -> expectationFailure "Expected Split transform"

Expand Down Expand Up @@ -3120,31 +3120,35 @@
prettyShowVarLitMapSum M.empty `shouldBe` ""

it "shows single positive coefficient" $ do
prettyShowVarLitMapSum (M.fromList [(1, 3)]) `shouldBe` "3 * 1 + "
prettyShowVarLitMapSum (M.fromList [(1, 3)]) `shouldBe` "3 * 1"

it "shows single negative coefficient with parentheses" $ do
prettyShowVarLitMapSum (M.fromList [(1, -2)]) `shouldBe` "(-2) * 1 + "
prettyShowVarLitMapSum (M.fromList [(1, -2)]) `shouldBe` "(-2) * 1"

it "shows positive and negative fractional coefficients exactly" $ do
prettyShowVarLitMapSum (M.fromList [(1, 1 % 2), (2, (-3) % 4)])
`shouldBe` "1 / 2 * 1 + (-3 / 4) * 2"

it "shows multiple coefficients" $ do
let result = prettyShowVarLitMapSum (M.fromList [(1, 2), (2, 3)])
result `shouldBe` "2 * 1 + 3 * 2 + "
result `shouldBe` "2 * 1 + 3 * 2"

describe "prettyShowPolyConstraint" $ do
it "shows LEQ constraint" $ do
prettyShowPolyConstraint (LEQ (M.fromList [(1, 2)]) 10) `shouldBe` "2 * 1 + " ++ " <= " ++ show (10 :: Rational)
prettyShowPolyConstraint (LEQ (M.fromList [(1, 2)]) 10) `shouldBe` "2 * 1" ++ " <= " ++ show (10 :: Rational)

it "shows GEQ constraint" $ do
prettyShowPolyConstraint (GEQ (M.fromList [(1, 1)]) 5) `shouldBe` "1 * 1 + " ++ " >= " ++ show (5 :: Rational)
prettyShowPolyConstraint (GEQ (M.fromList [(1, 1)]) 5) `shouldBe` "1 * 1" ++ " >= " ++ show (5 :: Rational)

it "shows EQ constraint" $ do
prettyShowPolyConstraint (EQ (M.fromList [(1, 1)]) 3) `shouldBe` "1 * 1 + " ++ " == " ++ show (3 :: Rational)
prettyShowPolyConstraint (EQ (M.fromList [(1, 1)]) 3) `shouldBe` "1 * 1" ++ " == " ++ show (3 :: Rational)

describe "prettyShowObjectiveFunction" $ do
it "shows Max objective" $ do
prettyShowObjectiveFunction (Max (M.fromList [(1, 2)])) `shouldBe` "max: 2 * 1 + "
prettyShowObjectiveFunction (Max (M.fromList [(1, 2)])) `shouldBe` "max: 2 * 1"

it "shows Min objective" $ do
prettyShowObjectiveFunction (Min (M.fromList [(1, 5)])) `shouldBe` "min: 5 * 1 + "
prettyShowObjectiveFunction (Min (M.fromList [(1, 5)])) `shouldBe` "min: 5 * 1"

describe "twoPhaseSimplex with multiple objectives" $ do
it "optimizes two objectives over the same feasible region" $ do
Expand Down
Loading