diff --git a/main/demand/demand.metta b/main/demand/demand.metta index 9249d95..256c49a 100644 --- a/main/demand/demand.metta +++ b/main/demand/demand.metta @@ -64,3 +64,59 @@ ) (= (getDemandByName $space $name) (match $space (demand $name $x) (demand $name $x))) + +(= (demandDecay $space $demand $value) + (let* ( + ($oldValue (match $space (demand $demand $oldValue) $oldValue)) + ($newValue (- $oldValue $value)) + ) + (if (>= $newValue 0) + (setDemandValue $space $demand $newValue) + (let* (($demandAtom (match $space (demand $demand $oldValue) (demand $demand $oldValue))) + (() (remove-atom $space $demandAtom)) + ) + ( + (remove-atom $space (skip $demand)) + ) + ) + ) +)) + +; New function to apply decay only to skipped demands +(= (decaySkippedDemands $space $decayValue) + (let* ( + ($skippedDemandNames (collapse (match $space (skip $x) $x))) + ($skippedDemands (collapse (match $space (demand $x $value) (demand $x $value)))) + ) + (if (== (size-atom $skippedDemands) 0 ) + (There is no skipped demand) + (demandDecay $space (superpose $skippedDemandNames) $decayValue) + )) +) +; New function to apply decay to all demands (both enabled and skipped) +(= (decayAllDemands $space $decayValue) + (let* ( + ($allDemands (getAllDemands $space)) + ($demandNames (collapse (match $space (demand $x $_) $x))) + ) + (if (== (size-atom $allDemands) 0 ) + (No demands in the space) + (demandDecay $space (superpose $demandNames) $decayValue) + )) +) + +; New function to apply different decay rates to enabled vs skipped demands +(= (decayDemandsWithDifferentRates $space $enabledDecayRate $skippedDecayRate) + (let* ( + ($enabledDemandNames (getEnabledDemands $space)) + ($enabledDemands (collapse (unify $enabledDemandNames (demand $x $_) $x ()))) + ($skippedDemandNames (collapse (match $space (skip $x) $x))) + ($_ (decaySkippedDemands $space $skippedDecayRate)) + ) + (if (== (size-atom $enabledDemands) 0) + (There is no skipped demand) + (demandDecay $space (superpose $enabledDemands) $enabledDecayRate) + + )) +) + diff --git a/main/demand/tests/demand-test.metta b/main/demand/tests/demand-test.metta index 4ebb80f..68a4d30 100644 --- a/main/demand/tests/demand-test.metta +++ b/main/demand/tests/demand-test.metta @@ -12,6 +12,7 @@ !(skipDemand &demandSpace competence) !(assertEqual (getDemandByName &demandSpace energy) (demand energy 0.6)) + !(assertEqual (exists-In &demandSpace (demand energy 0.6)) True) !(assertEqual (exists-In &demandSpace (demand competence 0.2)) True) !(assertEqual (getAllDemands &demandSpace ) ((demand energy 0.6) (demand affiliation 0.5) (demand competence 0.2))) @@ -24,3 +25,13 @@ !(assertEqual (fetch-demand &demandSpace energy) (demand energy 0.8)) !(assertEqual (fetch-demand-val &demandSpace energy) 0.8) !(assertEqual (getEnabledDemands &demandSpace) ((demand energy 0.8) (demand affiliation 0.5) )) + +!(bind! &decayspace (new-space)) +!(addDemand &decayspace energy 0.6) +!(addDemand &decayspace affiliation 0.5) +!(addDemand &decayspace competence 0.4) +!(skipDemand &decayspace competence) +!(getEnabledDemands &decayspace) +!(decaySkippedDemands &decayspace 0.1) +!(decayAllDemands &decayspace 0.05) +!(get-atoms &decayspace)