|
| 1 | +-- | |
| 2 | +-- Module: Linear.Simplex.Constraint.Simple.Util |
| 3 | +-- Description: Utility functions for simple constraints |
| 4 | +-- Copyright: (c) Junaid Rasheed, 2020-2024 |
| 5 | +-- License: BSD-3 |
| 6 | +-- Maintainer: jrasheed178@gmail.com |
| 7 | +-- Stability: experimental |
| 8 | +module Linear.Simplex.Constraint.Simple.Util where |
| 9 | + |
| 10 | +import qualified Data.List as L |
| 11 | +import qualified Data.Set as Set |
| 12 | +import Linear.Simplex.Constraint.Generic.Types |
| 13 | +import Linear.Simplex.Constraint.Simple.Types |
| 14 | +import Linear.Simplex.Constraint.Types |
| 15 | +import Linear.Simplex.Expr.Types |
| 16 | +import Linear.Simplex.Expr.Util |
| 17 | +import Linear.Simplex.Term.Types |
| 18 | +import Linear.Simplex.Var.Types |
| 19 | + |
| 20 | +substVarSimpleConstraint :: Var -> Expr -> SimpleConstraint -> SimpleConstraint |
| 21 | +substVarSimpleConstraint var varReplacement (a :<= b) = substVarExpr var varReplacement a :<= b |
| 22 | +substVarSimpleConstraint var varReplacement (a :>= b) = substVarExpr var varReplacement a :>= b |
| 23 | +substVarSimpleConstraint var varReplacement (a :== b) = substVarExpr var varReplacement a :== b |
| 24 | + |
| 25 | +constraintToSimpleConstraint :: Constraint -> SimpleConstraint |
| 26 | +constraintToSimpleConstraint constraint = |
| 27 | + case constraint of |
| 28 | + (a :<= b) -> uncurry (:<=) (calcLhsRhs a b) |
| 29 | + (a :>= b) -> uncurry (:>=) (calcLhsRhs a b) |
| 30 | + (a :== b) -> uncurry (:==) (calcLhsRhs a b) |
| 31 | + where |
| 32 | + calcLhsRhs a b = (lhs, rhs) |
| 33 | + where |
| 34 | + aConsts = sumExprConstTerms a |
| 35 | + bConsts = sumExprConstTerms b |
| 36 | + rhs = bConsts - aConsts |
| 37 | + |
| 38 | + aWithoutConst = simplifyExpr . zeroConstExpr $ a |
| 39 | + bWithoutConst = simplifyExpr . zeroConstExpr $ b |
| 40 | + |
| 41 | + lhs = subtractExpr aWithoutConst bWithoutConst |
| 42 | + calcRhs a b = rhs |
| 43 | + where |
| 44 | + aConsts = sumExprConstTerms a |
| 45 | + bConsts = sumExprConstTerms b |
| 46 | + rhs = bConsts - aConsts |
| 47 | + |
| 48 | + aWithoutConst = simplifyExpr . zeroConstExpr $ a |
| 49 | + bWithoutConst = simplifyExpr . zeroConstExpr $ b |
| 50 | + |
| 51 | + lhs = subtractExpr aWithoutConst bWithoutConst |
| 52 | + |
| 53 | +-- normalize simple constraints by moving all constants to the right |
| 54 | +normalizeSimpleConstraint :: SimpleConstraint -> SimpleConstraint |
| 55 | +normalizeSimpleConstraint (expr :<= num) = |
| 56 | + let exprList = exprToList expr |
| 57 | + |
| 58 | + isConstTerm (ConstTerm _) = True |
| 59 | + isConstTerm _ = False |
| 60 | + |
| 61 | + (sumExprConstTerms, nonConstTerms) = L.partition isConstTerm exprList |
| 62 | + |
| 63 | + constTermsVal = sum . map (\case (ConstTerm c) -> c; _ -> 0) $ sumExprConstTerms |
| 64 | + |
| 65 | + newExpr = listToExpr nonConstTerms |
| 66 | + newNum = num - constTermsVal |
| 67 | + in newExpr :<= newNum |
| 68 | +normalizeSimpleConstraint (expr :>= num) = |
| 69 | + let exprList = exprToList expr |
| 70 | + |
| 71 | + isConstTerm (ConstTerm _) = True |
| 72 | + isConstTerm _ = False |
| 73 | + |
| 74 | + (sumExprConstTerms, nonConstTerms) = L.partition isConstTerm exprList |
| 75 | + |
| 76 | + constTermsVal = sum . map (\case (ConstTerm c) -> c; _ -> 0) $ sumExprConstTerms |
| 77 | + |
| 78 | + newExpr = listToExpr nonConstTerms |
| 79 | + newNum = num - constTermsVal |
| 80 | + in newExpr :>= newNum |
| 81 | +normalizeSimpleConstraint (expr :== num) = |
| 82 | + let exprList = exprToList expr |
| 83 | + |
| 84 | + isConstTerm (ConstTerm _) = True |
| 85 | + isConstTerm _ = False |
| 86 | + |
| 87 | + (sumExprConstTerms, nonConstTerms) = L.partition isConstTerm exprList |
| 88 | + |
| 89 | + constTermsVal = sum . map (\case (ConstTerm c) -> c; _ -> 0) $ sumExprConstTerms |
| 90 | + |
| 91 | + newExpr = listToExpr nonConstTerms |
| 92 | + newNum = num - constTermsVal |
| 93 | + in newExpr :== newNum |
| 94 | + |
| 95 | +-- | Simplify coeff constraints by dividing the coefficient from both sides |
| 96 | +simplifyCoeff :: SimpleConstraint -> SimpleConstraint |
| 97 | +simplifyCoeff expr@(Expr (CoeffTerm coeff var) :<= num) |
| 98 | + | coeff == 0 = expr |
| 99 | + | coeff > 0 = Expr (VarTerm var) :<= (num / coeff) |
| 100 | + | coeff < 0 = Expr (VarTerm var) :>= (num / coeff) |
| 101 | +simplifyCoeff expr@(Expr (CoeffTerm coeff var) :>= num) |
| 102 | + | coeff == 0 = expr |
| 103 | + | coeff > 0 = Expr (VarTerm var) :>= (num / coeff) |
| 104 | + | coeff < 0 = Expr (VarTerm var) :<= (num / coeff) |
| 105 | +simplifyCoeff expr@(Expr (CoeffTerm coeff var) :== num) = if coeff == 0 then expr else Expr (VarTerm var) :== (num / coeff) |
| 106 | +simplifyCoeff expr = expr |
| 107 | + |
| 108 | +simplifySimpleConstraint :: SimpleConstraint -> SimpleConstraint |
| 109 | +simplifySimpleConstraint (expr :<= num) = simplifyCoeff . normalizeSimpleConstraint $ simplifyExpr expr :<= num |
| 110 | +simplifySimpleConstraint (expr :>= num) = simplifyCoeff . normalizeSimpleConstraint $ simplifyExpr expr :>= num |
| 111 | +simplifySimpleConstraint (expr :== num) = simplifyCoeff . normalizeSimpleConstraint $ simplifyExpr expr :== num |
| 112 | + |
| 113 | +simpleConstraintVars :: SimpleConstraint -> Set.Set Var |
| 114 | +simpleConstraintVars (expr :<= _) = exprVars expr |
| 115 | +simpleConstraintVars (expr :>= _) = exprVars expr |
| 116 | +simpleConstraintVars (expr :== _) = exprVars expr |
0 commit comments