Skip to content

demand decay #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions main/demand/demand.metta
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
!(bind! &demandspace (new-space))
!(bind! &cacheSpace (new-space))

(: Demand Type)
(: SkipDemand Type)
; (: demand (-> Symbol Number Demand))
Expand All @@ -9,30 +7,20 @@
; (: addDemand (-> hyperon::space::DynSpace Symbol Number Demand))
(= (addDemand $space $name $value)
(let* (
($demand (demand $name $value))
; ($duplicateDemandList (match $space (demand $name $v) (demand $name $v)))



(() (add-atom $space $demand))
)
($demand (demand $name $value))
; ($duplicateDemandList (match $space (demand $name $v) (demand $name $v)))
(() (add-atom $space $demand))
)
()
)
)

!(addDemand &demandspace energy 0.2)

(= (getAllDemands $space)
(collapse (match $space (demand $x $_) (demand $x $_)))
)

!(add-reduct &demandspace (superpose (
(demand energy 0.5)
(demand affiliation 0.5)
(demand competence 0.5)


)))

!(getAllDemands &self)
(= (skipDemand $space $demand )
(let* (
($skip (collapse (match $space (skip $demand) (skip $demand))))
Expand All @@ -45,7 +33,7 @@
)
)


!(skipDemand &self energy)
; !(add-reduct &demandspace (superpose (
; (skip competence 0.5)
; )))
Expand Down Expand Up @@ -74,6 +62,58 @@

)

; Fixed DemandDecay function to properly take space parameter
(= (DemandDecay $space $demand $value)
(let* (
($oldValue (collapse (match $space (demand $demand $oldValue) $oldValue)))
($newValue (- $oldValue $value))
)
(if (>= $newValue 0)
(setDemandValue $space $demand $newValue)
(let (($demandAtom (collapse (match $space (demand $demand $oldValue) (demand $demand $oldValue)))))
(remove-atom $space $demandAtom)
)
)
)
)

; 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)
(superpose (DemandDecay $space (superpose $skippedDemandNames) $decayValue))
))
)
!(DecaySkippedDemands &self 0.1)
; 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)
(superpose (DemandDecay $space (superpose $demandNames) $decayValue))
))
)

; New function to apply different decay rates to enabled vs skipped demands
(= (DecayDemandsWithDifferentRates $space $enabledDecayRate $skippedDecayRate)
(let* (
($enabledDemandNames (collapse (match $space (demand $x $_)
(if (== (collapse (match $space (skip $x) (skip $x))) ()) $x ()))))
($skippedDemandNames (collapse (match $space (skip $x) $x)))
($_ (superpose (DemandDecay $space (superpose $enabledDemandNames) $enabledDecayRate)))
($_ (superpose (DemandDecay $space (superpose $skippedDemandNames) $skippedDecayRate)))
)
(
()
))
)

!(skipDemand energy)
!(skipDemand affiliation)
Expand All @@ -84,9 +124,4 @@
!(getEnabledDemands)


!(get-atoms &demandspace)





!(get-atoms &demandspace)
18 changes: 18 additions & 0 deletions main/demand/tests/demand-test.metta
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@
; !(assertEqual (existsIn &demandSpace (demand energy 0.8)) True)
; ; !(getAllDemands &demandSpace)
; !(get-atoms &demandSpace)

; Added tests for the new decay functions
!(bind! &testDecaySpace (new-space))
!(addDemand &testDecaySpace energy 0.8)
!(addDemand &testDecaySpace affiliation 0.6)
!(addDemand &testDecaySpace competence 0.4)
!(skipDemand &testDecaySpace competence)

; Test decay of skipped demands only
!(DecaySkippedDemands &testDecaySpace 0.1)
; competence should now be 0.3, others unchanged

; Test decay of all demands
!(DecayAllDemands &testDecaySpace 0.05)
; All demands should be reduced by 0.05

; Test different decay rates
!(DecayDemandsWithDifferentRates &testDecaySpace 0.02 0.08)