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

- Use standard Data.Map selection and deletion operations for solver variable filtering. ([#20](https://github.com/rasheedja/simplex-method/pull/20))
- Remove unused pivot bindings and give the active row-update helper a descriptive name. ([#21](https://github.com/rasheedja/simplex-method/pull/21))
- 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))
Expand Down
14 changes: 8 additions & 6 deletions src/Linear/Simplex/Solver/TwoPhase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ findFeasibleSolution unsimplifiedSystem = do
M.map
( \DictValue {..} ->
DictValue
{ varMapSum = M.filterWithKey (\k _ -> k `notElem` artificialVars) varMapSum
{ varMapSum = M.withoutKeys varMapSum artificialVarSet
, ..
}
)
Expand Down Expand Up @@ -193,6 +193,8 @@ findFeasibleSolution unsimplifiedSystem = do

(systemWithBasicVars, artificialVars) = systemWithArtificialVars systemWithSlackVars maxVarWithSlackVars

artificialVarSet = Set.fromList artificialVars

finalMaxVar = if null artificialVars then maxVarWithSlackVars else maximum artificialVars

systemWithBasicVarsAsDictionary = tableauInDictionaryForm systemWithBasicVars
Expand Down Expand Up @@ -288,14 +290,14 @@ findFeasibleSolution unsimplifiedSystem = do
}
where
-- Filter out non-artificial entries
rowsToAdd = M.filterWithKey (\k _ -> k `elem` artificialVars) rows
negatedRows = M.map (\(DictValue rowVarMapSum rowConstant) -> DictValue (M.map negate rowVarMapSum) (negate rowConstant)) rowsToAdd
artificialVarSet = Set.fromList artificialVars
rowsToAdd = M.restrictKeys rows artificialVarSet
-- Negate rows, discard keys and artificial vars since the pivot objective does not care about them
negatedRowsWithoutArtificialVars =
map
( \(_, DictValue {..}) ->
DictValue
{ varMapSum = M.map negate $ M.filterWithKey (\k _ -> k `notElem` artificialVars) varMapSum
{ varMapSum = M.map negate $ M.withoutKeys varMapSum artificialVarSet
, constant = negate constant
}
)
Expand Down Expand Up @@ -469,7 +471,7 @@ postprocess originalVars transforms (Optimal varVals) =
let -- Unapply transforms to get variable values in original space
unappliedVarVals = unapplyTransformsToVarMap transforms varVals
-- Filter to only include original decision variables
filteredVarVals = M.filterWithKey (\k _ -> Set.member k originalVars) unappliedVarVals
filteredVarVals = M.restrictKeys unappliedVarVals originalVars
in Optimal filteredVarVals

-- | Compute the value of an objective function given variable values.
Expand Down Expand Up @@ -841,4 +843,4 @@ simplexPivot objective@(PivotObjective {variable = objectiveVar, function = obje
(error "pivot: Basic variable not found in Dict")
$ M.lookup leavingVariable dict

filterOutEnteringVarTerm = M.filterWithKey (\vName _ -> vName /= enteringVariable)
filterOutEnteringVarTerm = M.delete enteringVariable
Loading