Upload folder using huggingface_hub
Browse files- README.md +54 -3
- __pycache__/me.cpython-314.pyc +0 -0
- me.py +685 -0
- noe-model.dodl +0 -0
README.md
CHANGED
|
@@ -1,3 +1,54 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core Model
|
| 2 |
+
|
| 3 |
+
`me.py` implements a small stateful AI model. It combines a feed-forward neural network with state, goals, memory, rules, and a behavior program. It uses only the Python standard library.
|
| 4 |
+
|
| 5 |
+
## Create a model file
|
| 6 |
+
|
| 7 |
+
Run the script from the `core/model/` directory:
|
| 8 |
+
|
| 9 |
+
```bash
|
| 10 |
+
cd core/model
|
| 11 |
+
python3 me.py
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
This creates `noe-model.dodl` in the current directory. `create_model_file(path)` is also available when creating a model from Python; it writes the binary DODL format and returns the model object.
|
| 15 |
+
|
| 16 |
+
The path may be a string or another path-like value. Its parent directory must already exist. The file is binary; open it with `"rb"` when reading.
|
| 17 |
+
|
| 18 |
+
## Load and use a model
|
| 19 |
+
|
| 20 |
+
Deserialize the file into a model, then pass it to `AIEngine`:
|
| 21 |
+
|
| 22 |
+
```python
|
| 23 |
+
from model.me import AIEngine, deserialize_model
|
| 24 |
+
|
| 25 |
+
with open("noe-model.dodl", "rb") as model_file:
|
| 26 |
+
model = deserialize_model(model_file.read())
|
| 27 |
+
|
| 28 |
+
engine = AIEngine(model)
|
| 29 |
+
result = engine.step({"type": "user_input", "value": "hello"})
|
| 30 |
+
print(result["action"])
|
| 31 |
+
```
|
| 32 |
+
|
| 33 |
+
Alternatively, `AIEngine.import_model(data)` constructs an engine directly from serialized bytes. `AIEngine.export_model()` returns the engine's current model as serialized bytes; write those bytes to a file to persist the latest state:
|
| 34 |
+
|
| 35 |
+
```python
|
| 36 |
+
with open("noe-model.dodl", "wb") as model_file:
|
| 37 |
+
model_file.write(engine.export_model())
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
## Model format and limits
|
| 41 |
+
|
| 42 |
+
The file starts with the `DODL` magic signature and a versioned header. It stores JSON metadata followed by the neural network's float32 arrays. The format is intended for this module's serializer and deserializer; it is not a JSON document.
|
| 43 |
+
|
| 44 |
+
`serialize_model()` validates the memory, rule, and behavior limits and rejects files larger than 1 MiB. `deserialize_model()` checks the signature, format version, network shape, and encoded size before returning a model.
|
| 45 |
+
|
| 46 |
+
## Main API
|
| 47 |
+
|
| 48 |
+
- `create_model()` returns a fresh model in memory.
|
| 49 |
+
- `create_model_file(path)` creates a fresh model and writes it to `path`.
|
| 50 |
+
- `serialize_model(model)` converts a model to DODL bytes.
|
| 51 |
+
- `deserialize_model(data)` converts DODL bytes back to a model.
|
| 52 |
+
- `AIEngine(model=None)` runs and updates a model; without an argument it creates a fresh one.
|
| 53 |
+
- `AIEngine.step(input_value)` observes input, chooses and executes an action, evaluates it, learns, updates state, and returns the cycle result.
|
| 54 |
+
- `AIEngine.export_model()` serializes the engine's current model.
|
__pycache__/me.cpython-314.pyc
ADDED
|
Binary file (39.1 kB). View file
|
|
|
me.py
ADDED
|
@@ -0,0 +1,685 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ONFIG = {
|
| 11 |
+
"VERSION": 2,
|
| 12 |
+
"MAX_FILE_SIZE": 1024 * 1024,
|
| 13 |
+
"TARGET_FILE_SIZE": 950 * 1024,
|
| 14 |
+
"INPUT_SIZE": 16,
|
| 15 |
+
"HIDDEN_SIZE": 32,
|
| 16 |
+
"OUTPUT_SIZE": 8,
|
| 17 |
+
"MEMORY_LIMIT": 200,
|
| 18 |
+
"RULE_LIMIT": 64,
|
| 19 |
+
"BEHAVIOR_LIMIT": 128,
|
| 20 |
+
"LEARNING_RATE": 0.02,
|
| 21 |
+
"MIN_WEIGHT": -5,
|
| 22 |
+
"MAX_WEIGHT": 5,
|
| 23 |
+
"STATE_MIN": 0,
|
| 24 |
+
"STATE_MAX": 100,
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
MAGIC = b"DODL"
|
| 28 |
+
HEADER_SIZE = 16
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def clamp(value, minimum, maximum):
|
| 32 |
+
return max(minimum, min(maximum, value))
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def random_float(minimum, maximum):
|
| 36 |
+
return minimum + secrets.randbelow(1 << 53) / (1 << 53) * (maximum - minimum)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def random_int(minimum, maximum):
|
| 40 |
+
return secrets.randbelow(maximum - minimum + 1) + minimum
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def uuid_bytes():
|
| 44 |
+
return secrets.token_bytes(16).hex()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def now():
|
| 48 |
+
return int(time.time() * 1000)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
class Random:
|
| 52 |
+
def __init__(self, seed=None):
|
| 53 |
+
if seed is None:
|
| 54 |
+
seed = now()
|
| 55 |
+
self.seed = int(seed) & 0xFFFFFFFF or 1
|
| 56 |
+
|
| 57 |
+
def next(self):
|
| 58 |
+
value = self.seed
|
| 59 |
+
value ^= (value << 13) & 0xFFFFFFFF
|
| 60 |
+
value ^= value >> 17
|
| 61 |
+
value ^= (value << 5) & 0xFFFFFFFF
|
| 62 |
+
self.seed = value & 0xFFFFFFFF
|
| 63 |
+
return self.seed / 0x100000000
|
| 64 |
+
|
| 65 |
+
def float(self, minimum, maximum):
|
| 66 |
+
return minimum + self.next() * (maximum - minimum)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def encode_model_for_storage(buffer):
|
| 70 |
+
return base64.b64encode(buffer).decode("ascii")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def decode_model_from_storage(encoded):
|
| 74 |
+
return base64.b64decode(encoded, validate=True)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def create_initial_state():
|
| 78 |
+
timestamp = now()
|
| 79 |
+
return {
|
| 80 |
+
"energy": 72,
|
| 81 |
+
"curiosity": 50,
|
| 82 |
+
"confidence": 50,
|
| 83 |
+
"stability": 80,
|
| 84 |
+
"cycle": 0,
|
| 85 |
+
"lastAction": None,
|
| 86 |
+
"lastReward": 0,
|
| 87 |
+
"createdAt": timestamp,
|
| 88 |
+
"updatedAt": timestamp,
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def create_initial_goals():
|
| 93 |
+
return [
|
| 94 |
+
{"id": "survive", "priority": 100, "target": {"energyMin": 20}},
|
| 95 |
+
{"id": "explore", "priority": 50, "target": {"curiosityMin": 60}},
|
| 96 |
+
{"id": "learn", "priority": 40, "target": {"confidenceMin": 50}},
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def create_initial_rules():
|
| 101 |
+
return [
|
| 102 |
+
{
|
| 103 |
+
"id": "rest-low-energy",
|
| 104 |
+
"condition": {"type": "less-than", "variable": "energy", "value": 20},
|
| 105 |
+
"action": {"type": "rest"},
|
| 106 |
+
"priority": 100,
|
| 107 |
+
"enabled": True,
|
| 108 |
+
"success": 0,
|
| 109 |
+
"failure": 0,
|
| 110 |
+
},
|
| 111 |
+
{
|
| 112 |
+
"id": "explore-high-curiosity",
|
| 113 |
+
"condition": {"type": "greater-than", "variable": "curiosity", "value": 70},
|
| 114 |
+
"action": {"type": "explore"},
|
| 115 |
+
"priority": 60,
|
| 116 |
+
"enabled": True,
|
| 117 |
+
"success": 0,
|
| 118 |
+
"failure": 0,
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"id": "observe-default",
|
| 122 |
+
"condition": {"type": "always"},
|
| 123 |
+
"action": {"type": "observe"},
|
| 124 |
+
"priority": 10,
|
| 125 |
+
"enabled": True,
|
| 126 |
+
"success": 0,
|
| 127 |
+
"failure": 0,
|
| 128 |
+
},
|
| 129 |
+
]
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def create_initial_behavior():
|
| 133 |
+
return [
|
| 134 |
+
["READ", "energy"],
|
| 135 |
+
["LESS_THAN", 20],
|
| 136 |
+
["IF"],
|
| 137 |
+
["ACTION", "rest"],
|
| 138 |
+
["ELSE"],
|
| 139 |
+
["READ", "curiosity"],
|
| 140 |
+
["GREATER_THAN", 70],
|
| 141 |
+
["IF"],
|
| 142 |
+
["ACTION", "explore"],
|
| 143 |
+
["ELSE"],
|
| 144 |
+
["ACTION", "observe"],
|
| 145 |
+
["END"],
|
| 146 |
+
["END"],
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def _float32(value):
|
| 151 |
+
return struct.unpack("<f", struct.pack("<f", value))[0]
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
class TinyNetwork:
|
| 155 |
+
def __init__(self, random=None):
|
| 156 |
+
self.input_size = CONFIG["INPUT_SIZE"]
|
| 157 |
+
self.hidden_size = CONFIG["HIDDEN_SIZE"]
|
| 158 |
+
self.output_size = CONFIG["OUTPUT_SIZE"]
|
| 159 |
+
self.weights1 = array("f", [0]) * (self.input_size * self.hidden_size)
|
| 160 |
+
self.bias1 = array("f", [0]) * self.hidden_size
|
| 161 |
+
self.weights2 = array("f", [0]) * (self.hidden_size * self.output_size)
|
| 162 |
+
self.bias2 = array("f", [0]) * self.output_size
|
| 163 |
+
self.initialize(random or Random())
|
| 164 |
+
|
| 165 |
+
def initialize(self, random):
|
| 166 |
+
for index in range(len(self.weights1)):
|
| 167 |
+
self.weights1[index] = random.float(-0.1, 0.1)
|
| 168 |
+
for index in range(len(self.weights2)):
|
| 169 |
+
self.weights2[index] = random.float(-0.1, 0.1)
|
| 170 |
+
self.bias1 = array("f", [0]) * self.hidden_size
|
| 171 |
+
self.bias2 = array("f", [0]) * self.output_size
|
| 172 |
+
|
| 173 |
+
@staticmethod
|
| 174 |
+
def relu(value):
|
| 175 |
+
return max(0, value)
|
| 176 |
+
|
| 177 |
+
def forward(self, input_vector):
|
| 178 |
+
hidden = array("f", [0]) * self.hidden_size
|
| 179 |
+
output = array("f", [0]) * self.output_size
|
| 180 |
+
for hidden_index in range(self.hidden_size):
|
| 181 |
+
total = self.bias1[hidden_index]
|
| 182 |
+
start = hidden_index * self.input_size
|
| 183 |
+
for input_index in range(self.input_size):
|
| 184 |
+
total += input_vector[input_index] * self.weights1[start + input_index]
|
| 185 |
+
hidden[hidden_index] = self.relu(total)
|
| 186 |
+
for output_index in range(self.output_size):
|
| 187 |
+
total = self.bias2[output_index]
|
| 188 |
+
start = output_index * self.hidden_size
|
| 189 |
+
for hidden_index in range(self.hidden_size):
|
| 190 |
+
total += hidden[hidden_index] * self.weights2[start + hidden_index]
|
| 191 |
+
output[output_index] = total
|
| 192 |
+
return {"hidden": hidden, "output": output}
|
| 193 |
+
|
| 194 |
+
def learn(self, input_vector, target, learning_rate):
|
| 195 |
+
result = self.forward(input_vector)
|
| 196 |
+
hidden = result["hidden"]
|
| 197 |
+
output = result["output"]
|
| 198 |
+
output_errors = array("f", [0]) * self.output_size
|
| 199 |
+
for output_index in range(self.output_size):
|
| 200 |
+
output_error = target[output_index] - output[output_index]
|
| 201 |
+
output_errors[output_index] = output_error
|
| 202 |
+
start = output_index * self.hidden_size
|
| 203 |
+
for hidden_index in range(self.hidden_size):
|
| 204 |
+
index = start + hidden_index
|
| 205 |
+
self.weights2[index] = clamp(
|
| 206 |
+
self.weights2[index] + learning_rate * output_error * hidden[hidden_index],
|
| 207 |
+
CONFIG["MIN_WEIGHT"],
|
| 208 |
+
CONFIG["MAX_WEIGHT"],
|
| 209 |
+
)
|
| 210 |
+
self.bias2[output_index] += learning_rate * output_error
|
| 211 |
+
|
| 212 |
+
for hidden_index in range(self.hidden_size):
|
| 213 |
+
error = 0
|
| 214 |
+
for output_index in range(self.output_size):
|
| 215 |
+
error += output_errors[output_index] * self.weights2[
|
| 216 |
+
output_index * self.hidden_size + hidden_index
|
| 217 |
+
]
|
| 218 |
+
if hidden[hidden_index] <= 0:
|
| 219 |
+
error = 0
|
| 220 |
+
start = hidden_index * self.input_size
|
| 221 |
+
for input_index in range(self.input_size):
|
| 222 |
+
index = start + input_index
|
| 223 |
+
self.weights1[index] = clamp(
|
| 224 |
+
self.weights1[index] + learning_rate * error * input_vector[input_index],
|
| 225 |
+
CONFIG["MIN_WEIGHT"],
|
| 226 |
+
CONFIG["MAX_WEIGHT"],
|
| 227 |
+
)
|
| 228 |
+
self.bias1[hidden_index] += learning_rate * error
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
class MemoryStore:
|
| 232 |
+
def __init__(self, memory=None):
|
| 233 |
+
self.items = memory if memory is not None else []
|
| 234 |
+
|
| 235 |
+
def add(self, memory):
|
| 236 |
+
self.items.append(
|
| 237 |
+
{
|
| 238 |
+
"id": uuid_bytes(),
|
| 239 |
+
"timestamp": now(),
|
| 240 |
+
"input": memory.get("input"),
|
| 241 |
+
"action": memory.get("action"),
|
| 242 |
+
"result": memory.get("result"),
|
| 243 |
+
"reward": memory.get("reward"),
|
| 244 |
+
"importance": clamp(memory.get("importance", 50), 0, 100),
|
| 245 |
+
}
|
| 246 |
+
)
|
| 247 |
+
self.limit()
|
| 248 |
+
|
| 249 |
+
def limit(self):
|
| 250 |
+
while len(self.items) > CONFIG["MEMORY_LIMIT"]:
|
| 251 |
+
self.items.sort(key=lambda item: item["importance"])
|
| 252 |
+
self.items.pop(0)
|
| 253 |
+
|
| 254 |
+
def get_recent(self, count=10):
|
| 255 |
+
return self.items[-count:]
|
| 256 |
+
|
| 257 |
+
def clear(self):
|
| 258 |
+
self.items.clear()
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def _js_number(value):
|
| 262 |
+
if value is None:
|
| 263 |
+
return 0
|
| 264 |
+
try:
|
| 265 |
+
return float(value)
|
| 266 |
+
except (TypeError, ValueError):
|
| 267 |
+
return math.nan
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
class RuleEngine:
|
| 271 |
+
def __init__(self, rules=None):
|
| 272 |
+
self.rules = rules if rules is not None else []
|
| 273 |
+
|
| 274 |
+
def evaluate_condition(self, condition, state):
|
| 275 |
+
if not condition:
|
| 276 |
+
return False
|
| 277 |
+
condition_type = condition.get("type")
|
| 278 |
+
variable = condition.get("variable")
|
| 279 |
+
if condition_type == "always":
|
| 280 |
+
return True
|
| 281 |
+
if condition_type == "less-than":
|
| 282 |
+
return _js_number(state.get(variable)) < condition.get("value")
|
| 283 |
+
if condition_type == "greater-than":
|
| 284 |
+
return _js_number(state.get(variable)) > condition.get("value")
|
| 285 |
+
if condition_type == "equals":
|
| 286 |
+
value = state.get(variable)
|
| 287 |
+
expected = condition.get("value")
|
| 288 |
+
return type(value) is type(expected) and value == expected
|
| 289 |
+
return False
|
| 290 |
+
|
| 291 |
+
def get_applicable(self, state):
|
| 292 |
+
applicable = [
|
| 293 |
+
rule
|
| 294 |
+
for rule in self.rules
|
| 295 |
+
if rule.get("enabled") and self.evaluate_condition(rule.get("condition"), state)
|
| 296 |
+
]
|
| 297 |
+
return sorted(applicable, key=lambda rule: rule["priority"], reverse=True)
|
| 298 |
+
|
| 299 |
+
def select(self, state):
|
| 300 |
+
applicable = self.get_applicable(state)
|
| 301 |
+
return applicable[0] if applicable else None
|
| 302 |
+
|
| 303 |
+
def reward(self, rule_id, reward):
|
| 304 |
+
rule = next((item for item in self.rules if item.get("id") == rule_id), None)
|
| 305 |
+
if rule:
|
| 306 |
+
if reward > 0:
|
| 307 |
+
rule["success"] += 1
|
| 308 |
+
rule["priority"] += 1
|
| 309 |
+
else:
|
| 310 |
+
rule["failure"] += 1
|
| 311 |
+
rule["priority"] -= 1
|
| 312 |
+
rule["priority"] = clamp(rule["priority"], 0, 1000)
|
| 313 |
+
|
| 314 |
+
def evolve(self):
|
| 315 |
+
for rule in self.rules:
|
| 316 |
+
total = rule["success"] + rule["failure"]
|
| 317 |
+
if total >= 10 and rule["success"] / total < 0.1:
|
| 318 |
+
rule["enabled"] = False
|
| 319 |
+
if len(self.rules) > CONFIG["RULE_LIMIT"]:
|
| 320 |
+
self.rules = sorted(self.rules, key=lambda rule: rule["priority"], reverse=True)[
|
| 321 |
+
: CONFIG["RULE_LIMIT"]
|
| 322 |
+
]
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
class BehaviorEngine:
|
| 326 |
+
def __init__(self, program=None):
|
| 327 |
+
self.program = program if program is not None else []
|
| 328 |
+
|
| 329 |
+
def run(self, state):
|
| 330 |
+
action = "observe"
|
| 331 |
+
stack = []
|
| 332 |
+
skip = False
|
| 333 |
+
for index, instruction in enumerate(self.program):
|
| 334 |
+
if index >= CONFIG["BEHAVIOR_LIMIT"]:
|
| 335 |
+
break
|
| 336 |
+
op = instruction[0]
|
| 337 |
+
arg = instruction[1] if len(instruction) > 1 else None
|
| 338 |
+
if op == "READ":
|
| 339 |
+
stack.append(state.get(arg))
|
| 340 |
+
elif op == "LESS_THAN":
|
| 341 |
+
stack.append(_js_number(stack.pop()) < arg)
|
| 342 |
+
elif op == "GREATER_THAN":
|
| 343 |
+
stack.append(_js_number(stack.pop()) > arg)
|
| 344 |
+
elif op == "IF":
|
| 345 |
+
condition = bool(stack.pop())
|
| 346 |
+
stack.append({"type": "if", "condition": condition, "active": condition})
|
| 347 |
+
skip = not condition
|
| 348 |
+
elif op == "ELSE":
|
| 349 |
+
block = stack.pop() if stack else None
|
| 350 |
+
if isinstance(block, dict) and block.get("type") == "if":
|
| 351 |
+
block["active"] = not block["condition"]
|
| 352 |
+
stack.append(block)
|
| 353 |
+
skip = not block["active"]
|
| 354 |
+
elif op == "ACTION" and not skip:
|
| 355 |
+
action = arg
|
| 356 |
+
elif op == "END":
|
| 357 |
+
skip = False
|
| 358 |
+
return action
|
| 359 |
+
|
| 360 |
+
def mutate(self, random):
|
| 361 |
+
if len(self.program) >= CONFIG["BEHAVIOR_LIMIT"]:
|
| 362 |
+
return
|
| 363 |
+
actions = ["rest", "explore", "observe", "learn"]
|
| 364 |
+
mutation = random_int(0, 2)
|
| 365 |
+
if mutation == 0:
|
| 366 |
+
self.program.append(["ACTION", actions[random_int(0, len(actions) - 1)]])
|
| 367 |
+
elif mutation == 1:
|
| 368 |
+
if len(self.program) > 3:
|
| 369 |
+
self.program.pop(random_int(0, len(self.program) - 1))
|
| 370 |
+
else:
|
| 371 |
+
instruction = self.program[random_int(0, len(self.program) - 1)]
|
| 372 |
+
if instruction[0] in ("LESS_THAN", "GREATER_THAN"):
|
| 373 |
+
instruction[1] = random_int(5, 95)
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
def create_model():
|
| 377 |
+
random = Random(random_int(1, 0xFFFFFFFF))
|
| 378 |
+
network = TinyNetwork(random)
|
| 379 |
+
timestamp = now()
|
| 380 |
+
return {
|
| 381 |
+
"version": CONFIG["VERSION"],
|
| 382 |
+
"id": list(secrets.token_bytes(16)),
|
| 383 |
+
"createdAt": timestamp,
|
| 384 |
+
"updatedAt": timestamp,
|
| 385 |
+
"randomSeed": random.seed,
|
| 386 |
+
"network": {
|
| 387 |
+
"inputSize": network.input_size,
|
| 388 |
+
"hiddenSize": network.hidden_size,
|
| 389 |
+
"outputSize": network.output_size,
|
| 390 |
+
"weights1": network.weights1,
|
| 391 |
+
"bias1": network.bias1,
|
| 392 |
+
"weights2": network.weights2,
|
| 393 |
+
"bias2": network.bias2,
|
| 394 |
+
},
|
| 395 |
+
"memory": [],
|
| 396 |
+
"rules": create_initial_rules(),
|
| 397 |
+
"behavior": create_initial_behavior(),
|
| 398 |
+
"state": create_initial_state(),
|
| 399 |
+
"goals": create_initial_goals(),
|
| 400 |
+
}
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
def validate_model(model):
|
| 404 |
+
if len(model["memory"]) > CONFIG["MEMORY_LIMIT"]:
|
| 405 |
+
raise ValueError("Memory limit exceeded.")
|
| 406 |
+
if len(model["rules"]) > CONFIG["RULE_LIMIT"]:
|
| 407 |
+
raise ValueError("Rule limit exceeded.")
|
| 408 |
+
if len(model["behavior"]) > CONFIG["BEHAVIOR_LIMIT"]:
|
| 409 |
+
raise ValueError("Behavior limit exceeded.")
|
| 410 |
+
return True
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
def _float_array_bytes(values):
|
| 414 |
+
return struct.pack(f"<{len(values)}f", *values)
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def serialize_model(model):
|
| 418 |
+
validate_model(model)
|
| 419 |
+
model["updatedAt"] = now()
|
| 420 |
+
network = model["network"]
|
| 421 |
+
metadata = {
|
| 422 |
+
"version": model["version"],
|
| 423 |
+
"id": list(model["id"]),
|
| 424 |
+
"createdAt": model["createdAt"],
|
| 425 |
+
"updatedAt": model["updatedAt"],
|
| 426 |
+
"randomSeed": model["randomSeed"],
|
| 427 |
+
"memory": model["memory"],
|
| 428 |
+
"rules": model["rules"],
|
| 429 |
+
"behavior": model["behavior"],
|
| 430 |
+
"state": model["state"],
|
| 431 |
+
"goals": model["goals"],
|
| 432 |
+
"networkShape": {
|
| 433 |
+
"inputSize": network["inputSize"],
|
| 434 |
+
"hiddenSize": network["hiddenSize"],
|
| 435 |
+
"outputSize": network["outputSize"],
|
| 436 |
+
},
|
| 437 |
+
}
|
| 438 |
+
metadata_bytes = json.dumps(metadata, separators=(",", ":"), allow_nan=False).encode("utf-8")
|
| 439 |
+
network_bytes = b"".join(
|
| 440 |
+
_float_array_bytes(network[key]) for key in ("weights1", "bias1", "weights2", "bias2")
|
| 441 |
+
)
|
| 442 |
+
total_size = HEADER_SIZE + len(metadata_bytes) + len(network_bytes)
|
| 443 |
+
if total_size > CONFIG["MAX_FILE_SIZE"]:
|
| 444 |
+
raise ValueError(f"Model exceeds 1 MB: {total_size} bytes")
|
| 445 |
+
header = MAGIC + struct.pack("<HHII", CONFIG["VERSION"], 0, len(metadata_bytes), len(network_bytes))
|
| 446 |
+
return header + metadata_bytes + network_bytes
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
def create_model_file(path):
|
| 450 |
+
model = create_model()
|
| 451 |
+
with open(path, "wb") as model_file:
|
| 452 |
+
model_file.write(serialize_model(model))
|
| 453 |
+
return model
|
| 454 |
+
|
| 455 |
+
|
| 456 |
+
def deserialize_model(buffer):
|
| 457 |
+
buffer = bytes(buffer)
|
| 458 |
+
if len(buffer) < HEADER_SIZE or buffer[:4] != MAGIC:
|
| 459 |
+
raise ValueError("Invalid DODL model file.")
|
| 460 |
+
version, _, metadata_size, network_size = struct.unpack_from("<HHII", buffer, 4)
|
| 461 |
+
if version != CONFIG["VERSION"]:
|
| 462 |
+
raise ValueError("Unsupported model version.")
|
| 463 |
+
if HEADER_SIZE + metadata_size + network_size != len(buffer):
|
| 464 |
+
raise ValueError("Corrupt model size.")
|
| 465 |
+
metadata_start = HEADER_SIZE
|
| 466 |
+
metadata = json.loads(buffer[metadata_start : metadata_start + metadata_size].decode("utf-8"))
|
| 467 |
+
shape = metadata["networkShape"]
|
| 468 |
+
input_size = shape["inputSize"]
|
| 469 |
+
hidden_size = shape["hiddenSize"]
|
| 470 |
+
output_size = shape["outputSize"]
|
| 471 |
+
lengths = (
|
| 472 |
+
input_size * hidden_size,
|
| 473 |
+
hidden_size,
|
| 474 |
+
hidden_size * output_size,
|
| 475 |
+
output_size,
|
| 476 |
+
)
|
| 477 |
+
if any(not isinstance(size, int) or size < 0 for size in (input_size, hidden_size, output_size)):
|
| 478 |
+
raise ValueError("Invalid network shape.")
|
| 479 |
+
if network_size != sum(lengths) * 4:
|
| 480 |
+
raise ValueError("Corrupt model size.")
|
| 481 |
+
offset = metadata_start + metadata_size
|
| 482 |
+
arrays = []
|
| 483 |
+
for length in lengths:
|
| 484 |
+
values = array("f")
|
| 485 |
+
values.frombytes(buffer[offset : offset + length * 4])
|
| 486 |
+
if struct.pack("=I", 1) != struct.pack("<I", 1):
|
| 487 |
+
values.byteswap()
|
| 488 |
+
arrays.append(values)
|
| 489 |
+
offset += length * 4
|
| 490 |
+
model = {
|
| 491 |
+
"version": metadata["version"],
|
| 492 |
+
"id": bytearray(metadata["id"]),
|
| 493 |
+
"createdAt": metadata["createdAt"],
|
| 494 |
+
"updatedAt": metadata["updatedAt"],
|
| 495 |
+
"randomSeed": metadata["randomSeed"],
|
| 496 |
+
"network": {
|
| 497 |
+
"inputSize": input_size,
|
| 498 |
+
"hiddenSize": hidden_size,
|
| 499 |
+
"outputSize": output_size,
|
| 500 |
+
"weights1": arrays[0],
|
| 501 |
+
"bias1": arrays[1],
|
| 502 |
+
"weights2": arrays[2],
|
| 503 |
+
"bias2": arrays[3],
|
| 504 |
+
},
|
| 505 |
+
"memory": metadata["memory"],
|
| 506 |
+
"rules": metadata["rules"],
|
| 507 |
+
"behavior": metadata["behavior"],
|
| 508 |
+
"state": metadata["state"],
|
| 509 |
+
"goals": metadata["goals"],
|
| 510 |
+
}
|
| 511 |
+
validate_model(model)
|
| 512 |
+
return model
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
def load_stored_model(storage, key):
|
| 516 |
+
stored_model = storage.getItem(key) if hasattr(storage, "getItem") else storage.get(key)
|
| 517 |
+
if not stored_model:
|
| 518 |
+
return None
|
| 519 |
+
return deserialize_model(decode_model_from_storage(stored_model))
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def save_stored_model(storage, key, model):
|
| 523 |
+
encoded = encode_model_for_storage(serialize_model(model))
|
| 524 |
+
if hasattr(storage, "setItem"):
|
| 525 |
+
storage.setItem(key, encoded)
|
| 526 |
+
else:
|
| 527 |
+
storage[key] = encoded
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
class AIEngine:
|
| 531 |
+
def __init__(self, model=None):
|
| 532 |
+
self.model = model if model is not None else create_model()
|
| 533 |
+
self.random = Random(self.model["randomSeed"])
|
| 534 |
+
stored_network = self.model["network"]
|
| 535 |
+
self.network = TinyNetwork(self.random)
|
| 536 |
+
self.network.input_size = stored_network["inputSize"]
|
| 537 |
+
self.network.hidden_size = stored_network["hiddenSize"]
|
| 538 |
+
self.network.output_size = stored_network["outputSize"]
|
| 539 |
+
self.network.weights1 = stored_network["weights1"]
|
| 540 |
+
self.network.bias1 = stored_network["bias1"]
|
| 541 |
+
self.network.weights2 = stored_network["weights2"]
|
| 542 |
+
self.network.bias2 = stored_network["bias2"]
|
| 543 |
+
self.memory = MemoryStore(self.model["memory"])
|
| 544 |
+
self.rules = RuleEngine(self.model["rules"])
|
| 545 |
+
self.behavior = BehaviorEngine(self.model["behavior"])
|
| 546 |
+
self.state = self.model["state"]
|
| 547 |
+
self.goals = self.model["goals"]
|
| 548 |
+
|
| 549 |
+
def encode_input(self, input_value):
|
| 550 |
+
vector = array("f", [0]) * CONFIG["INPUT_SIZE"]
|
| 551 |
+
vector[0] = self.state["energy"] / 100
|
| 552 |
+
vector[1] = self.state["curiosity"] / 100
|
| 553 |
+
vector[2] = self.state["confidence"] / 100
|
| 554 |
+
vector[3] = self.state["stability"] / 100
|
| 555 |
+
if isinstance(input_value, dict):
|
| 556 |
+
value = input_value.get("value")
|
| 557 |
+
if isinstance(value, str):
|
| 558 |
+
vector[4] = min(len(value) / 100, 1)
|
| 559 |
+
if input_value.get("type") == "user_input":
|
| 560 |
+
vector[5] = 1
|
| 561 |
+
vector[6] = min(len(self.memory.items) / CONFIG["MEMORY_LIMIT"], 1)
|
| 562 |
+
return vector
|
| 563 |
+
|
| 564 |
+
def observe(self, input_value):
|
| 565 |
+
return {"input": input_value, "timestamp": now(), "state": dict(self.state)}
|
| 566 |
+
|
| 567 |
+
def interpret(self, perception):
|
| 568 |
+
return self.network.forward(self.encode_input(perception["input"]))
|
| 569 |
+
|
| 570 |
+
def decide(self, perception):
|
| 571 |
+
rule = self.rules.select(self.state)
|
| 572 |
+
if rule:
|
| 573 |
+
return {"type": rule["action"]["type"], "ruleId": rule["id"]}
|
| 574 |
+
return {"type": self.behavior.run(self.state), "ruleId": None}
|
| 575 |
+
|
| 576 |
+
def execute(self, action):
|
| 577 |
+
action_type = action["type"]
|
| 578 |
+
if action_type == "rest":
|
| 579 |
+
self.state["energy"] = clamp(self.state["energy"] + 10, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
|
| 580 |
+
self.state["curiosity"] = clamp(self.state["curiosity"] - 2, 0, 100)
|
| 581 |
+
elif action_type == "explore":
|
| 582 |
+
self.state["energy"] = clamp(self.state["energy"] - 8, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
|
| 583 |
+
self.state["curiosity"] = clamp(self.state["curiosity"] - 10, 0, 100)
|
| 584 |
+
elif action_type == "learn":
|
| 585 |
+
self.state["energy"] = clamp(self.state["energy"] - 3, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
|
| 586 |
+
self.state["confidence"] = clamp(self.state["confidence"] + 5, 0, 100)
|
| 587 |
+
else:
|
| 588 |
+
self.state["energy"] = clamp(self.state["energy"] - 1, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
|
| 589 |
+
self.state["curiosity"] = clamp(self.state["curiosity"], 0, 100)
|
| 590 |
+
return {"action": action_type, "success": True, "timestamp": now()}
|
| 591 |
+
|
| 592 |
+
def evaluate(self, perception, action, result):
|
| 593 |
+
reward = 0
|
| 594 |
+
if self.state["energy"] >= 20:
|
| 595 |
+
reward += 1
|
| 596 |
+
if action["type"] == "rest" and self.state["energy"] > 50:
|
| 597 |
+
reward -= 0.2
|
| 598 |
+
if action["type"] == "explore":
|
| 599 |
+
reward += 0.5
|
| 600 |
+
return {"reward": reward, "perception": perception, "action": action, "result": result}
|
| 601 |
+
|
| 602 |
+
def learn(self, evaluation):
|
| 603 |
+
input_vector = self.encode_input(evaluation["perception"]["input"])
|
| 604 |
+
target = array("f", [evaluation["reward"]]) * CONFIG["OUTPUT_SIZE"]
|
| 605 |
+
self.network.learn(input_vector, target, CONFIG["LEARNING_RATE"])
|
| 606 |
+
action = evaluation["action"]
|
| 607 |
+
if action["ruleId"]:
|
| 608 |
+
self.rules.reward(action["ruleId"], evaluation["reward"])
|
| 609 |
+
self.memory.add(
|
| 610 |
+
{
|
| 611 |
+
"input": evaluation["perception"]["input"],
|
| 612 |
+
"action": action["type"],
|
| 613 |
+
"result": evaluation["result"],
|
| 614 |
+
"reward": evaluation["reward"],
|
| 615 |
+
"importance": abs(evaluation["reward"]) * 50 + 50,
|
| 616 |
+
}
|
| 617 |
+
)
|
| 618 |
+
if self.random.next() < 0.05:
|
| 619 |
+
self.behavior.mutate(self.random)
|
| 620 |
+
self.rules.evolve()
|
| 621 |
+
|
| 622 |
+
def update_state(self, evaluation):
|
| 623 |
+
self.state["lastAction"] = evaluation["action"]["type"]
|
| 624 |
+
self.state["lastReward"] = evaluation["reward"]
|
| 625 |
+
self.state["cycle"] += 1
|
| 626 |
+
self.state["updatedAt"] = now()
|
| 627 |
+
self.state["curiosity"] += 1 if evaluation["reward"] > 0 else -1
|
| 628 |
+
self.state["curiosity"] = clamp(self.state["curiosity"], 0, 100)
|
| 629 |
+
|
| 630 |
+
def step(self, input_value):
|
| 631 |
+
perception = self.observe(input_value)
|
| 632 |
+
interpretation = self.interpret(perception)
|
| 633 |
+
action = self.decide(perception)
|
| 634 |
+
result = self.execute(action)
|
| 635 |
+
evaluation = self.evaluate(perception, action, result)
|
| 636 |
+
self.learn(evaluation)
|
| 637 |
+
self.update_state(evaluation)
|
| 638 |
+
self.sync_model()
|
| 639 |
+
return {
|
| 640 |
+
"perception": perception,
|
| 641 |
+
"interpretation": interpretation,
|
| 642 |
+
"action": action,
|
| 643 |
+
"result": result,
|
| 644 |
+
"evaluation": evaluation,
|
| 645 |
+
"state": self.state,
|
| 646 |
+
}
|
| 647 |
+
|
| 648 |
+
def sync_model(self):
|
| 649 |
+
self.model["updatedAt"] = now()
|
| 650 |
+
self.model["randomSeed"] = self.random.seed
|
| 651 |
+
network = self.model["network"]
|
| 652 |
+
network["weights1"] = self.network.weights1
|
| 653 |
+
network["bias1"] = self.network.bias1
|
| 654 |
+
network["weights2"] = self.network.weights2
|
| 655 |
+
network["bias2"] = self.network.bias2
|
| 656 |
+
self.model["memory"] = self.memory.items
|
| 657 |
+
self.model["rules"] = self.rules.rules
|
| 658 |
+
self.model["behavior"] = self.behavior.program
|
| 659 |
+
self.model["state"] = self.state
|
| 660 |
+
|
| 661 |
+
def export_model(self):
|
| 662 |
+
self.sync_model()
|
| 663 |
+
return serialize_model(self.model)
|
| 664 |
+
|
| 665 |
+
@classmethod
|
| 666 |
+
def import_model(cls, buffer):
|
| 667 |
+
return cls(deserialize_model(buffer))
|
| 668 |
+
|
| 669 |
+
def get_state(self):
|
| 670 |
+
return self.state
|
| 671 |
+
|
| 672 |
+
def get_memory(self):
|
| 673 |
+
return self.memory.items
|
| 674 |
+
|
| 675 |
+
def get_rules(self):
|
| 676 |
+
return self.rules.rules
|
| 677 |
+
|
| 678 |
+
def get_behavior(self):
|
| 679 |
+
return self.behavior.program
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
if __name__ == "__main__":
|
| 683 |
+
output_path = "noe-model.dodl"
|
| 684 |
+
create_model_file(output_path)
|
| 685 |
+
print(f"Model file created: {output_path}")
|
noe-model.dodl
ADDED
|
Binary file (4.52 kB). View file
|
|
|