From 081a3e519a94ada17100bfceac907b8530060002 Mon Sep 17 00:00:00 2001 From: Marek Wolan Date: Sun, 8 Oct 2023 17:57:45 +0100 Subject: [PATCH] Fix certain observation bugs --- sandbox.ipynb | 167 ++++++++++--------- src/primaite/game/agent/observations.py | 55 ++++--- src/primaite/game/session.py | 203 ++++++++++++------------ 3 files changed, 228 insertions(+), 197 deletions(-) diff --git a/sandbox.ipynb b/sandbox.ipynb index 0191f0ae..b3b3be0d 100644 --- a/sandbox.ipynb +++ b/sandbox.ipynb @@ -70,66 +70,66 @@ "name": "stderr", "output_type": "stream", "text": [ - "2023-10-08 14:40:34,910: Added node 162d4e70-4aa3-4663-b5cb-1b2127d002c6 to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,914: Added node 0f316f2c-f9b9-4972-9fbe-c69900b12c28 to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,916: Added node 6ab23b82-7182-48ac-9bbe-8315772bc4ff to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,918: Added node 5ebb4df7-b285-44a6-8579-92932206542d to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,920: Added node c99b357f-1874-4220-a428-d8b87d7383bb to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,924: Added node 5f98cbb1-9045-4f6e-91f7-4eaa78437f11 to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,926: Added node 27288000-bbfa-4ef8-a413-3e42cfe2e5d4 to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,930: Added node cc54ae23-c46f-4adc-a211-5c32a0f307ad to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,933: Added node 91d75e63-31c8-4fac-922c-d008a21b14dc to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,935: Added node 6e29546f-ab79-41f6-8b3c-439c03e27ab4 to Network 0449bb3a-abb6-418d-b041-8742cddc9c55\n", - "2023-10-08 14:40:34,937: NIC be:b1:a2:ce:eb:4c/192.168.1.1 connected to Link be:b1:a2:ce:eb:4c/192.168.1.1<-->8f:1d:3f:32:1c:d6\n", - "2023-10-08 14:40:34,938: SwitchPort 8f:1d:3f:32:1c:d6 connected to Link be:b1:a2:ce:eb:4c/192.168.1.1<-->8f:1d:3f:32:1c:d6\n", - "2023-10-08 14:40:34,939: Link be:b1:a2:ce:eb:4c/192.168.1.1<-->8f:1d:3f:32:1c:d6 up\n", - "2023-10-08 14:40:34,939: Link be:b1:a2:ce:eb:4c/192.168.1.1<-->8f:1d:3f:32:1c:d6 up\n", - "2023-10-08 14:40:34,940: Added link b8070c26-6ad0-4d7e-aed8-c1bcdcf9b438 to connect be:b1:a2:ce:eb:4c/192.168.1.1 and 8f:1d:3f:32:1c:d6\n", - "2023-10-08 14:40:34,942: NIC dc:48:6c:bd:8b:b1/192.168.1.1 connected to Link dc:48:6c:bd:8b:b1/192.168.1.1<-->b2:14:a5:82:c0:7a\n", - "2023-10-08 14:40:34,943: SwitchPort b2:14:a5:82:c0:7a connected to Link dc:48:6c:bd:8b:b1/192.168.1.1<-->b2:14:a5:82:c0:7a\n", - "2023-10-08 14:40:34,945: Link dc:48:6c:bd:8b:b1/192.168.1.1<-->b2:14:a5:82:c0:7a up\n", - "2023-10-08 14:40:34,946: Link dc:48:6c:bd:8b:b1/192.168.1.1<-->b2:14:a5:82:c0:7a up\n", - "2023-10-08 14:40:34,946: Added link 102f5506-a939-4af7-8ebb-8e173e18283c to connect dc:48:6c:bd:8b:b1/192.168.1.1 and b2:14:a5:82:c0:7a\n", - "2023-10-08 14:40:34,947: SwitchPort 00:9f:54:21:e2:f2 connected to Link 00:9f:54:21:e2:f2<-->68:69:bf:51:6c:c0/192.168.1.10\n", - "2023-10-08 14:40:34,949: Link 00:9f:54:21:e2:f2<-->68:69:bf:51:6c:c0/192.168.1.10 up\n", - "2023-10-08 14:40:34,950: NIC 68:69:bf:51:6c:c0/192.168.1.10 connected to Link 00:9f:54:21:e2:f2<-->68:69:bf:51:6c:c0/192.168.1.10\n", - "2023-10-08 14:40:34,951: Link 00:9f:54:21:e2:f2<-->68:69:bf:51:6c:c0/192.168.1.10 up\n", - "2023-10-08 14:40:34,952: Added link 6136fd05-7a16-4afd-aebd-cdf6e255689b to connect 00:9f:54:21:e2:f2 and 68:69:bf:51:6c:c0/192.168.1.10\n", - "2023-10-08 14:40:34,952: SwitchPort 48:cc:7b:ac:dd:f9 connected to Link 48:cc:7b:ac:dd:f9<-->64:15:7d:f0:cd:ce/192.168.1.12\n", - "2023-10-08 14:40:34,954: Link 48:cc:7b:ac:dd:f9<-->64:15:7d:f0:cd:ce/192.168.1.12 up\n", - "2023-10-08 14:40:34,954: NIC 64:15:7d:f0:cd:ce/192.168.1.12 connected to Link 48:cc:7b:ac:dd:f9<-->64:15:7d:f0:cd:ce/192.168.1.12\n", - "2023-10-08 14:40:34,955: Link 48:cc:7b:ac:dd:f9<-->64:15:7d:f0:cd:ce/192.168.1.12 up\n", - "2023-10-08 14:40:34,956: Added link 02c6f4e4-3674-4189-a5a1-334fa86921f6 to connect 48:cc:7b:ac:dd:f9 and 64:15:7d:f0:cd:ce/192.168.1.12\n", - "2023-10-08 14:40:34,957: SwitchPort e4:e3:bb:bf:9e:04 connected to Link e4:e3:bb:bf:9e:04<-->81:cd:6e:b8:3d:6c/192.168.1.14\n", - "2023-10-08 14:40:34,958: Link e4:e3:bb:bf:9e:04<-->81:cd:6e:b8:3d:6c/192.168.1.14 up\n", - "2023-10-08 14:40:34,959: NIC 81:cd:6e:b8:3d:6c/192.168.1.14 connected to Link e4:e3:bb:bf:9e:04<-->81:cd:6e:b8:3d:6c/192.168.1.14\n", - "2023-10-08 14:40:34,960: Link e4:e3:bb:bf:9e:04<-->81:cd:6e:b8:3d:6c/192.168.1.14 up\n", - "2023-10-08 14:40:34,961: Added link 57e0f89d-265b-4d27-838b-828ae9800688 to connect e4:e3:bb:bf:9e:04 and 81:cd:6e:b8:3d:6c/192.168.1.14\n", - "2023-10-08 14:40:34,962: SwitchPort 71:5f:fc:32:79:9f connected to Link 71:5f:fc:32:79:9f<-->29:fa:41:0b:f5:1b/192.168.1.16\n", - "2023-10-08 14:40:34,964: Link 71:5f:fc:32:79:9f<-->29:fa:41:0b:f5:1b/192.168.1.16 up\n", - "2023-10-08 14:40:34,965: NIC 29:fa:41:0b:f5:1b/192.168.1.16 connected to Link 71:5f:fc:32:79:9f<-->29:fa:41:0b:f5:1b/192.168.1.16\n", - "2023-10-08 14:40:34,966: Link 71:5f:fc:32:79:9f<-->29:fa:41:0b:f5:1b/192.168.1.16 up\n", - "2023-10-08 14:40:34,967: Added link 1f382171-5e0d-4a76-9500-27dc68c3c7ee to connect 71:5f:fc:32:79:9f and 29:fa:41:0b:f5:1b/192.168.1.16\n", - "2023-10-08 14:40:34,968: SwitchPort 66:5d:d0:ba:c1:91 connected to Link 66:5d:d0:ba:c1:91<-->0d:22:07:53:7a:e1/192.168.1.110\n", - "2023-10-08 14:40:34,969: Link 66:5d:d0:ba:c1:91<-->0d:22:07:53:7a:e1/192.168.1.110 up\n", - "2023-10-08 14:40:34,970: NIC 0d:22:07:53:7a:e1/192.168.1.110 connected to Link 66:5d:d0:ba:c1:91<-->0d:22:07:53:7a:e1/192.168.1.110\n", - "2023-10-08 14:40:34,971: Link 66:5d:d0:ba:c1:91<-->0d:22:07:53:7a:e1/192.168.1.110 up\n", - "2023-10-08 14:40:34,972: Added link d8ea175e-50c8-4597-99bf-ac9001b30c77 to connect 66:5d:d0:ba:c1:91 and 0d:22:07:53:7a:e1/192.168.1.110\n", - "2023-10-08 14:40:34,972: SwitchPort 22:f5:91:5a:bb:b1 connected to Link 22:f5:91:5a:bb:b1<-->82:e5:30:d9:0e:85/192.168.10.21\n", - "2023-10-08 14:40:34,974: Link 22:f5:91:5a:bb:b1<-->82:e5:30:d9:0e:85/192.168.10.21 up\n", - "2023-10-08 14:40:34,975: NIC 82:e5:30:d9:0e:85/192.168.10.21 connected to Link 22:f5:91:5a:bb:b1<-->82:e5:30:d9:0e:85/192.168.10.21\n", - "2023-10-08 14:40:34,976: Link 22:f5:91:5a:bb:b1<-->82:e5:30:d9:0e:85/192.168.10.21 up\n", - "2023-10-08 14:40:34,977: Added link 40ba49b9-e334-45ce-93da-a1459b80e9a2 to connect 22:f5:91:5a:bb:b1 and 82:e5:30:d9:0e:85/192.168.10.21\n", - "2023-10-08 14:40:34,978: SwitchPort 70:77:d0:12:cd:a0 connected to Link 70:77:d0:12:cd:a0<-->ef:20:20:d8:9a:11/192.168.10.22\n", - "2023-10-08 14:40:34,980: Link 70:77:d0:12:cd:a0<-->ef:20:20:d8:9a:11/192.168.10.22 up\n", - "2023-10-08 14:40:34,981: NIC ef:20:20:d8:9a:11/192.168.10.22 connected to Link 70:77:d0:12:cd:a0<-->ef:20:20:d8:9a:11/192.168.10.22\n", - "2023-10-08 14:40:34,982: Link 70:77:d0:12:cd:a0<-->ef:20:20:d8:9a:11/192.168.10.22 up\n", - "2023-10-08 14:40:34,982: Added link c36027fe-052f-4eb6-b6c6-10bf817c7ac9 to connect 70:77:d0:12:cd:a0 and ef:20:20:d8:9a:11/192.168.10.22\n", - "2023-10-08 14:40:34,983: SwitchPort 62:da:0d:de:eb:27 connected to Link 62:da:0d:de:eb:27<-->b8:2b:a3:f0:18:b9/192.168.10.110\n", - "2023-10-08 14:40:34,985: Link 62:da:0d:de:eb:27<-->b8:2b:a3:f0:18:b9/192.168.10.110 up\n", - "2023-10-08 14:40:34,986: NIC b8:2b:a3:f0:18:b9/192.168.10.110 connected to Link 62:da:0d:de:eb:27<-->b8:2b:a3:f0:18:b9/192.168.10.110\n", - "2023-10-08 14:40:34,987: Link 62:da:0d:de:eb:27<-->b8:2b:a3:f0:18:b9/192.168.10.110 up\n", - "2023-10-08 14:40:34,988: Added link 9469edcd-6b36-4333-b948-3eeccf24abcb to connect 62:da:0d:de:eb:27 and b8:2b:a3:f0:18:b9/192.168.10.110\n" + "2023-10-08 17:56:35,831: Added node af2f9c15-ecb4-4b65-b48f-63f12acddb88 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,836: Added node 47158854-0917-4037-a6a2-33dde56a120f to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,840: Added node cba8ce63-8064-4f80-bcfe-95ca65221dfa to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,846: Added node e01e7c2b-02ac-4e2d-b7bb-8bc3b6ea6509 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,857: Added node bd5d85ba-5980-45c7-8b28-020a2cfeba0f to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,863: Added node 39e0e37c-4d72-4c76-93cb-4f9c29651ef4 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,878: Added node 7d1063f9-b5e5-4753-966e-1b630325b266 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,881: Added node d85b6abb-0f9e-4853-af26-c9b410e1cb94 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,884: Added node 63b18888-98aa-4182-a014-02999d095bd0 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,888: Added node f514cf8a-a3f1-46d6-be00-994364241ef4 to Network cbd56fbb-104f-4823-9ee6-f4a968343b31\n", + "2023-10-08 17:56:35,890: NIC 27:a9:09:ed:30:5a/192.168.1.1 connected to Link 27:a9:09:ed:30:5a/192.168.1.1<-->cb:6f:24:8c:7a:20\n", + "2023-10-08 17:56:35,891: SwitchPort cb:6f:24:8c:7a:20 connected to Link 27:a9:09:ed:30:5a/192.168.1.1<-->cb:6f:24:8c:7a:20\n", + "2023-10-08 17:56:35,893: Link 27:a9:09:ed:30:5a/192.168.1.1<-->cb:6f:24:8c:7a:20 up\n", + "2023-10-08 17:56:35,896: Link 27:a9:09:ed:30:5a/192.168.1.1<-->cb:6f:24:8c:7a:20 up\n", + "2023-10-08 17:56:35,897: Added link 41d994cb-2976-4aa2-b306-649cef4deb80 to connect 27:a9:09:ed:30:5a/192.168.1.1 and cb:6f:24:8c:7a:20\n", + "2023-10-08 17:56:35,899: NIC 5c:fa:b1:a4:69:ec/192.168.1.1 connected to Link 5c:fa:b1:a4:69:ec/192.168.1.1<-->68:54:d7:42:04:87\n", + "2023-10-08 17:56:35,900: SwitchPort 68:54:d7:42:04:87 connected to Link 5c:fa:b1:a4:69:ec/192.168.1.1<-->68:54:d7:42:04:87\n", + "2023-10-08 17:56:35,901: Link 5c:fa:b1:a4:69:ec/192.168.1.1<-->68:54:d7:42:04:87 up\n", + "2023-10-08 17:56:35,903: Link 5c:fa:b1:a4:69:ec/192.168.1.1<-->68:54:d7:42:04:87 up\n", + "2023-10-08 17:56:35,904: Added link d582a248-e968-40eb-9d1b-67143d729e0c to connect 5c:fa:b1:a4:69:ec/192.168.1.1 and 68:54:d7:42:04:87\n", + "2023-10-08 17:56:35,905: SwitchPort c6:bd:77:78:4b:5d connected to Link c6:bd:77:78:4b:5d<-->1c:d9:92:e8:d6:3b/192.168.1.10\n", + "2023-10-08 17:56:35,908: Link c6:bd:77:78:4b:5d<-->1c:d9:92:e8:d6:3b/192.168.1.10 up\n", + "2023-10-08 17:56:35,909: NIC 1c:d9:92:e8:d6:3b/192.168.1.10 connected to Link c6:bd:77:78:4b:5d<-->1c:d9:92:e8:d6:3b/192.168.1.10\n", + "2023-10-08 17:56:35,911: Link c6:bd:77:78:4b:5d<-->1c:d9:92:e8:d6:3b/192.168.1.10 up\n", + "2023-10-08 17:56:35,912: Added link 13315780-1fcc-4c85-b94b-ef8f14c88a8a to connect c6:bd:77:78:4b:5d and 1c:d9:92:e8:d6:3b/192.168.1.10\n", + "2023-10-08 17:56:35,913: SwitchPort cd:46:af:c4:33:65 connected to Link cd:46:af:c4:33:65<-->aa:cf:2f:71:13:5b/192.168.1.12\n", + "2023-10-08 17:56:35,916: Link cd:46:af:c4:33:65<-->aa:cf:2f:71:13:5b/192.168.1.12 up\n", + "2023-10-08 17:56:35,917: NIC aa:cf:2f:71:13:5b/192.168.1.12 connected to Link cd:46:af:c4:33:65<-->aa:cf:2f:71:13:5b/192.168.1.12\n", + "2023-10-08 17:56:35,918: Link cd:46:af:c4:33:65<-->aa:cf:2f:71:13:5b/192.168.1.12 up\n", + "2023-10-08 17:56:35,919: Added link 6c2a80f7-f36d-4df6-ac84-d354e5d517dd to connect cd:46:af:c4:33:65 and aa:cf:2f:71:13:5b/192.168.1.12\n", + "2023-10-08 17:56:35,920: SwitchPort 2c:d2:67:ef:68:a8 connected to Link 2c:d2:67:ef:68:a8<-->e1:09:5e:98:ee:a2/192.168.1.14\n", + "2023-10-08 17:56:35,923: Link 2c:d2:67:ef:68:a8<-->e1:09:5e:98:ee:a2/192.168.1.14 up\n", + "2023-10-08 17:56:35,924: NIC e1:09:5e:98:ee:a2/192.168.1.14 connected to Link 2c:d2:67:ef:68:a8<-->e1:09:5e:98:ee:a2/192.168.1.14\n", + "2023-10-08 17:56:35,925: Link 2c:d2:67:ef:68:a8<-->e1:09:5e:98:ee:a2/192.168.1.14 up\n", + "2023-10-08 17:56:35,926: Added link 1cfdd4f2-22be-4e69-8f1f-daef8e18f543 to connect 2c:d2:67:ef:68:a8 and e1:09:5e:98:ee:a2/192.168.1.14\n", + "2023-10-08 17:56:35,927: SwitchPort 9b:13:8c:a0:8c:82 connected to Link 9b:13:8c:a0:8c:82<-->cc:c2:84:03:1c:42/192.168.1.16\n", + "2023-10-08 17:56:35,929: Link 9b:13:8c:a0:8c:82<-->cc:c2:84:03:1c:42/192.168.1.16 up\n", + "2023-10-08 17:56:35,930: NIC cc:c2:84:03:1c:42/192.168.1.16 connected to Link 9b:13:8c:a0:8c:82<-->cc:c2:84:03:1c:42/192.168.1.16\n", + "2023-10-08 17:56:35,932: Link 9b:13:8c:a0:8c:82<-->cc:c2:84:03:1c:42/192.168.1.16 up\n", + "2023-10-08 17:56:35,933: Added link 031111e1-3b05-49ce-bd1f-2cdf77b210f4 to connect 9b:13:8c:a0:8c:82 and cc:c2:84:03:1c:42/192.168.1.16\n", + "2023-10-08 17:56:35,934: SwitchPort a1:70:9e:43:1c:07 connected to Link a1:70:9e:43:1c:07<-->e7:58:3c:ed:f7:37/192.168.1.110\n", + "2023-10-08 17:56:35,937: Link a1:70:9e:43:1c:07<-->e7:58:3c:ed:f7:37/192.168.1.110 up\n", + "2023-10-08 17:56:35,938: NIC e7:58:3c:ed:f7:37/192.168.1.110 connected to Link a1:70:9e:43:1c:07<-->e7:58:3c:ed:f7:37/192.168.1.110\n", + "2023-10-08 17:56:35,939: Link a1:70:9e:43:1c:07<-->e7:58:3c:ed:f7:37/192.168.1.110 up\n", + "2023-10-08 17:56:35,941: Added link f15884e7-0df6-4fa5-bb72-406cb2bdff45 to connect a1:70:9e:43:1c:07 and e7:58:3c:ed:f7:37/192.168.1.110\n", + "2023-10-08 17:56:35,943: SwitchPort a5:da:f2:03:21:e3 connected to Link a5:da:f2:03:21:e3<-->cf:63:9f:62:fe:df/192.168.10.21\n", + "2023-10-08 17:56:35,946: Link a5:da:f2:03:21:e3<-->cf:63:9f:62:fe:df/192.168.10.21 up\n", + "2023-10-08 17:56:35,947: NIC cf:63:9f:62:fe:df/192.168.10.21 connected to Link a5:da:f2:03:21:e3<-->cf:63:9f:62:fe:df/192.168.10.21\n", + "2023-10-08 17:56:35,948: Link a5:da:f2:03:21:e3<-->cf:63:9f:62:fe:df/192.168.10.21 up\n", + "2023-10-08 17:56:35,950: Added link cc6767fa-25de-4daa-bd47-37b49b15a881 to connect a5:da:f2:03:21:e3 and cf:63:9f:62:fe:df/192.168.10.21\n", + "2023-10-08 17:56:35,951: SwitchPort eb:5b:86:14:bd:d1 connected to Link eb:5b:86:14:bd:d1<-->3d:73:a6:62:97:3a/192.168.10.22\n", + "2023-10-08 17:56:35,953: Link eb:5b:86:14:bd:d1<-->3d:73:a6:62:97:3a/192.168.10.22 up\n", + "2023-10-08 17:56:35,954: NIC 3d:73:a6:62:97:3a/192.168.10.22 connected to Link eb:5b:86:14:bd:d1<-->3d:73:a6:62:97:3a/192.168.10.22\n", + "2023-10-08 17:56:35,955: Link eb:5b:86:14:bd:d1<-->3d:73:a6:62:97:3a/192.168.10.22 up\n", + "2023-10-08 17:56:35,958: Added link b29563ed-0636-4188-ba28-52b74a04da27 to connect eb:5b:86:14:bd:d1 and 3d:73:a6:62:97:3a/192.168.10.22\n", + "2023-10-08 17:56:35,959: SwitchPort e3:3a:0b:03:0b:8c connected to Link e3:3a:0b:03:0b:8c<-->d4:ff:37:8d:e4:3d/192.168.10.110\n", + "2023-10-08 17:56:35,961: Link e3:3a:0b:03:0b:8c<-->d4:ff:37:8d:e4:3d/192.168.10.110 up\n", + "2023-10-08 17:56:35,963: NIC d4:ff:37:8d:e4:3d/192.168.10.110 connected to Link e3:3a:0b:03:0b:8c<-->d4:ff:37:8d:e4:3d/192.168.10.110\n", + "2023-10-08 17:56:35,963: Link e3:3a:0b:03:0b:8c<-->d4:ff:37:8d:e4:3d/192.168.10.110 up\n", + "2023-10-08 17:56:35,964: Added link f6fe7757-a8c1-4cdc-a2b2-49d247117903 to connect e3:3a:0b:03:0b:8c and d4:ff:37:8d:e4:3d/192.168.10.110\n" ] }, { @@ -153,9 +153,9 @@ { "data": { "text/plain": [ - "[,\n", - " ,\n", - " ]" + "[,\n", + " ,\n", + " ]" ] }, "execution_count": 7, @@ -185,25 +185,44 @@ "name": "stderr", "output_type": "stream", "text": [ - "2023-10-08 14:40:35,046: Stepping primaite session. Step counter: 0\n", - "2023-10-08 14:40:35,047: Sending simulation state to agent client_1_green_user\n", - "2023-10-08 14:40:35,049: Getting agent action\n", - "2023-10-08 14:40:35,050: Formatting agent action DONOTHING\n", - "2023-10-08 14:40:35,051: Sending request to simulation: ['do_nothing']\n", - "2023-10-08 14:40:35,052: Sending simulation state to agent client_1_data_manipulation_red_bot\n" + "2023-10-08 17:56:36,041: Stepping primaite session. Step counter: 0\n", + "2023-10-08 17:56:36,043: Sending simulation state to agent client_1_green_user\n", + "2023-10-08 17:56:36,045: Getting agent action\n", + "2023-10-08 17:56:36,047: Formatting agent action DONOTHING\n", + "2023-10-08 17:56:36,048: Sending request to simulation: ['do_nothing']\n", + "2023-10-08 17:56:36,050: Sending simulation state to agent client_1_data_manipulation_red_bot\n" ] }, { - "ename": "AttributeError", - "evalue": "'NoneType' object has no attribute 'observe'", + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n", + "[]\n" + ] + }, + { + "ename": "TypeError", + "evalue": "unhashable type: 'DataManipulationBot'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m/home/cade/repos/PrimAITE/sandbox.ipynb Cell 10\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m sess\u001b[39m.\u001b[39;49mstep()\n", - "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/session.py:75\u001b[0m, in \u001b[0;36mPrimaiteSession.step\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 72\u001b[0m sim_state \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msimulation\u001b[39m.\u001b[39mdescribe_state()\n\u001b[1;32m 74\u001b[0m \u001b[39m# 6. each agent takes most recent state and converts it to CAOS observation\u001b[39;00m\n\u001b[0;32m---> 75\u001b[0m agent_obs \u001b[39m=\u001b[39m agent\u001b[39m.\u001b[39;49mconvert_state_to_obs(sim_state)\n\u001b[1;32m 77\u001b[0m \u001b[39m# 7. meanwhile each agent also takes state and calculates reward\u001b[39;00m\n\u001b[1;32m 78\u001b[0m agent_reward \u001b[39m=\u001b[39m agent\u001b[39m.\u001b[39mcalculate_reward_from_state(sim_state)\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/session.py:80\u001b[0m, in \u001b[0;36mPrimaiteSession.step\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 77\u001b[0m sim_state \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39msimulation\u001b[39m.\u001b[39mdescribe_state()\n\u001b[1;32m 79\u001b[0m \u001b[39m# 6. each agent takes most recent state and converts it to CAOS observation\u001b[39;00m\n\u001b[0;32m---> 80\u001b[0m agent_obs \u001b[39m=\u001b[39m agent\u001b[39m.\u001b[39;49mconvert_state_to_obs(sim_state)\n\u001b[1;32m 82\u001b[0m \u001b[39m# 7. meanwhile each agent also takes state and calculates reward\u001b[39;00m\n\u001b[1;32m 83\u001b[0m agent_reward \u001b[39m=\u001b[39m agent\u001b[39m.\u001b[39mcalculate_reward_from_state(sim_state)\n", "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/interface.py:40\u001b[0m, in \u001b[0;36mAbstractAgent.convert_state_to_obs\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mconvert_state_to_obs\u001b[39m(\u001b[39mself\u001b[39m, state: Dict) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m ObsType:\n\u001b[1;32m 36\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 37\u001b[0m \u001b[39m state : dict state directly from simulation.describe_state\u001b[39;00m\n\u001b[1;32m 38\u001b[0m \u001b[39m output : dict state according to CAOS.\u001b[39;00m\n\u001b[1;32m 39\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 40\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mobservation_space\u001b[39m.\u001b[39;49mobserve(state)\n", - "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'observe'" + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:608\u001b[0m, in \u001b[0;36mObservationSpace.observe\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 607\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mobserve\u001b[39m(\u001b[39mself\u001b[39m, state) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Dict:\n\u001b[0;32m--> 608\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mobs\u001b[39m.\u001b[39;49mobserve(state)\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:571\u001b[0m, in \u001b[0;36mUC2RedObservation.observe\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 568\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdefault_observation\n\u001b[1;32m 570\u001b[0m obs \u001b[39m=\u001b[39m {}\n\u001b[0;32m--> 571\u001b[0m obs[\u001b[39m'\u001b[39m\u001b[39mNODES\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m {i\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m: node\u001b[39m.\u001b[39mobserve(state) \u001b[39mfor\u001b[39;00m i, node \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mnodes)}\n\u001b[1;32m 572\u001b[0m \u001b[39mreturn\u001b[39;00m obs\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:571\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 568\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdefault_observation\n\u001b[1;32m 570\u001b[0m obs \u001b[39m=\u001b[39m {}\n\u001b[0;32m--> 571\u001b[0m obs[\u001b[39m'\u001b[39m\u001b[39mNODES\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m {i\u001b[39m+\u001b[39m\u001b[39m1\u001b[39m: node\u001b[39m.\u001b[39;49mobserve(state) \u001b[39mfor\u001b[39;00m i, node \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mnodes)}\n\u001b[1;32m 572\u001b[0m \u001b[39mreturn\u001b[39;00m obs\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:328\u001b[0m, in \u001b[0;36mNodeObservation.observe\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 326\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mservices)\n\u001b[1;32m 327\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfolders)\n\u001b[0;32m--> 328\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39mSERVICES\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m {i \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m: service\u001b[39m.\u001b[39mobserve(state) \u001b[39mfor\u001b[39;00m i, service \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mservices)}\n\u001b[1;32m 329\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39mFOLDERS\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m {i \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m: folder\u001b[39m.\u001b[39mobserve(state) \u001b[39mfor\u001b[39;00m i, folder \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfolders)}\n\u001b[1;32m 330\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39moperating_status\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m node_state[\u001b[39m\"\u001b[39m\u001b[39moperating_state\u001b[39m\u001b[39m\"\u001b[39m]\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:328\u001b[0m, in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 326\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mservices)\n\u001b[1;32m 327\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfolders)\n\u001b[0;32m--> 328\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39mSERVICES\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m {i \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m: service\u001b[39m.\u001b[39;49mobserve(state) \u001b[39mfor\u001b[39;00m i, service \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mservices)}\n\u001b[1;32m 329\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39mFOLDERS\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m {i \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m: folder\u001b[39m.\u001b[39mobserve(state) \u001b[39mfor\u001b[39;00m i, folder \u001b[39min\u001b[39;00m \u001b[39menumerate\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfolders)}\n\u001b[1;32m 330\u001b[0m obs[\u001b[39m\"\u001b[39m\u001b[39moperating_status\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m node_state[\u001b[39m\"\u001b[39m\u001b[39moperating_state\u001b[39m\u001b[39m\"\u001b[39m]\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:124\u001b[0m, in \u001b[0;36mServiceObservation.observe\u001b[0;34m(self, state)\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mwhere \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 122\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdefault_observation\n\u001b[0;32m--> 124\u001b[0m service_state \u001b[39m=\u001b[39m access_from_nested_dict(state, \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mwhere)\n\u001b[1;32m 125\u001b[0m \u001b[39mif\u001b[39;00m service_state \u001b[39mis\u001b[39;00m NOT_PRESENT_IN_STATE:\n\u001b[1;32m 126\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdefault_observation\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:37\u001b[0m, in \u001b[0;36maccess_from_nested_dict\u001b[0;34m(dictionary, keys)\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[39mif\u001b[39;00m k \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m dictionary:\n\u001b[1;32m 36\u001b[0m \u001b[39mreturn\u001b[39;00m NOT_PRESENT_IN_STATE\n\u001b[0;32m---> 37\u001b[0m \u001b[39mreturn\u001b[39;00m access_from_nested_dict(dictionary[k], keys)\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:37\u001b[0m, in \u001b[0;36maccess_from_nested_dict\u001b[0;34m(dictionary, keys)\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[39mif\u001b[39;00m k \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m dictionary:\n\u001b[1;32m 36\u001b[0m \u001b[39mreturn\u001b[39;00m NOT_PRESENT_IN_STATE\n\u001b[0;32m---> 37\u001b[0m \u001b[39mreturn\u001b[39;00m access_from_nested_dict(dictionary[k], keys)\n", + " \u001b[0;31m[... skipping similar frames: access_from_nested_dict at line 37 (1 times)]\u001b[0m\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:37\u001b[0m, in \u001b[0;36maccess_from_nested_dict\u001b[0;34m(dictionary, keys)\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[39mif\u001b[39;00m k \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m dictionary:\n\u001b[1;32m 36\u001b[0m \u001b[39mreturn\u001b[39;00m NOT_PRESENT_IN_STATE\n\u001b[0;32m---> 37\u001b[0m \u001b[39mreturn\u001b[39;00m access_from_nested_dict(dictionary[k], keys)\n", + "File \u001b[0;32m~/repos/PrimAITE/src/primaite/game/agent/observations.py:35\u001b[0m, in \u001b[0;36maccess_from_nested_dict\u001b[0;34m(dictionary, keys)\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[39mreturn\u001b[39;00m dictionary\n\u001b[1;32m 34\u001b[0m k \u001b[39m=\u001b[39m keys\u001b[39m.\u001b[39mpop(\u001b[39m0\u001b[39m)\n\u001b[0;32m---> 35\u001b[0m \u001b[39mif\u001b[39;00m k \u001b[39mnot\u001b[39;49;00m \u001b[39min\u001b[39;49;00m dictionary:\n\u001b[1;32m 36\u001b[0m \u001b[39mreturn\u001b[39;00m NOT_PRESENT_IN_STATE\n\u001b[1;32m 37\u001b[0m \u001b[39mreturn\u001b[39;00m access_from_nested_dict(dictionary[k], keys)\n", + "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'DataManipulationBot'" ] } ], diff --git a/src/primaite/game/agent/observations.py b/src/primaite/game/agent/observations.py index 21f623fd..c5b931ee 100644 --- a/src/primaite/game/agent/observations.py +++ b/src/primaite/game/agent/observations.py @@ -3,7 +3,6 @@ from typing import Any, Dict, Hashable, List, Optional, TYPE_CHECKING from gym import spaces from pydantic import BaseModel -from primaite.game.session import PrimaiteSession from primaite.simulator.sim_container import Simulation if TYPE_CHECKING: @@ -56,8 +55,8 @@ class AbstractObservation(ABC): """Subclasses must define the shape that they expect""" ... - @abstractmethod @classmethod + @abstractmethod def from_config(cls, config:Dict, session:"PrimaiteSession"): """Create this observation space component form a serialised format. @@ -132,7 +131,7 @@ class ServiceObservation(AbstractObservation): return spaces.Dict({"operating_status": spaces.Discrete(7), "health_status": spaces.Discrete(6)}) @classmethod - def from_config(cls, config: Dict, session: PrimaiteSession, parent_where:Optional[List[str]]=None): + def from_config(cls, config: Dict, session: "PrimaiteSession", parent_where:Optional[List[str]]=None): return cls(where=parent_where+["services",session.ref_map_services[config['service_ref']]]) @@ -235,7 +234,7 @@ class FolderObservation(AbstractObservation): ) @classmethod - def from_config(cls, config: Dict, session: PrimaiteSession, parent_where:Optional[List[str]]): + def from_config(cls, config: Dict, session: "PrimaiteSession", parent_where:Optional[List[str]]): where = parent_where + ["folders", config['folder_name']] file_configs = config["files"] @@ -324,7 +323,6 @@ class NodeObservation(AbstractObservation): return self.default_observation obs = {} - obs["SERVICES"] = {i + 1: service.observe(state) for i, service in enumerate(self.services)} obs["FOLDERS"] = {i + 1: folder.observe(state) for i, folder in enumerate(self.folders)} obs["operating_status"] = node_state["operating_state"] @@ -349,7 +347,7 @@ class NodeObservation(AbstractObservation): return spaces.Dict(space_shape) @classmethod - def from_config(cls, config: Dict, session: "PrimaiteSession", parent_where:Optional[List[str]]= None): + def from_config(cls, config: Dict, session: "PrimaiteSession", parent_where:Optional[List[str]]= None) -> "NodeObservation": node_uuid = session.ref_map_nodes[config['node_ref']] if parent_where is None: where = ["network", "nodes", node_uuid] @@ -361,12 +359,10 @@ class NodeObservation(AbstractObservation): folder_configs = config.get('folders', {}) folders = [FolderObservation.from_config(config=c,session=session, parent_where=where) for c in folder_configs] nic_uuids = session.simulation.network.nodes[node_uuid].nics.keys() - nic_configs = [{'nic_uuid':n for n in nic_uuids }] + nic_configs = [{'nic_uuid':n for n in nic_uuids }] if nic_uuids else [] nics = [NicObservation.from_config(config=c, session=session, parent_where=where) for c in nic_configs] logon_status = config.get('logon_status',False) - cls(where=where, services=services, folders=folders, nics=nics, logon_status=logon_status) - return super().from_config(config, session) - + return cls(where=where, services=services, folders=folders, nics=nics, logon_status=logon_status) class AclObservation(AbstractObservation): @@ -486,7 +482,7 @@ class NullObservation(AbstractObservation): return spaces.Dict({}) @classmethod - def from_config(cls, cfg:Dict) -> "NullObservation": + def from_config(cls, config:Dict, session:Optional["PrimaiteSession"]=None) -> "NullObservation": return cls() class ICSObservation(NullObservation): pass @@ -539,15 +535,15 @@ class UC2BlueObservation(AbstractObservation): }) @classmethod - def from_config(cls, config:Dict, sess:"PrimaiteSession"): + def from_config(cls, config:Dict, session:"PrimaiteSession"): node_configs = config["nodes"] - nodes = [NodeObservation.from_config(n) for n in node_configs] + nodes = [NodeObservation.from_config(config=n, session=session) for n in node_configs] link_configs = config["links"] - links = [LinkObservation.from_config(l) for l in link_configs] + links = [LinkObservation.from_config(config=l, session=session) for l in link_configs] acl_config = config["acl"] - acl = AclObservation.from_config(acl_config) + acl = AclObservation.from_config(config=acl_config, session=session) ics_config = config["ics"] ics = ICSObservation.from_config(ics_config) @@ -561,19 +557,30 @@ class UC2RedObservation(AbstractObservation): self.where:Optional[List[str]] = where self.nodes: List[NodeObservation] = nodes - self.default_observation=...#TODO + self.default_observation : Dict = { + "NODES": {i+1: n.default_observation for i,n in enumerate(self.nodes)}, + } - def observe(self, state: Dict) -> Any: - return super().observe(state) + def observe(self, state: Dict) -> Dict: + if self.where is None: + return self.default_observation + + obs = {} + obs['NODES'] = {i+1: node.observe(state) for i, node in enumerate(self.nodes)} + return obs @property def space(self) -> spaces.Space: - ... #TODO + return spaces.Dict({ + "NODES": spaces.Dict({i+1: node.space for i, node in enumerate(self.nodes)}), + }) @classmethod - def from_config(cls, config: Dict, sim:Simulation): + def from_config(cls, config: Dict, session: "PrimaiteSession"): + node_configs = config["nodes"] + nodes = [NodeObservation.from_config(config=cfg, session=session) for cfg in node_configs] + return cls(nodes=nodes, where=["network"]) - ... #TODO class UC2GreenObservation(NullObservation): pass @@ -605,10 +612,10 @@ class ObservationSpace: @classmethod def from_config(cls, config:Dict, session:"PrimaiteSession") -> "ObservationSpace": if config['type'] == "UC2BlueObservation": - return cls(UC2BlueObservation(config['options'])) + return cls(UC2BlueObservation.from_config(config.get('options',{}), session=session)) elif config['type'] == "UC2RedObservation": - return cls(UC2RedObservation(config['options'])) + return cls(UC2RedObservation.from_config(config.get('options',{}), session=session)) elif config['type'] == "UC2GreenObservation": - return cls(UC2GreenObservation(config["options"])) + return cls(UC2GreenObservation.from_config(config.get("options",{}), session=session)) else: raise ValueError("Observation space type invalid") diff --git a/src/primaite/game/session.py b/src/primaite/game/session.py index f0ae05c6..4bcf26e4 100644 --- a/src/primaite/game/session.py +++ b/src/primaite/game/session.py @@ -21,6 +21,7 @@ from primaite.game.agent.observations import ( NicObservation, NodeObservation, NullObservation, + ObservationSpace, ServiceObservation, UC2BlueObservation, UC2GreenObservation, @@ -177,7 +178,7 @@ class PrimaiteSession: if service_type in service_types_mapping: new_node.software_manager.install(service_types_mapping[service_type]) new_service = new_node.software_manager.software[service_type] - ref_map_services[service_ref] = new_service + sess.ref_map_services[service_ref] = new_service else: print(f"service type not found {service_type}") # service-dependent options @@ -198,12 +199,12 @@ class PrimaiteSession: net.add_node(new_node) new_node.power_on() - ref_map_nodes[node_ref] = new_node.uuid + sess.ref_map_nodes[node_ref] = new_node.uuid # 2. create links between nodes for link_cfg in links_cfg: - node_a = net.nodes[ref_map_nodes[link_cfg["endpoint_a_ref"]]] - node_b = net.nodes[ref_map_nodes[link_cfg["endpoint_b_ref"]]] + node_a = net.nodes[sess.ref_map_nodes[link_cfg["endpoint_a_ref"]]] + node_b = net.nodes[sess.ref_map_nodes[link_cfg["endpoint_b_ref"]]] if isinstance(node_a, Switch): endpoint_a = node_a.switch_ports[link_cfg["endpoint_a_port"]] else: @@ -213,7 +214,7 @@ class PrimaiteSession: else: endpoint_b = node_b.ethernet_port[link_cfg["endpoint_b_port"]] new_link = net.connect(endpoint_a=endpoint_a, endpoint_b=endpoint_b) - ref_map_links[link_cfg["ref"]] = new_link.uuid + sess.ref_map_links[link_cfg["ref"]] = new_link.uuid # 3. create agents game_cfg = cfg["game_config"] @@ -229,108 +230,112 @@ class PrimaiteSession: reward_function_cfg = agent_cfg["reward_function"] # CREATE OBSERVATION SPACE - if observation_space_cfg is None: - obs_space = NullObservation() - elif observation_space_cfg["type"] == "UC2BlueObservation": - node_obs_list = [] - link_obs_list = [] + obs_space=ObservationSpace.from_config(observation_space_cfg, sess) - # node ip to index maps ip addresses to node id, as there are potentially multiple nics on a node, there are multiple ip addresses - node_ip_to_index = {} - for node_idx, node_cfg in enumerate(nodes_cfg): - n_ref = node_cfg["ref"] - n_obj = net.nodes[ref_map_nodes[n_ref]] - for nic_uuid, nic_obj in n_obj.nics.items(): - node_ip_to_index[nic_obj.ip_address] = node_idx + 2 + """ + # if observation_space_cfg is None: + # obs_space = NullObservation() + # elif observation_space_cfg["type"] == "UC2BlueObservation": + # node_obs_list = [] + # link_obs_list = [] - for node_obs_cfg in observation_space_cfg["options"]["nodes"]: - node_ref = node_obs_cfg["node_ref"] - folder_obs_list = [] - service_obs_list = [] - if "services" in node_obs_cfg: - for service_obs_cfg in node_obs_cfg["services"]: - service_obs_list.append( - ServiceObservation( - where=[ - "network", - "nodes", - ref_map_nodes[node_ref], - "services", - ref_map_services[service_obs_cfg["service_ref"]], - ] - ) - ) - if "folders" in node_obs_cfg: - for folder_obs_cfg in node_obs_cfg["folders"]: - file_obs_list = [] - if "files" in folder_obs_cfg: - for file_obs_cfg in folder_obs_cfg["files"]: - file_obs_list.append( - FileObservation( - where=[ - "network", - "nodes", - ref_map_nodes[node_ref], - "folders", - folder_obs_cfg["folder_name"], - "files", - file_obs_cfg["file_name"], - ] - ) - ) - folder_obs_list.append( - FolderObservation( - where=[ - "network", - "nodes", - ref_map_nodes[node_ref], - "folders", - folder_obs_cfg["folder_name"], - ], - files=file_obs_list, - ) - ) - nic_obs_list = [] - for nic_uuid in net.nodes[ref_map_nodes[node_obs_cfg["node_ref"]]].nics.keys(): - nic_obs_list.append( - NicObservation(where=["network", "nodes", ref_map_nodes[node_ref], "NICs", nic_uuid]) - ) - node_obs_list.append( - NodeObservation( - where=["network", "nodes", ref_map_nodes[node_ref]], - services=service_obs_list, - folders=folder_obs_list, - nics=nic_obs_list, - logon_status=False, - ) - ) - for link_obs_cfg in observation_space_cfg["options"]["links"]: - link_ref = link_obs_cfg["link_ref"] - link_obs_list.append(LinkObservation(where=["network", "links", ref_map_links[link_ref]])) + # # node ip to index maps ip addresses to node id, as there are potentially multiple nics on a node, there are multiple ip addresses + # node_ip_to_index = {} + # for node_idx, node_cfg in enumerate(nodes_cfg): + # n_ref = node_cfg["ref"] + # n_obj = net.nodes[ref_map_nodes[n_ref]] + # for nic_uuid, nic_obj in n_obj.nics.items(): + # node_ip_to_index[nic_obj.ip_address] = node_idx + 2 - acl_obs = AclObservation( - node_ip_to_id=node_ip_to_index, - ports=game_cfg["ports"], - protocols=game_cfg["ports"], - where=["network", "nodes", observation_space_cfg["options"]["acl"]["router_node_ref"]], - ) - obs_space = UC2BlueObservation( - nodes=node_obs_list, links=link_obs_list, acl=acl_obs, ics=ICSObservation() - ) - elif observation_space_cfg["type"] == "UC2RedObservation": - obs_space = UC2RedObservation.from_config(observation_space_cfg["options"], sim=sim) - elif observation_space_cfg["type"] == "UC2GreenObservation": - obs_space = UC2GreenObservation.from_config(observation_space_cfg.get('options',{})) - else: - print("observation space config not specified correctly.") - obs_space = NullObservation() + # for node_obs_cfg in observation_space_cfg["options"]["nodes"]: + # node_ref = node_obs_cfg["node_ref"] + # folder_obs_list = [] + # service_obs_list = [] + # if "services" in node_obs_cfg: + # for service_obs_cfg in node_obs_cfg["services"]: + # service_obs_list.append( + # ServiceObservation( + # where=[ + # "network", + # "nodes", + # ref_map_nodes[node_ref], + # "services", + # ref_map_services[service_obs_cfg["service_ref"]], + # ] + # ) + # ) + # if "folders" in node_obs_cfg: + # for folder_obs_cfg in node_obs_cfg["folders"]: + # file_obs_list = [] + # if "files" in folder_obs_cfg: + # for file_obs_cfg in folder_obs_cfg["files"]: + # file_obs_list.append( + # FileObservation( + # where=[ + # "network", + # "nodes", + # ref_map_nodes[node_ref], + # "folders", + # folder_obs_cfg["folder_name"], + # "files", + # file_obs_cfg["file_name"], + # ] + # ) + # ) + # folder_obs_list.append( + # FolderObservation( + # where=[ + # "network", + # "nodes", + # ref_map_nodes[node_ref], + # "folders", + # folder_obs_cfg["folder_name"], + # ], + # files=file_obs_list, + # ) + # ) + # nic_obs_list = [] + # for nic_uuid in net.nodes[ref_map_nodes[node_obs_cfg["node_ref"]]].nics.keys(): + # nic_obs_list.append( + # NicObservation(where=["network", "nodes", ref_map_nodes[node_ref], "NICs", nic_uuid]) + # ) + # node_obs_list.append( + # NodeObservation( + # where=["network", "nodes", ref_map_nodes[node_ref]], + # services=service_obs_list, + # folders=folder_obs_list, + # nics=nic_obs_list, + # logon_status=False, + # ) + # ) + # for link_obs_cfg in observation_space_cfg["options"]["links"]: + # link_ref = link_obs_cfg["link_ref"] + # link_obs_list.append(LinkObservation(where=["network", "links", ref_map_links[link_ref]])) + + # acl_obs = AclObservation( + # node_ip_to_id=node_ip_to_index, + # ports=game_cfg["ports"], + # protocols=game_cfg["ports"], + # where=["network", "nodes", observation_space_cfg["options"]["acl"]["router_node_ref"]], + # ) + # obs_space = UC2BlueObservation( + # nodes=node_obs_list, links=link_obs_list, acl=acl_obs, ics=ICSObservation() + # ) + # elif observation_space_cfg["type"] == "UC2RedObservation": + # obs_space = UC2RedObservation.from_config(observation_space_cfg["options"], sim=sim) + # elif observation_space_cfg["type"] == "UC2GreenObservation": + # obs_space = UC2GreenObservation.from_config(observation_space_cfg.get('options',{})) + # else: + # print("observation space config not specified correctly.") + # obs_space = NullObservation() + """ # CREATE ACTION SPACE action_space_cfg['options']['node_uuids'] = [] # if a list of nodes is defined, convert them from node references to node UUIDs for action_node_option in action_space_cfg.get('options',{}).pop('nodes', {}): if 'node_ref' in action_node_option: - node_uuid = ref_map_nodes[action_node_option['node_ref']] + node_uuid = sess.ref_map_nodes[action_node_option['node_ref']] action_space_cfg['options']['node_uuids'].append(node_uuid) # Each action space can potentially have a different list of nodes that it can apply to. Therefore, # we will pass node_uuids as a part of the action space config. @@ -342,7 +347,7 @@ class PrimaiteSession: if 'options' in action_config: if 'target_router_ref' in action_config['options']: _target = action_config['options']['target_router_ref'] - action_config['options']['target_router_uuid'] = ref_map_nodes[_target] + action_config['options']['target_router_uuid'] = sess.ref_map_nodes[_target] action_space = ActionManager.from_config(sess, action_space_cfg)