This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
bqre1p_agent.py
1302 lines (1139 loc) · 52.6 KB
/
bqre1p_agent.py
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
#
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
import collections
import copy
import dataclasses
import logging
import math
import time
from typing import ClassVar, Dict, List, Optional, Set, Tuple
import numpy as np
import torch
from conf import agents_cfgs
from fairdiplomacy import pydipcc
from fairdiplomacy.agents.base_agent import AgentState
from fairdiplomacy.agents.base_search_agent import (
SearchResult,
make_set_orders_dicts,
sample_orders_from_policy,
)
from fairdiplomacy.agents.bilateral_stats import BilateralStats
from fairdiplomacy.agents.searchbot_agent import SearchBotAgent, SearchBotAgentState, CFRData
from fairdiplomacy.agents.base_strategy_model_rollouts import RolloutResultsCache
from fairdiplomacy.agents.br_corr_bilateral_search import (
extract_bp_policy_for_powers,
compute_weights_for_opponent_joint_actions,
compute_best_action_against_reweighted_opponent_joint_actions,
sample_joint_actions,
filter_invalid_actions_from_policy,
rescore_bp_from_bilateral_views,
compute_payoff_matrix_for_all_opponents,
BRCorrBilateralSearchResult,
)
from fairdiplomacy.agents.base_strategy_model_wrapper import BaseStrategyModelWrapper
from fairdiplomacy.agents.plausible_order_sampling import PlausibleOrderSampler
from fairdiplomacy.models.consts import POWERS
from fairdiplomacy.typedefs import (
Action,
BilateralConditionalValueTable,
JointAction,
Phase,
PlausibleOrders,
PlayerType,
PlayerTypePolicies,
Policy,
Power,
PowerPolicies,
RolloutResults,
)
from fairdiplomacy.utils.timing_ctx import TimingCtx
from fairdiplomacy.utils.sampling import sample_p_dict
from fairdiplomacy.utils.agent_interruption import raise_if_should_stop
RescoringPolicyName = str
class BRMResult(SearchResult):
def __init__(
self,
bp_policies: PowerPolicies,
ptype_avg_policies: PlayerTypePolicies,
ptype_final_policies: PlayerTypePolicies,
beliefs: Dict[Power, np.ndarray],
agent_type: PlayerType,
use_final_iter: bool,
brm_data: Optional["BRMData"],
bilateral_stats: Optional[BilateralStats] = None,
):
self.bp_policies = bp_policies
self.ptype_avg_policies = ptype_avg_policies
self.ptype_final_policies = ptype_final_policies
self.brm_data = brm_data # type: ignore
# make sure we don't hold on to mutable belief arrays
self.beliefs = copy.deepcopy(beliefs)
self.agent_type = agent_type
self.use_final_iter = use_final_iter
self.bilateral_stats = bilateral_stats
def get_bp_policy(self) -> PowerPolicies:
return self.bp_policies
def get_agent_policy(self) -> PowerPolicies:
return self.ptype_avg_policies[self.agent_type]
def get_population_policy(self, power: Optional[Power] = None) -> PowerPolicies:
return belief_weighted_policy(self.beliefs, self.ptype_avg_policies, power)
def sample_action(self, power) -> Action:
ptype_policies = (
self.ptype_final_policies if self.use_final_iter else self.ptype_avg_policies
)
return sample_p_dict(ptype_policies[self.agent_type][power])
def avg_utility(self, pwr: Power) -> float:
assert self.brm_data is not None
return sum(
cfr_data.avg_utility(pwr) * self.beliefs[pwr][ptype]
for ptype, cfr_data in self.brm_data.type_cfr_data.items()
)
def avg_action_utility(self, pwr: Power, a: Action) -> float:
assert self.brm_data is not None
return sum(
cfr_data.avg_action_utility(pwr, a) * self.beliefs[pwr][ptype]
for ptype, cfr_data in self.brm_data.type_cfr_data.items()
)
def is_early_exit(self) -> bool:
return self.brm_data is None
def get_bilateral_stats(self) -> BilateralStats:
assert self.bilateral_stats is not None
return self.bilateral_stats
class BRMData:
def __init__(self, type_cfr_data: Dict[PlayerType, CFRData]):
self.type_cfr_data: Dict[PlayerType, CFRData] = type_cfr_data
def get_best_type_data(self):
return self.type_cfr_data[0]
def get_type_data(self, ptype: PlayerType):
return self.type_cfr_data[ptype]
class BeliefState:
def __init__(self, player_types: List[PlayerType]):
self.player_types = player_types
self.updated_phases: Set[str]
self.beliefs: Dict[Power, np.ndarray]
self.reset()
@property
def num_player_types(self):
return len(self.player_types)
def __str__(self):
return "\n".join(
[
f"{pwr}: {np.array2string(belief, precision=2, suppress_small=True)}\n" # type: ignore
for pwr, belief in self.beliefs.items()
]
)
def reset(self):
self.updated_phases = set()
self.beliefs = {
pwr: np.ones(self.num_player_types) / self.num_player_types for pwr in POWERS
}
def belief_weighted_policy(
beliefs: Dict[Power, np.ndarray],
ptype_policies: Dict[PlayerType, PowerPolicies],
power: Optional[Power],
) -> PowerPolicies:
ret = {}
for pwr in beliefs:
if power is not None and pwr != power:
continue
ret[pwr] = {}
for ptype, ptype_prob in enumerate(beliefs[pwr].tolist()):
ptype_policy = ptype_policies[ptype][pwr]
for action, action_prob in ptype_policy.items():
if action not in ret[pwr]:
ret[pwr][action] = 0
ret[pwr][action] += ptype_prob * action_prob
ret_sorted = {
pwr: dict(sorted(pi.items(), key=lambda ac_p: -ac_p[1])) for pwr, pi in ret.items()
}
return ret_sorted
class BayesAgentState(SearchBotAgentState):
def __init__(self, power: Power, player_types: List[PlayerType]):
super().__init__(power)
self.belief_state = BeliefState(player_types)
self.dynamic_lambda_scale_cache: Dict[Phase, Dict[Power, float]] = {}
@dataclasses.dataclass
class PlayerTypeSpec:
# To be used to identify blueprint policy.
BLUEPRINT_POLICY_NAME: ClassVar[RescoringPolicyName] = "BP"
qre_lambda: float
# Model path or BLUEPRINT_POLICY_NAME. Used to debug print and such.
policy_name: RescoringPolicyName
# Optional for BLUEPRINT_POLICY_NAME. Required otherwise.
rescore_policy_path: Optional[str]
# Arbitrary extra string to identify this type. The collection of all type should have unique names.
extra_tag: str = ""
@property
def name(self) -> str:
name = "{:}*{:.2e}".format(self.policy_name, self.qre_lambda)
if self.extra_tag:
name = f"{name}_{self.extra_tag}"
return name
def assert_valid(self) -> None:
try:
assert 0 <= self.qre_lambda
assert (self.policy_name == self.BLUEPRINT_POLICY_NAME) == (
self.rescore_policy_path is None
)
except AssertionError:
logging.error("Invalid spec: %s", self)
raise
def _make_bqre_data(
policies: Dict[RescoringPolicyName, PowerPolicies],
specs: Dict[PlayerType, PlayerTypeSpec],
qre: agents_cfgs.SearchBotAgent.QRE,
scale_lambdas: float,
pow_lambdas: float,
scale_lambdas_by_power: Dict[Power, float],
agent_power: Optional[Power],
) -> BRMData:
logging.info(f"Making BQRE data with scale_lambdas {scale_lambdas} pow_lambdas {pow_lambdas}")
return BRMData(
{
player_type: CFRData(
bp_policy=policies[spec.policy_name],
use_optimistic_cfr=False,
qre=agents_cfgs.SearchBotAgent.QRE(
**{
**qre.to_dict(),
"qre_lambda": math.pow(spec.qre_lambda, pow_lambdas) * scale_lambdas,
}
),
agent_power=agent_power,
scale_lambdas_by_power=scale_lambdas_by_power,
)
for player_type, spec in specs.items()
}
)
def _migrate_old_lambdas(cfg: agents_cfgs.BQRE1PAgent) -> agents_cfgs.BQRE1PAgent:
# Next 2 lines -> deep copy.
cfg_proto = type(cfg.to_editable())()
cfg_proto.CopyFrom(cfg.to_editable())
if cfg.player_types.which_player_types is not None:
assert (
cfg.lambda_min is None and cfg.lambda_multiplier is None
), "Do not specify lambda_min and lambda_multiplier when using player_types"
else:
assert cfg.lambda_min is not None and cfg.lambda_multiplier is not None
assert cfg.lambda_min > 0.0, "qre_lambda needs to be > 0"
assert (
cfg.lambda_multiplier > 0.0 and cfg.lambda_multiplier != 1.0
), "lambda_multiplier needs to be > 0 and not equal to 1"
cfg_proto.player_types.log_uniform.min_lambda = cfg.lambda_min * cfg.lambda_multiplier
assert cfg.num_player_types is not None
cfg_proto.player_types.log_uniform.max_lambda = cfg.lambda_min * (
cfg.lambda_multiplier ** cfg.num_player_types
)
cfg_proto.ClearField("lambda_min")
cfg_proto.ClearField("lambda_multiplier")
logging.info("Migrated lambda-player-type configuration:\n%s", cfg_proto)
return cfg_proto.to_frozen()
def _compute_player_specs(
cfg: agents_cfgs.BQRE1PAgent.PlayerTypes, num_player_types: int
) -> List[PlayerTypeSpec]:
assert num_player_types >= 1
if cfg.log_uniform is not None:
subcfg = cfg.log_uniform
assert subcfg.min_lambda is not None
assert subcfg.max_lambda is not None
num_policies = max(1, len(subcfg.policies))
assert (num_player_types - int(subcfg.include_zero_lambda)) % num_policies == 0, (
"num_player_types should account for the zero type and all policies",
)
num_lambdas = (num_player_types - int(subcfg.include_zero_lambda)) // num_policies
if num_lambdas == 0:
lambdas = []
elif num_lambdas == 1:
assert subcfg.max_lambda == subcfg.min_lambda
lambdas = [subcfg.min_lambda]
else:
multiplier = (subcfg.max_lambda / subcfg.min_lambda) ** (1.0 / (num_lambdas - 1))
# This slightly weird way of computing the lambda is to maintain exact consistency
# with earlier behavior
lambdas = [
float(np.float32(subcfg.min_lambda / multiplier)) * (multiplier ** x)
for x in range(1, num_lambdas + 1)
]
specs = []
if subcfg.include_zero_lambda:
# We don't care wichh policy to user for lambda=0, as we will ignore
# it anyways. So we just use the BP.
specs.append(
PlayerTypeSpec(
qre_lambda=0.0,
policy_name=PlayerTypeSpec.BLUEPRINT_POLICY_NAME,
rescore_policy_path=None,
)
)
if not subcfg.policies:
# Using "blueprint" policy.
specs.extend(
PlayerTypeSpec(
qre_lambda=qre_lambda,
policy_name=PlayerTypeSpec.BLUEPRINT_POLICY_NAME,
rescore_policy_path=None,
)
for qre_lambda in lambdas
)
else:
for policy in subcfg.policies:
if policy.model_path is None:
assert policy.name is None
specs.extend(
PlayerTypeSpec(
qre_lambda=qre_lambda,
policy_name=PlayerTypeSpec.BLUEPRINT_POLICY_NAME,
rescore_policy_path=None,
)
for qre_lambda in lambdas
)
else:
assert policy.name is not None
specs.extend(
PlayerTypeSpec(
qre_lambda=qre_lambda,
policy_name=policy.name,
rescore_policy_path=policy.model_path,
)
for qre_lambda in lambdas
)
else:
raise RuntimeError("Unknown player types")
assert specs, "No player types!"
# Names must be unique.
[[most_common_name, freq]] = collections.Counter(x.name for x in specs).most_common(1)
assert freq == 1, f"Name duplication for player type: {most_common_name}"
return specs
class BQRE1PAgent(SearchBotAgent):
"""One-ply bayes qre rm with base_strategy_model-policy rollouts"""
def __init__(self, cfg: agents_cfgs.BQRE1PAgent, *, skip_base_strategy_model_cache=False):
################# refactored from base class ###########################
super().__init__(
cfg.base_searchbot_cfg, skip_base_strategy_model_cache=skip_base_strategy_model_cache
)
assert cfg.num_player_types is not None
self.num_player_types = cfg.num_player_types
assert cfg.agent_type is not None
# The rest of the code uses 0-based indexing of the types. This should
# be the only place we convert 1-based to 0-based.
self.agent_type = cfg.agent_type - 1
self.player_types = list(range(self.num_player_types))
self.agent_type_is_public = cfg.agent_type_is_public
assert self.exploited_agent_power is None, "exploited agent power is not supported"
##########################################################################
self.qre_type2spec: Dict[PlayerType, PlayerTypeSpec] = {}
self.qre_extra_rescoring_models: Dict[RescoringPolicyName, PlausibleOrderSampler] = {}
self.pow_lambdas_1901 = cfg.pow_lambdas_1901
self.scale_lambdas_1901 = cfg.scale_lambdas_1901
self.pow_lambdas_1901_spring = cfg.pow_lambdas_1901_spring
self.scale_lambdas_1901_spring = cfg.scale_lambdas_1901_spring
self.dynamic_lambda_stdev_espilon = cfg.dynamic_lambda_stdev_espilon
self.dynamic_lambda_stdev_baseline = cfg.dynamic_lambda_stdev_baseline
self.dynamic_lambda_stdev_num_samples = cfg.dynamic_lambda_stdev_num_samples
if self.dynamic_lambda_stdev_num_samples > 0:
assert (
self.dynamic_lambda_stdev_espilon is not None
and self.dynamic_lambda_stdev_espilon > 0
), "Must specify self.dynamic_lambda_stdev_epsilon if using dynamic lambda"
assert (
self.dynamic_lambda_stdev_baseline is not None
and self.dynamic_lambda_stdev_baseline > 0
), "Must specify self.dynamic_lambda_stdev_baseline if using dynamic lambda"
cfg = _migrate_old_lambdas(cfg)
self.set_player_type_spec(_compute_player_specs(cfg.player_types, len(self.player_types)))
if cfg.base_searchbot_cfg.qre.target_pi != "BLUEPRINT":
assert cfg.base_searchbot_cfg.qre.target_pi == "UNIFORM", cfg.base_searchbot_cfg.qre
assert all(
spec.rescore_policy_path is None for spec in self.qre_type2spec.values()
), "When target_pi is uniform extra policies are not supported"
logging.info(
"Performing bayes quantal response equilibrium hedge with different player types:\n%s",
"\n".join(
f"{i}: {spec.name} {spec}" for i, spec in enumerate(self.qre_type2spec.values())
),
)
self.log_intermediate_iterations = cfg.base_searchbot_cfg.log_intermediate_iterations
assert self.qre is not None, "base_searchbot_cfg qre needs to be set."
logging.info(f"Initialized BQRE1P Agent: {self.__dict__}")
def initialize_state(self, power: Power) -> AgentState:
state = BayesAgentState(power=power, player_types=self.player_types)
if self.agent_type_is_public:
pure_type_belief = np.zeros(self.num_player_types)
pure_type_belief[self.agent_type] = 1
state.belief_state.beliefs[power] = pure_type_belief[:]
return state
def get_exploited_agent_power(self) -> Optional[Power]:
assert self.exploited_agent is None
return None
def set_player_type_spec(
self,
specs: List[PlayerTypeSpec],
*,
skip_base_strategy_model_cache=False,
allow_reuse_model=True,
) -> None:
"""(Re-)initializes type configuration from a list of specs."""
assert len(self.player_types) == len(specs), (len(self.player_types), len(specs))
base_strategy_model_wrapper_kwargs = dict(
device=self.base_strategy_model.device,
max_batch_size=self.base_strategy_model.max_batch_size,
half_precision=self.base_strategy_model.half_precision,
)
self.qre_type2spec = dict(zip(self.player_types, specs))
self.qre_extra_rescoring_models, old_qre_extra_rescoring_models = (
{},
self.qre_extra_rescoring_models,
)
for spec in self.qre_type2spec.values():
spec.assert_valid()
if (
spec.rescore_policy_path is not None
and spec.rescore_policy_path not in self.qre_extra_rescoring_models
):
if (
spec.rescore_policy_path in old_qre_extra_rescoring_models
and allow_reuse_model
):
self.qre_extra_rescoring_models[
spec.policy_name
] = old_qre_extra_rescoring_models[spec.rescore_policy_path]
else:
base_strategy_model = BaseStrategyModelWrapper(
spec.rescore_policy_path, **base_strategy_model_wrapper_kwargs
)
assert self.parlai_model_orders is None
self.qre_extra_rescoring_models[spec.policy_name] = PlausibleOrderSampler(
self.order_sampler.cfg, base_strategy_model=base_strategy_model
)
def get_player_type_strings(self) -> List[str]:
return [self.qre_type2spec[x].name for x in self.player_types]
def _get_scale_pow_lambdas(self, game: pydipcc.Game) -> Tuple[float, float]:
scale_lambdas = 1.0
pow_lambdas = 1.0
if game.current_year == 1901:
if self.scale_lambdas_1901 is not None:
scale_lambdas = self.scale_lambdas_1901
if self.pow_lambdas_1901 is not None:
pow_lambdas = self.pow_lambdas_1901
if game.current_short_phase == "S1901M":
if self.scale_lambdas_1901_spring is not None:
scale_lambdas = self.scale_lambdas_1901_spring
if self.pow_lambdas_1901_spring is not None:
pow_lambdas = self.pow_lambdas_1901_spring
return (scale_lambdas, pow_lambdas)
def _get_dynamic_lambda_scale(
self, game: pydipcc.Game, agent_power: Optional[Power], agent_state: Optional[AgentState]
) -> Dict[Power, float]:
if not self.dynamic_lambda_stdev_num_samples:
return {power: 1.0 for power in POWERS}
assert agent_state is not None, "If using dynamic lambda, agent_state should not be None"
assert self.dynamic_lambda_stdev_espilon is not None
assert self.dynamic_lambda_stdev_baseline is not None
phase = game.get_current_phase()
assert isinstance(agent_state, BayesAgentState)
if phase not in agent_state.dynamic_lambda_scale_cache:
# Rollout *through* exactly one set of movement phase orders.
override_max_rollout_length = 1 if phase.endswith("M") else 2
# Shape [len(set_orders_dicts), num_powers, num_value_functions].
rollout_results = self.base_strategy_model_rollouts.do_rollouts_multi(
game,
agent_power=agent_power,
set_orders_dicts=[{}] * self.dynamic_lambda_stdev_num_samples,
override_max_rollout_length=override_max_rollout_length,
)
rollout_results = rollout_results.squeeze(2)
means_by_power = torch.mean(rollout_results, dim=0)
variances_by_power = torch.mean(
torch.square(rollout_results - means_by_power.unsqueeze(0)), dim=0
)
means_by_power_list = means_by_power.cpu().tolist()
variances_by_power_list = variances_by_power.cpu().tolist()
assert len(means_by_power_list) == len(POWERS)
assert len(variances_by_power_list) == len(POWERS)
epsilon = self.dynamic_lambda_stdev_espilon
stdevs_by_power_list = [
math.sqrt(variance + epsilon * epsilon) for variance in variances_by_power_list
]
logging.info(
f"Dynamic lambda: power means: {list(zip(POWERS,['%.3f' % x for x in means_by_power_list]))}"
)
logging.info(
f"Dynamic lambda: power stdevs (after epsilon {epsilon}): {list(zip(POWERS,['%.3f' % x for x in stdevs_by_power_list]))}"
)
dynamic_lambda_scale_by_power_list = [
stdev / self.dynamic_lambda_stdev_baseline for stdev in stdevs_by_power_list
]
dynamic_lambda_scale_by_power = dict(zip(POWERS, dynamic_lambda_scale_by_power_list))
agent_state.dynamic_lambda_scale_cache[phase] = dynamic_lambda_scale_by_power
logging.info(
f"Dynamic lambda final scale: {[(power,'%.3f' % x) for (power,x) in agent_state.dynamic_lambda_scale_cache[phase].items()]}"
)
return agent_state.dynamic_lambda_scale_cache[phase]
def _handle_extra_plausible_and_rescoring(
self,
game: pydipcc.Game,
bp_policy: PowerPolicies,
extra_plausible_orders: Optional[PlausibleOrders],
agent_power: Optional[Power],
):
if extra_plausible_orders:
for pwr, policy in extra_plausible_orders.items():
for action in policy:
if action not in bp_policy[pwr]:
logging.info(f"Adding extra plausible orders {pwr}: {action}")
bp_policy[pwr][action] = 0.0
bp_policy = self.order_sampler.rescore_actions(
game, has_press=self.has_press, agent_power=agent_power, input_policy=bp_policy
)
biasing_policies = {PlayerTypeSpec.BLUEPRINT_POLICY_NAME: bp_policy}
for name, sampler in self.qre_extra_rescoring_models.items():
logging.info("Querying %s", name)
biasing_policies[name] = sampler.rescore_actions(
game, has_press=self.has_press, agent_power=agent_power, input_policy=bp_policy
)
return bp_policy, biasing_policies
def run_search(
self,
game: pydipcc.Game,
*,
bp_policy: Optional[PowerPolicies] = None,
early_exit_for_power: Optional[Power] = None,
timings: Optional[TimingCtx] = None,
extra_plausible_orders: Optional[PlausibleOrders] = None,
agent_power: Optional[Power] = None,
agent_state: Optional[AgentState],
rollout_results_cache: Optional[RolloutResultsCache] = None,
pre_rescored_bp: Optional[PowerPolicies] = None,
) -> BRMResult:
"""Same as get_all_power_prob_distributions but also returns the stats about the bcfr"""
# If there are no locations to order, bail
if early_exit_for_power and len(game.get_orderable_locations()[early_exit_for_power]) == 0:
if agent_power is not None:
assert early_exit_for_power == agent_power
return self._early_quit_bcfr_result(
power=early_exit_for_power, agent_state=agent_state
)
if timings is None:
timings = TimingCtx()
timings.start("one-time")
deadline: Optional[float] = (
time.monotonic() + self.max_seconds if self.max_seconds > 0 else None
)
if agent_state is not None:
assert isinstance(agent_state, BayesAgentState)
belief_state = agent_state.belief_state
else:
belief_state = BeliefState(self.player_types)
logging.info(f"BEGINNING BQRE run_search, agent_power={agent_power}")
if rollout_results_cache is not None:
maybe_rollout_results_cache = rollout_results_cache
else:
maybe_rollout_results_cache = (
self.base_strategy_model_rollouts.build_cache()
if self.cache_rollout_results
else None
)
# Compute blueprint policy for each of the different types
with timings.create_subcontext() as sub_timings, sub_timings("get_plausible_order"):
if bp_policy is None:
bp_policy = self.get_plausible_orders_policy(
game, agent_power=agent_power, agent_state=agent_state
)
bp_policy, biasing_policies = self._handle_extra_plausible_and_rescoring(
game, bp_policy, extra_plausible_orders, agent_power
)
scale_lambdas, pow_lambdas = self._get_scale_pow_lambdas(game)
scale_lambdas_by_power = self._get_dynamic_lambda_scale(game, agent_power, agent_state)
bqre_data = _make_bqre_data(
biasing_policies,
specs=self.qre_type2spec,
qre=self.qre,
pow_lambdas=pow_lambdas,
scale_lambdas=scale_lambdas,
scale_lambdas_by_power=scale_lambdas_by_power,
agent_power=agent_power,
)
if agent_power is not None:
bilateral_stats = BilateralStats(
game, agent_power, bqre_data.get_best_type_data().power_plausible_orders
)
else:
bilateral_stats = None
power_is_loser = {} # make typechecker happy
last_search_iter = False
for bqre_iter in range(self.n_rollouts):
if last_search_iter:
logging.info(f"Early exit from BCFR after {bqre_iter} iterations by timeout")
break
elif deadline is not None and time.monotonic() >= deadline:
last_search_iter = True
timings.start("start")
# do verbose logging on 2^x iters
verbose_log_iter = self.is_verbose_log_iter(bqre_iter) or last_search_iter
# Sample player types
ptypes = {
pwr: np.random.choice(self.player_types, 1, p=belief_state.beliefs[pwr])[
0
] # type:ignore
for pwr in POWERS
}
timings.start("query_policy")
# check if the *best* type is a loser
power_is_loser = self.get_power_loser_dict(bqre_data.get_best_type_data(), bqre_iter)
ptype_power_action_ps = {
ptype: self.get_cur_iter_strategies(cfr_data, bqre_iter)
for ptype, cfr_data in bqre_data.type_cfr_data.items()
}
power_action_ps = {
pwr: ptype_power_action_ps[ptype][pwr] for pwr, ptype in ptypes.items()
}
timings.start("apply_orders")
# sample policy for all powers
plausible_orders = {
pwr: bqre_data.get_type_data(ptype).power_plausible_orders[pwr]
for pwr, ptype in ptypes.items()
}
idxs, power_sampled_orders = sample_orders_from_policy(
plausible_orders, power_action_ps
)
if bilateral_stats is not None:
bilateral_stats.accum_bilateral_probs(power_sampled_orders, weight=bqre_iter)
set_orders_dicts = make_set_orders_dicts(plausible_orders, power_sampled_orders)
timings.stop()
all_set_orders_dicts = list(set_orders_dicts)
with timings.create_subcontext("rollout") as inner_timings, inner_timings("rollout"):
all_rollout_results_tensor = self.base_strategy_model_rollouts.do_rollouts_multi_maybe_cached(
game,
agent_power=agent_power,
set_orders_dicts=set_orders_dicts,
cache=maybe_rollout_results_cache,
timings=inner_timings,
)
timings.start("bcfr")
for pwr, actions in plausible_orders.items():
# pop this power and player type's results
scores, all_rollout_results_tensor = (
all_rollout_results_tensor[: len(actions)],
all_rollout_results_tensor[len(actions) :],
)
pwr_set_order_dicts, all_set_orders_dicts = (
all_set_orders_dicts[: len(actions)],
all_set_orders_dicts[len(actions) :],
)
# Results for the first value function as RolloutResults.
default_results: RolloutResults = [
(orders, dict(zip(POWERS, scores[..., 0].tolist())))
for orders, scores in zip(pwr_set_order_dicts, scores)
]
if bilateral_stats is not None:
bilateral_stats.accum_bilateral_values(pwr, bqre_iter, default_results)
# logging.info(f"Results {pwr} = {results}")
# calculate regrets
# Shape: [len(pwr_set_order_dicts), num value functions].
all_action_utilities = scores[:, POWERS.index(pwr)]
for cur_ptype in self.player_types:
spec = self.qre_type2spec[cur_ptype]
action_utilities = all_action_utilities[..., 0].tolist()
cfr_data = bqre_data.get_type_data(cur_ptype)
state_utility: float = np.dot(
ptype_power_action_ps[cur_ptype][pwr], action_utilities
) # type: ignore
# update bcfr data structures for particular player type and power
cfr_data.update(
pwr=pwr,
actions=actions,
state_utility=state_utility,
action_utilities=action_utilities,
which_strategy_to_accumulate=pydipcc.CFRStats.ACCUMULATE_PREV_ITER,
cfr_iter=bqre_iter,
)
# log some action values
if verbose_log_iter and cur_ptype == self.player_types[0] and actions[0] != ():
self.log_bcfr_iter_state(
game=game,
pwr=pwr,
actions=actions,
brm_data=bqre_data,
brm_iter=bqre_iter,
state_utility=state_utility,
action_utilities=action_utilities,
power_sampled_orders=power_sampled_orders,
beliefs=belief_state.beliefs[pwr],
pre_rescore_bp=pre_rescored_bp[pwr] if pre_rescored_bp else None,
)
if maybe_rollout_results_cache is not None:
logging.info(f"{maybe_rollout_results_cache}")
assert (
all_rollout_results_tensor.shape[0] == 0
), "all_rollout_results_tensor should be empty"
timings.start("to_dict")
# return prob. distributions for each power, player type
ptype_avg_ret, ptype_final_ret = {}, {}
power_is_loser = self.get_power_loser_dict(bqre_data.get_best_type_data(), self.n_rollouts)
for player_type in self.player_types:
avg_ret, final_ret = {}, {}
cfr_data = bqre_data.get_type_data(player_type)
for p in POWERS:
if power_is_loser[p] or p == self.exploited_agent_power:
avg_ret[p] = final_ret[p] = cfr_data.bp_policy(p)
else:
avg_ret[p] = cfr_data.avg_policy(p)
final_ret[p] = cfr_data.cur_iter_policy(p)
ptype_avg_ret[player_type] = avg_ret
ptype_final_ret[player_type] = final_ret
logging.info(
"Raw Values: %s",
{
p: f"{x:.3f}"
for p, x in zip(
POWERS,
self.base_strategy_model.get_values(
game, has_press=self.has_press, agent_power=agent_power
),
)
},
)
logging.info(
"BQRE Values (Agent Type: %s): %s",
self.agent_type,
{p: f"{bqre_data.get_type_data(self.agent_type).avg_utility(p):.3f}" for p in POWERS},
)
timings.stop()
timings.pprint(logging.getLogger("timings").info)
if bilateral_stats is not None:
cfr_data = bqre_data.get_best_type_data()
if self.log_bilateral_values:
bilateral_stats.log(cfr_data, min_order_prob=self.bilateral_cfg.min_order_prob)
# Update bcfr result cache
bcfr_result = BRMResult(
bp_policies=bp_policy,
ptype_avg_policies=ptype_avg_ret,
ptype_final_policies=ptype_final_ret,
brm_data=bqre_data,
beliefs=belief_state.beliefs,
agent_type=self.agent_type,
use_final_iter=self.use_final_iter,
bilateral_stats=bilateral_stats,
)
return bcfr_result
def run_bilateral_search_with_conditional_evs(
self,
game: pydipcc.Game,
*,
bp_policy: PowerPolicies,
early_exit_for_power: Optional[Power] = None,
timings: Optional[TimingCtx] = None,
extra_plausible_orders: Optional[PlausibleOrders] = None,
agent_power: Power,
other_power: Power,
agent_state: Optional[AgentState],
conditional_evs: BilateralConditionalValueTable,
pre_rescored_bp: Optional[PowerPolicies] = None,
) -> BRMResult:
"""Same as run_search but with all conditional evs precomputed, and specialized to 2 players.
This function is mostly a copypasta of run_search for performance, where a lot of options
have been removed and the specialization to 2 players allows computing things much faster.
"""
# If there are no locations to order, bail
if early_exit_for_power and len(game.get_orderable_locations()[early_exit_for_power]) == 0:
assert early_exit_for_power == agent_power
return self._early_quit_bcfr_result(
power=early_exit_for_power, agent_state=agent_state
)
if timings is None:
timings = TimingCtx()
timings.start("rswce_one-time")
assert self.loser_bp_value <= 0
assert self.exploited_agent_power is None
if agent_state is not None:
assert isinstance(agent_state, BayesAgentState)
belief_state = agent_state.belief_state
else:
belief_state = BeliefState(self.player_types)
logging.info(f"BEGINNING BQRE run_search_with_conditional_evs, agent_power={agent_power}")
# Compute blueprint policy for each of the different types
with timings.create_subcontext() as sub_timings, sub_timings("rswce_get_plausible_order"):
bp_policy, biasing_policies = self._handle_extra_plausible_and_rescoring(
game, bp_policy, extra_plausible_orders, agent_power
)
scale_lambdas, pow_lambdas = self._get_scale_pow_lambdas(game)
scale_lambdas_by_power = self._get_dynamic_lambda_scale(game, agent_power, agent_state)
bqre_data = _make_bqre_data(
biasing_policies,
specs=self.qre_type2spec,
qre=self.qre,
pow_lambdas=pow_lambdas,
scale_lambdas=scale_lambdas,
scale_lambdas_by_power=scale_lambdas_by_power,
agent_power=agent_power,
)
for bqre_iter in range(self.n_rollouts):
timings.start("rswce_start")
# do verbose logging on 2^x iters
verbose_log_iter = self.is_verbose_log_iter(bqre_iter)
# Sample player types
# Written as an iteration over POWERS specifically to preserve the ordering
# of the items within the dictionary to be the same order as POWERS, which
# affects the ordering of many downstream dictionaries and the order in which
# our logs will record things.
ptypes = {
pwr: np.random.choice(self.player_types, 1, p=belief_state.beliefs[pwr])[
0
] # type:ignore
for pwr in POWERS
if pwr in (agent_power, other_power)
}
timings.start("rswce_query_policy")
ptype_power_action_ps = {
ptype: self.get_cur_iter_strategies(cfr_data, bqre_iter)
for ptype, cfr_data in bqre_data.type_cfr_data.items()
}
power_action_ps = {
pwr: ptype_power_action_ps[ptype][pwr] for pwr, ptype in ptypes.items()
}
timings.start("rswce_apply_orders")
# sample policy for all powers
plausible_orders = {
pwr: bqre_data.get_type_data(ptype).power_plausible_orders[pwr]
for pwr, ptype in ptypes.items()
}
_idxs, power_sampled_orders = sample_orders_from_policy(
plausible_orders, power_action_ps
)
timings.stop()
timings.start("rswce_bcfr")
for pwr, actions in plausible_orders.items():
if pwr == agent_power:
other_sampled_action = power_sampled_orders[other_power]
scores_list = [
conditional_evs[(agent_action, other_sampled_action)]
for agent_action in actions
]
else:
agent_sampled_action = power_sampled_orders[agent_power]
scores_list = [
conditional_evs[(agent_sampled_action, other_action)]
for other_action in actions
]
# scores_list is a list of tensors of shape (7,), indicating the value estimate for all 7 players
# conditional on each plausible action of pwr and the sampled action of the other power besides pwr.
# Stack into an (N,7) tensor.
scores = torch.stack(scores_list, 0)
# logging.info(f"Results {pwr} = {results}")
# calculate regrets
# Shape: [len(pwr_set_order_dicts), num value functions].
all_action_utilities = scores[:, POWERS.index(pwr)]
for cur_ptype in self.player_types:
spec = self.qre_type2spec[cur_ptype]
action_utilities = all_action_utilities[..., 0].tolist()
cfr_data = bqre_data.get_type_data(cur_ptype)
state_utility: float = np.dot(
ptype_power_action_ps[cur_ptype][pwr], action_utilities
) # type: ignore
# update bcfr data structures for particular player type and power
cfr_data.update(
pwr=pwr,
actions=actions,
state_utility=state_utility,
action_utilities=action_utilities,
which_strategy_to_accumulate=pydipcc.CFRStats.ACCUMULATE_PREV_ITER,
cfr_iter=bqre_iter,
)
# log some action values
if verbose_log_iter and cur_ptype == self.player_types[0] and actions[0] != ():
self.log_bcfr_iter_state(
game=game,
pwr=pwr,
actions=actions,
brm_data=bqre_data,
brm_iter=bqre_iter,
state_utility=state_utility,
action_utilities=action_utilities,
power_sampled_orders=power_sampled_orders,
beliefs=belief_state.beliefs[pwr],
pre_rescore_bp=pre_rescored_bp[pwr] if pre_rescored_bp else None,
)
timings.start("rswce_to_dict")
# return prob. distributions for each power, player type
ptype_avg_ret, ptype_final_ret = {}, {}
for player_type in self.player_types:
avg_ret, final_ret = {}, {}
cfr_data = bqre_data.get_type_data(player_type)
for p in POWERS: