From 4698e433ea29861317ed153e30a477d520e0930c Mon Sep 17 00:00:00 2001 From: abrahm17 Date: Tue, 19 Aug 2025 17:02:21 +0300 Subject: [PATCH 1/4] feat:added demand decay for enabled and skipped demands --- main/demand/demand.metta | 85 ++++++++++++++++++++--------- main/demand/tests/demand-test.metta | 18 ++++++ 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/main/demand/demand.metta b/main/demand/demand.metta index d788fd13..71bd6e62 100644 --- a/main/demand/demand.metta +++ b/main/demand/demand.metta @@ -1,6 +1,4 @@ !(bind! &demandspace (new-space)) -!(bind! &cacheSpace (new-space)) - (: Demand Type) (: SkipDemand Type) ; (: demand (-> Symbol Number Demand)) @@ -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)))) @@ -45,7 +33,7 @@ ) ) - +!(skipDemand &self energy) ; !(add-reduct &demandspace (superpose ( ; (skip competence 0.5) ; ))) @@ -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) @@ -84,9 +124,4 @@ !(getEnabledDemands) -!(get-atoms &demandspace) - - - - - +!(get-atoms &demandspace) \ No newline at end of file diff --git a/main/demand/tests/demand-test.metta b/main/demand/tests/demand-test.metta index 92ce6728..e86a560e 100644 --- a/main/demand/tests/demand-test.metta +++ b/main/demand/tests/demand-test.metta @@ -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) \ No newline at end of file From 28e66a0eca385289c3bffe23068c730d69f58aaf Mon Sep 17 00:00:00 2001 From: abrahm17 Date: Tue, 19 Aug 2025 18:25:30 +0300 Subject: [PATCH 2/4] fix:fix issues related to superpose --- main/demand/demand.metta | 62 ++++++++++++++------------- main/demand/tests/demand-test.metta | 66 +++++++++++------------------ 2 files changed, 57 insertions(+), 71 deletions(-) diff --git a/main/demand/demand.metta b/main/demand/demand.metta index 71bd6e62..5bc093f8 100644 --- a/main/demand/demand.metta +++ b/main/demand/demand.metta @@ -15,13 +15,20 @@ ) ) -!(addDemand &demandspace energy 0.2) +(= (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 $_))) ) -!(getAllDemands &self) -(= (skipDemand $space $demand ) + + +(= (skipDemand $space $demand) (let* ( ($skip (collapse (match $space (skip $demand) (skip $demand)))) ( $isNotFound (== $skip ())) @@ -33,11 +40,6 @@ ) ) -!(skipDemand &self energy) -; !(add-reduct &demandspace (superpose ( -; (skip competence 0.5) -; ))) - (= (getEnabledDemands $space) (let* ( ( $skippedDemands (collapse (match $space (, (demand $x $_) (skip $x)) (demand $x $_)) )) @@ -61,21 +63,24 @@ ) ) +(= (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 (collapse (match $space (demand $demand $oldValue) $oldValue))) + ($oldValue (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))))) + (let $demandAtom (match $space (demand $demand $oldValue) (demand $demand $oldValue)) + (do (remove-atom $space $demandAtom) + (remove-atom $space (skip $demand)) ) ) ) -) +)) ; New function to apply decay only to skipped demands (= (DecaySkippedDemands $space $decayValue) @@ -85,10 +90,9 @@ ) (if (== (size-atom $skippedDemands) 0 ) (There is no skipped demand) - (superpose (DemandDecay $space (superpose $skippedDemandNames) $decayValue)) + (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* ( @@ -97,31 +101,31 @@ ) (if (== (size-atom $allDemands) 0 ) (No demands in the space) - (superpose (DemandDecay $space (superpose $demandNames) $decayValue)) + (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 ())))) + ($enabledDemandNames (getEnabledDemands $space)) + ($enabledDemands (collapse (unify $enabledDemandNames (demand $x $_) $x ()))) ($skippedDemandNames (collapse (match $space (skip $x) $x))) - ($_ (superpose (DemandDecay $space (superpose $enabledDemandNames) $enabledDecayRate))) - ($_ (superpose (DemandDecay $space (superpose $skippedDemandNames) $skippedDecayRate))) + ($_ (DecaySkippedDemands $space $skippedDecayRate)) ) - ( - () + (if (== (size-atom $enabledDemands) 0) + (There is no skipped demand) + (DemandDecay $space (superpose $enabledDemands) $enabledDecayRate) + )) ) - -!(skipDemand energy) -!(skipDemand affiliation) - -!(setDemandValue energy 0.6) - - -!(getEnabledDemands) - +!(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) \ No newline at end of file diff --git a/main/demand/tests/demand-test.metta b/main/demand/tests/demand-test.metta index e86a560e..5b246b53 100644 --- a/main/demand/tests/demand-test.metta +++ b/main/demand/tests/demand-test.metta @@ -2,53 +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 (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) - -; 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) \ No newline at end of file +!(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) )) + +!(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) From 204e3cb6c53b22ec6c00e4bb0593a64bce5a97b6 Mon Sep 17 00:00:00 2001 From: abrahm17 Date: Wed, 20 Aug 2025 10:11:05 +0300 Subject: [PATCH 3/4] fix:fixed a naming problem for The functions --- main/demand/demand.metta | 26 +++++++++++++------------- main/demand/tests/demand-test.metta | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/main/demand/demand.metta b/main/demand/demand.metta index 5bc093f8..d91d41db 100644 --- a/main/demand/demand.metta +++ b/main/demand/demand.metta @@ -65,8 +65,8 @@ ) (= (getDemandByName $space $name) (match $space (demand $name $x) (demand $name $x))) -; Fixed DemandDecay function to properly take space parameter -(= (DemandDecay $space $demand $value) +; Fixed demandDecay function to properly take space parameter +(= (demandDecay $space $demand $value) (let* ( ($oldValue (match $space (demand $demand $oldValue) $oldValue)) ($newValue (- $oldValue $value)) @@ -83,40 +83,40 @@ )) ; New function to apply decay only to skipped demands -(= (DecaySkippedDemands $space $decayValue) +(= (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) + (demandDecay $space (superpose $skippedDemandNames) $decayValue) )) ) ; New function to apply decay to all demands (both enabled and skipped) -(= (DecayAllDemands $space $decayValue) +(= (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) + (demandDecay $space (superpose $demandNames) $decayValue) )) ) ; New function to apply different decay rates to enabled vs skipped demands -(= (DecayDemandsWithDifferentRates $space $enabledDecayRate $skippedDecayRate) +(= (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)) + ($_ (decaySkippedDemands $space $skippedDecayRate)) ) (if (== (size-atom $enabledDemands) 0) (There is no skipped demand) - (DemandDecay $space (superpose $enabledDemands) $enabledDecayRate) - + (demandDecay $space (superpose $enabledDemands) $enabledDecayRate) + )) ) !(addDemand &demandspace energy 0.6) @@ -124,8 +124,8 @@ !(addDemand &demandspace competence 0.4) !(skipDemand &demandspace competence) !(getEnabledDemands &demandspace) -!(DecaySkippedDemands &demandspace 0.1) -!(DecayAllDemands &demandspace 0.05) -!(DecayDemandsWithDifferentRates &demandspace 0.05 0.5) +!(decaySkippedDemands &demandspace 0.1) +!(decayAllDemands &demandspace 0.05) +!(decayDemandsWithDifferentRates &demandspace 0.05 0.05) !(get-atoms &demandspace) \ No newline at end of file diff --git a/main/demand/tests/demand-test.metta b/main/demand/tests/demand-test.metta index 5b246b53..7f8feb75 100644 --- a/main/demand/tests/demand-test.metta +++ b/main/demand/tests/demand-test.metta @@ -31,6 +31,6 @@ !(addDemand &decayspace competence 0.4) !(skipDemand &decayspace competence) !(getEnabledDemands &decayspace) -!(DecaySkippedDemands &decayspace 0.1) -!(DecayAllDemands &decayspace 0.05) +!(decaySkippedDemands &decayspace 0.1) +!(decayAllDemands &decayspace 0.05) !(get-atoms &decayspace) From f9e578e3b3e1cd27c75a5e21ecd769277c8af7e6 Mon Sep 17 00:00:00 2001 From: abrahm17 Date: Tue, 26 Aug 2025 11:45:30 +0300 Subject: [PATCH 4/4] fix:removed non metta 'do' symbol --- main/demand/demand.metta | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/main/demand/demand.metta b/main/demand/demand.metta index d91d41db..347d2b48 100644 --- a/main/demand/demand.metta +++ b/main/demand/demand.metta @@ -73,9 +73,10 @@ ) (if (>= $newValue 0) (setDemandValue $space $demand $newValue) - (let $demandAtom (match $space (demand $demand $oldValue) (demand $demand $oldValue)) - (do - (remove-atom $space $demandAtom) + (let* (($demandAtom (match $space (demand $demand $oldValue) (demand $demand $oldValue))) + (() (remove-atom $space $demandAtom)) + ) + ( (remove-atom $space (skip $demand)) ) )