Repository navigation
Expand file tree
/
Copy pathexperiments.py
More file actions
1422 lines (1174 loc) · 55.3 KB
/
Copy pathexperiments.py
File metadata and controls
1422 lines (1174 loc) · 55.3 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
"""
Experiments module for ALT Metrics experiments.
This module contains:
- Checkpoint management (save/load/update)
- Random policy simulation
- Q-learning experiment runners (theory-based and adaptive)
- Complete metrics calculation wrapper
- Multiprocessing experiment workers
"""
import numpy as np
import pickle
import os
import time
import glob
import random
import gc
import re
import multiprocessing as mp
from tqdm import tqdm
try:
from IPython.display import display, Javascript
except ImportError:
display = None # Not in Colab/Jupyter environment
Javascript = None
import filelock
import config
from config import (
IN_COLAB, FULL_REWARD, BASE_EPISODES, USE_MULTIPROCESSING, NUM_PROCESSES
)
from metrics import (
calculate_episodes_theory_based,
compute_alt_metrics, compute_rp_metrics,
compute_efficiency, compute_reward_fairness, compute_tt_fairness, compute_fairness,
calculate_alt_ratio
)
from environment import RandomPolicyEnvironment, Environment
# DQN imports (optional - only loaded if DQN experiments are run)
try:
from dqn_environment import DQNEnvironment
from dqn_agents import DQNAgent
DQN_AVAILABLE = True
except ImportError:
DQN_AVAILABLE = False
print("[WARNING] DQN not available - install PyTorch to enable DQN experiments")
#===============================================================================
# Checkpoint Management
#===============================================================================
def keep_colab_alive():
"""Keep Colab session alive with more frequent pings."""
if IN_COLAB and display is not None and Javascript is not None:
display(Javascript('''
function ClickConnect(){
console.log("Keeping Colab alive...");
document.querySelector("colab-toolbar-button#connect").click()
}
setInterval(ClickConnect, 30000);
'''))
print("KeepAlive activated: Colab will stay connected")
def save_result(result):
"""Save result with complete data for reproducibility."""
if 'is_random_baseline' in result and result['is_random_baseline']:
filename = f"random_{result['num_agents']}agents_{result['reward_type']}.pkl"
else:
filename = f"result_{result['num_agents']}agents_{result['state_type']}_{result['reward_type']}.pkl"
filepath = os.path.join(config.checkpoint_dir, filename)
try:
with open(filepath, 'wb') as f:
pickle.dump(result, f)
update_master_checkpoint(result)
return True
except Exception as e:
print(f"Error saving result: {e}")
return False
def update_master_checkpoint(new_result=None):
"""Update master checkpoint file with file locking to prevent corruption."""
lock_path = os.path.join(config.checkpoint_dir, 'checkpoint.lock')
checkpoint_file = os.path.join(config.checkpoint_dir, 'experiment_checkpoint.pkl')
# Clean up stale lock files (older than 1 hour)
if os.path.exists(lock_path):
lock_age = time.time() - os.path.getmtime(lock_path)
if lock_age > 3600: # 1 hour timeout
print(f"[WARNING] Removing stale lock file (age: {lock_age/3600:.1f} hours)")
try:
os.remove(lock_path)
except Exception as e:
print(f"[ERROR] Could not remove stale lock: {e}")
with filelock.FileLock(lock_path, timeout=60):
if os.path.exists(checkpoint_file):
try:
with open(checkpoint_file, 'rb') as f:
checkpoint = pickle.load(f)
results_list = checkpoint.get('results_list', [])
except Exception as e:
print(f"Error loading checkpoint: {e}")
results_list = []
else:
results_list = []
if new_result is not None:
exists = False
for i, r in enumerate(results_list):
# Check if result with same parameters exists
if 'is_random_baseline' in new_result and new_result['is_random_baseline']:
if ('is_random_baseline' in r and r.get('is_random_baseline') and
r['num_agents'] == new_result['num_agents'] and
r['reward_type'] == new_result['reward_type']):
results_list[i] = new_result
exists = True
break
else:
if (r['num_agents'] == new_result['num_agents'] and
r.get('state_type', '') == new_result.get('state_type', '') and
r['reward_type'] == new_result['reward_type']):
results_list[i] = new_result
exists = True
break
if not exists:
results_list.append(new_result)
checkpoint = {
'results_list': results_list,
'timestamp': time.time(),
'version': '2.0'
}
try:
with open(checkpoint_file, 'wb') as f:
pickle.dump(checkpoint, f)
return results_list
except Exception as e:
print(f"Error saving master checkpoint: {e}")
return results_list
def load_results():
"""Load results from checkpoints - robust fallback to individual files."""
lock_path = os.path.join(config.checkpoint_dir, 'checkpoint.lock')
master_file = os.path.join(config.checkpoint_dir, 'experiment_checkpoint.pkl')
with filelock.FileLock(lock_path, timeout=60):
# TRY 1: Master checkpoint (optimization - fast path)
if os.path.exists(master_file):
try:
with open(master_file, 'rb') as f:
checkpoint = pickle.load(f)
if 'results_list' in checkpoint:
results_list = checkpoint['results_list']
print(f"Loaded {len(results_list)} experiments from master checkpoint")
return results_list
except Exception as e:
print(f"Warning: Could not load master checkpoint: {e}")
# TRY 2: Individual files (ALWAYS try if master missing/corrupt)
# Use os.listdir() instead of glob to avoid issues with brackets in directory names
results_list = []
if os.path.exists(config.checkpoint_dir):
all_files = os.listdir(config.checkpoint_dir)
# Filter for result and random checkpoint files
checkpoint_files = [f for f in all_files
if (f.startswith('result_') or f.startswith('random_'))
and f.endswith('.pkl')]
if checkpoint_files:
print(f"Loading from {len(checkpoint_files)} individual checkpoint files...")
for filename in checkpoint_files:
filepath = os.path.join(config.checkpoint_dir, filename)
try:
with open(filepath, 'rb') as f:
result = pickle.load(f)
results_list.append(result)
except Exception as e:
print(f"Warning: Could not load {filename}: {e}")
if results_list:
print(f"✓ Loaded {len(results_list)} experiments from individual files")
# Rebuild master checkpoint for next time (skip for now to avoid lock issues)
# update_master_checkpoint()
return results_list
print("No checkpoint files found - starting fresh")
return []
def is_experiment_completed(results_list, num_agents, state_type, reward_type, is_random=False):
"""Check if experiment already completed."""
for r in results_list:
if is_random:
if ('is_random_baseline' in r and r.get('is_random_baseline') and
r['num_agents'] == num_agents and
r['reward_type'] == reward_type):
return True
else:
if (r['num_agents'] == num_agents and
r.get('state_type', '') == state_type and
r['reward_type'] == reward_type and
not r.get('is_random_baseline', False)):
return True
return False
def delete_all_results():
"""Delete entire results directory and recreate it."""
import shutil
try:
if os.path.exists(base_dir):
shutil.rmtree(base_dir)
print(f"Deleted entire results directory: {base_dir}")
# Recreate all directories
os.makedirs(base_dir, exist_ok=True)
for dir_path in figure_dirs.values():
os.makedirs(dir_path, exist_ok=True)
os.makedirs(config.checkpoint_dir, exist_ok=True)
print("All results deleted and directories recreated")
except Exception as e:
print(f"Error deleting results: {e}")
#===============================================================================
# Checkpoint Continuation Functions
#===============================================================================
def find_compatible_runs(base_episodes, experiment_type, agent_type):
"""Find existing run directories with matching configuration.
Args:
base_episodes: BASE_EPISODES value (e.g., 1000)
experiment_type: 'theory' or 'adaptive'
agent_type: 'ql' or 'dqn'
Returns:
List of dicts with:
- dir_name: directory name
- full_path: full path to directory
- agent_counts: list of agent counts found in checkpoints
- checkpoint_count: number of checkpoint files
"""
results = []
base_dir_path = config.base_results_dir
# Pattern: run_{experiment_type}_base{episodes}_{agent_type}_agents[...]_{timestamp}
pattern = f"run_{experiment_type}_base{base_episodes}_{agent_type}_agents*"
search_pattern = os.path.join(base_dir_path, pattern)
matched_dirs = glob.glob(search_pattern)
for dir_path in matched_dirs:
if os.path.isdir(dir_path):
checkpoint_dir = os.path.join(dir_path, 'checkpoints')
if os.path.exists(checkpoint_dir):
# Count checkpoint files (exclude master checkpoint)
# Escape the checkpoint_dir path for glob (handles [ ] characters in directory names)
escaped_checkpoint_dir = glob.escape(checkpoint_dir)
checkpoints = glob.glob(os.path.join(escaped_checkpoint_dir, '*.pkl'))
checkpoint_count = len([f for f in checkpoints
if 'experiment_checkpoint' not in f and 'checkpoint.lock' not in f])
# Extract agent counts from checkpoint filenames
agent_counts = set()
for f in checkpoints:
match = re.search(r'(\d+)agents', os.path.basename(f))
if match:
agent_counts.add(int(match.group(1)))
if checkpoint_count > 0: # Only include directories with actual checkpoints
results.append({
'dir_name': os.path.basename(dir_path),
'full_path': dir_path,
'agent_counts': sorted(agent_counts),
'checkpoint_count': checkpoint_count,
})
# Sort by most recent (timestamp in directory name)
results.sort(key=lambda x: x['dir_name'], reverse=True)
return results
def rename_run_directory(run_info, new_agent_counts):
"""Rename directory to include updated agent counts.
Args:
run_info: Dict from find_compatible_runs()
new_agent_counts: List of agent counts to include
Returns:
New directory name (basename only)
Example:
Old: run_theory_base1000_ql_agents[2,3,5,8]_20251205_221651
New: run_theory_base1000_ql_agents[2,3,5,8,10]_20251205_221651
"""
from datetime import datetime
old_path = run_info['full_path']
old_name = run_info['dir_name']
# Extract timestamp from old name
match = re.search(r'_(\d{8}_\d{6})$', old_name)
timestamp = match.group(1) if match else datetime.now().strftime('%Y%m%d_%H%M%S')
# Extract config parts
match = re.search(r'run_([^_]+)_base(\d+)_([^_]+)_agents', old_name)
if not match:
print(f"Warning: Could not parse directory name: {old_name}")
return old_name
experiment_type = match.group(1)
base_episodes = match.group(2)
agent_type = match.group(3)
# Build new name with ALL agent counts (merge old + new)
all_agents = sorted(set(run_info['agent_counts'] + list(new_agent_counts)))
agents_str = ','.join(map(str, all_agents))
new_name = f"run_{experiment_type}_base{base_episodes}_{agent_type}_agents[{agents_str}]_{timestamp}"
# Rename directory
new_path = os.path.join(os.path.dirname(old_path), new_name)
try:
os.rename(old_path, new_path)
print(f"Renamed: {old_name}")
print(f" to: {new_name}")
except Exception as e:
print(f"Error renaming directory: {e}")
return old_name
return new_name
def copy_run_directory(run_info, new_agent_counts):
"""Copy checkpoints to NEW directory with NEW timestamp.
Args:
run_info: Dict from find_compatible_runs()
new_agent_counts: List of agent counts to include
Returns:
New directory name (basename only)
Example:
Old: run_theory_base1000_ql_agents[2,3,5,8]_20251205_221651
New: run_theory_base1000_ql_agents[2,3,5,8,10]_20251207_143022
^^^^^^^^^^^^^^ NEW timestamp
"""
import shutil
from datetime import datetime
old_path = run_info['full_path']
old_name = run_info['dir_name']
old_checkpoint_dir = os.path.join(old_path, 'checkpoints')
# Extract config parts
match = re.search(r'run_([^_]+)_base(\d+)_([^_]+)_agents', old_name)
if not match:
print(f"Warning: Could not parse directory name: {old_name}")
return old_name
experiment_type = match.group(1)
base_episodes = match.group(2)
agent_type = match.group(3)
# Build new name with ALL agent counts (merge old + new) and NEW timestamp
all_agents = sorted(set(run_info['agent_counts'] + list(new_agent_counts)))
agents_str = ','.join(map(str, all_agents))
new_timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
new_name = f"run_{experiment_type}_base{base_episodes}_{agent_type}_agents[{agents_str}]_{new_timestamp}"
# Create new directory
new_path = os.path.join(os.path.dirname(old_path), new_name)
new_checkpoint_dir = os.path.join(new_path, 'checkpoints')
try:
# Create directory structure
os.makedirs(new_checkpoint_dir, exist_ok=True)
# Copy ALL checkpoint files from old to new
if os.path.exists(old_checkpoint_dir):
checkpoint_files = glob.glob(os.path.join(glob.escape(old_checkpoint_dir), '*.pkl'))
checkpoint_files = [f for f in checkpoint_files if 'experiment_checkpoint' not in f]
copied_count = 0
for src_file in checkpoint_files:
dest_file = os.path.join(new_checkpoint_dir, os.path.basename(src_file))
shutil.copy2(src_file, dest_file)
copied_count += 1
print(f"Created: {new_name}")
print(f"Copied {copied_count} checkpoint files from: {old_name}")
else:
print(f"Warning: No checkpoints found in {old_name}")
except Exception as e:
print(f"Error copying directory: {e}")
return None
return new_name
def run_experiments_continue(run_dir, experiment_type='theory', agent_type='ql'):
"""Continue experiments in existing run directory.
This function reuses the existing run directory and only runs missing experiments.
Args:
run_dir: Path to existing run directory (absolute or relative)
experiment_type: 'theory' or 'adaptive'
agent_type: 'ql' or 'dqn'
Returns:
List of all results (existing + newly completed)
"""
from config import TEST_AGENT_COUNTS, BASE_EPISODES, create_subdirectories
# Ensure run_dir is absolute path
if not os.path.isabs(run_dir):
run_dir = os.path.abspath(run_dir)
print(f"\n{'='*80}")
print(f"CONTINUING EXPERIMENTS IN EXISTING DIRECTORY")
print(f"{'='*80}")
print(f"Run directory: {run_dir}")
# Create subdirectories if missing (shouldn't be needed, but safe)
subdirs = create_subdirectories(run_dir)
# UPDATE config module variables to point to this directory
config.checkpoint_dir = subdirs['checkpoints']
config.figure_dirs = subdirs
config.base_dir = run_dir
print(f"Checkpoints: {config.checkpoint_dir}")
# Load EXISTING results from this directory
results_list = load_results()
print(f"\nLoaded {len(results_list)} existing experiment results")
# Show what's already completed
if results_list:
print("\nAlready completed:")
for r in results_list:
if r.get('is_random_baseline'):
print(f" - Random baseline: {r['num_agents']} agents, {r['reward_type']}")
else:
print(f" - Q-Learning: {r['num_agents']} agents, {r.get('state_type', 'N/A')}, {r['reward_type']}")
# The rest is IDENTICAL to run_experiments() - generate experiments_to_run list, run multiprocessing
# We'll copy the remaining code from run_experiments() here
agent_counts = TEST_AGENT_COUNTS
state_types = ['Type-A', 'Type-B']
reward_types = ['ILF', 'IQF']
# Verify DQN availability if requested
if agent_type == 'dqn' and not DQN_AVAILABLE:
raise ImportError("DQN requested but PyTorch not available. Install with: pip install torch")
# Print what's already complete
print("\n" + "="*50)
print("COMPLETED EXPERIMENTS")
print("="*50)
for r in results_list:
agent_count = r['num_agents']
state = r.get('state_type', 'Random')
reward = r['reward_type']
is_rand = r.get('is_random_baseline', False)
episodes = r.get('num_episodes', 'N/A')
if is_rand:
print(f" ✓ {agent_count} agents, Random Baseline, {reward} ({episodes} episodes)")
else:
print(f" ✓ {agent_count} agents, {state}, {reward} ({episodes} episodes)")
# Generate experiments to run list (skip completed ones)
experiments_to_run = []
for num_agents in agent_counts:
for state_type in state_types:
for reward_type in reward_types:
if not is_experiment_completed(results_list, num_agents, state_type, reward_type):
experiments_to_run.append((
num_agents, state_type, reward_type, experiment_type, agent_type
))
# Random baselines
for num_agents in agent_counts:
for reward_type in reward_types:
if not is_experiment_completed(results_list, num_agents, None, reward_type, is_random=True):
experiments_to_run.append((
num_agents, None, reward_type, experiment_type, agent_type, True
))
# Print what needs to be run
print("\n" + "="*50)
print("MISSING EXPERIMENTS")
print("="*50)
if not experiments_to_run:
print(" (none - all experiments complete!)")
print(f"\nNo new experiments to run! All {len(results_list)} experiments already completed.")
return results_list
for exp in experiments_to_run:
if len(exp) > 5 and exp[5]: # is_random flag
print(f" ✗ {exp[0]} agents, Random Baseline, {exp[2]}")
else:
print(f" ✗ {exp[0]} agents, {exp[1]}, {exp[2]}")
print(f"\nTotal missing: {len(experiments_to_run)} experiments")
# Run experiments with multiprocessing
if USE_MULTIPROCESSING and len(experiments_to_run) > 1:
print(f"\nRunning {len(experiments_to_run)} experiments with {NUM_PROCESSES} parallel processes...")
with mp.Pool(processes=NUM_PROCESSES, initializer=init_worker, initargs=(checkpoint_dir,)) as pool:
pool.map(run_single_experiment_worker, experiments_to_run)
else:
print(f"\nRunning {len(experiments_to_run)} experiments sequentially...")
for exp in experiments_to_run:
run_single_experiment_worker(exp)
# Reload results to include newly completed experiments
results_list = load_results()
print(f"\nAll experiments completed! Total results: {len(results_list)}")
return results_list
#===============================================================================
# Random Policy Simulation
#===============================================================================
def simulate_random_policy(num_agents, num_episodes, reward_type='ILF'):
"""Simulate agents with purely random policies and track metrics."""
print(f"Simulating random policy: {num_agents} agents, {num_episodes} episodes, {reward_type}...")
env = RandomPolicyEnvironment(num_agents, reward_type=reward_type)
# Initialize tracking variables
top_agents_per_episode = []
terminal_occurrences_per_episode = []
unique_winners_per_episode = []
round_counts = []
# Progress tracking
for _ in tqdm(range(num_episodes), desc="Random Episodes", ncols=100):
# Run episode
step_result = env.step()
# Track results
top_agents_per_episode.append(step_result['top_agents'])
terminal_occurrences_per_episode.append(step_result['terminal_occurrences'])
round_counts.append(step_result['rounds'])
# Track unique winners
if sum(step_result['top_agents']) == 1:
for i in range(num_agents):
if step_result['top_agents'][i] == 1:
unique_winners_per_episode.append(i)
break
# Update environment for next episode
env.update_for_next_episode()
# Calculate metrics
alt_metrics, beta_values, alt_computation_times = compute_alt_metrics(
num_episodes, num_agents, terminal_occurrences_per_episode, top_agents_per_episode
)
# RP metrics (calculated but not displayed for Paper 1)
rp_metrics = compute_rp_metrics(
num_episodes, num_agents, top_agents_per_episode
)
# Traditional metrics
efficiency_result = compute_efficiency(
num_episodes, num_agents, env.total_reward_per_agent, env.full_reward
)
reward_fairness_result = compute_reward_fairness(
env.total_reward_per_agent
)
tt_fairness_result = compute_tt_fairness(
top_agents_per_episode, num_agents, num_episodes
)
fairness_result = compute_fairness(
unique_winners_per_episode, num_agents
)
# Compile results
metrics = {
**alt_metrics,
'RP_avg': rp_metrics['RP_avg'],
'AWE_avg': rp_metrics['AWE_avg'],
'WPE_avg': rp_metrics['WPE_avg'],
'Efficiency': efficiency_result['Efficiency'],
'Reward_Fairness': reward_fairness_result['Reward_Fairness'],
'TT_Fairness': tt_fairness_result['TT_Fairness'],
'Fairness': fairness_result['Fairness']
}
# Add ALT Ratio calculations
result = {
'num_agents': num_agents,
'num_episodes': num_episodes,
'reward_type': reward_type,
'metrics': metrics,
'beta_values': beta_values,
'rp_values': {
'AWE_per_agent': rp_metrics['AWE_per_agent'],
'WPE_per_agent': rp_metrics['WPE_per_agent'],
'RP_per_agent': rp_metrics['RP_per_agent']
},
'is_random_baseline': True,
'avg_rounds': sum(round_counts) / len(round_counts) if round_counts else 0
}
# Calculate ALT Ratio
alt_ratios, estimated_agents, percentages = calculate_alt_ratio(result)
result['alt_ratios'] = alt_ratios
result['estimated_agents'] = estimated_agents
result['percentages'] = percentages
# Save results
success = save_result(result)
if success:
print(f"[OK] Completed random baseline: {num_agents} agents, {reward_type}")
else:
print(f"⚠ Completed but failed to save: {num_agents} agents, {reward_type}")
return result
#===============================================================================
# Q-Learning Experiments
#===============================================================================
def run_q_learning_experiment(num_agents, num_episodes, state_type='Type-A', reward_type='ILF', decay_schedule='linear'):
from datetime import datetime
def debug_log(msg):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[DEBUG {timestamp}] {msg}", flush=True)
debug_log(f"=" * 80)
debug_log(f"Q-LEARNING START: {num_agents} agents, {num_episodes} episodes, {state_type}, {reward_type}")
debug_log(f"=" * 80)
"""Run Q-learning experiment with tracking of metrics."""
print(f"Running Q-learning: {num_agents} agents, {num_episodes} episodes, {state_type}, {reward_type}...")
env = Environment(num_agents, num_positions=3, state_type=state_type, reward_type=reward_type)
# Calculate target episode for linear schedule
target_episode = int(env.epsilon_decay_target * num_episodes)
# Set decay for other code that accesses it
env.decay = (0.9 - env.epsilon_min) / target_episode
print(f"Will reach epsilon_min={env.epsilon_min} at episode {target_episode} (schedule: {decay_schedule})")
# Initialize tracking variables
top_agents_per_episode = []
terminal_occurrences_per_episode = []
unique_winners_per_episode = []
round_counts = []
epsilon_per_episode = [] # Track epsilon value per episode
# Progress bar for tracking
for episode in tqdm(range(num_episodes), desc="Episodes", ncols=100):
# Update current episode for Q-table tracking
env.current_episode = episode
if decay_schedule == 'exponential':
# Exponential epsilon decay
if env.epsilon > env.epsilon_min:
env.epsilon = max(env.epsilon_min, env.epsilon * env.decay)
else:
# Direct linear interpolation to epsilon_min by target_episode
if episode < target_episode:
progress = episode / target_episode # 0.0 to 1.0
env.epsilon = 0.9 - progress * (0.9 - env.epsilon_min)
else:
env.epsilon = env.epsilon_min
# Reset environment
env.reset()
episode_done = False
round_count = 0
max_rounds = 100 # Safety limit to prevent infinite loops
# Run episode
while not episode_done and round_count < max_rounds:
step_result = env.step()
episode_done = step_result['done']
round_count += 1
# Track results
top_agents_per_episode.append(step_result['top_agents'])
terminal_occurrences_per_episode.append(step_result['terminal_occurrences'])
round_counts.append(round_count)
epsilon_per_episode.append(env.epsilon) # Track epsilon value
# Track unique winners
if sum(step_result['top_agents']) == 1:
for i in range(num_agents):
if step_result['top_agents'][i] == 1:
unique_winners_per_episode.append(i)
break
# Update environment for next episode
env.update_for_next_episode()
# Print epsilon progress at 10% intervals
if episode > 0 and episode % (num_episodes // 10) == 0:
print(f"Episode {episode}/{num_episodes} ({episode/num_episodes:.1%}): epsilon = {env.epsilon:.4f}")
# Get total rewards
total_reward_per_agent = [agent.total_reward for agent in env.agents]
# Calculate comprehensive metrics using wrapper
result = calculate_all_metrics(
num_agents, num_episodes, state_type, reward_type,
top_agents_per_episode, terminal_occurrences_per_episode,
unique_winners_per_episode, total_reward_per_agent, epsilon_per_episode
)
# Add Q-table data for analysis
# Convert state_visits format from {(state, agent): data} to {agent: {state: count}}
state_visits_by_agent = {}
for (state_idx, agent_id), visit_data in env.state_visits.items():
if agent_id not in state_visits_by_agent:
state_visits_by_agent[agent_id] = {}
state_visits_by_agent[agent_id][state_idx] = visit_data['count']
result['state_visits'] = state_visits_by_agent
result['terminal_positions_log'] = env.terminal_positions_log
result['exclusive_wins_log'] = env.exclusive_wins_log
result['q_tables'] = {i: agent.Q for i, agent in enumerate(env.agents)}
result['avg_rounds'] = sum(round_counts) / len(round_counts) if round_counts else 0
# Calculate ALT Ratio
alt_ratios, estimated_agents, percentages = calculate_alt_ratio(result)
result['alt_ratios'] = alt_ratios
result['estimated_agents'] = estimated_agents
result['percentages'] = percentages
# Save results
success = save_result(result)
if success:
print(f"[OK] Completed Q-learning: {num_agents} agents, {state_type}, {reward_type}")
else:
print(f"⚠ Completed but failed to save: {num_agents} agents, {state_type}, {reward_type}")
return result
def run_adaptive_q_learning_experiment(num_agents, state_type='Type-A', reward_type='ILF',
alt_thresholds=None, patience=5000, max_episodes=150000):
"""Run Q-learning until ALT thresholds are met or max episodes reached."""
print(f"Running adaptive Q-learning: {num_agents} agents, {state_type}, {reward_type}...")
if alt_thresholds is None:
alt_thresholds = {
'CALT': 0.6,
'FALT': 0.7,
'EALT': 0.7,
'qFALT': 0.5,
'qEALT': 0.5,
'AALT': 0.4
}
env = Environment(num_agents, num_positions=3, state_type=state_type, reward_type=reward_type)
# Exponential decay for adaptive
env.decay = 0.9995
# Initialize tracking
top_agents_per_episode = []
terminal_occurrences_per_episode = []
unique_winners_per_episode = []
round_counts = []
epsilon_per_episode = []
converged = False
patience_counter = 0
episode = 0
print(f"Max episodes: {max_episodes}, Patience: {patience}")
print(f"Thresholds: {alt_thresholds}")
while episode < max_episodes and not converged:
# Update current episode
env.current_episode = episode
# Exponential epsilon decay
if env.epsilon > env.epsilon_min:
env.epsilon = max(env.epsilon_min, env.epsilon * env.decay)
# Run episode
env.reset()
episode_done = False
round_count = 0
max_rounds = 100
while not episode_done and round_count < max_rounds:
step_result = env.step()
episode_done = step_result['done']
round_count += 1
# Track results
top_agents_per_episode.append(step_result['top_agents'])
terminal_occurrences_per_episode.append(step_result['terminal_occurrences'])
round_counts.append(round_count)
epsilon_per_episode.append(env.epsilon)
if sum(step_result['top_agents']) == 1:
for i in range(num_agents):
if step_result['top_agents'][i] == 1:
unique_winners_per_episode.append(i)
break
env.update_for_next_episode()
episode += 1
# Check convergence every 1000 episodes
if episode % 1000 == 0 and episode >= num_agents:
alt_metrics, _, _ = compute_alt_metrics(
episode, num_agents, terminal_occurrences_per_episode, top_agents_per_episode
)
thresholds_met = all(alt_metrics[k] >= v for k, v in alt_thresholds.items())
if thresholds_met:
patience_counter += 1
if patience_counter >= patience:
converged = True
print(f"Converged at episode {episode}!")
else:
patience_counter = 0
if episode % 10000 == 0:
print(f"Episode {episode}: {alt_metrics}")
num_episodes = episode
print(f"Completed {num_episodes} episodes ({'converged' if converged else 'max reached'})")
# Get total rewards
total_reward_per_agent = [agent.total_reward for agent in env.agents]
# Calculate comprehensive metrics
result = calculate_all_metrics(
num_agents, num_episodes, state_type, reward_type,
top_agents_per_episode, terminal_occurrences_per_episode,
unique_winners_per_episode, total_reward_per_agent, epsilon_per_episode
)
# Add Q-table data
# Convert state_visits format from {(state, agent): data} to {agent: {state: count}}
state_visits_by_agent = {}
for (state_idx, agent_id), visit_data in env.state_visits.items():
if agent_id not in state_visits_by_agent:
state_visits_by_agent[agent_id] = {}
state_visits_by_agent[agent_id][state_idx] = visit_data['count']
result['state_visits'] = state_visits_by_agent
result['terminal_positions_log'] = env.terminal_positions_log
result['exclusive_wins_log'] = env.exclusive_wins_log
result['q_tables'] = {i: agent.Q for i, agent in enumerate(env.agents)}
result['avg_rounds'] = sum(round_counts) / len(round_counts) if round_counts else 0
result['converged'] = converged
# Calculate ALT Ratio
alt_ratios, estimated_agents, percentages = calculate_alt_ratio(result)
result['alt_ratios'] = alt_ratios
result['estimated_agents'] = estimated_agents
result['percentages'] = percentages
# Save results
success = save_result(result)
if success:
print(f"[OK] Completed adaptive Q-learning: {num_agents} agents, {state_type}, {reward_type}")
else:
print(f"⚠ Completed but failed to save: {num_agents} agents, {state_type}, {reward_type}")
return result
#===============================================================================
# DQN Experiments
#===============================================================================
def run_dqn_experiment(num_agents, num_episodes, state_type='Type-A', reward_type='ILF'):
"""Run DQN experiment with tracking of metrics."""
if not DQN_AVAILABLE:
raise ImportError("DQN not available - install PyTorch with: pip install torch")
print(f"Running DQN: {num_agents} agents, {num_episodes} episodes, {state_type}, {reward_type}...")
env = DQNEnvironment(num_agents, num_positions=3, state_type=state_type, reward_type=reward_type)
# Calculate target episode for epsilon decay
target_episode = int(env.epsilon_decay_target * num_episodes)
print(f"Will reach epsilon_min={env.epsilon_min} at episode {target_episode}")
# Initialize tracking variables
top_agents_per_episode = []
terminal_occurrences_per_episode = []
unique_winners_per_episode = []
round_counts = []
epsilon_per_episode = []
# Progress bar for tracking
for episode in tqdm(range(num_episodes), desc="Episodes", ncols=100):
# Linear epsilon decay
if episode < target_episode:
progress = episode / target_episode
env.epsilon = 0.9 - progress * (0.9 - env.epsilon_min)
else:
env.epsilon = env.epsilon_min
# Reset environment
state = env.reset()
positions, terminal_states = state
done = False
round_count = 0
max_rounds = 100
# Get initial state vectors for all agents
state_vectors = []
for agent in env.agents:
if state_type == 'Type-A':
state_vec = agent.encode_state_to_vector(positions)
else:
state_vec = agent.encode_state_to_vector(positions, terminal_states)
state_vectors.append(state_vec)
# Run episode
while not done and round_count < max_rounds:
# Select actions for all agents
actions = []
for i, agent in enumerate(env.agents):
action = agent.select_action(state_vectors[i], env.epsilon)
actions.append(action)
# Execute step
next_state, rewards, done = env.step(actions)
next_positions, next_terminal_states = next_state
# Get next state vectors
next_state_vectors = []
for agent in env.agents:
if state_type == 'Type-A':
next_state_vec = agent.encode_state_to_vector(next_positions)
else:
next_state_vec = agent.encode_state_to_vector(next_positions, next_terminal_states)
next_state_vectors.append(next_state_vec)
# Store transitions and train
for i, agent in enumerate(env.agents):
agent.store_transition(state_vectors[i], actions[i], rewards[i],
next_state_vectors[i], done)
agent.train_step()
agent.total_reward += rewards[i]
# Update state
state_vectors = next_state_vectors
positions = next_positions
terminal_states = next_terminal_states
round_count += 1
# Track results
top_agents_per_episode.append(env.current_top_agents)
terminal_occurrences_per_episode.append(sum(env.current_top_agents))
round_counts.append(round_count)
epsilon_per_episode.append(env.epsilon)
# Track unique winners
if sum(env.current_top_agents) == 1:
for i in range(num_agents):
if env.current_top_agents[i] == 1:
unique_winners_per_episode.append(i)
break
# Update environment for next episode (Type-B only)
env.update_for_next_episode()
# Print progress at 10% intervals
if episode > 0 and episode % (num_episodes // 10) == 0:
print(f"Episode {episode}/{num_episodes} ({episode/num_episodes:.1%}): epsilon = {env.epsilon:.4f}")
# Get total rewards
total_reward_per_agent = [agent.total_reward for agent in env.agents]
# Calculate comprehensive metrics using wrapper
result = calculate_all_metrics(
num_agents, num_episodes, state_type, reward_type,
top_agents_per_episode, terminal_occurrences_per_episode,