Skip to content

Commit a5e7e2e

Browse files
committed
chore: replace twoPhaseSimplex with VarDomain version
1 parent 77072ca commit a5e7e2e

2 files changed

Lines changed: 72 additions & 118 deletions

File tree

src/Linear/Simplex/Solver/TwoPhase.hs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module Linear.Simplex.Solver.TwoPhase
1515
( findFeasibleSolution
1616
, optimizeFeasibleSystem
1717
, twoPhaseSimplex
18-
, twoPhaseSimplex'
1918
-- Internal functions exported for testing
2019
, preprocess
2120
, postprocess
@@ -399,57 +398,39 @@ optimizeFeasibleSystem objFunction fsys@(FeasibleSystem {dict = phase1Dict, ..})
399398
)
400399
(M.toList objFunction.objective)
401400

402-
-- | Perform the two phase simplex method with a given 'ObjectiveFunction' a system of 'PolyConstraint's.
403-
-- Assumes the 'ObjectiveFunction' and 'PolyConstraint' is not empty.
404-
-- Returns a pair with the first item being the 'Integer' variable equal to the 'ObjectiveFunction'
405-
-- and the second item being a map of the values of all 'Integer' variables appearing in the system, including the 'ObjectiveFunction'.
406-
twoPhaseSimplex :: (MonadIO m, MonadLogger m) => ObjectiveFunction -> [PolyConstraint] -> m (Maybe Result)
407-
twoPhaseSimplex objFunction unsimplifiedSystem = do
408-
logMsg LevelInfo $
409-
"twoPhaseSimplex: Solving system " <> showT unsimplifiedSystem <> " with objective " <> showT objFunction
410-
phase1Result <- findFeasibleSolution unsimplifiedSystem
411-
case phase1Result of
412-
Just feasibleSystem -> do
413-
logMsg LevelInfo $
414-
"twoPhaseSimplex: Feasible system found for "
415-
<> showT unsimplifiedSystem
416-
<> "; Feasible system: "
417-
<> showT feasibleSystem
418-
optimizedSystem <- optimizeFeasibleSystem objFunction feasibleSystem
419-
logMsg LevelInfo $
420-
"twoPhaseSimplex: Optimized system found for "
421-
<> showT unsimplifiedSystem
422-
<> "; Optimized system: "
423-
<> showT optimizedSystem
424-
pure optimizedSystem
425-
Nothing -> do
426-
logMsg LevelInfo $ "twoPhaseSimplex: Phase 1 gives infeasible result for " <> showT unsimplifiedSystem
427-
pure Nothing
428-
429401
-- | Perform the two phase simplex method with variable domain information.
430402
-- Variables not in the VarDomainMap are assumed to be Unbounded (no lower bound).
431403
-- This function applies necessary transformations before solving and unapplies them after.
432404
-- The returned Result contains variable values and objective value in the original space.
433405
-- TODO: use this as twoPhaseSimplex, add instructions in CHANGELOG for old users
434-
twoPhaseSimplex' :: (MonadIO m, MonadLogger m) => VarDomainMap -> ObjectiveFunction -> [PolyConstraint] -> m (Maybe Result)
435-
twoPhaseSimplex' domainMap objFunction constraints = do
406+
twoPhaseSimplex :: (MonadIO m, MonadLogger m) => VarDomainMap -> ObjectiveFunction -> [PolyConstraint] -> m (Maybe Result)
407+
twoPhaseSimplex domainMap objFunction constraints = do
436408
logMsg LevelInfo $
437-
"twoPhaseSimplex': Solving system with domain map " <> showT domainMap
409+
"twoPhaseSimplex: Solving system with domain map " <> showT domainMap
438410
let (transformedObj, transformedConstraints, transforms) = preprocess objFunction domainMap constraints
439411
logMsg LevelInfo $
440-
"twoPhaseSimplex': Applied transforms " <> showT transforms
412+
"twoPhaseSimplex: Applied transforms " <> showT transforms
441413
<> "; Transformed objective: " <> showT transformedObj
442414
<> "; Transformed constraints: " <> showT transformedConstraints
443-
mResult <- twoPhaseSimplex transformedObj transformedConstraints
444-
case mResult of
415+
phase1Result <- findFeasibleSolution transformedConstraints
416+
case phase1Result of
445417
Nothing -> do
446-
logMsg LevelInfo "twoPhaseSimplex': No solution found"
418+
logMsg LevelInfo "twoPhaseSimplex: No feasible solution found in phase 1"
447419
pure Nothing
448-
Just result -> do
449-
let finalResult = postprocess objFunction transforms result
420+
Just feasibleSystem -> do
450421
logMsg LevelInfo $
451-
"twoPhaseSimplex': Postprocessed result: " <> showT finalResult
452-
pure (Just finalResult)
422+
"twoPhaseSimplex: Feasible system found for transformed system; Feasible system: "
423+
<> showT feasibleSystem
424+
mOptimizedSystem <- optimizeFeasibleSystem transformedObj feasibleSystem
425+
case mOptimizedSystem of
426+
Nothing -> do
427+
logMsg LevelInfo "twoPhaseSimplex: No optimized solution found in phase 2"
428+
pure Nothing
429+
Just result -> do
430+
let finalResult = postprocess objFunction transforms result
431+
logMsg LevelInfo $
432+
"twoPhaseSimplex: Postprocessed result: " <> showT finalResult
433+
pure (Just finalResult)
453434

454435
-- | Postprocess the result by unapplying variable transformations and computing
455436
-- the objective value in the original space.

0 commit comments

Comments
 (0)