Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
111 changes: 75 additions & 36 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,31 +7,28 @@
; (: 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))
)
()
)
)

(= (fetch-demand $space $name)
(match $space (demand $name $x) (demand $name $x))
)

(= (fetch-demand-val $space $name)
(let ($x $y $z) (fetch-demand $space $name) $z)
)

(= (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)


)))

(= (skipDemand $space $demand )
(= (skipDemand $space $demand)
(let* (
($skip (collapse (match $space (skip $demand) (skip $demand))))
( $isNotFound (== $skip ()))
Expand All @@ -45,11 +40,6 @@
)
)


; !(add-reduct &demandspace (superpose (
; (skip competence 0.5)
; )))

(= (getEnabledDemands $space)
(let* (
( $skippedDemands (collapse (match $space (, (demand $x $_) (skip $x)) (demand $x $_)) ))
Expand All @@ -73,20 +63,69 @@
)

)
(= (getDemandByName $space $name) (match $space (demand $name $x) (demand $name $x)))

; Fixed DemandDecay function to properly take space parameter
(= (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))
(do
Copy link
Contributor

@samwondim samwondim Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abrham17 is do supposed to be a MeTTa function?

(remove-atom $space $demandAtom)
(remove-atom $space (skip $demand))
)
)
)
))

!(skipDemand energy)
!(skipDemand affiliation)

!(setDemandValue energy 0.6)


!(getEnabledDemands)


!(get-atoms &demandspace)




; 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)

))
)
!(addDemand &demandspace energy 0.6)
!(addDemand &demandspace affiliation 0.5)
!(addDemand &demandspace competence 0.4)
!(skipDemand &demandspace competence)
!(getEnabledDemands &demandspace)
!(DecaySkippedDemands &demandspace 0.1)
!(DecayAllDemands &demandspace 0.05)
!(DecayDemandsWithDifferentRates &demandspace 0.05 0.5)

!(get-atoms &demandspace)
46 changes: 23 additions & 23 deletions main/demand/tests/demand-test.metta
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
!(register-module! ../../../utilities-module)
!(import! &self utilities-module:utils)
!(import! &self hyperon-openpsi:psi-utilities:psi_utils)
!(import! &self hyperon-openpsi:main:demand)
!(import! &self hyperon-openpsi:main:demand:demand)

!(bind! &demandSpace (new-space))


;TODO: move these to utilities-module
(= (existsIn $space $atom)
(case (get-metatype $atom)
(
(Expression (let $matchedAtoms (collapse (match $space $atom $atom)) (
if (== $matchedAtoms ()) False True
)
))
(Symbol ( let $res (collapse (get-type-space $space $atom)) (not (== (%Undefined%) $res)) ))
)
)
)
!(addDemand &demandSpace energy 0.6)
!(addDemand &demandSpace affiliation 0.5)
!(addDemand &demandSpace competence 0.2)
!(skipDemand &demandSpace competence)

; !(assertEqual (existsIn &demandSpace (demand energy 1.4)) False)
; !(assertEqual (existsIn &demandSpace (demand competence 0.2)) True)
; !(assertEqual (getAllDemands &demandSpace ) ((demand energy 0.6) (demand affiliation 0.5) (demand competence 0.2)))
; !(assertEqual (existsIn &demandSpace (skip competence)) True)
!(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)))
;!(assertEqual (exists-In &demandSpace (skip competence)) True)

!(setDemandValue &demandSpace energy 0.8)
;!(assertEqual (exists-In &demandSpace (demand energy 0.6)) False)
;!(assertEqual (exists-In &demandSpace (demand energy 0.8)) True)

!(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) ))

; !(assertEqual (getEnabledDemands &demandSpace) ((demand affiliation 0.5) (demand energy 0.6)))
; !(setDemandValue &demandSpace energy 0.8)
; !(assertEqual (existsIn &demandSpace (demand energy 0.8)) True)
; ; !(getAllDemands &demandSpace)
; !(get-atoms &demandSpace)
!(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)