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