Skip to content

Commit

Permalink
Merge pull request trueagi-io#550 from astroseger/simple-bhv-binding
Browse files Browse the repository at this point in the history
Minimal integration of Hypervectors to Metta (via PyBHV python library)
  • Loading branch information
Necr0x0Der authored Jan 29, 2024
2 parents 169da1d + f580941 commit 598a051
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/sandbox/bhv_binding/01_example_majority.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!(extend-py! bhv_binding)

!(bind! &a (bhv-new))
!(bind! &b (bhv-new))
!(bind! &c (bhv-new))

!(bind! &abc (bhv-majority &a &b &c ))

!(bhv-std-apart-relative &a &abc)
!(bhv-is-related &a &abc)


21 changes: 21 additions & 0 deletions python/sandbox/bhv_binding/02_example_perm.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
!(extend-py! bhv_binding)

!(bind! &a (bhv-new))
!(bind! &b (bhv-new))

!(bind! &perm1 (bhv-new-perm))
(= (perm1 $x) (bhv-apply-perm &perm1 $x))

!(bind! ab (bhv-majority &a &b ))

; Should return False since a is permuted but ab is not permuted
!(bhv-is-related (perm1 &a) ab)

;Should return True
!(bhv-is-related (perm1 &a) (perm1 ab))






17 changes: 17 additions & 0 deletions python/sandbox/bhv_binding/03_example_dict.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
!(extend-py! bhv_binding)

!(bind! &v1 (bhv-new))
!(bind! &v2 (bhv-new))
!(bind! &v3 (bhv-new))
!(bind! &k1 (bhv-new))
!(bind! &k2 (bhv-new))
!(bind! &k3 (bhv-new))


!(bind! dict (bhv-majority (bhv-bind &v1 &k1) (bhv-bind &v2 &k2) (bhv-bind &v3 &k3) ))
!(bind! v1_retrived (bhv-bind &k1 dict))

!(bhv-std-apart-relative v1_retrived &v1)
!(bhv-is-related v1_retrived &v1)


32 changes: 32 additions & 0 deletions python/sandbox/bhv_binding/04_example_dollar_of_mexico.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
!(extend-py! bhv_binding)

!(bind! &name (bhv-new))
!(bind! &capital_city (bhv-new))
!(bind! &money (bhv-new))
!(bind! &united_states (bhv-new))
!(bind! &washington_dc (bhv-new))
!(bind! &dollar (bhv-new))
!(bind! &mexico (bhv-new))
!(bind! &mexico_city (bhv-new))
!(bind! &peso (bhv-new))


!(bind! USA (bhv-majority (bhv-bind &name &united_states) (bhv-bind &capital_city &washington_dc) (bhv-bind &money &dollar) ))
!(bind! MEX (bhv-majority (bhv-bind &name &mexico) (bhv-bind &capital_city &mexico_city) (bhv-bind &money &peso) ))

!(bind! Pair (bhv-bind USA MEX))

; should return True here
!(bhv-is-related (bhv-bind USA &money) &dollar)


!(bind! dollar_of_mexico (bhv-bind &dollar Pair))

; should return True here. "Dollar of mexico" is peso
!(bhv-is-related dollar_of_mexico &peso)






16 changes: 16 additions & 0 deletions python/sandbox/bhv_binding/bhv_binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from hyperon.atoms import OperationAtom
from hyperon.ext import register_atoms
from bhv.np import NumPyBoolBHV as BHV, NumPyBoolPermutation as Perm


@register_atoms
def my_atoms():
return {
'bhv-new': OperationAtom('bhv-new', BHV.rand),
'bhv-majority': OperationAtom('bhv-majority', lambda *args: BHV.majority(list(args))),
'bhv-bind': OperationAtom('bhv-bind', lambda a,b: a^b),
'bhv-std-apart-relative': OperationAtom('bhv-std-apart-relative', lambda a,b: a.std_apart(b, relative = True)),
'bhv-is-related': OperationAtom('bhv-is-related', lambda a,b: a.related(b)),
'bhv-new-perm': OperationAtom('bhv-perm', Perm.random),
'bhv-apply-perm': OperationAtom('bhv-apply-perm', lambda a, b: a(b))
}

0 comments on commit 598a051

Please sign in to comment.