-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
1307 lines (1108 loc) · 47.6 KB
/
environment.py
File metadata and controls
1307 lines (1108 loc) · 47.6 KB
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
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
import colorsys
import torch
import numpy as np
import pandas as pd
from Agents.Control.StaticAgent import StaticAgent
from Agents.Control.StaticAgent import StaticType
from Agents.Control.SAAAgent import SAAAgent
from Agents.PPO.PPOAgent import PPOAgent
#from BetaPPOAgent import PPOAgent
from Agents.DQN.DQNAgent import DQNAgent
from Agents.DPG.DPGAgent import DPGAgent
from Agents.Control.RandomStartAgent import FixedStartAgent
from Agents.MFOS.MFOSAgent2 import AblatedMFOSAgent
from Agents.MFOS.MFOSAgent2 import MFOSAgent
from collections import deque
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# PPO Agent Parameters
timeHorizon = 1024 # steps T_h
numParallelActors = 16 # N_a
recurrentSequenceLength = 4
discountFactor = 0.8 # gamma
gaeParameter = 0.95 # lambda
policyClipFraction = 0.2 # epsilon
numGradientEpochs = 10
learningRate = 0.00025
transmissionWeight = 1
beta = .75
bandwidthDistortionFactor = beta # 0 - 1 Beta_bw
centerDistortionFactor = beta # 0 - 1 Beta_f_c
# ppo reward weights
collisionTransmissionTolRatio = 0.0125 # for pulsed aversions
collisionTransmissionTolRatio = 0.33 # for constant aversions Use worst reward for pulses, not effective in 2.4-2.5GHz live data
collisionTransmissionTolRatio = 0.08 # effective in 2.4-2.5GHz live data, Use worst reward for pulses
collisionTransmissionTolRatio = 0.04 # effective in 2.59-2.69GHz live data, Use worst reward for pulses
# collisionTransmissionTolRatio = 0.033 # Shane's recommendation 30 * collision
# collisionTransmissionTolRatio = 0.0355
# collisionWeight = 29
# collisionTransmissionTolRatio = .0275
collisionTransmissionTolRatio = 0.033
collisionWeight = (transmissionWeight / collisionTransmissionTolRatio) #* (1 - beta) # 0 - 50 alpha_c
# Radar system parameters
startingFrequency = 2400 # MHz
channelBandwidth = 100 # MHz
fftSize = 1024 # samples
binSize = channelBandwidth / fftSize # MHz
pri = 204.8 # usec
cpiLen = 256 # pulses
hoCaeWindowSize = 64 # n the Hardware-Optimized Cell Averaging Estimation (HO-CAE)
hoCaeOrderSelection = 5 # k
hoCaeScalar = 16 # alpha
# DDQN Main Scenario Parameters
memoryBufferSize = 2000 # transitions
batchSize = 32
sharedChannelBandwidth = 100 # MHz
targetRCS = 0.1 # m^2
fullyConnectedLayerSizes = [256, 128,84] # neurons
episodeLength = 10 # DDRQN
positionStates = 50 # P
coherentProcessingInterval = 1000 # pulses CPI
learningRate = 0.001 # alpha
targetNetworkUpdate = 250 # steps
subChannelBandwidth = 20 # MHz
discountFactor = 0.9 # gamma
lstmSize = 84 # DDRQN
rewardParameters = (5,6) # (Beta_1, Beta_2)
velocityStates = 10 # V
pulseRepetitionInterval = 0.41 # ms (PRI)
def initState(fftSize=1024):
return np.zeros(fftSize, dtype=bool)
def updateStateInterval(previousState, interval):
if interval == None:
return previousState
start, stop = interval
exec_start = max(0, start)
exec_stop = min(fftSize, stop)
if exec_start < exec_stop:
previousState[exec_start:exec_stop] = True
return previousState
def computeCollisions(previousState, interval):
start, stop = interval
exec_start = max(0, start)
exec_stop = min(fftSize, stop)
if exec_start < exec_stop:
return np.count_nonzero(previousState[exec_start:exec_stop])
return 0
# Returns action corresponding to longest deadspace of previous state bandwidth
def getLargestDeadSpaceInterval(prevState):
if prevState.dtype != bool:
raise TypeError("Expected a boolean numpy array")
is_false = ~prevState
padded = np.concatenate(([0], is_false.view(np.int8), [0]))
diffs = np.diff(padded)
starts = np.where(diffs == 1)[0]
ends = np.where(diffs == -1)[0]
if len(starts) == 0:
return None # no available space
lengths = ends - starts
idx = np.argmax(lengths)
return int(starts[idx]), int(ends[idx])
def computeRewardsForAgents(
cognitiveAgents,
binOwnership
):
"""
Generic reward computation for any agent group.
Parameters
----------
numAgents : int
Number of agents in this group
agentActionMap : dict[int, (start, stop)]
Actions for this agent group
agentPrevRewardMap : dict[int, float]
Per-step rewards
agentCumulativeRewardMap : dict[int, float]
Accumulated rewards
interferingActionMaps : list[dict]
Other agents' action maps that cause interference
"""
for cogAgent in cognitiveAgents:
currAction = cogAgent.currentAction
if currAction is None:
continue
raw_start, raw_stop = currAction
raw_bw_bins = raw_stop - raw_start
reward = 0.0
if raw_bw_bins > 0:
agent_id = cognitiveAgents.index(cogAgent) + 2
if cogAgent.isTransmitting:
left_overflow = max(0, -raw_start)
right_overflow = max(0, raw_stop - fftSize)
overflow_bins = left_overflow + right_overflow
exec_start = max(0, raw_start)
exec_stop = min(fftSize, raw_stop)
ownership_slice = binOwnership[exec_start:exec_stop]
total_bins = exec_stop - exec_start
if multiAgent:
owned_mask = (ownership_slice == agent_id)
owned_bins = np.sum(owned_mask)
collision_bins = total_bins - owned_bins
else:
# Only static causes collision
collision_mask = (ownership_slice == 1)
collision_bins = np.sum(collision_mask)
# Add overflow as collision
collision_bins += overflow_bins
txFrac = (total_bins * binSize) / channelBandwidth
collisionFrac = (collision_bins * binSize) / channelBandwidth
cleanTxFrac = max(0.0, txFrac - collisionFrac)
# Store Collision amount
cogAgent.collisions.append(collision_bins * binSize)
rewardSpectrum = transmissionWeight * cleanTxFrac - collisionWeight * collisionFrac
avgCenterFreq = cogAgent.getAveCenterFreqForCPI()
avgBW = cogAgent.getAveBwForCPI()
agentCenterFreq, agentBW = intervalToCenterFreqBW(currAction)
deltaBW = abs(agentBW - avgBW)
deltaCenterFreq = abs(agentCenterFreq - avgCenterFreq)
rewardAdapt = (bandwidthDistortionFactor * deltaBW / channelBandwidth) + (centerDistortionFactor * deltaCenterFreq / channelBandwidth)
reward = rewardSpectrum - rewardAdapt
else: # Listening but not transmitting
left_overflow = max(0, -raw_start)
right_overflow = max(0, raw_stop - fftSize)
overflow_bins = left_overflow + right_overflow
exec_start = max(0, raw_start)
exec_stop = min(fftSize, raw_stop)
ownership_slice = binOwnership[exec_start:exec_stop]
total_bins = exec_stop - exec_start
if multiAgent:
owned_mask = (ownership_slice == agent_id)
owned_bins = np.sum(owned_mask)
collision_bins = total_bins - owned_bins
else:
# Only static causes collision
collision_mask = (ownership_slice == 1)
collision_bins = np.sum(collision_mask)
# Add overflow as collision
collision_bins += overflow_bins
collisionFrac = (collision_bins * binSize) / channelBandwidth
reward -= (collisionFrac * (collisionWeight / 30))
cogAgent.storeReward(reward)
def worstReward(rewardMap, end_t, window=256):
return min(
rewardMap[t] if 0 <= t < len(rewardMap) else 0.0
for t in range(end_t - window, end_t)
)
def sum_recent_rewards(rewardMap, end_t, window=256):
"""
Sum rewards in [end_t - window, end_t)
Missing timesteps are treated as 0.
"""
return sum(
rewardMap[t] if 0 <= t < len(rewardMap) else 0.0
for t in range(end_t - window, end_t)
)
def build_labeled_state(
staticState,
listOfAgents,
binOwnership,
fftSize=1024
):
# -------------------------------
# State is just ownership
# -------------------------------
state = binOwnership.copy()
# -------------------------------
# Alpha mask (visibility)
# -------------------------------
alpha_mask = np.zeros(fftSize, dtype=float)
# Static always visible
alpha_mask[staticState] = 1.0
# -------------------------------
# Collision mask
# -------------------------------
collision_mask = np.zeros(fftSize, dtype=bool)
# -------------------------------
# Process agents
# -------------------------------
for idx, agent in enumerate(listOfAgents):
if agent.currentAction is None:
continue
s, e = agent.currentAction
s = max(0, s)
e = min(fftSize, e)
if s >= e:
continue
agent_id = idx + 2
if agent.isTransmitting:
ownership_slice = binOwnership[s:e]
# collision = transmitting where not owner
if multiAgent:
local_collision = (ownership_slice != agent_id)
else:
local_collision = (ownership_slice == 1)
collision_mask[s:e] |= local_collision
# transmitting always visible
alpha_mask[s:e] = 1.0
else:
# listening: semi-transparent, but don't override TX
listen_mask = (alpha_mask[s:e] < 1.0)
alpha_mask[s:e][listen_mask] = 0.3
# -------------------------------
# Collision override
# -------------------------------
collision_label = len(listOfAgents) + 2
state[collision_mask] = collision_label
alpha_mask[collision_mask] = 1.0
return state, alpha_mask
def build_agent_colormap(n_colors):
"""
n_colors includes:
- index 0: Free (white)
- last index: Collision (red)
- everything in between: agent colors
"""
colors = []
# 0: Free (neutral background)
colors.append("#f7f7f7") # softer than pure white
# Middle colors: evenly spaced hues, avoid red (0°)
n_middle = n_colors - 2
for i in range(n_middle):
hue = (i + 1) / (n_middle + 1) # spreads across spectrum
sat = 0.75 # strong color
val = 0.85 # not too bright (avoid white)
r, g, b = colorsys.hsv_to_rgb(hue, sat, val)
colors.append(f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}")
# Last: Collision (red)
colors.append("#d62728")
return ListedColormap(colors)
def intervalToCenterFreqBW(interval):
if interval == None:
return (0,0)
intervalBW = binSize * (interval[1] - interval[0]) # MHz
centerFreq = startingFrequency + ((binSize * interval[0]) + (intervalBW / 2)) # MHz
return (centerFreq, intervalBW)
def updateBinOwnership(binOwnership, staticState, cognitiveAgents):
"""
Simultaneous ownership update with:
- Static priority
- Single-claim wins
- Multi-claim resolved via previous ownership
- Release of bins when agents leave
Parameters
----------
binOwnership : np.ndarray[int]
Ownership map (modified in-place)
staticState : np.ndarray[bool]
Static occupancy (True = owned by static)
cognitiveAgents : list
Each agent must have:
- currentAction: (start, stop) or None
Returns
-------
None
"""
# -------------------------------
# Step 0: copy previous ownership
# -------------------------------
prevOwnership = binOwnership.copy()
# -------------------------------
# Step 1: reset to static baseline
# -------------------------------
binOwnership[:] = 0
binOwnership[staticState] = 1 # static always wins
# -------------------------------
# Step 2: build claim map
# -------------------------------
claim_counts = np.zeros(fftSize, dtype=np.int32)
claimants = [[] for _ in range(fftSize)]
transmitters = [[] for _ in range(fftSize)] # NEW
for idx, agent in enumerate(cognitiveAgents):
if agent.currentAction is None:
continue
start, stop = agent.currentAction
start = max(0, start)
stop = min(fftSize, stop)
if start >= stop:
continue
agent_id = idx + 2
for i in range(start, stop):
claim_counts[i] += 1
claimants[i].append(agent_id)
if agent.isTransmitting:
transmitters[i].append(agent_id) # NEW
# -------------------------------
# Step 3: resolve ownership
# -------------------------------
for i in range(fftSize):
# Static always dominates
if staticState[i]:
continue
if claim_counts[i] == 1:
binOwnership[i] = claimants[i][0]
elif claim_counts[i] > 1:
prev_owner = prevOwnership[i]
# ----------------------------------
# Case 1: previous owner keeps it
# ----------------------------------
if prev_owner >= 2 and prev_owner in claimants[i]:
binOwnership[i] = prev_owner
# ----------------------------------
# Case 2: no previous owner → NEW RULE
# ----------------------------------
elif prev_owner == 0:
tx_list = transmitters[i]
if len(tx_list) == 1:
# exactly one transmitter → wins
binOwnership[i] = tx_list[0]
else:
# 0 or multiple transmitters → no owner
binOwnership[i] = 0
# ----------------------------------
# Case 3: previous owner lost claim
# ----------------------------------
else:
binOwnership[i] = 0
# else: remains 0
def HOCAE(frame, window_size, k, Pfa):
'''
Implementation of the HO-CAE algorithm for spectrum detections.
frame: the frame for the threshold to be calculated on.
WindowSize: estimator size. This should be a power of 2.
k: order statistic for estimate selection.
Pfa: Pfa used for threshold calculation.
'''
frame = np.asarray(frame)
# Calculate the alpha value
alpha = window_size * (Pfa ** (-1 / window_size) - 1)
# Estimate the noise floor using overlapping windows
estimates = []
lower = 0
upper = window_size
step = window_size // 2
while upper <= len(frame):
tot = np.sum(frame[lower:upper])
estimates.append(tot / window_size)
lower += step
upper += step
estimate = np.array(estimates)
# Select the order statistic (convert MATLAB's 1-based indexing to Python)
sorted_estimate = np.sort(estimate)
thresh = alpha * sorted_estimate[k - 1]
return thresh, estimate
def mean_std_every_n(rewards, n=4096):
rewards = np.asarray(rewards)
usable_len = (len(rewards) // n) * n
blocks = rewards[:usable_len].reshape(-1, n)
mean = blocks.mean(axis=1)
std = blocks.std(axis=1)
x = np.arange(len(mean)) * n
return x, mean, std
def fill_small_gaps(occupancy, max_gap=10):
filled = occupancy.copy()
n = len(occupancy)
i = 0
while i < n:
if not occupancy[i]:
start = i
# find end of gap
while i < n and not occupancy[i]:
i += 1
end = i
gap_size = end - start
# check if bounded by True on both sides
left = start - 1
right = end
if (
gap_size <= max_gap and
left >= 0 and right < n and
occupancy[left] and occupancy[right]
):
filled[start:end] = True
else:
i += 1
return filled
def compute_state_from_file(f):
data = np.fromfile(f, dtype=np.complex64, count=fftSize)
if data.size < fftSize:
return None
# FFT → frequency domain
X = np.fft.fftshift(np.fft.fft(data))
mag = np.abs(X)
# HO-CAE detection
thresh, _ = HOCAE(
mag,
window_size=32,
k=hoCaeOrderSelection,
Pfa=1e-2
)
# Boolean occupancy state
occupancy = mag > thresh
occupancy = fill_small_gaps(occupancy=occupancy, max_gap=10)
return occupancy
currentState = staticState = initState(fftSize) # S
occupiedBwPerIteration = []
spectrumSampleSize=30_000
allStates = []
deadspace = [] # MHz
device = "cpu"
seed = 432069
staticAgentRNG = np.random.default_rng(seed)
seed += 1
randomStartAgentRNG = np.random.default_rng(seed)
seed += 1
dqnAgentRNG = np.random.default_rng(seed)
seed += 1
ppoSeed=seed
seed += 1
mfosSeed=seed
seed += 1
torch.Generator(device=device).manual_seed(seed)
transmissionWeight = 1
beta = .5
bandwidthDistortionFactor = beta # 0 - 1 Beta_bw
centerDistortionFactor = beta # 0 - 1 Beta_f_c
# ppo reward weights
collisionTransmissionTolRatio = 0.0125 # for pulsed aversions
collisionTransmissionTolRatio = 0.33 # for constant aversions Use worst reward for pulses, not effective in 2.4-2.5GHz live data
collisionTransmissionTolRatio = 0.08 # effective in 2.4-2.5GHz live data, Use worst reward for pulses
collisionTransmissionTolRatio = 0.04 # effective in 2.59-2.69GHz live data, Use worst reward for pulses
# collisionTransmissionTolRatio = 0.033 # Shane's recommendation 30 * collision
# collisionTransmissionTolRatio = 0.0355
# collisionWeight = 29
# collisionTransmissionTolRatio = .0275
collisionTransmissionTolRatio = 0.033
collisionWeight = (transmissionWeight / collisionTransmissionTolRatio) #* (1 - beta) # 0 - 50 alpha_c
output_file = "agent_eval_summary.xlsx"
liveDataFilename = '../spectrum_245ghz.dat' # 2.4-2.5 GHz
# liveDataFilename = '../spectrum_264ghz.dat' # 2.59-2.69 GHz
storedStateFile = '../spectrum_245ghz.npz' if liveDataFilename == '../spectrum_245ghz.dat' else '../spectrum_264ghz.npz' # 2.4-2.5 GHz
startingFrequency = 2400 if storedStateFile == '../spectrum_245ghz.npz' else 2590
fileSize = os.path.getsize(liveDataFilename)
# If precomputed file exists, just load it
sim = False # Set False for live data
if not sim:
if os.path.exists(storedStateFile):
npz = np.load(storedStateFile)
liveData = npz["states"] # shape (num_samples, fftSize), dtype=bool
print("Loaded precomputed states:", liveData.shape)
else:
liveData = []
with open(liveDataFilename, "rb") as f:
while True:
state = compute_state_from_file(f)
if state is None:
break
liveData.append(state)
liveData = np.stack(liveData) # (num_samples, fftSize)
# Save for future reuse
np.savez_compressed(storedStateFile, states=liveData)
print("Saved precomputed states:", liveData.shape)
iterations = 1_000_000 if sim else liveData.shape[0]
multiAgent = False
# iterations *= 3
eval = False
timestep = pulseWidth = 10.24
iterationsInPulse = int(pri / timestep)
allCogAgents = []
# Static Agents For Simulating Environment
staticAgents = []
numLargeAgents = 0 # pw .1 - .25K, interval 10K, 150-175 bins wide
numSkinnyAgents = 0 # pw .25K, interval 2K, 20 bins wide
numPulsedAgents = 0 # pw .1K, interval = 4K, 30-40 bins wide on/off
numRectangleAgents = 0 # pw = 50, interval = 10 -250, 60-680 bins
numStaticAgents = numLargeAgents + numSkinnyAgents + numPulsedAgents + numRectangleAgents
for staticAgent in range(numLargeAgents):
staticAgents.append(StaticAgent(rng=staticAgentRNG, staticType=StaticType.Fat))
for staticAgent in range(numSkinnyAgents):
staticAgents.append(StaticAgent(rng=staticAgentRNG, staticType=StaticType.Skinny))
for staticAgent in range(numPulsedAgents):
staticAgents.append(StaticAgent(rng=staticAgentRNG, staticType=StaticType.Pulsed))
for staticAgent in range(numRectangleAgents):
staticAgents.append(StaticAgent(rng=staticAgentRNG, staticType=StaticType.Rectangular))
# Random Single Action Agent
numRandomStartAgents = 5
randomStartAgents = []
randomStartAgentStartIndices = []
for randAgent in range(numRandomStartAgents):
randomStartAgents.append(FixedStartAgent(rng=randomStartAgentRNG))
randomStartAgents[randAgent].storeAction(intervalToCenterFreqBW(randomStartAgents[randAgent].currentAction))
randomStartAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(randomStartAgents[randAgent])
# SAA Agent Parameters
numSaaAgents = 5 # Sense-And-Avoid
saaAgents = []
saaAgentStartIndices = []
for saaAgent in range(numSaaAgents):
saaAgents.append(SAAAgent())
saaAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(saaAgents[saaAgent])
# PPO Agent Parameters
numPpoAgents = 5 # Proximal Policy Optimization
ppoAgents = []
ppoAgentStartIndices = []
for ppoAgent in range(numPpoAgents):
ppoAgents.append(PPOAgent(fftSize=fftSize, cpiLen=cpiLen, device=device, seed=ppoSeed+ppoAgent))
ppoAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(ppoAgents[ppoAgent])
# DQN Agent Parameters
BANDWIDTHS = [96, 128, 160] #[32, 64, 96]
CENTERS = np.linspace(0, fftSize-1, 32, dtype=int)
DQN_ACTIONS = []
for bw in BANDWIDTHS:
for c in CENTERS:
start = max(0, c - bw // 2)
stop = min(fftSize, start + bw)
if stop - start == bw:
DQN_ACTIONS.append((start, stop))
numDqnAgents = 5
dqnAgents = []
dqnAgentStartIndices = []
for dqnAgent in range(numDqnAgents):
dqnAgents.append(DQNAgent(fftSize=fftSize, actionList=DQN_ACTIONS, cpiLen=cpiLen, device=device))
dqnAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(dqnAgents[dqnAgent])
# M-FOS Agent Initialization
numMfosAgents = 5
mfosAgents = []
mfosAgentStartIndices = []
for mfosAgentI in range(numMfosAgents):
# base_genome = {
# "lr": 1.4e-5,
# "gamma": 0.989,
# "exploration_center": 0.151,
# "exploration_bw": 0.14,
# "entropy_coef": .00013
# }
base_genome = None # Random Genomes
mfosAgent = MFOSAgent(
population_size=5,
base_genome=base_genome,
mutation_scale=0.05,
elite_fraction=.4,
fresh_fraction=0.2,
seed=seed + mfosAgentI + 1, #42075 is good for random genomes and weights?
device=device,
fftSize=fftSize,
cpiLen=cpiLen
)
mfosAgents.append(mfosAgent)
mfosAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(mfosAgent)
# DPG Agent Initialization
numDpgAgents = 0
dpgAgents = []
dpgAgentStartIndices = []
for i in range(numDpgAgents):
dpgAgents.append(DPGAgent(fftSize=fftSize, device=device))
dpgAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(dpgAgents[i])
# Ablated M-FOS Agent Initialization
numAblatedMfosAgents = 5
ablatedMFOSAgents = []
ablatedMfosAgentStartIndices = []
for mfosAgentI in range(numAblatedMfosAgents):
ablatedMfosAgent = AblatedMFOSAgent(
fftSize=fftSize,
cpiLen=cpiLen,
device=device,
seed=seed + mfosAgentI + 1 #42075 is good for random genomes and weights?
)
ablatedMFOSAgents.append(ablatedMfosAgent)
ablatedMfosAgentStartIndices.append(torch.randint(low=0, high=iterationsInPulse, size=(1,)).item())
allCogAgents.append(ablatedMfosAgent)
lastPulseStates = []
for agent in allCogAgents:
lastPulseStates.append(deque(maxlen=iterationsInPulse))
binOwnership = np.zeros(fftSize, dtype=np.int8) # 0=unowned, 1=staticOwner, 2+=cogUser
# main loop
for i in range(iterations): # 1 = 12.8 microseconds
if not eval and i == int(iterations * .8):
eval = True
for ppoAgent in ppoAgents:
ppoAgent.policy.eval()
for dqnAgent in dqnAgents:
dqnAgent.policy.eval()
dqnAgent.epsilon = 0.0
for mfosAgent in mfosAgents:
mfosAgent.set_eval_mode()
for ablatedMfosAgent in ablatedMFOSAgents:
ablatedMfosAgent.set_eval_mode()
if i % 100_000 == 0:
print(int(i/1000), "K iterations completed.")
# store previous state space without the active agents action
for idx, _ in enumerate(allCogAgents):
prevStateWithoutAgent = staticState.copy()
if multiAgent:
for idx2, agent2 in enumerate(allCogAgents):
if idx != idx2 and agent2.isTransmitting:
prevStateWithoutAgent = updateStateInterval(prevStateWithoutAgent, agent2.currentAction)
lastPulseStates[idx].append(prevStateWithoutAgent)
# Generate actions for SAA agents
for saaAgentI in range(numSaaAgents):
if i % iterationsInPulse == saaAgentStartIndices[saaAgentI]:
saaAgent = saaAgents[saaAgentI]
interval = getLargestDeadSpaceInterval(lastPulseStates[saaAgentI+numRandomStartAgents][-1])
saaAgent.currentAction = interval
action = intervalToCenterFreqBW(interval)
saaAgent.storeAction(action)
elif i % iterationsInPulse == ((saaAgentStartIndices[saaAgentI]+1) % iterationsInPulse): # Pulse lasts one iteration, then listens for PRI duration
saaAgents[saaAgentI].isTransmitting = False
# Generate actions for Random Start agents
for randomStartAgentI in range(numRandomStartAgents):
if i % iterationsInPulse == randomStartAgentStartIndices[randomStartAgentI]: # every 204.8 usec
randomStartAgents[randomStartAgentI].storeAction(intervalToCenterFreqBW( randomStartAgents[randomStartAgentI].currentAction))
elif i % iterationsInPulse == ((randomStartAgentStartIndices[randomStartAgentI]+1) % iterationsInPulse): # Pulse lasts one iteration, then listens for PRI duration
randomStartAgents[randomStartAgentI].isTransmitting = False
# Generate actions for PPO agents
for ppoAgentI in range(numPpoAgents):
if i % iterationsInPulse == ppoAgentStartIndices[ppoAgentI]: # every 204.8 usec
agentStates = lastPulseStates[ppoAgentI + numRandomStartAgents + numSaaAgents]
if len(agentStates) == iterationsInPulse:
ppoAgent = ppoAgents[ppoAgentI]
obs_seq = np.stack(agentStates)
ppoAgent.select_action(obs_seq, eval_mode=eval)
ppoAgent.storeAction(intervalToCenterFreqBW(ppoAgent.currentAction))
elif i % iterationsInPulse == ((ppoAgentStartIndices[ppoAgentI]+1) % iterationsInPulse): # Pulse lasts one iteration, then listens for PRI duration
ppoAgents[ppoAgentI].isTransmitting = False
# Generate actions for DQN agents
for dqnAgentI in range(numDqnAgents):
if i % iterationsInPulse == dqnAgentStartIndices[dqnAgentI]:
state_t = lastPulseStates[dqnAgentI + numRandomStartAgents + numSaaAgents + numPpoAgents][-1].astype(np.float32)
dqnAgent = dqnAgents[dqnAgentI]
action_idx = dqnAgent.select_action(state_t, rng=dqnAgentRNG, eval_mode=eval)
interval = DQN_ACTIONS[action_idx]
dqnAgent.currentAction = interval
action = intervalToCenterFreqBW(interval)
dqnAgent.storeAction(action)
elif i % iterationsInPulse == ((dqnAgentStartIndices[dqnAgentI]+1) % iterationsInPulse):
dqnAgents[dqnAgentI].isTransmitting = False
# M-FOS agent action selection
for mfosAgentI in range(numMfosAgents):
if i % iterationsInPulse == mfosAgentStartIndices[mfosAgentI]:
mfosAgent = mfosAgents[mfosAgentI]
agentStates = lastPulseStates[mfosAgentI + numRandomStartAgents + numSaaAgents + numPpoAgents + numDqnAgents]
if len(agentStates) == iterationsInPulse:
obs_seq = np.stack(agentStates)
mfosAgent.select_action(obs_seq)
mfosAgent.storeAction(intervalToCenterFreqBW(mfosAgent.currentAction))
elif i % iterationsInPulse == ((mfosAgentStartIndices[mfosAgentI]+1) % iterationsInPulse):
mfosAgents[mfosAgentI].isTransmitting = False
# DPG Agent action selection
for dpgAgentI in range(numDpgAgents):
if i % iterationsInPulse == dpgAgentStartIndices[dpgAgentI]:
agentStates = lastPulseStates[dpgAgentI + numRandomStartAgents + numSaaAgents + numPpoAgents + numDqnAgents + numMfosAgents]
if len(agentStates) == iterationsInPulse:
state_dpg = agentStates[-1].astype(np.float32)
dpgAgent = dpgAgents[dpgAgentI]
obs_seq = np.stack(agentStates)
dpgAgent.select_action(obs_seq, eval_mode=eval)
dpgAgent.storeAction(intervalToCenterFreqBW(dpgAgent.currentAction))
elif i % iterationsInPulse == ((dpgAgentStartIndices[dpgAgentI]+1) % iterationsInPulse):
dpgAgents[dpgAgentI].isTransmitting = False
# Ablated M-FOS agent action selection
for ablatedMfosAgentI in range(numAblatedMfosAgents):
if i % iterationsInPulse == ablatedMfosAgentStartIndices[mfosAgentI]:
ablatedMfosAgent = ablatedMFOSAgents[ablatedMfosAgentI]
agentStates = lastPulseStates[ablatedMfosAgentI + numRandomStartAgents + numSaaAgents + numPpoAgents + numDqnAgents + numMfosAgents + numDpgAgents]
if len(agentStates) == iterationsInPulse:
obs_seq = np.stack(agentStates)
ablatedMfosAgent.select_action(obs_seq)
ablatedMfosAgent.storeAction(intervalToCenterFreqBW(ablatedMfosAgent.currentAction))
elif i % iterationsInPulse == ((ablatedMfosAgentStartIndices[ablatedMfosAgentI]+1) % iterationsInPulse):
ablatedMFOSAgents[ablatedMfosAgentI].isTransmitting = False
# Static Agent Actions. Simulate frequency changes
currentState = initState(fftSize)
for staticAgent in staticAgents:
staticAgent.iterateCurrentAction(iteration=i)
for j in range(numStaticAgents):
# Every 50_000 iterations, choose a new action
if ((staticAgents[j].staticType == StaticType.Fat or StaticType.Pulsed) and (j + 1) * 100_000 == i) or (staticAgents[j].staticType == StaticType.Skinny and (j + 1) * 30_000 == i):
staticAgents[j].takeRandomAction()
for staticAgent in staticAgents:
currentState = updateStateInterval(currentState, staticAgent.currentAction)
if sim == False: # Use Live Data
currentState = currentState | liveData[i%len(liveData)]
staticState = currentState.copy()
# Update state
if multiAgent:
for agent in allCogAgents:
if agent.isTransmitting:
currentState = updateStateInterval(currentState, agent.currentAction)
occupiedBwPerIteration.append(np.sum(currentState) * binSize)
updateBinOwnership(
binOwnership=binOwnership,
staticState=staticState,
cognitiveAgents=allCogAgents
)
# Only build labeled state for final sample size
if i >= iterations-spectrumSampleSize:
allStates.append(build_labeled_state(
staticState=staticState,
listOfAgents=allCogAgents,
binOwnership=binOwnership,
fftSize=fftSize
))
deadSpaceInterval = getLargestDeadSpaceInterval(currentState)
if deadSpaceInterval == None:
deadspace.append(0)
else:
deadspace.append((deadSpaceInterval[1] - deadSpaceInterval[0]) * binSize)
# Compute reward for cognitive agents
computeRewardsForAgents(
cognitiveAgents=allCogAgents,
binOwnership=binOwnership
)
if not eval and i > 0 and len(lastPulseStates) > 0 and len(lastPulseStates[0]) == iterationsInPulse: # every 204.8 usec
# Update PPO Agents
for ppoAgent in ppoAgents:
if len(ppoAgent.allRewards) > 0 and len(ppoAgent.pulseRewards) == 0:
ppoAgent.store_reward(
reward=ppoAgent.allRewards[-1],
done=False
)
ppoAgent.update()
# Update DQN Agents
for dqnAgent in dqnAgents:
if len(dqnAgent.allRewards) > 0 and len(dqnAgent.pulseRewards) == 0:
dqnAgent.buffer.push(
state_t,
action_idx,
dqnAgent.allRewards[-1],
currentState.astype(np.float32),
False
)
dqnAgent.train_step(rng=dqnAgentRNG)
# Update M-FOS Agents
for mfosAgent in mfosAgents:
if len(mfosAgent.allRewards) > 0 and len(mfosAgent.pulseRewards) == 0:
mfosAgent.record_reward(reward=mfosAgent.allRewards[-1])
mfosAgent.update()
# Update DPG Agents:
for dpgAgent in dpgAgents:
if len(dpgAgent.allRewards) > 0 and len(dpgAgent.pulseRewards) == 0:
dpgAgent.buffer.push(
state_dpg,
dpgAgent.lastAction,
dpgAgent.allRewards[-1],
currentState.astype(np.float32),
False
)
dpgAgent.train_step()
# Update Ablated M-FOS Agents
for ablatedMfosAgent in ablatedMFOSAgents:
if len(ablatedMfosAgent.allRewards) > 0 and len(ablatedMfosAgent.pulseRewards) == 0:
ablatedMfosAgent.record_reward(reward=ablatedMfosAgent.allRewards[-1])
ablatedMfosAgent.update()
if not eval:
if i % (iterationsInPulse * 1000) == 0:
for dqnAgent in dqnAgents:
dqnAgent.target.load_state_dict(dqnAgent.policy.state_dict())
if i % (cpiLen * iterationsInPulse * 8) == 0 and i > 0:
for mfosAgent in mfosAgents:
mfosAgent.finish_individual()
# 2️⃣ If generation complete → evolve
if mfosAgent.is_generation_complete():
best = np.argmax(mfosAgent.fitness)
print("Best genome:", mfosAgent.population[best].genome)
print(f"Evolving MFOS Agent {idx+1} population...")
mfosAgent.evolve()
liveData = None
# Print Cumulative Rewards
cumulativeRewardString = "Cumulative Evaluation Reward:"
for randomStartAgent in range(numRandomStartAgents):
print("Random Start Agent", randomStartAgent+1 if randomStartAgent > 0 else "", cumulativeRewardString, sum(randomStartAgents[randomStartAgent].allRewards[int(len(randomStartAgents[randomStartAgent].allRewards)*.8):]))
for saaAgent in range(numSaaAgents):
print("SAA Agent", saaAgent+1 if saaAgent > 0 else "", cumulativeRewardString, sum(saaAgents[saaAgent].allRewards[int(len(saaAgents[saaAgent].allRewards)*.8):]))
for ppoAgent in range(numPpoAgents):
print("PPO Agent", ppoAgent+1 if ppoAgent > 0 else "", cumulativeRewardString, sum(ppoAgents[ppoAgent].allRewards[int(len(ppoAgents[ppoAgent].allRewards)*.8):]))
for dqnAgent in range(numDqnAgents):
print("DQN Agent", dqnAgent+1 if dqnAgent > 0 else "", cumulativeRewardString, sum(dqnAgents[dqnAgent].allRewards[int(len(dqnAgents[dqnAgent].allRewards)*.8):]))
for mfosAgent in range(numMfosAgents):
print("M-FOS Agent", mfosAgent+1 if mfosAgent > 0 else "", cumulativeRewardString, sum(mfosAgents[mfosAgent].allRewards[int(len(mfosAgents[mfosAgent].allRewards)*.8):]))
for dpgAgent in range(numDpgAgents):
print("DPG Agent", dpgAgent+1 if dpgAgent > 0 else "", cumulativeRewardString, sum(dpgAgents[dpgAgent].allRewards[int(len(dpgAgents[dpgAgent].allRewards)*.8):]))
for ablatedMFOSAgent in range(numAblatedMfosAgents):
print("Ablated M-FOS Agent", ablatedMFOSAgent+1 if ablatedMFOSAgent > 0 else "", cumulativeRewardString, sum(ablatedMFOSAgents[ablatedMFOSAgent].allRewards[int(len(ablatedMFOSAgents[ablatedMFOSAgent].allRewards)*.8):]))
# Spectrum Usage and collisions per agent over time
states_list, alphas_list = zip(*allStates)
stateMatrix = np.stack(states_list)
alphaMatrix = np.stack(alphas_list)
colors = []
colorCount = numRandomStartAgents + numSaaAgents + numPpoAgents + numDqnAgents + numMfosAgents + numDpgAgents + numAblatedMfosAgents + 3
cmap = build_agent_colormap(colorCount)
bounds = []
for i in range(colorCount+1):
bounds.append(i)
norm = BoundaryNorm(bounds, cmap.N)
ticks = []
for i in range(colorCount):