File size: 79,106 Bytes
402bb0b 5f7e99a 2bd7ac2 cc69dae 5f7e99a 387e8e2 5f7e99a 246b6b9 5f7e99a d35c51d 6e246aa 246b6b9 6e246aa d35c51d 6e246aa 246b6b9 6e246aa d35c51d 6e246aa 5f7e99a 2bd7ac2 3fc376c 5f7e99a 6e246aa 5f7e99a 6e246aa 5f7e99a eca0206 1fdcc4a a27301b fc20dd6 a27301b 1fdcc4a 814b3f2 38ade5b a27301b fc20dd6 1fdcc4a a27301b 5f7e99a 1fdcc4a eca0206 a27301b 5f7e99a 4892b12 5f7e99a 2bd7ac2 353c034 6e246aa 3fc376c d9782fd 3fc376c 246b6b9 3fc376c 6e246aa 3fc376c 246b6b9 3fc376c 246b6b9 3fc376c 6e246aa 3fc376c fc20dd6 246b6b9 3fc376c 6e246aa 3fc376c d9782fd 3fc376c 6e246aa 3fc376c d9782fd 3fc376c d9782fd 3fc376c 6e246aa 3fc376c 6e246aa 3fc376c 6e246aa 3fc376c 6e246aa 3fc376c 6e246aa 3fc376c 6e246aa fc20dd6 3fc376c 246b6b9 3fc376c 246b6b9 3fc376c 353c034 2bd7ac2 353c034 2bd7ac2 6e246aa 2bd7ac2 353c034 2bd7ac2 353c034 2bd7ac2 6e246aa 2bd7ac2 6e246aa 353c034 2bd7ac2 fc20dd6 2bd7ac2 246b6b9 2bd7ac2 246b6b9 2bd7ac2 6e246aa 5f7e99a 6e246aa 86c188f 6e246aa 5f7e99a 6e246aa 5f7e99a 3fc376c fc20dd6 5f7e99a b93ff7c cc69dae b93ff7c 246b6b9 cc69dae 4892b12 cc69dae b93ff7c cc69dae b93ff7c cc69dae 3fc376c fc20dd6 cc69dae 246b6b9 3c0e9c0 246b6b9 bd9a8ac 6e246aa 246b6b9 5f7e99a bd9a8ac 246b6b9 5f7e99a 86c188f 5f7e99a 246b6b9 387e8e2 86c188f 5f7e99a 3fc376c 5f7e99a 1fdcc4a fc20dd6 a27301b 5f7e99a 86c188f 4892b12 b93ff7c 4892b12 b93ff7c 5f7e99a 6e246aa 4892b12 3fc376c 4892b12 3fc376c 4892b12 3fc376c 6e246aa 4892b12 6e246aa 4892b12 6e246aa 4892b12 5f7e99a fc20dd6 5f7e99a 353c034 5f7e99a 1fdcc4a fc20dd6 a27301b fc20dd6 a27301b 1fdcc4a 38ade5b a27301b fc20dd6 1fdcc4a a27301b fc20dd6 a27301b 1fdcc4a 4892b12 5f7e99a 4892b12 1fdcc4a fc20dd6 4892b12 1fdcc4a 4892b12 1fdcc4a 4892b12 3fc376c 4892b12 3fc376c 4892b12 5f7e99a 4892b12 5f7e99a fc20dd6 4892b12 fc20dd6 eca0206 fc20dd6 eca0206 fc20dd6 eca0206 5f7e99a 353c034 5f7e99a fc20dd6 5f7e99a fc20dd6 5f7e99a 1fdcc4a fc20dd6 4892b12 38ade5b 1fdcc4a 814b3f2 1fdcc4a fc20dd6 a27301b 5f7e99a 1fdcc4a fc20dd6 eca0206 a27301b 1fdcc4a 814b3f2 38ade5b 1fdcc4a 4892b12 1fdcc4a fc20dd6 1fdcc4a 353c034 5f7e99a 86c188f fc20dd6 5f7e99a eca0206 a27301b 5f7e99a fc20dd6 5f7e99a 86c188f 38ade5b 387e8e2 fc20dd6 38ade5b 86c188f 38ade5b 86c188f fc20dd6 38ade5b 387e8e2 0d84a74 50c7688 0d84a74 50c7688 0d84a74 50c7688 5f7e99a 86c188f 7ef2ed3 86c188f 7ef2ed3 86c188f 7ef2ed3 86c188f 7ef2ed3 86c188f 7ef2ed3 86c188f fc20dd6 7ef2ed3 4892b12 fc20dd6 4892b12 fc20dd6 7ef2ed3 fc20dd6 4892b12 fc20dd6 7ef2ed3 fc20dd6 4892b12 fc20dd6 7ef2ed3 fc20dd6 7ef2ed3 fc20dd6 4892b12 fc20dd6 7ef2ed3 fc20dd6 7ef2ed3 fc20dd6 4892b12 fc20dd6 4892b12 fc20dd6 4892b12 fc20dd6 4892b12 fc20dd6 7ef2ed3 fc20dd6 7ef2ed3 fc20dd6 7ef2ed3 fc20dd6 6e246aa fc20dd6 6e246aa fc20dd6 6e246aa fc20dd6 6e246aa fc20dd6 4892b12 fc20dd6 86c188f fc20dd6 5f7e99a 2bd7ac2 3fc376c 2bd7ac2 3fc376c 7ef2ed3 6e246aa 7ef2ed3 2bd7ac2 353c034 2bd7ac2 3fc376c 7ef2ed3 2bd7ac2 5f7e99a 7ef2ed3 6e246aa 7ef2ed3 3fc376c d9782fd 3fc376c d9782fd 3fc376c d9782fd 3fc376c d9782fd 3fc376c 353c034 2bd7ac2 8f71783 2bd7ac2 353c034 2bd7ac2 8f71783 2bd7ac2 6e246aa 2bd7ac2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 | #!/usr/bin/env python3
# Copyright (c) Napol Thanarangkaun
# Licensed under the MIT License - See LICENSE file for details
from array import array
import argparse
import copy
import json
import math
import os
import secrets
import tempfile
import time
try:
from .model_config import CONFIG, HEADER_SIZE, MAGIC
from .model_formats import (
decode_model_from_storage,
deserialize_model,
encode_model_for_storage,
load_stored_model,
save_stored_model,
serialize_gguf_model,
serialize_model,
validate_model,
)
from .model_memory import MemoryStore, evaluate_memory_retrieval
from .model_network import TinyNetwork, softmax
from .model_providers import (
BiocomputationProvider,
BiocomputationProviderError,
LLMProvider,
LLMProviderError,
normalize_provider_response,
)
from .model_rules import BehaviorEngine, RuleEngine, _js_number
from .model_utils import Random, clamp, now, random_float, random_int, uuid_bytes
except ImportError: # run as a script from core/model
from model_config import CONFIG, HEADER_SIZE, MAGIC
from model_formats import (
decode_model_from_storage,
deserialize_model,
encode_model_for_storage,
load_stored_model,
save_stored_model,
serialize_gguf_model,
serialize_model,
validate_model,
)
from model_memory import MemoryStore, evaluate_memory_retrieval
from model_network import TinyNetwork, softmax
from model_providers import (
BiocomputationProvider,
BiocomputationProviderError,
LLMProvider,
LLMProviderError,
normalize_provider_response,
)
from model_rules import BehaviorEngine, RuleEngine, _js_number
from model_utils import Random, clamp, now, random_float, random_int, uuid_bytes
PIXEL_INPUT_SIZE = 8
PIXEL_OUTPUT_SIZE = 2
ACTION_LABELS = ("rest", "explore", "learn", "observe")
# Input-vector layout for action-state-text-v1 and action-context-v2 networks.
FEATURE_TEXT_LENGTH = 4
FEATURE_IS_USER_INPUT = 5
FEATURE_MEMORY_FILL = 6
FEATURE_TEXT_HASH_START = 7
FEATURE_TEXT_HASH_END_CONTEXTUAL = 9
FEATURE_GOAL_GAP_START = 9
FEATURE_RECALLED_ACTION_START = 12
# Pixel network layout, mirrored by pixel_model.mjs.
PIXEL_FEATURES = (
"x", "y", "velocityX", "velocityY", "pointerDx", "pointerDy", "hasPointer", "excitement",
)
def create_initial_self_model():
return {
"actions": {},
"predictionCount": 0,
"meanAbsoluteError": 0.0,
"lastPrediction": None,
"lastOutcome": None,
"lastError": 0.0,
}
def create_initial_bio_signals():
return {"novelty": 0.0, "stress": 0.1, "recovery": 0.5, "arousal": 0.05}
def create_initial_workspace():
return {
"version": 2,
"cycle": 0,
"focus": "idle",
"externalInput": None,
"internalState": {},
"presentSelf": None,
"providerAnalysis": None,
"goal": None,
"intention": None,
"prediction": None,
"outcome": None,
"predictionError": 0.0,
"memories": [],
"broadcast": {},
}
def create_initial_state():
timestamp = now()
return {
"energy": 72,
"curiosity": 50,
"confidence": 50,
"stability": 80,
"cycle": 0,
"lastAction": None,
"lastReward": 0,
"bioSignals": create_initial_bio_signals(),
"selfModel": create_initial_self_model(),
"workspace": create_initial_workspace(),
"createdAt": timestamp,
"updatedAt": timestamp,
}
def create_initial_goals():
return [
{"id": "survive", "priority": 100, "target": {"energyMin": 20}},
{"id": "explore", "priority": 50, "target": {"curiosityMin": 60}},
{"id": "learn", "priority": 40, "target": {"confidenceMin": 50}},
]
def create_initial_rules():
return [
{
"id": "rest-low-energy",
"condition": {"type": "less-than", "variable": "energy", "value": 20},
"action": {"type": "rest"},
"priority": 100,
"enabled": True,
"safety": True,
"success": 0,
"failure": 0,
},
{
"id": "explore-high-curiosity",
"condition": {"type": "greater-than", "variable": "curiosity", "value": 70},
"action": {"type": "explore"},
"priority": 60,
"enabled": True,
"success": 0,
"failure": 0,
},
{
"id": "observe-default",
"condition": {"type": "always"},
"action": {"type": "observe"},
"priority": 10,
"enabled": True,
"success": 0,
"failure": 0,
},
]
def create_initial_behavior():
return [
["READ", "energy"],
["LESS_THAN", 20],
["IF"],
["ACTION", "rest"],
["ELSE"],
["READ", "curiosity"],
["GREATER_THAN", 70],
["IF"],
["ACTION", "explore"],
["ELSE"],
["ACTION", "observe"],
["END"],
["END"],
]
def _pixel_training_example(random):
x = random.next()
y = random.next()
velocity_x = random.float(-1, 1)
velocity_y = random.float(-1, 1)
has_pointer = 1 if random.next() < 0.8 else 0
if has_pointer:
pointer_x = random.next()
pointer_y = random.next()
pointer_dx = pointer_x - x
pointer_dy = (pointer_y - y) * 0.7
else:
pointer_dx = 0
pointer_dy = 0
excitement = random.next()
inputs = array(
"f",
[x, y, velocity_x, velocity_y, pointer_dx, pointer_dy, has_pointer, excitement],
)
distance = math.hypot(pointer_dx, pointer_dy)
steering_x = -velocity_x * 0.04
steering_y = -velocity_y * 0.04
if has_pointer and distance < 0.3 and distance > 0:
strength = (0.3 - distance) / 0.3 * (0.65 + excitement * 0.35)
steering_x -= pointer_dx / distance * strength
steering_y -= pointer_dy / distance * strength
if x < 0.08:
steering_x += (0.08 - x) / 0.08 * 0.8
elif x > 0.92:
steering_x -= (x - 0.92) / 0.08 * 0.8
if y < 0.08:
steering_y += (0.08 - y) / 0.08 * 0.8
elif y > 0.92:
steering_y -= (y - 0.92) / 0.08 * 0.8
targets = array("f", [clamp(steering_x, -1, 1), clamp(steering_y, -1, 1)])
return inputs, targets
def _pixel_validation_metrics(network, examples):
squared_errors = [0.0] * network.output_size
absolute_errors = [0.0] * network.output_size
for inputs, targets in examples:
outputs = network.forward(inputs)["output"]
for index in range(network.output_size):
error = outputs[index] - targets[index]
squared_errors[index] += error * error
absolute_errors[index] += abs(error)
sample_count = len(examples)
mse = sum(squared_errors) / (sample_count * network.output_size)
return {
"mse": mse,
"mae_x": absolute_errors[0] / sample_count,
"mae_y": absolute_errors[1] / sample_count,
}
ACTION_LEARNING_RATE = 0.05
ACTION_LEARNING_RATE_DECAY = 0.1
ACTION_WEIGHT_DECAY = 1e-4
ACTION_GRAD_CLIP = 1.0
PIXEL_LEARNING_RATE = 0.01
def _action_policy_target(energy, curiosity, confidence):
if energy < 20:
return "rest"
if curiosity > 70:
return "explore"
if confidence < 40:
return "learn"
return "observe"
def _action_policy_example(energy, curiosity, confidence, stability=80):
inputs = array("f", [0]) * CONFIG["INPUT_SIZE"]
inputs[0] = energy / 100
inputs[1] = curiosity / 100
inputs[2] = confidence / 100
inputs[3] = stability / 100
return inputs, _action_policy_target(energy, curiosity, confidence)
def _action_policy_validation_examples():
return [
_action_policy_example(energy, curiosity, confidence)
for energy in (0, 19, 20, 50, 100)
for curiosity in (0, 70, 71, 100)
for confidence in (0, 39, 40, 60, 100)
]
def _action_policy_held_out_examples():
return [
_action_policy_example(energy, curiosity, confidence)
for energy in (5, 15, 35, 65, 90)
for curiosity in (10, 65, 75, 90)
for confidence in (10, 35, 45, 75, 95)
]
def _action_policy_metrics(network, examples):
correct = 0
rule_baseline_correct = 0
squared_error = 0.0
cross_entropy = 0.0
class_totals = {label: 0 for label in ACTION_LABELS}
class_correct = {label: 0 for label in ACTION_LABELS}
for inputs, target in examples:
target_index = ACTION_LABELS.index(target)
outputs = network.forward(inputs)["output"]
prediction = ACTION_LABELS[max(range(len(ACTION_LABELS)), key=lambda index: outputs[index])]
rule_prediction = _action_policy_target(
inputs[0] * 100, inputs[1] * 100, inputs[2] * 100
)
if prediction == target:
correct += 1
class_correct[target] += 1
if rule_prediction == target:
rule_baseline_correct += 1
class_totals[target] += 1
for index, output in enumerate(outputs[: len(ACTION_LABELS)]):
expected = 1.0 if index == target_index else 0.0
squared_error += (output - expected) ** 2
cross_entropy -= math.log(max(softmax(outputs[: len(ACTION_LABELS)])[target_index], 1e-12))
sample_count = len(examples)
return {
"accuracy": correct / sample_count,
"baseline_accuracy": max(class_totals.values()) / sample_count,
"rule_baseline_accuracy": rule_baseline_correct / sample_count,
"mse": squared_error / (sample_count * len(ACTION_LABELS)),
"cross_entropy": cross_entropy / sample_count,
"per_action_accuracy": {
label: class_correct[label] / class_totals[label]
if class_totals[label]
else 0.0
for label in ACTION_LABELS
},
"per_action_support": class_totals,
}
def _network_from_model(model):
stored = model["network"]
network = TinyNetwork(
Random(1),
input_size=stored["inputSize"],
hidden_size=stored["hiddenSize"],
output_size=stored["outputSize"],
)
network.weights1 = stored["weights1"]
network.bias1 = stored["bias1"]
network.weights2 = stored["weights2"]
network.bias2 = stored["bias2"]
return network
def evaluate_action_model(model):
"""Score a model on validation and separate held-out policy scenarios."""
if not isinstance(model, dict):
model = load_model_file(model)
network = _network_from_model(model)
if network.output_size < len(ACTION_LABELS):
raise ValueError("Action models must have at least four output values.")
metrics = _action_policy_metrics(network, _action_policy_validation_examples())
metrics["held_out"] = _action_policy_metrics(
network, _action_policy_held_out_examples()
)
metrics["policy_trained"] = bool(model["network"].get("policyTrained", False))
return metrics
def train_action_model(
path="noe-action-model.dodl",
sample_count=1200,
epochs=20,
seed=None,
patience=5,
metrics_callback=None,
):
if not isinstance(sample_count, int) or isinstance(sample_count, bool) or sample_count < 1:
raise ValueError("sample_count must be a positive integer.")
if not isinstance(epochs, int) or isinstance(epochs, bool) or epochs < 1:
raise ValueError("epochs must be a positive integer.")
if not isinstance(patience, int) or isinstance(patience, bool) or patience < 1:
raise ValueError("patience must be a positive integer.")
if metrics_callback is not None and not callable(metrics_callback):
raise ValueError("metrics_callback must be callable.")
random = Random(seed if seed is not None else random_int(1, 0xFFFFFFFF))
model = create_model(seed=random.seed)
network = TinyNetwork(random, output_size=len(ACTION_LABELS))
for hidden_index in range(network.hidden_size):
start = hidden_index * network.input_size
for input_index in range(FEATURE_TEXT_LENGTH, network.input_size):
network.weights1[start + input_index] = 0
examples = [
_action_policy_example(
random.float(0, 100),
random.float(0, 100),
random.float(0, 100),
)
for _ in range(sample_count)
]
validation_examples = _action_policy_validation_examples()
best_score = (-1.0, float("inf"))
best_weights = None
stale_epochs = 0
for epoch in range(1, epochs + 1):
for index in range(sample_count - 1, 0, -1):
swap_index = int(random.next() * (index + 1))
examples[index], examples[swap_index] = examples[swap_index], examples[index]
learning_rate = ACTION_LEARNING_RATE / (1 + ACTION_LEARNING_RATE_DECAY * (epoch - 1))
for inputs, label in examples:
targets = array("f", [0]) * len(ACTION_LABELS)
targets[ACTION_LABELS.index(label)] = 1
network.learn(
inputs, targets, learning_rate,
loss="cross_entropy", weight_decay=ACTION_WEIGHT_DECAY, grad_clip=ACTION_GRAD_CLIP,
)
metrics = _action_policy_metrics(network, validation_examples)
if metrics_callback is not None:
metrics_callback(epoch, metrics)
score = (metrics["accuracy"], -metrics["cross_entropy"])
if score > best_score:
best_score = score
best_weights = {
key: array("f", getattr(network, key))
for key in ("weights1", "bias1", "weights2", "bias2")
}
stale_epochs = 0
else:
stale_epochs += 1
if stale_epochs >= patience:
break
for key, values in best_weights.items():
setattr(network, key, values)
model["randomSeed"] = random.seed
model["network"] = {
"inputSize": network.input_size,
"hiddenSize": network.hidden_size,
"outputSize": network.output_size,
"weights1": network.weights1,
"bias1": network.bias1,
"weights2": network.weights2,
"bias2": network.bias2,
"policyTrained": True,
"trainingMethod": "threshold-imitation-v2",
"featureSchema": "action-state-text-v1",
}
model["updatedAt"] = now()
gguf_path = os.path.splitext(os.fsdecode(path))[0] + ".F32.gguf"
_write_model_artifacts(
((path, serialize_model(model)), (gguf_path, serialize_gguf_model(model)))
)
return model
def train_pixel_model(
path="noe-pixel-model.dodl",
sample_count=1200,
epochs=12,
seed=None,
patience=4,
metrics_callback=None,
):
if not isinstance(sample_count, int) or sample_count < 1:
raise ValueError("sample_count must be a positive integer.")
if not isinstance(epochs, int) or epochs < 1:
raise ValueError("epochs must be a positive integer.")
if not isinstance(patience, int) or patience < 1:
raise ValueError("patience must be a positive integer.")
if metrics_callback is not None and not callable(metrics_callback):
raise ValueError("metrics_callback must be callable.")
random = Random(seed if seed is not None else random_int(1, 0xFFFFFFFF))
model = create_model(seed=random.seed)
network = TinyNetwork(random, input_size=PIXEL_INPUT_SIZE, output_size=PIXEL_OUTPUT_SIZE)
validation_count = max(128, min(512, sample_count // 5))
validation_examples = [_pixel_training_example(random) for _ in range(validation_count)]
examples = [_pixel_training_example(random) for _ in range(sample_count)]
best_loss = float("inf")
best_weights = None
stale_epochs = 0
for epoch in range(1, epochs + 1):
for index in range(sample_count - 1, 0, -1):
swap_index = int(random.next() * (index + 1))
examples[index], examples[swap_index] = examples[swap_index], examples[index]
learning_rate = PIXEL_LEARNING_RATE / (1 + ACTION_LEARNING_RATE_DECAY * (epoch - 1))
for inputs, targets in examples:
network.learn(inputs, targets, learning_rate, weight_decay=ACTION_WEIGHT_DECAY)
metrics = _pixel_validation_metrics(network, validation_examples)
if metrics_callback is not None:
metrics_callback(epoch, metrics)
if metrics["mse"] < best_loss:
best_loss = metrics["mse"]
best_weights = {
key: array("f", getattr(network, key))
for key in ("weights1", "bias1", "weights2", "bias2")
}
stale_epochs = 0
else:
stale_epochs += 1
if stale_epochs >= patience:
break
for key, values in best_weights.items():
setattr(network, key, values)
model["randomSeed"] = random.seed
model["network"] = {
"inputSize": network.input_size,
"hiddenSize": network.hidden_size,
"outputSize": network.output_size,
"weights1": network.weights1,
"bias1": network.bias1,
"weights2": network.weights2,
"bias2": network.bias2,
"trainingMethod": "pixel-steering-v1",
"featureSchema": "pixel-steering-v1",
}
model["updatedAt"] = now()
gguf_path = os.path.splitext(os.fsdecode(path))[0] + ".F32.gguf"
_write_model_artifacts(
((path, serialize_model(model)), (gguf_path, serialize_gguf_model(model)))
)
return model
def train_action_model_from_outcomes(
model, episodes=None, *, path=None, epochs=3, learning_rate=0.02, seed=1
):
"""Fine-tune a trained policy with reward-weighted policy-gradient steps on recorded outcomes.
Episodes default to the model's own memory. Actions that earned more than the mean reward
are reinforced and those that earned less are discouraged; rules still override the policy.
"""
if not isinstance(model, dict):
model = load_model_file(model)
validate_model(model)
network_info = model["network"]
if not network_info.get("policyTrained") or network_info["outputSize"] < len(ACTION_LABELS):
raise ValueError("A trained action policy is required.")
if not isinstance(epochs, int) or isinstance(epochs, bool) or epochs < 1:
raise ValueError("epochs must be a positive integer.")
if isinstance(learning_rate, bool) or not isinstance(learning_rate, (int, float)) or not 0 < learning_rate < math.inf:
raise ValueError("learning_rate must be a positive finite number.")
if not isinstance(seed, int) or isinstance(seed, bool):
raise ValueError("seed must be an integer.")
def finite_number(value):
return isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value)
metrics = ("energy", "curiosity", "confidence")
usable = [
episode for episode in (model["memory"] if episodes is None else episodes)
if isinstance(episode, dict) and episode.get("action") in ACTION_LABELS
and finite_number(episode.get("reward")) and isinstance(episode.get("previousState"), dict)
and all(finite_number(episode["previousState"].get(key)) for key in metrics)
]
if not usable:
raise ValueError("No usable outcome episodes.")
result = copy.deepcopy(model)
# Recall features are built without memory so an episode never retrieves itself.
feature_model = copy.deepcopy(model)
feature_model["memory"] = []
engine = AIEngine(feature_model)
examples = []
for episode in usable:
engine.state.update({key: episode["previousState"][key] for key in metrics})
examples.append((engine.encode_input(episode.get("input")), episode["action"], episode["reward"]))
mean_reward = sum(reward for _, _, reward in examples) / len(examples)
random = Random(seed)
network = _network_from_model(result)
for _ in range(epochs):
for index in range(len(examples) - 1, 0, -1):
swap_index = int(random.next() * (index + 1))
examples[index], examples[swap_index] = examples[swap_index], examples[index]
for inputs, action, reward in examples:
targets = array("f", [0]) * network.output_size
targets[ACTION_LABELS.index(action)] = 1
network.learn(
inputs, targets, learning_rate * (reward - mean_reward),
loss="cross_entropy", grad_clip=ACTION_GRAD_CLIP,
)
result["network"]["trainingMethod"] = "outcome-policy-gradient-v1"
result["updatedAt"] = now()
if path is not None:
gguf_path = os.path.splitext(os.fsdecode(path))[0] + ".F32.gguf"
_write_model_artifacts(((path, serialize_model(result)), (gguf_path, serialize_gguf_model(result))))
return result, {"episodes": len(examples), "mean_reward": mean_reward, "epochs": epochs}
def create_model(seed=None):
random = Random(seed if seed is not None else random_int(1, 0xFFFFFFFF))
network = TinyNetwork(random)
timestamp = now()
return {
"version": CONFIG["VERSION"],
"id": list(secrets.token_bytes(16)),
"createdAt": timestamp,
"updatedAt": timestamp,
"randomSeed": random.seed,
"network": {
"inputSize": network.input_size,
"hiddenSize": network.hidden_size,
"outputSize": network.output_size,
"weights1": network.weights1,
"bias1": network.bias1,
"weights2": network.weights2,
"bias2": network.bias2,
"policyTrained": False,
"trainingMethod": "untrained",
"featureSchema": "action-state-text-v1",
},
"memory": [],
"rules": create_initial_rules(),
"behavior": create_initial_behavior(),
"state": create_initial_state(),
"goals": create_initial_goals(),
}
def load_model_file(path):
path = os.fspath(path)
with open(path, "rb") as model_file:
data = model_file.read()
if data.startswith(MAGIC):
return deserialize_model(data)
if os.path.splitext(path)[1].lower() != ".onnx":
raise ValueError("Unsupported model file format.")
try:
import onnx
except ImportError as error:
raise ValueError("Loading ONNX models requires the 'onnx' package.") from error
onnx_model = onnx.load(path)
nodes = list(onnx_model.graph.node)
if len(nodes) != 3 or [node.op_type for node in nodes] != ["Gemm", "Relu", "Gemm"]:
raise ValueError("ONNX models must contain a Gemm -> Relu -> Gemm network.")
first_gemm, relu, second_gemm = nodes
if (
first_gemm.output[0] != relu.input[0]
or relu.output[0] != second_gemm.input[0]
or len(first_gemm.input) < 2
or len(second_gemm.input) < 2
):
raise ValueError("ONNX model layers are not connected as Gemm -> Relu -> Gemm.")
initializers = {
initializer.name: onnx.numpy_helper.to_array(initializer)
for initializer in onnx_model.graph.initializer
}
def get_gemm_parameters(node):
attributes = {attribute.name: attribute.i for attribute in node.attribute}
if attributes.get("transA", 0):
raise ValueError("ONNX Gemm layers with transA are not supported.")
try:
weights = initializers[node.input[1]]
weights = weights.tolist() if hasattr(weights, "tolist") else weights
if not weights or not isinstance(weights[0], (list, tuple)):
raise ValueError("ONNX Gemm weights must be two-dimensional.")
weights = [list(row) for row in weights]
if not attributes.get("transB", 0):
weights = [list(row) for row in zip(*weights)]
bias = initializers[node.input[2]] if len(node.input) > 2 else None
except KeyError as error:
raise ValueError("ONNX Gemm weights and biases must be initializers.") from error
if not weights or not weights[0] or any(len(row) != len(weights[0]) for row in weights):
raise ValueError("ONNX Gemm weights must be a non-empty rectangular matrix.")
if bias is None:
bias = [0.0] * len(weights)
else:
bias = bias.tolist() if hasattr(bias, "tolist") else list(bias)
if len(bias) != len(weights):
raise ValueError("ONNX Gemm bias dimensions do not match its weights.")
alpha = next((attribute.f for attribute in node.attribute if attribute.name == "alpha"), 1.0)
beta = next((attribute.f for attribute in node.attribute if attribute.name == "beta"), 1.0)
return (
[[value * alpha for value in row] for row in weights],
[value * beta for value in bias],
)
weights1, bias1 = get_gemm_parameters(first_gemm)
weights2, bias2 = get_gemm_parameters(second_gemm)
if len(weights2[0]) != len(weights1):
raise ValueError("ONNX Gemm layer dimensions do not match.")
model = create_model()
model["network"] = {
"inputSize": len(weights1[0]),
"hiddenSize": len(weights1),
"outputSize": len(weights2),
"weights1": array("f", (value for row in weights1 for value in row)),
"bias1": array("f", bias1),
"weights2": array("f", (value for row in weights2 for value in row)),
"bias2": array("f", bias2),
}
return model
def merge_models(model_a, model_b, alpha=0.5):
if not isinstance(model_a, dict):
model_a = load_model_file(model_a)
if not isinstance(model_b, dict):
model_b = load_model_file(model_b)
validate_model(model_a)
validate_model(model_b)
if isinstance(alpha, bool) or not isinstance(alpha, (int, float)) or not math.isfinite(alpha):
raise ValueError("alpha must be a finite number between 0 and 1.")
if not 0 <= alpha <= 1:
raise ValueError("alpha must be between 0 and 1.")
if model_a["version"] != model_b["version"]:
raise ValueError("Models must have matching versions.")
network_a = model_a["network"]
network_b = model_b["network"]
if (network_a.get("policyTrained") and network_b.get("policyTrained")
and network_a.get("featureSchema", "unspecified") != network_b.get("featureSchema", "unspecified")):
raise ValueError("Trained policies must have matching feature schemas to merge.")
shape_keys = ("inputSize", "hiddenSize", "outputSize")
shape_a = tuple(network_a[key] for key in shape_keys)
shape_b = tuple(network_b[key] for key in shape_keys)
def expected_lengths(shape):
input_size, hidden_size, output_size = shape
if any(not isinstance(size, int) or size <= 0 for size in shape):
raise ValueError("Models have invalid network shapes.")
return {
"weights1": input_size * hidden_size,
"bias1": hidden_size,
"weights2": hidden_size * output_size,
"bias2": output_size,
}
lengths_a = expected_lengths(shape_a)
lengths_b = expected_lengths(shape_b)
for key in lengths_a:
if len(network_a[key]) != lengths_a[key] or len(network_b[key]) != lengths_b[key]:
raise ValueError(f"Models have invalid {key} array lengths.")
merged = copy.deepcopy(model_a)
merged["id"] = list(secrets.token_bytes(16))
merged["createdAt"] = now()
merged["updatedAt"] = merged["createdAt"]
for key, size in zip(shape_keys, shape_b):
merged["network"][key] = size
input_size, hidden_size, output_size = shape_b
adapted_a = {}
for key, rows_a, columns_a, rows_b, columns_b in (
("weights1", shape_a[1], shape_a[0], hidden_size, input_size),
("weights2", shape_a[2], shape_a[1], output_size, hidden_size),
):
values = list(network_b[key])
source = network_a[key]
for row in range(min(rows_a, rows_b)):
for column in range(min(columns_a, columns_b)):
values[row * columns_b + column] = source[row * columns_a + column]
adapted_a[key] = values
for key, source_size, target_size in (
("bias1", shape_a[1], hidden_size),
("bias2", shape_a[2], output_size),
):
values = list(network_b[key])
values[: min(source_size, target_size)] = network_a[key][: min(source_size, target_size)]
adapted_a[key] = values
for key, expected_length in lengths_b.items():
values_a = adapted_a[key]
values_b = network_b[key]
merged["network"][key] = array(
"f",
((1 - alpha) * value_a + alpha * value_b for value_a, value_b in zip(values_a, values_b)),
)
merged["network"]["policyTrained"] = bool(
network_a.get("policyTrained", False)
and network_b.get("policyTrained", False)
)
merged["network"]["trainingMethod"] = "weight-merge-v1"
merged["network"]["featureSchema"] = (
network_a.get("featureSchema", "unspecified")
if network_a.get("featureSchema") == network_b.get("featureSchema")
else "unspecified"
)
return merged
def _write_model_artifacts(artifacts):
staged = []
try:
for path, data in artifacts:
path = os.fsdecode(os.fspath(path))
directory = os.path.dirname(os.path.abspath(path))
prefix = f".{os.path.basename(path)}."
with tempfile.NamedTemporaryFile(
mode="wb", dir=directory, prefix=prefix, suffix=".tmp", delete=False
) as temporary_file:
staged.append((temporary_file.name, path))
temporary_file.write(data)
for temporary_path, path in staged:
os.replace(temporary_path, path)
finally:
for temporary_path, _ in staged:
try:
os.unlink(temporary_path)
except FileNotFoundError:
pass
def create_model_file(path, seed=None):
model = create_model(seed)
_write_model_artifacts(((path, serialize_model(model)),))
return model
def create_gguf_model_file(path, model):
_write_model_artifacts(((path, serialize_gguf_model(model)),))
class AIEngine:
def __init__(self, model=None, provider=None, *, event_callback=None):
if event_callback is not None and not callable(event_callback):
raise ValueError("event_callback must be callable.")
self.model = model if model is not None else create_model()
validate_model(self.model)
self.provider = provider
self.event_callback = event_callback
self.random = Random(self.model["randomSeed"])
stored_network = self.model["network"]
self.network = TinyNetwork(self.random)
self.network.input_size = stored_network["inputSize"]
self.network.hidden_size = stored_network["hiddenSize"]
self.network.output_size = stored_network["outputSize"]
self.network.weights1 = stored_network["weights1"]
self.network.bias1 = stored_network["bias1"]
self.network.weights2 = stored_network["weights2"]
self.network.bias2 = stored_network["bias2"]
self.policy_trained = bool(stored_network.get("policyTrained", False))
self.memory = MemoryStore(self.model["memory"])
self.rules = RuleEngine(self.model["rules"])
self.behavior = BehaviorEngine(self.model["behavior"])
self.state = self.model["state"]
bio_signals = self.state.setdefault("bioSignals", create_initial_bio_signals())
for key, value in create_initial_bio_signals().items():
bio_signals.setdefault(key, value)
self_model = self.state.setdefault("selfModel", create_initial_self_model())
for key, value in create_initial_self_model().items():
self_model.setdefault(key, value)
self.workspace = self.state.setdefault("workspace", create_initial_workspace())
self.goals = self.model["goals"]
def _emit_event(self, event):
if self.event_callback is not None:
try:
self.event_callback(event)
except Exception:
pass
def encode_input(self, input_value, workspace=None):
vector = array("f", [0]) * self.network.input_size
contextual = self.model["network"].get("featureSchema") == "action-context-v2"
if contextual and workspace is None:
workspace = self.open_workspace(self.observe(input_value), persist=False)
state_features = (
self.state["energy"] / 100,
self.state["curiosity"] / 100,
self.state["confidence"] / 100,
self.state["stability"] / 100,
)
for index, value in enumerate(state_features[: len(vector)]):
vector[index] = value
if isinstance(input_value, dict):
value = input_value.get("value")
if isinstance(value, str) and len(vector) > FEATURE_TEXT_LENGTH:
vector[FEATURE_TEXT_LENGTH] = min(len(value) / 100, 1)
feature_start = FEATURE_TEXT_HASH_START
feature_end = FEATURE_TEXT_HASH_END_CONTEXTUAL if contextual else len(vector)
feature_count = feature_end - feature_start
if feature_count > 0:
for token in value.lower().split():
token_hash = 2166136261
for byte in token.encode("utf-8"):
token_hash = ((token_hash ^ byte) * 16777619) & 0xFFFFFFFF
bucket = token_hash % feature_count
sign = 1 if token_hash & 0x80000000 else -1
vector[feature_start + bucket] += sign
feature_values = vector[feature_start:feature_end]
magnitude = max((abs(value) for value in feature_values), default=0)
if magnitude > 1:
for index in range(feature_start, feature_end):
vector[index] /= magnitude
if input_value.get("type") == "user_input" and len(vector) > FEATURE_IS_USER_INPUT:
vector[FEATURE_IS_USER_INPUT] = 1
if len(vector) > FEATURE_MEMORY_FILL:
vector[FEATURE_MEMORY_FILL] = min(len(self.memory.items) / CONFIG["MEMORY_LIMIT"], 1)
if contextual:
goal = next((goal for goal in self.goals if goal.get("id") == workspace.get("goal")), None)
if goal:
for index, key in enumerate(("energy", "curiosity", "confidence"), FEATURE_GOAL_GAP_START):
target = goal.get("target", {}).get(key + "Min", goal.get("target", {}).get(key, self.state[key]))
vector[index] = clamp((target - self.state[key]) / 100, 0, 1)
for match in workspace.get("memories", []):
action = match["episode"].get("action")
if action in ACTION_LABELS:
index = FEATURE_RECALLED_ACTION_START + ACTION_LABELS.index(action)
vector[index] = max(vector[index], match["relevance"])
return vector
def observe(self, input_value):
return {"input": copy.deepcopy(input_value), "timestamp": now(), "state": copy.deepcopy(self.state)}
def interpret(self, perception, input_vector=None):
if input_vector is None:
input_vector = self.encode_input(perception["input"])
return self.network.forward(input_vector)
def sense_bio_signals(self, input_value):
signals = self.state["bioSignals"]
input_type = input_value.get("type") if isinstance(input_value, dict) else None
signals["novelty"] = clamp(
signals["novelty"] * 0.8 + (0.3 if input_type == "user_input" else 0.05),
0,
1,
)
signals["stress"] = clamp(
signals["stress"] * 0.9 + (0.15 if self.state["energy"] < 20 else 0),
0,
1,
)
signals["recovery"] = clamp(
signals["recovery"] * 0.9 + (0.1 if self.state["energy"] >= 50 else 0),
0,
1,
)
signals["arousal"] = clamp(
signals["novelty"] * 0.6 + signals["stress"] * 0.4, 0, 1
)
def apply_action_to_bio_signals(self, action):
signals = self.state["bioSignals"]
if action["type"] == "rest":
changes = {"stress": -0.15, "recovery": 0.2, "novelty": -0.1}
elif action["type"] == "explore":
changes = {"stress": 0.1, "recovery": -0.1, "novelty": 0.15}
elif action["type"] == "learn":
changes = {"stress": 0.03, "recovery": -0.05, "novelty": -0.05}
else:
changes = {"stress": 0, "recovery": 0, "novelty": -0.02}
for key, change in changes.items():
signals[key] = clamp(signals[key] + change, 0, 1)
signals["arousal"] = clamp(
signals["novelty"] * 0.6 + signals["stress"] * 0.4, 0, 1
)
def open_workspace(self, perception, persist=True):
input_value = perception["input"]
focus = input_value.get("type", "input") if isinstance(input_value, dict) else "input"
salient_goal = None
highest_score = 0
for goal in self.goals:
target = goal.get("target", {})
deficit = max(
(
value - self.state.get(key[:-3] if key.endswith("Min") else key, value)
for key, value in target.items()
if isinstance(value, (int, float))
),
default=0,
)
score = goal.get("priority", 0) * clamp(deficit / 100, 0, 1)
if score > highest_score:
highest_score = score
salient_goal = goal.get("id")
workspace = {
"version": 2,
"cycle": self.state["cycle"] + 1,
"focus": focus,
"externalInput": copy.deepcopy(input_value),
"internalState": {
key: self.state[key]
for key in ("energy", "curiosity", "confidence", "stability")
},
"providerAnalysis": None,
"goal": salient_goal,
"intention": None,
"prediction": None,
"outcome": None,
"predictionError": 0.0,
"memories": self.memory.recall(input_value),
"broadcast": {},
}
if persist:
self.workspace = workspace
self.state["workspace"] = workspace
return workspace
def decide(self, perception, workspace=None):
def selected(action_type, rule_id, source):
if workspace is not None:
workspace["decisionSource"] = source
return {"type": action_type, "ruleId": rule_id}
rule = self.rules.select(self.state)
if rule and rule.get("safety", False):
return selected(rule["action"]["type"], rule["id"], "safety_rule")
default_action = rule is None or rule["action"]["type"] == "observe"
signals = self.state["bioSignals"]
if workspace and default_action and signals["stress"] < 0.75 and signals["recovery"] > 0.1:
predicted_action = self.select_predicted_action(workspace)
if predicted_action is not None:
return selected(predicted_action, None, "prediction")
if workspace.get("goal") == "learn":
return selected("learn", None, "goal")
if workspace.get("goal") == "explore" and signals["novelty"] >= 0.7:
return selected("explore", None, "goal")
if rule and rule["action"]["type"] != "observe":
return selected(rule["action"]["type"], rule["id"], "rule")
if self.policy_trained and self.network.output_size >= len(ACTION_LABELS):
return selected(self._policy_action(perception), None, "policy")
if rule:
return selected(rule["action"]["type"], rule["id"], "rule")
return selected(self.behavior.run(self.state), None, "behavior")
def select_predicted_action(self, workspace=None):
best_action = None
best_score = 0.0
if workspace is not None:
workspace["candidates"] = []
for action_type in ACTION_LABELS:
action_model = self.state["selfModel"]["actions"].get(action_type, {})
count = action_model.get("count", 0)
if count <= 0:
continue
prediction = self.predict_action({"type": action_type}, workspace)
if prediction["energy"] < 20:
continue
score = 0.0
for goal in self.goals:
for key, target in goal.get("target", {}).items():
state_key = key[:-3] if key.endswith("Min") else key
if state_key not in prediction:
continue
before = max(0, target - self.state[state_key])
after = max(0, target - prediction[state_key])
score += goal.get("priority", 0) * (before - after)
score *= count / (count + 1)
if workspace is not None:
workspace["candidates"].append({
"action": action_type, "prediction": prediction,
"observations": count, "uncertainty": 1 / (count + 1),
"goalScore": score,
})
if score > best_score:
best_action = action_type
best_score = score
return best_action
def _policy_action(self, perception):
outputs = self.network.forward(self.encode_input(perception["input"], self.workspace))["output"]
return ACTION_LABELS[max(range(len(ACTION_LABELS)), key=lambda index: outputs[index])]
def predict_action(self, action, workspace=None):
action_model = self.state["selfModel"]["actions"].get(action["type"], {})
mean_delta = action_model.get("meanDelta", {})
metrics = ("energy", "curiosity", "confidence")
episodes = []
for match in (workspace or {}).get("memories", []):
episode = match["episode"]
previous = episode.get("previousState")
outcome = episode.get("outcome")
if episode.get("action") != action["type"] or not isinstance(previous, dict) or not isinstance(outcome, dict):
continue
if all(
isinstance(values.get(key), (int, float))
and not isinstance(values[key], bool)
and math.isfinite(values[key])
for values in (previous, outcome) for key in metrics
):
episodes.append(episode)
if episodes:
mean_delta = {
key: sum(episode["outcome"][key] - episode["previousState"][key]
for episode in episodes) / len(episodes)
for key in metrics
}
return {
key: clamp(self.state[key] + mean_delta.get(key, 0), 0, 100)
for key in metrics
}
def update_self_model(self, action, previous_state, prediction):
metrics = ("energy", "curiosity", "confidence")
observed = {key: self.state[key] for key in metrics}
error = sum(abs(prediction[key] - observed[key]) for key in metrics) / len(metrics)
self_model = self.state["selfModel"]
action_model = self_model["actions"].setdefault(
action["type"], {"count": 0, "meanDelta": {key: 0.0 for key in metrics}}
)
action_model["count"] += 1
for key in metrics:
delta = observed[key] - previous_state[key]
mean_delta = action_model["meanDelta"]
mean_delta[key] += (delta - mean_delta[key]) / action_model["count"]
self_model["predictionCount"] += 1
count = self_model["predictionCount"]
self_model["meanAbsoluteError"] += (error - self_model["meanAbsoluteError"]) / count
self_model["lastPrediction"] = {"action": action["type"], "state": prediction}
self_model["lastOutcome"] = {"action": action["type"], "state": observed}
self_model["lastError"] = error
return {"predicted": prediction, "observed": observed, "absoluteError": error}
def execute(self, action):
action_type = action["type"]
if action_type == "rest":
self.state["energy"] = clamp(self.state["energy"] + 10, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
self.state["curiosity"] = clamp(self.state["curiosity"] - 2, 0, 100)
elif action_type == "explore":
self.state["energy"] = clamp(self.state["energy"] - 8, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
self.state["curiosity"] = clamp(self.state["curiosity"] - 10, 0, 100)
elif action_type == "learn":
self.state["energy"] = clamp(self.state["energy"] - 3, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
self.state["confidence"] = clamp(self.state["confidence"] + 5, 0, 100)
else:
self.state["energy"] = clamp(self.state["energy"] - 1, CONFIG["STATE_MIN"], CONFIG["STATE_MAX"])
self.state["curiosity"] = clamp(self.state["curiosity"], 0, 100)
return {"action": action_type, "success": True, "timestamp": now()}
def evaluate(self, perception, action, result):
reward = 0
if self.state["energy"] >= 20:
reward += 1
if action["type"] == "rest" and self.state["energy"] > 50:
reward -= 0.2
if action["type"] == "explore":
reward += 0.5
return {"reward": reward, "perception": perception, "action": action, "result": result}
def learn(self, evaluation, input_vector=None):
action = evaluation["action"]
if action["ruleId"]:
self.rules.reward(action["ruleId"], evaluation["reward"])
self.memory.add(
{
"input": evaluation["perception"]["input"],
"action": action["type"],
"result": evaluation["result"],
"reward": evaluation["reward"],
"importance": abs(evaluation["reward"]) * 50 + 50,
"previousState": {
key: evaluation["perception"]["state"][key]
for key in ("energy", "curiosity", "confidence")
},
"prediction": self.workspace["prediction"],
"outcome": self.workspace["outcome"],
}
)
if self.random.next() < 0.05:
self.behavior.mutate(self.random)
self.rules.evolve()
def update_state(self, evaluation):
self.state["lastAction"] = evaluation["action"]["type"]
self.state["lastReward"] = evaluation["reward"]
self.state["cycle"] += 1
self.state["updatedAt"] = now()
self.state["curiosity"] += 1 if evaluation["reward"] > 0 else -1
self.state["curiosity"] = clamp(self.state["curiosity"], 0, 100)
def step(self, input_value, provider_analysis=None, workspace=None):
perception = self.observe(input_value)
self.sense_bio_signals(input_value)
if workspace is None:
workspace = self.open_workspace(perception)
else:
workspace = copy.deepcopy(workspace)
self.workspace = workspace
self.state["workspace"] = workspace
input_vector = self.encode_input(perception["input"], workspace)
interpretation = self.interpret(perception, input_vector)
workspace["providerAnalysis"] = copy.deepcopy(provider_analysis)
workspace["internalState"]["bioSignals"] = copy.deepcopy(self.state["bioSignals"])
workspace["presentSelf"] = {
"here": {"input": copy.deepcopy(perception["input"])},
"now": {"timestamp": perception["timestamp"], "cycle": self.state["cycle"] + 1},
"self": {
**{key: perception["state"][key] for key in ("energy", "curiosity", "confidence", "stability", "lastAction")},
"bioSignals": copy.deepcopy(self.state["bioSignals"]),
},
}
action = self.decide(perception, workspace)
prediction = self.predict_action(action, workspace)
workspace["intention"] = action["type"]
workspace["prediction"] = prediction
result = self.execute(action)
self.apply_action_to_bio_signals(action)
evaluation = self.evaluate(perception, action, result)
self.update_state(evaluation)
prediction_result = self.update_self_model(action, perception["state"], prediction)
workspace["outcome"] = prediction_result["observed"]
workspace["predictionError"] = prediction_result["absoluteError"]
workspace["broadcast"] = {
"externalInput": workspace["externalInput"],
"internalState": workspace["internalState"],
"presentSelf": workspace["presentSelf"],
"providerAnalysis": workspace["providerAnalysis"],
"goal": workspace["goal"],
"intention": workspace["intention"],
"decisionSource": workspace.get("decisionSource"),
"prediction": workspace["prediction"],
"outcome": workspace["outcome"],
"predictionError": workspace["predictionError"],
"memories": workspace["memories"],
"candidates": workspace.get("candidates", []),
"bioSignals": copy.deepcopy(self.state["bioSignals"]),
}
self.learn(evaluation, input_vector)
self.sync_model()
self._emit_event({
"event": "decision",
"source": workspace.get("decisionSource") if workspace.get("decisionSource") in ("safety_rule", "rule", "goal", "prediction", "policy", "behavior") else "unknown",
"action": action["type"] if action["type"] in ACTION_LABELS else "other",
"success": bool(result.get("success")),
})
return copy.deepcopy({
"perception": perception,
"interpretation": interpretation,
"action": action,
"prediction": prediction_result,
"workspace": dict(workspace),
"result": result,
"evaluation": evaluation,
"state": self.state,
})
def respond(self, input_value, provider=None, *, context_filter=None):
"""Generate a provider reply, then run one local engine step."""
active_provider = provider or self.provider or LLMProvider()
workspace = self.open_workspace(self.observe(input_value), persist=False)
provider_context = {
"user_input": copy.deepcopy(input_value),
"state": {key: self.state[key] for key in ("energy", "curiosity", "confidence", "stability")},
"memories": [],
}
if context_filter is not None:
full_state = copy.deepcopy(self.state)
full_state["workspace"] = copy.deepcopy(workspace)
provider_context = context_filter({
"user_input": copy.deepcopy(input_value),
"state": full_state,
"memories": [copy.deepcopy(match["episode"]) for match in workspace["memories"]],
})
if not isinstance(provider_context, dict) or set(provider_context) != {"user_input", "state", "memories"}:
raise ValueError("context_filter must return user_input, state, and memories.")
started = time.monotonic()
try:
if hasattr(active_provider, "generate_with_analysis"):
provider_response = active_provider.generate_with_analysis(**provider_context)
else:
provider_response = active_provider.generate(**provider_context)
normalized = normalize_provider_response(provider_response)
except Exception:
self._emit_event({"event": "provider", "outcome": "error", "duration_seconds": time.monotonic() - started})
raise
self._emit_event({"event": "provider", "outcome": "success", "duration_seconds": time.monotonic() - started})
result = self.step(input_value, provider_analysis=normalized["analysis"], workspace=workspace)
result["reply"] = normalized["reply"]
return result
def compute_biological(
self, task, provider=None, *, approval_id, idempotency_key=None
):
"""Submit an explicit task to a biological-computation service."""
if not isinstance(approval_id, str) or not approval_id.strip():
raise ValueError("A valid human approval ID is required.")
active_provider = provider or BiocomputationProvider()
return active_provider.compute(
task,
approval_id=approval_id,
idempotency_key=idempotency_key,
)
def sync_model(self):
self.model["updatedAt"] = now()
self.model["randomSeed"] = self.random.seed
network = self.model["network"]
network["weights1"] = self.network.weights1
network["bias1"] = self.network.bias1
network["weights2"] = self.network.weights2
network["bias2"] = self.network.bias2
self.model["memory"] = self.memory.items
self.model["rules"] = self.rules.rules
self.model["behavior"] = self.behavior.program
self.model["state"] = self.state
def export_model(self):
self.sync_model()
return serialize_model(self.model)
@classmethod
def import_model(cls, buffer):
return cls(deserialize_model(buffer))
def get_state(self):
return self.state
def get_memory(self):
return self.memory.items
def get_rules(self):
return self.rules.rules
def get_behavior(self):
return self.behavior.program
def evaluate_task_outcomes(records):
"""Summarize independently measured application outcomes by method and task."""
if not isinstance(records, list) or not records:
raise ValueError("records must be a non-empty list.")
methods = {}
for record in records:
if not isinstance(record, dict):
raise ValueError("Each outcome must be an object.")
task_id, method, success = (record.get(key) for key in ("task_id", "method", "success"))
if not isinstance(task_id, str) or not task_id.strip() or not isinstance(method, str) or not method.strip() or not isinstance(success, bool):
raise ValueError("Each outcome needs a task_id, method, and boolean success.")
tasks = methods.setdefault(method, {})
if task_id in tasks:
raise ValueError("Each method and task_id pair must be unique.")
measurements = {}
for key in ("latency_seconds", "cost_usd"):
if key in record:
value = record[key]
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or value < 0:
raise ValueError(f"{key} must be a non-negative finite number.")
measurements[key] = value
tasks[task_id] = {"success": success, **measurements}
summaries = {
method: {"tasks": len(tasks), "success_rate": sum(task["success"] for task in tasks.values()) / len(tasks)}
for method, tasks in methods.items()
}
for method, tasks in methods.items():
for key in ("latency_seconds", "cost_usd"):
values = [task[key] for task in tasks.values() if key in task]
if values:
summaries[method][f"mean_{key}"] = sum(values) / len(values)
summaries[method][f"{key}_count"] = len(values)
comparisons = {}
if "rule_baseline" in methods:
baseline = methods["rule_baseline"]
for method, tasks in methods.items():
if method == "rule_baseline":
continue
shared = tasks.keys() & baseline.keys()
comparisons[method] = {
"paired_tasks": len(shared),
"success_rate_difference": (sum(int(tasks[task]["success"]) - int(baseline[task]["success"]) for task in shared) / len(shared)) if shared else None,
}
for key in ("latency_seconds", "cost_usd"):
measured = [task for task in shared if key in tasks[task] and key in baseline[task]]
if measured:
comparisons[method][f"paired_{key}_count"] = len(measured)
comparisons[method][f"mean_{key}_difference"] = sum(tasks[task][key] - baseline[task][key] for task in measured) / len(measured)
return {"methods": summaries, "vs_rule_baseline": comparisons}
def evaluate_action_scenarios(model, scenarios):
"""Compare prediction-informed, policy-only, and threshold-baseline behavior."""
if not isinstance(model, dict):
model = load_model_file(model)
validate_model(model)
if not model["network"].get("policyTrained") or model["network"]["outputSize"] < len(ACTION_LABELS):
raise ValueError("A trained action policy is required.")
if not isinstance(scenarios, list) or not scenarios:
raise ValueError("scenarios must be a non-empty list.")
class PolicyEngine(AIEngine):
def select_predicted_action(self, workspace=None):
return None
class BaselineEngine(PolicyEngine):
def _policy_action(self, perception):
return _action_policy_target(
self.state["energy"], self.state["curiosity"], self.state["confidence"]
)
state_keys = ("energy", "curiosity", "confidence", "stability")
runs = {"prediction": [], "policy": [], "rule_baseline": []}
for scenario in scenarios:
if not isinstance(scenario, dict):
raise ValueError("Each scenario must be an object.")
initial = scenario.get("initial_state", {})
inputs = scenario.get("inputs")
objectives = scenario.get("objectives")
trajectory_objectives = scenario.get("trajectory_objectives")
expected_actions = scenario.get("expected_actions")
if not isinstance(initial, dict) or any(key not in state_keys for key in initial):
raise ValueError("initial_state must contain only supported state values.")
if not isinstance(inputs, list) or not inputs:
raise ValueError("Each scenario must contain a non-empty inputs list.")
if expected_actions is not None and (
not isinstance(expected_actions, list) or len(expected_actions) != len(inputs)
or any(action is not None and action not in ACTION_LABELS for action in expected_actions)
):
raise ValueError("expected_actions must contain an action or null for each input.")
if not isinstance(objectives, dict) or not objectives:
raise ValueError("Each scenario must contain objectives.")
for key, value in initial.items():
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value <= 100:
raise ValueError(f"Invalid initial state value for {key}.")
if trajectory_objectives is not None and (not isinstance(trajectory_objectives, dict) or not trajectory_objectives):
raise ValueError("trajectory_objectives must be a non-empty object.")
for objective_set in (objectives, trajectory_objectives or {}):
for key, bounds in objective_set.items():
if key not in state_keys or not isinstance(bounds, dict) or not bounds or any(bound not in ("min", "max") for bound in bounds):
raise ValueError("Objectives must specify min or max for supported state values.")
for value in bounds.values():
if isinstance(value, bool) or not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value <= 100:
raise ValueError("Objective bounds must be finite values between 0 and 100.")
if "min" in bounds and "max" in bounds and bounds["min"] > bounds["max"]:
raise ValueError("Objective min cannot exceed max.")
def satisfies(state, bounds_by_key):
return all(
("min" not in bounds or state[key] >= bounds["min"])
and ("max" not in bounds or state[key] <= bounds["max"])
for key, bounds in bounds_by_key.items()
)
scenario_model = copy.deepcopy(model)
if "goals" in scenario:
scenario_model["goals"] = copy.deepcopy(scenario["goals"])
validate_model(scenario_model)
for label, engine_type in (("prediction", AIEngine), ("policy", PolicyEngine), ("rule_baseline", BaselineEngine)):
engine = engine_type(copy.deepcopy(scenario_model))
engine.state.update(initial)
actions = []
sources = []
total_reward = 0.0
prediction_error = 0.0
minimum_energy = engine.state["energy"]
trajectory_success = satisfies(engine.state, trajectory_objectives) if trajectory_objectives is not None else None
for input_value in inputs:
result = engine.step(copy.deepcopy(input_value))
actions.append(result["action"]["type"])
sources.append(result["workspace"].get("decisionSource", "unknown"))
total_reward += result["evaluation"]["reward"]
prediction_error += result["prediction"]["absoluteError"]
minimum_energy = min(minimum_energy, result["state"]["energy"])
if trajectory_objectives is not None:
trajectory_success = trajectory_success and satisfies(engine.state, trajectory_objectives)
final_state = {key: engine.state[key] for key in state_keys}
succeeded = satisfies(final_state, objectives)
runs[label].append({
"actions": actions,
"decision_sources": sources,
"labeled_actions": sum(action is not None for action in expected_actions) if expected_actions is not None else 0,
"correct_actions": sum(expected is not None and actual == expected for actual, expected in zip(actions, expected_actions)) if expected_actions is not None else 0,
"total_reward": total_reward,
"mean_prediction_error": prediction_error / len(inputs),
"minimum_energy": minimum_energy,
"final_state": final_state,
"success": succeeded,
"trajectory_success": trajectory_success,
})
return {
label: {
"success_rate": sum(run["success"] for run in results) / len(results),
"trajectory_success_rate": (sum(run["trajectory_success"] for run in results if run["trajectory_success"] is not None) / sum(run["trajectory_success"] is not None for run in results)) if any(run["trajectory_success"] is not None for run in results) else None,
"mean_total_reward": sum(run["total_reward"] for run in results) / len(results),
"mean_prediction_error": sum(run["mean_prediction_error"] for run in results) / len(results),
"decision_source_counts": {
source: sum(run["decision_sources"].count(source) for run in results)
for source in ("safety_rule", "rule", "goal", "prediction", "policy", "behavior", "unknown")
},
"override_rate": sum(source in ("safety_rule", "rule", "goal", "prediction") for run in results for source in run["decision_sources"]) / sum(len(run["decision_sources"]) for run in results),
"action_accuracy": (sum(run["correct_actions"] for run in results) / sum(run["labeled_actions"] for run in results)) if any(run["labeled_actions"] for run in results) else None,
"scenarios": results,
}
for label, results in runs.items()
}
def train_action_model_from_episodes(path, training_episodes, validation_episodes, *, test_scenarios=None, epochs=20, seed=1):
"""Train on successful caller-provided demonstrations, validated on separate episodes."""
if not isinstance(epochs, int) or isinstance(epochs, bool) or epochs < 1:
raise ValueError("epochs must be a positive integer.")
if not isinstance(seed, int) or isinstance(seed, bool):
raise ValueError("seed must be an integer.")
if any(not isinstance(episodes, list) or not episodes for episodes in (training_episodes, validation_episodes)):
raise ValueError("Training and validation episodes must be non-empty lists.")
if any(not isinstance(episode, dict) for episode in training_episodes + validation_episodes):
raise ValueError("Each episode must be an object.")
if test_scenarios is not None and (not isinstance(test_scenarios, list) or not test_scenarios or any(not isinstance(item, dict) for item in test_scenarios)):
raise ValueError("test_scenarios must be a non-empty list of objects.")
splits = [training_episodes, validation_episodes] + ([test_scenarios] if test_scenarios is not None else [])
if any("group" in episode for split in splits for episode in split):
if any(not isinstance(episode.get("group"), str) or not episode["group"].strip() for split in splits for episode in split):
raise ValueError("Every episode and test scenario must have a non-empty group when grouping is used.")
groups = [set(episode["group"] for episode in split) for split in splits]
if any(groups[first] & groups[second] for first in range(len(groups)) for second in range(first + 1, len(groups))):
raise ValueError("Training, validation, and test groups must be distinct.")
try:
training_ids = {json.dumps(episode, sort_keys=True, allow_nan=False) for episode in training_episodes}
validation_ids = {json.dumps(episode, sort_keys=True, allow_nan=False) for episode in validation_episodes}
except (TypeError, ValueError) as error:
raise ValueError("Episodes must contain finite JSON values.") from error
if training_ids & validation_ids:
raise ValueError("Training and validation episodes must be distinct.")
def scenario_id(episode):
return json.dumps(
{key: episode.get(key, {} if key in ("initial_state", "goals") else None)
for key in ("initial_state", "inputs", "objectives", "goals")},
sort_keys=True, allow_nan=False,
)
if {scenario_id(episode) for episode in training_episodes} & {scenario_id(episode) for episode in validation_episodes}:
raise ValueError("Training and validation scenarios must be distinct.")
if test_scenarios is not None:
if not isinstance(test_scenarios, list) or not test_scenarios or any(not isinstance(item, dict) for item in test_scenarios):
raise ValueError("test_scenarios must be a non-empty list of objects.")
try:
used = {scenario_id(episode) for episode in training_episodes + validation_episodes}
if any(scenario_id(scenario) in used for scenario in test_scenarios):
raise ValueError("Test scenarios must be distinct from training and validation episodes.")
except (TypeError, AttributeError) as error:
raise ValueError("Scenarios must contain finite JSON values.") from error
class DemonstrationEngine(AIEngine):
def decide(self, perception, workspace=None):
rule = self.rules.select(self.state)
if rule and (rule.get("safety", False) or rule["action"]["type"] != "observe"):
if rule["action"]["type"] != self.demonstrated_action:
raise ValueError("A demonstrated action conflicts with a rule override.")
return {"type": self.demonstrated_action, "ruleId": rule["id"]}
return {"type": self.demonstrated_action, "ruleId": None}
template = create_model()
template["network"]["policyTrained"] = True
template["network"]["featureSchema"] = "action-context-v2"
def collect(episodes):
examples = []
for episode in episodes:
if not isinstance(episode, dict):
raise ValueError("Each episode must be an object.")
inputs = episode.get("inputs")
actions = episode.get("actions")
if not isinstance(inputs, list) or not isinstance(actions, list) or len(inputs) != len(actions) or not inputs:
raise ValueError("Episode inputs and actions must be non-empty lists of equal length.")
if any(action not in ACTION_LABELS for action in actions):
raise ValueError("Episode contains an unsupported action.")
scenario = {key: episode[key] for key in ("initial_state", "inputs", "objectives", "trajectory_objectives", "goals") if key in episode}
evaluate_action_scenarios(template, [scenario])
model = copy.deepcopy(template)
if "goals" in episode:
model["goals"] = copy.deepcopy(episode["goals"])
engine = DemonstrationEngine(model)
engine.state.update(episode.get("initial_state", {}))
def validate_trajectory():
for key, bounds in (episode.get("trajectory_objectives") or {}).items():
if ("min" in bounds and engine.state[key] < bounds["min"]) or ("max" in bounds and engine.state[key] > bounds["max"]):
raise ValueError("Demonstration does not satisfy its trajectory objectives.")
validate_trajectory()
for input_value, action in zip(inputs, actions):
examples.append((engine.encode_input(input_value), action))
engine.demonstrated_action = action
engine.step(copy.deepcopy(input_value))
validate_trajectory()
for key, bounds in episode["objectives"].items():
if ("min" in bounds and engine.state[key] < bounds["min"]) or ("max" in bounds and engine.state[key] > bounds["max"]):
raise ValueError("Demonstration does not satisfy its final-state objectives.")
return examples
examples = collect(training_episodes)
validation = collect(validation_episodes)
random = Random(seed)
network = TinyNetwork(random)
best_weights = None
best_score = (-1.0, float("-inf"))
for epoch in range(epochs):
for index in range(len(examples) - 1, 0, -1):
swap_index = int(random.next() * (index + 1))
examples[index], examples[swap_index] = examples[swap_index], examples[index]
learning_rate = ACTION_LEARNING_RATE / (1 + ACTION_LEARNING_RATE_DECAY * epoch)
for inputs, action in examples:
targets = array("f", [0]) * network.output_size
targets[ACTION_LABELS.index(action)] = 1
network.learn(
inputs, targets, learning_rate,
loss="cross_entropy", weight_decay=ACTION_WEIGHT_DECAY, grad_clip=ACTION_GRAD_CLIP,
)
metrics = _action_policy_metrics(network, validation)
score = (metrics["accuracy"], -metrics["cross_entropy"])
if score > best_score:
best_score = score
best_weights = {key: array("f", getattr(network, key)) for key in ("weights1", "bias1", "weights2", "bias2")}
model = create_model()
model["randomSeed"] = random.seed
model["network"].update(best_weights)
model["network"]["policyTrained"] = True
model["network"]["trainingMethod"] = "demonstrations-v1"
model["network"]["featureSchema"] = "action-context-v2"
gguf_path = os.path.splitext(os.fsdecode(path))[0] + ".F32.gguf"
report = {"validation": _action_policy_metrics(_network_from_model(model), validation)}
if test_scenarios is not None:
report["test_scenarios"] = evaluate_action_scenarios(model, test_scenarios)
results = report["test_scenarios"]
report["test_comparison"] = {
label: {
"scenarios": len(results[label]["scenarios"]),
"success_rate_difference": results[label]["success_rate"] - results["rule_baseline"]["success_rate"],
"override_rate": results[label]["override_rate"],
}
for label in ("prediction", "policy")
}
_write_model_artifacts(((path, serialize_model(model)), (gguf_path, serialize_gguf_model(model))))
return model, report
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create or train a Noesis model.")
training_modes = parser.add_mutually_exclusive_group()
training_modes.add_argument(
"--train-pixel",
action="store_true",
help="Train a pixel steering model and write noe-pixel-model.dodl.",
)
training_modes.add_argument(
"--train-actions",
action="store_true",
help="Train an action-selection model and write noe-action-model.dodl.",
)
training_modes.add_argument("--train-demonstrations", metavar="JSON", help="Train from demonstration splits in a JSON file.")
training_modes.add_argument(
"--train-outcomes", action="store_true",
help="Fine-tune the trained policy in --model from its recorded outcomes and write --output.",
)
training_modes.add_argument("--evaluate-scenarios", metavar="JSON", help="Evaluate scenario objects from a JSON file.")
training_modes.add_argument("--evaluate-outcomes", metavar="JSON", help="Evaluate application outcome records from a JSON file.")
training_modes.add_argument("--evaluate-memory", metavar="JSON", help="Evaluate retrieval queries from a JSON file.")
parser.add_argument("--samples", type=int, default=1200)
parser.add_argument("--epochs", type=int, default=12)
parser.add_argument("--patience", type=int, default=4)
parser.add_argument("--seed", type=int)
parser.add_argument("--output")
parser.add_argument("--model", default="noe-model.dodl", help="Model file for scenario or memory evaluation.")
arguments = parser.parse_args()
output_path = "noe-model.dodl"
if arguments.train_demonstrations:
with open(arguments.train_demonstrations, encoding="utf-8") as source:
dataset = json.load(source)
if not isinstance(dataset, dict) or set(dataset) - {"training", "validation", "test_scenarios"} or not {"training", "validation"} <= set(dataset):
parser.error("demonstrations JSON requires training and validation lists and optional test_scenarios")
output_path = arguments.output or "noe-demonstration-model.dodl"
_, report = train_action_model_from_episodes(
output_path, dataset["training"], dataset["validation"],
test_scenarios=dataset.get("test_scenarios"), epochs=arguments.epochs,
seed=arguments.seed if arguments.seed is not None else 1,
)
print(json.dumps(report, indent=2, allow_nan=False))
elif arguments.evaluate_scenarios or arguments.evaluate_outcomes or arguments.evaluate_memory:
dataset_path = arguments.evaluate_scenarios or arguments.evaluate_outcomes or arguments.evaluate_memory
with open(dataset_path, encoding="utf-8") as source:
dataset = json.load(source)
if arguments.evaluate_scenarios:
report = evaluate_action_scenarios(arguments.model, dataset)
elif arguments.evaluate_outcomes:
report = evaluate_task_outcomes(dataset)
else:
report = evaluate_memory_retrieval(MemoryStore(load_model_file(arguments.model)["memory"]), dataset)
print(json.dumps(report, indent=2, allow_nan=False))
elif arguments.train_outcomes:
output_path = arguments.output or "noe-outcome-model.dodl"
_, report = train_action_model_from_outcomes(
arguments.model, path=output_path,
seed=arguments.seed if arguments.seed is not None else 1,
)
print(json.dumps(report, indent=2, allow_nan=False))
print(f"Outcome-tuned model written: {output_path}")
elif arguments.train_actions:
output_path = arguments.output or "noe-action-model.dodl"
def report_action_metrics(epoch, metrics):
action_accuracy = " ".join(
f"{label}={accuracy:.3f}"
for label, accuracy in metrics["per_action_accuracy"].items()
)
print(
f"epoch {epoch}: validation_accuracy={metrics['accuracy']:.3f} "
f"baseline_accuracy={metrics['baseline_accuracy']:.3f} "
f"validation_mse={metrics['mse']:.6f} "
f"per_action_accuracy[{action_accuracy}]"
)
model = train_action_model(
output_path,
sample_count=arguments.samples,
epochs=arguments.epochs,
seed=arguments.seed,
patience=arguments.patience,
metrics_callback=report_action_metrics,
)
held_out_metrics = evaluate_action_model(model)["held_out"]
held_out_accuracy = " ".join(
f"{label}={accuracy:.3f}"
for label, accuracy in held_out_metrics["per_action_accuracy"].items()
)
print(
f"held_out_accuracy={held_out_metrics['accuracy']:.3f} "
f"per_action_accuracy[{held_out_accuracy}]"
)
print(f"Action model trained: {output_path}")
gguf_output_path = os.path.splitext(output_path)[0] + ".F32.gguf"
print(f"GGUF model file created: {gguf_output_path}")
elif arguments.train_pixel:
output_path = arguments.output or "noe-pixel-model.dodl"
def report_metrics(epoch, metrics):
print(
f"epoch {epoch}: validation_mse={metrics['mse']:.6f} "
f"x_mae={metrics['mae_x']:.6f} y_mae={metrics['mae_y']:.6f}"
)
train_pixel_model(
output_path,
sample_count=arguments.samples,
epochs=arguments.epochs,
seed=arguments.seed,
patience=arguments.patience,
metrics_callback=report_metrics,
)
gguf_output_path = os.path.splitext(output_path)[0] + ".F32.gguf"
print(f"Pixel control model trained: {output_path}")
print(f"GGUF model file created: {gguf_output_path}")
else:
model = create_model_file(output_path, arguments.seed)
print(f"Model file created: {output_path}")
gguf_output_path = "noe-model.F32.gguf"
create_gguf_model_file(gguf_output_path, model)
print(f"GGUF model file created: {gguf_output_path}") |