diff --git a/ChangeLog.md b/ChangeLog.md index 6f08c37..ba9a84e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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)) diff --git a/src/Linear/Simplex/Solver/TwoPhase.hs b/src/Linear/Simplex/Solver/TwoPhase.hs index 87e04af..47f7c1e 100644 --- a/src/Linear/Simplex/Solver/TwoPhase.hs +++ b/src/Linear/Simplex/Solver/TwoPhase.hs @@ -106,7 +106,7 @@ findFeasibleSolution unsimplifiedSystem = do M.map ( \DictValue {..} -> DictValue - { varMapSum = M.filterWithKey (\k _ -> k `notElem` artificialVars) varMapSum + { varMapSum = M.withoutKeys varMapSum artificialVarSet , .. } ) @@ -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 @@ -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 } ) @@ -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. @@ -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