Skip to content

Commit

Permalink
Convert MS to Codelet
Browse files Browse the repository at this point in the history
EltonCN committed Sep 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d415726 commit 837723d
Showing 2 changed files with 249 additions and 63 deletions.
294 changes: 231 additions & 63 deletions dev/memory_storage_codelet.ipynb
Original file line number Diff line number Diff line change
@@ -9,14 +9,13 @@
"import json\n",
"import weakref\n",
"import json\n",
"import asyncio\n",
"import threading\n",
"import time\n",
"from concurrent.futures import ThreadPoolExecutor\n",
"from typing import Optional, cast, List\n",
"\n",
"import redis\n",
"\n",
"\n",
"import cst_python as cst\n",
"from cst_python.core.entities import Memory, Mind"
]
@@ -71,8 +70,10 @@
"metadata": {},
"outputs": [],
"source": [
"class MemoryStorageCodelet:\n",
"class MemoryStorageCodelet(cst.Codelet):\n",
" def __init__(self, mind:Mind, node_name:Optional[str]=None, mind_name:Optional[str]=None, request_timeout:float=500e-3) -> None:\n",
" super().__init__()\n",
" \n",
" self._mind = mind\n",
" self._request_timeout = request_timeout\n",
" \n",
@@ -113,6 +114,12 @@
"\n",
" self._request = None\n",
"\n",
" def calculate_activation(self) -> None:\n",
" pass\n",
"\n",
" def access_memory_objects(self) -> None:\n",
" pass\n",
"\n",
" def proc(self) -> None:\n",
" \n",
" #Check new memories\n",
@@ -138,7 +145,7 @@
" \n",
" else: #Send impostor with owner\n",
" memory_impostor = {\"name\":memory.get_name(),\n",
" \"evalution\" : 0.0,\n",
" \"evaluation\" : 0.0,\n",
" \"I\": \"\",\n",
" \"id\" : \"0.0\",\n",
" \"owner\": self._node_name}\n",
@@ -196,6 +203,9 @@
" def update_memory(self, memory_name:str) -> None:\n",
" print(self._node_name, \"Updating memory\", memory_name)\n",
"\n",
" if memory_name not in self._memories:\n",
" self._pubsub.unsubscribe(f\"{self._mind_name}:memories:{memory_name}:update\")\n",
"\n",
" timestamp = float(self._client.hget(f\"{self._mind_name}:memories:{memory_name}\", \"timestamp\"))\n",
" memory = self._memories[memory_name]\n",
" memory_timestamp = memory.get_timestamp()\n",
@@ -263,7 +273,8 @@
" del self._waiting_request_events[memory_name]\n",
"\n",
" def __del__(self) -> None:\n",
" self._pubsub_thread.stop()"
" self._pubsub_thread.stop()\n",
" self._retrieve_executor.shutdown(cancel_futures=True)"
]
},
{
@@ -282,7 +293,11 @@
"metadata": {},
"outputs": [],
"source": [
"ms_codelet = MemoryStorageCodelet(mind, \"node0\")"
"ms_codelet = MemoryStorageCodelet(mind, \"node0\")\n",
"ms_codelet.time_step = 100\n",
"\n",
"mind.insert_codelet(ms_codelet)\n",
"mind.start()"
]
},
{
@@ -291,7 +306,7 @@
"metadata": {},
"outputs": [],
"source": [
"ms_codelet.proc()"
"time.sleep(1)"
]
},
{
@@ -322,7 +337,11 @@
{
"data": {
"text/plain": [
"{'name': 'Memory1', 'evalution': '0.0', 'I': '', 'id': '0.0', 'owner': 'node0'}"
"{'name': 'Memory1',\n",
" 'evaluation': '0.0',\n",
" 'I': '',\n",
" 'id': '0.0',\n",
" 'owner': 'node0'}"
]
},
"execution_count": 9,
@@ -342,18 +361,39 @@
"source": [
"mind2 = cst.Mind()\n",
"mind2_memory1 = mind2.create_memory_object(\"Memory1\", \"\")\n",
"mind2_ms_codelet = MemoryStorageCodelet(mind2)"
"mind2_ms_codelet = MemoryStorageCodelet(mind2)\n",
"mind2_ms_codelet.time_step = 100\n",
"mind2.insert_codelet(mind2_ms_codelet)\n",
"mind2.start()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1 Retrieve Memory1\n",
"node1 Requesting Memory1\n",
"node0 Tranfering Memory1\n",
"node0 Send memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Send memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node0 Retrieve Memory1\n",
"node0 INFO \"\"\n",
"node1 INFO \"\"\n"
]
},
{
"data": {
"text/plain": [
"MemoryObject [idmemoryobject=0, timestamp=1725654264.298408, evaluation=0.0, I=, name=Memory1]"
"MemoryObject [idmemoryobject=0.0, timestamp=1726077369.7999365, evaluation=0.0, I=, name=Memory1]"
]
},
"execution_count": 11,
@@ -409,45 +449,20 @@
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1 Retrieve Memory1\n",
"node1 Requesting Memory1\n",
"node0 Tranfering Memory1\n",
"node0 Send memory Memory1\n"
]
}
],
"outputs": [],
"source": [
"mind2_ms_codelet.proc()"
"time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Send memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Retrieve Memory1\n",
"node0 INFO \"\"\n",
"node1 INFO \"\"\n"
]
},
{
"data": {
"text/plain": [
"MemoryObject [idmemoryobject=0.0, timestamp=1725654264.7690487, evaluation=0.0, I=, name=Memory1]"
"MemoryObject [idmemoryobject=0.0, timestamp=1726077369.7999365, evaluation=0.0, I=, name=Memory1]"
]
},
"execution_count": 15,
@@ -468,12 +483,11 @@
"data": {
"text/plain": [
"{'name': 'Memory1',\n",
" 'evalution': '0.0',\n",
" 'evaluation': '0.0',\n",
" 'I': '\"\"',\n",
" 'id': '0',\n",
" 'owner': '',\n",
" 'timestamp': '1725654264.298408',\n",
" 'evaluation': '0.0'}"
" 'timestamp': '1726077369.5866976'}"
]
},
"execution_count": 16,
@@ -518,36 +532,29 @@
"node0 Send memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Retrieve Memory1\n"
"node1 Retrieve Memory1\n",
"node1 INFO INFO \"INFO\"\n"
]
}
],
"source": [
"ms_codelet.proc()"
"time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node1 INFO INFO \"INFO\"\n"
]
},
{
"data": {
"text/plain": [
"{'name': 'Memory1',\n",
" 'evalution': '0.0',\n",
" 'evaluation': '0.0',\n",
" 'I': '\"INFO\"',\n",
" 'id': '0.0',\n",
" 'owner': '',\n",
" 'timestamp': '1725654264.794752',\n",
" 'evaluation': '0.0'}"
" 'timestamp': '1726077370.926107'}"
]
},
"execution_count": 19,
@@ -567,7 +574,7 @@
{
"data": {
"text/plain": [
"MemoryObject [idmemoryobject=0.0, timestamp=1725654264.8138657, evaluation=0.0, I=INFO, name=Memory1]"
"MemoryObject [idmemoryobject=0.0, timestamp=1726077371.003417, evaluation=0.0, I=INFO, name=Memory1]"
]
},
"execution_count": 20,
@@ -585,7 +592,7 @@
"metadata": {},
"outputs": [],
"source": [
"mind2_ms_codelet.proc()"
"time.sleep(1)"
]
},
{
@@ -597,12 +604,11 @@
"data": {
"text/plain": [
"{'name': 'Memory1',\n",
" 'evalution': '0.0',\n",
" 'evaluation': '0.0',\n",
" 'I': '\"INFO\"',\n",
" 'id': '0.0',\n",
" 'owner': '',\n",
" 'timestamp': '1725654264.794752',\n",
" 'evaluation': '0.0'}"
" 'timestamp': '1726077370.926107'}"
]
},
"execution_count": 22,
@@ -634,7 +640,7 @@
],
"source": [
"mind2_memory1.set_info(\"INFO2\")\n",
"mind2_ms_codelet.proc()"
"time.sleep(1)"
]
},
{
@@ -646,12 +652,11 @@
"data": {
"text/plain": [
"{'name': 'Memory1',\n",
" 'evalution': '0.0',\n",
" 'evaluation': '0.0',\n",
" 'I': '\"INFO2\"',\n",
" 'id': '0.0',\n",
" 'owner': '',\n",
" 'timestamp': '1725654302.9360394',\n",
" 'evaluation': '0.0'}"
" 'timestamp': '1726077373.0085642'}"
]
},
"execution_count": 24,
@@ -671,7 +676,7 @@
{
"data": {
"text/plain": [
"MemoryObject [idmemoryobject=0.0, timestamp=1725654302.943039, evaluation=0.0, I=INFO2, name=Memory1]"
"MemoryObject [idmemoryobject=0.0, timestamp=1726077373.1104536, evaluation=0.0, I=INFO2, name=Memory1]"
]
},
"execution_count": 25,
@@ -683,6 +688,169 @@
"memory1"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node0 Updating memory Memory1\n",
"node0 Send memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Updating memory Memory1\n",
"node1 Retrieve Memory1\n",
"node1 INFO 1 1\n"
]
}
],
"source": [
"memory1.set_info(1)\n",
"time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mind2_memory1.get_info()"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node0 Updating memory Memory1\n",
"node0 Send memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Updating memory Memory1\n",
"node1 Retrieve Memory1\n",
"node1 INFO 1 \"1\"\n"
]
}
],
"source": [
"memory1.set_info(\"1\")\n",
"time.sleep(1)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1'"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mind2_memory1.get_info()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node0 Updating memory Memory1\n",
"node0 Send memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Retrieve Memory1\n",
"node1 INFO True true\n"
]
}
],
"source": [
"memory1.set_info(True)\n",
"time.sleep(1)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, bool)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mind2_memory1.get_info(), type(mind2_memory1.get_info())"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"node0 Updating memory Memory1\n",
"node0 Send memory Memory1\n",
"node1 Updating memory Memory1\n",
"node0 Updating memory Memory1\n",
"node1 Retrieve Memory1\n",
"node1 INFO [1, 2, 3] [1, 2, 3]\n"
]
},
{
"data": {
"text/plain": [
"([1, 2, 3], list)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"memory1.set_info([1,2,3])\n",
"time.sleep(1)\n",
"mind2_memory1.get_info(), type(mind2_memory1.get_info())"
]
},
{
"cell_type": "code",
"execution_count": null,
18 changes: 18 additions & 0 deletions dev/weak_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import weakref

class Dummy:

def __init__(self, value):
self.value = value

weak_dict = weakref.WeakValueDictionary()

var = Dummy(1)

weak_dict["var"] = var

print("var" in weak_dict)

del var

print("var" in weak_dict)

0 comments on commit 837723d

Please sign in to comment.