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

- Extract optimal variable values directly from dictionary constants without an intermediate tableau conversion. ([#22](https://github.com/rasheedja/simplex-method/pull/22))
- Remove QuickCheck from the library dependencies while retaining it for property tests. ([#24](https://github.com/rasheedja/simplex-method/pull/24))
- `twoPhaseSimplex` now takes a `VarDomainMap` as its first argument
- Specify each variable's domain using smart constructors: `nonNegative`, `unbounded`, `lowerBoundOnly`, `upperBoundOnly`, or `boundedRange`
Expand Down
51 changes: 11 additions & 40 deletions src/Linear/Simplex/Solver/TwoPhase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import Linear.Simplex.Types
)
import Linear.Simplex.Util
( combineVarLitMapSums
, dictionaryFormToTableau
, foldVarLitMap
, insertPivotObjectiveToDict
, isMax
Expand Down Expand Up @@ -331,49 +330,21 @@ optimizeFeasibleSystem objFunction fsys@(FeasibleSystem {dict = phase1Dict, ..})
logMsg LevelInfo "optimizeFeasibleSystem: Objective is unbounded (ratio test failed)"
pure Unbounded
Just resultDict -> do
let result = displayResults (dictionaryFormToTableau resultDict)
let result = displayResults resultDict
logMsg LevelInfo $ "optimizeFeasibleSystem: Found optimal solution: " <> showT result
pure result
where
-- \| displayResults takes a 'Tableau' and returns an 'OptimisationOutcome'. The 'Tableau'
-- represents the final tableau of a linear program after the simplex
-- algorithm has been applied. The 'OptimisationOutcome' contains the values of all
-- variables appearing in the system.
--
-- The function first filters out the rows of the tableau that correspond
-- to the slack and artificial variables. It then extracts the values of
-- the remaining variables and stores them in a map. If the objective
-- function is a maximization problem, the map contains the values of the
-- variables as they appear in the final tableau. If the objective function
-- is a minimization problem, the map contains the values of the variables
-- as they appear in the final tableau, except for the objective variable,
-- which is negated.
displayResults :: Tableau -> OptimisationOutcome
displayResults tableau =
Optimal extractVarVals
-- Extract basic-variable values directly from the final dictionary.
-- Omit slack/artificial variables and restore the minimization objective sign.
displayResults :: Dict -> OptimisationOutcome
displayResults resultDict =
Optimal $ M.mapWithKey valueOf originalRows
where
extractVarVals =
let tableauWithOriginalVars =
M.filterWithKey
( \basicVarName _ ->
basicVarName `notElem` slackVars ++ artificialVars
)
tableau
in case objFunction of
Max _ ->
M.map
( \tableauRow ->
tableauRow.rhs
)
tableauWithOriginalVars
Min _ ->
M.mapWithKey -- We maximized -objVar, so we negate the objVar to get the final value
( \basicVarName tableauRow ->
if basicVarName == objectiveVar
then negate $ tableauRow.rhs
else tableauRow.rhs
)
tableauWithOriginalVars
originalRows = M.filterWithKey (\var _ -> var `notElem` slackVars ++ artificialVars) resultDict
valueOf var row =
if not (isMax objFunction) && var == objectiveVar
then negate row.constant
else row.constant

-- \| Objective to use when optimising the linear program if no artificial
-- variables were necessary in the first phase. It is essentially the original
Expand Down
Loading