From 6a3334d39fc456f2e952370917e30f41bd27e444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Thu, 18 Apr 2024 22:32:18 +0300 Subject: [PATCH] d --- v3/entities.py | 19 +++ v3/functions.py | 329 ++++++++++++++++++++++++++++++++++++++++++++++++ v3/gen-Python | 2 + v3/llm.py | 10 ++ v3/main.py | 11 ++ 5 files changed, 371 insertions(+) create mode 100644 v3/entities.py create mode 100644 v3/functions.py create mode 100755 v3/gen-Python create mode 100644 v3/llm.py create mode 100644 v3/main.py diff --git a/v3/entities.py b/v3/entities.py new file mode 100644 index 0000000..025a913 --- /dev/null +++ b/v3/entities.py @@ -0,0 +1,19 @@ +class MemoryContext: + def __init__(self): + self.hiddenItems = [] + self.mismatchedItems = [] + self.playfieldItems = {} + self.playfieldSize = 0 + self.recentField = None + self.selectedId = None + self.selectedItems = [] + self.victory = False + + def __repr__(self): + return self.__str__() + + def __str__(self): + return f"MemoryContext(plyfieldI/playfieldS/recentF/selectedId/selectedI: '{self.playfieldItems}'/'{self.playfieldSize}'/'{self.recentField}'/'{self.selectedId}'/'{self.selectedItems}')" + +def memory_createEmptyContext(): + return MemoryContext() diff --git a/v3/functions.py b/v3/functions.py new file mode 100644 index 0000000..cc231d8 --- /dev/null +++ b/v3/functions.py @@ -0,0 +1,329 @@ +from entities import * +from llm import * + +# Detect victory +# +# Conditions: +# 1. Matching items have just been hidden and all items are hidden now +@llm_by_value +def memory_detectVictory( + c: MemoryContext +) -> MemoryContext: + if ( + c.recentField == "hiddenItems" and + len(c.hiddenItems) == len(c.playfieldItems) + ): + c.victory = True + c.recentField = "victory" + return c + + c.recentField = None + return c +#} + +# Deselect mismatched items +# +# Conditions: +# 1. Two items are selected and they are of different groups +@llm_by_value +def memory_deselectMismatchedItems( + c: MemoryContext +) -> MemoryContext: + if ( + c.recentField == "selectedItems" and + len(c.selectedItems) == 2 and + c.playfieldItems[c.selectedItems[0]] != c.playfieldItems[c.selectedItems[1]] + ): + c.mismatchedItems.clear() + c.mismatchedItems.append(c.selectedItems[0]) + c.mismatchedItems.append(c.selectedItems[1]) + c.recentField = "mismatchedItems" + return c + + c.recentField = None + return c +#} + +# Generate constant playfield suitable for testing and debugging +@llm_by_value +def memory_generateConstPlayfield( + c: MemoryContext +) -> MemoryContext: + idGroups: dict[int, int] = { } + id = 0 + for gid in range(0, c.playfieldSize): + idGroups[id] = gid + id += 1 + idGroups[id] = gid + id += 1 + #} + c.playfieldItems = idGroups + c.recentField = "playfieldItems" + return c +#} + +# Hide matching selected items +# +# Conditions: +# 1. Two items are selected and they are of the same group +@llm_by_value +def memory_hideMatchingItems( + c: MemoryContext +) -> MemoryContext: + if ( + c.recentField == "selectedItems" and + len(c.selectedItems) == 2 and + c.playfieldItems[c.selectedItems[0]] == c.playfieldItems[c.selectedItems[1]] + ): + c.hiddenItems.append(c.selectedItems[0]) + c.hiddenItems.append(c.selectedItems[1]) + c.recentField = "hiddenItems" + return c + + c.recentField = None + return c +#} + +# Select item +# +# Conditions: +# 1. There are already two selected items +# 2. Item has just been selected +@llm_by_value +def memory_selectItem( + c: MemoryContext +) -> MemoryContext: + if ( + len(c.selectedItems) == 2 + ): + c.selectedItems = [] + + if ( + c.recentField == "selectedId" + ): + c.selectedItems.append(c.selectedId) + c.recentField = "selectedItems" + return c + + c.recentField = None + return c +#} + +# Test. + +def test_memory_generateConstPlayfield( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + if ( + c.recentField == "playfieldItems" and + len(c.playfieldItems) == 4 and + c.playfieldItems[0] == 0 and + c.playfieldItems[1] == 0 and + c.playfieldItems[2] == 1 and + c.playfieldItems[3] == 1 + ): + return "OK: memory_generateConstPlayfield" + #} + return "ERR: memory_generateConstPlayfield" +#} + +def test_memory_selectItem( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select the first item. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # See if it's in selectedItems now. + if ( + c.recentField == "selectedItems" and + len(c.selectedItems) == 1 and + c.selectedItems[0] == 0 + ): + return "OK: selectItem" + #} + return "ERR: selectItem" +#} + +def test_selectTwoItems( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select the first two items. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # See if it's both items are selected now. + if ( + c.recentField == "selectedItems" and + len(c.selectedItems) == 2 and + c.selectedItems[0] == 0 and + c.selectedItems[1] == 1 + ): + return "OK: selectTwoItems" + #} + return "ERR: selectTwoItems" +#} + +def test_selectThreeItems( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select three items. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 2 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # See if only one (last) item is selected now. + if ( + c.recentField == "selectedItems" and + len(c.selectedItems) == 1 and + c.selectedItems[0] == 2 + ): + return "OK: selectThreeItems" + #} + return "ERR: selectThreeItems" +#} + +def test_memory_hideMatchingItems( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select two items of the same group. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # Hide matching items. + c = memory_hideMatchingItems(c) + + # See if the two selected items match. + if ( + c.recentField == "hiddenItems" and + len(c.hiddenItems) == 2 and + c.hiddenItems[0] == 0 and + c.hiddenItems[1] == 1 + ): + return "OK: memory_hideMatchingItems" + #} + return "ERR: memory_hideMatchingItems" +#} + +def test_memory_deselectMismatchedItems( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select two items of different groups. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 2 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # Detect mismatching. + c = memory_deselectMismatchedItems(c) + + # See if the two selected items do not match. + if ( + c.recentField == "mismatchedItems" and + len(c.mismatchedItems) == 2 and + c.mismatchedItems[0] == 0 and + c.mismatchedItems[1] == 2 + ): + return "OK: memory_deselectMismatchedItems" + #} + return "ERR: memory_deselectMismatchedItems" +#} + +def test_memory_detectVictory( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + # Select the first two items of the same group. + c.selectedId = 0 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 1 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # Hide the first pair. + c = memory_hideMatchingItems(c) + + # Select the last two items of the same group. + c.selectedId = 2 + c.recentField = "selectedId" + c = memory_selectItem(c) + c.selectedId = 3 + c.recentField = "selectedId" + c = memory_selectItem(c) + + # Hide the second pair. + c = memory_hideMatchingItems(c) + + # Detect victory. + c = memory_detectVictory(c) + + # See if victory has been detected. + if ( + c.recentField == "victory" and + c.victory == True + ): + return "OK: memory_detectVictory" + #} + return "ERR: memory_detectVictory" +#} + +def test_passContextByValue( +) -> str: + c = memory_createEmptyContext() + c.playfieldSize = 2 + c = memory_generateConstPlayfield(c) + + c.selectedId = 0 + c.recentField = "selectedId" + c1 = memory_selectItem(c) + + c.selectedId = 1 + c.recentField = "selectedId" + c2 = memory_selectItem(c) + + # See if c1 and c2 have different recentField. + if ( + c1.selectedId != c2.selectedId + ): + return "OK: passContextByValue" + + return "ERR: passContextByValue" +#} + diff --git a/v3/gen-Python b/v3/gen-Python new file mode 100755 index 0000000..44f45f4 --- /dev/null +++ b/v3/gen-Python @@ -0,0 +1,2 @@ +echo "python3 main.py" > test_memory_Python +chmod +x test_memory_Python diff --git a/v3/llm.py b/v3/llm.py new file mode 100644 index 0000000..ab3067e --- /dev/null +++ b/v3/llm.py @@ -0,0 +1,10 @@ +import copy + +# Make deep copies of arguments to mimic behaviour of strongly typed languages. +# https://stackoverflow.com/a/15398021 +def llm_by_value(f): + def _f(*args, **kwargs): + argsCopy = copy.deepcopy(args) + kwargsCopy = copy.deepcopy(kwargs) + return f(*argsCopy, **kwargsCopy) + return _f diff --git a/v3/main.py b/v3/main.py new file mode 100644 index 0000000..f83e394 --- /dev/null +++ b/v3/main.py @@ -0,0 +1,11 @@ +from entities import * +from functions import * + +print(test_memory_generateConstPlayfield()) +print(test_selectOneItem()) +print(test_selectTwoItems()) +print(test_selectThreeItems()) +print(test_memory_hideMatchingItems()) +print(test_memory_deselectMismatchedItems()) +print(test_memory_detectVictory()) +print(test_passContextByValue())