This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecise_patch.diff
More file actions
290 lines (288 loc) · 13.8 KB
/
precise_patch.diff
File metadata and controls
290 lines (288 loc) · 13.8 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
--- a/research/world_model/src/gitcg_world_model/action_adapter.py
+++ b/research/world_model/src/gitcg_world_model/action_adapter.py
@@ -8,5 +8,5 @@
full_state_json: str | None,
step_index: int,
metadata: dict[str, Any] | None = None,
- enable_result_based_action_relabel: bool = True,
+ enable_result_based_action_relabel: bool = False,
) -> BuiltDecisionContext:
--- a/research/world_model/src/gitcg_world_model/public_state.py
+++ b/research/world_model/src/gitcg_world_model/public_state.py
@@ -1,22 +1,27 @@
round_number = int(step.player_view.round_number) if step.player_view is not None else 0
phase = str(step.player_view.phase) if step.player_view is not None else ""
+ semantic_key = None
+ option_kind = selected_spec.kind.value if selected_spec is not None else None
+ used_dice_count = 0
+ if selected_spec is not None:
+ semantic_key = semantic_action_key_for_spec(selected_spec)
+ used_dice_count = public_spent_dice_for_spec(selected_spec)
+ elif action_code is not None:
+ try:
+ semantic_key = semantic_action_key_for_code(action_code)
+ used_dice_count = public_spent_dice_for_code(action_code)
+ except KeyError:
+ semantic_key = None
+ used_dice_count = 0
record = PublicActionRecord(
acting_player=int(step.acting_player),
request_type=step.request_type,
round_number=round_number,
phase=phase,
payload=payload,
- semantic_key=(
- semantic_action_key_for_spec(selected_spec)
- if selected_spec is not None
- else (semantic_action_key_for_code(action_code) if action_code is not None else None)
- ),
- option_kind=(selected_spec.kind.value if selected_spec is not None else None),
- used_dice_count=(
- public_spent_dice_for_spec(selected_spec)
- if selected_spec is not None
- else (public_spent_dice_for_code(action_code) if action_code is not None else 0)
- ),
+ semantic_key=semantic_key,
+ option_kind=option_kind,
+ used_dice_count=used_dice_count,
known_payload=known_payload,
selected_legal_index=selected_legal_index,
legal_option_count=len(step.legal_low_level_codes),
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -34,5 +34,17 @@
)
+def _legal_option_kinds(context: DecisionContext) -> tuple[OptionKind, ...]:
+ if context.legal_low_level_specs:
+ return tuple(spec.kind for spec in context.legal_low_level_specs)
+ kinds: list[OptionKind] = []
+ for action_code in context.legal_low_level_codes:
+ try:
+ kinds.append(low_level_kind_for_code(int(action_code)))
+ except KeyError:
+ continue
+ return tuple(kinds)
+
+
def step_search_context_summary(context: DecisionContext) -> tuple[float, ...]:
visible_state = context.player_view
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -11,17 +11,15 @@
return False
legal_count = len(context.legal_low_level_codes)
round_number = int(context.player_view.round_number)
- has_end = any(
- low_level_kind_for_code(int(action_code)) == OptionKind.ACTION_DECLARE_END
- for action_code in context.legal_low_level_codes
- )
+ legal_kinds = _legal_option_kinds(context)
+ has_end = any(kind == OptionKind.ACTION_DECLARE_END for kind in legal_kinds)
productive_non_end = any(
- low_level_kind_for_code(int(action_code)) in {
+ kind in {
OptionKind.ACTION_USE_SKILL,
OptionKind.ACTION_PLAY_CARD,
OptionKind.ACTION_ELEMENTAL_TUNING,
}
- for action_code in context.legal_low_level_codes
+ for kind in legal_kinds
)
if round_number <= 3:
return legal_count >= 6 or (has_end and productive_non_end)
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -3,14 +3,12 @@
round_number = float(getattr(visible_state, "round_number", 0))
phase = str(getattr(visible_state, "phase", ""))
legal_count = float(len(context.legal_low_level_codes))
- has_end = any(
- low_level_kind_for_code(int(action_code)) == OptionKind.ACTION_DECLARE_END
- for action_code in context.legal_low_level_codes
- )
+ legal_kinds = _legal_option_kinds(context)
+ has_end = any(kind == OptionKind.ACTION_DECLARE_END for kind in legal_kinds)
productive = sum(
1
- for action_code in context.legal_low_level_codes
- if low_level_kind_for_code(int(action_code)) in {
+ for kind in legal_kinds
+ if kind in {
OptionKind.ACTION_USE_SKILL,
OptionKind.ACTION_PLAY_CARD,
OptionKind.ACTION_ELEMENTAL_TUNING,
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -12,25 +12,47 @@
) -> _RangeUpdateResult:
if time.perf_counter() > deadline:
raise _SearchTimeout()
- cache_key = (
- _public_belief_cache_key(pre_public_belief),
- semantic_action_key_for_code(selected_action_code),
- _hypothesis_cache_key(sampled_self_hypothesis),
+ public_state = mask_state_for_player(post_state, perspective_player=pre_public_belief.root_player)
+ next_opponent_public_dice_count = tracker.opponent_public_dice_count_from_state(public_state)
+ next_opponent_public_hand_count = tracker.opponent_public_hand_count_from_state(public_state)
+ next_revealed = tuple(int(value) for value in tracker.revealed_opponent_card_definition_ids())
+ should_rebuild_opponent_range = (
+ not math.isclose(
+ float(next_opponent_public_dice_count),
+ float(pre_public_belief.opponent_public_dice_count),
+ rel_tol=0.0,
+ abs_tol=1.0e-6,
+ )
+ or not math.isclose(
+ float(next_opponent_public_hand_count),
+ float(pre_public_belief.opponent_public_hand_count),
+ rel_tol=0.0,
+ abs_tol=1.0e-6,
+ )
+ or next_revealed != tuple(int(value) for value in pre_public_belief.revealed_opponent_card_definition_ids)
)
- updated_opponent_range = self._advance_cache.get(cache_key)
- if updated_opponent_range is None:
- updated_opponent_range = self._advance_opponent_range(
- pre_public_belief=pre_public_belief,
- selected_action_code=selected_action_code,
- sampled_self_hypothesis=sampled_self_hypothesis,
- sampled_opponent_hypothesis=sampled_opponent_hypothesis,
- deadline=deadline,
+ if should_rebuild_opponent_range:
+ cache_key = (
+ _public_belief_cache_key(pre_public_belief),
+ semantic_action_key_for_code(selected_action_code),
+ _hypothesis_cache_key(sampled_self_hypothesis),
+ _hypothesis_cache_key(sampled_opponent_hypothesis),
)
- if updated_opponent_range.count > 0:
- self._advance_cache[cache_key] = updated_opponent_range
- if len(self._advance_cache) > 256:
- self._advance_cache.clear()
- public_state = mask_state_for_player(post_state, perspective_player=pre_public_belief.root_player)
+ updated_opponent_range = self._advance_cache.get(cache_key)
+ if updated_opponent_range is None:
+ updated_opponent_range = self._advance_opponent_range(
+ pre_public_belief=pre_public_belief,
+ selected_action_code=selected_action_code,
+ sampled_self_hypothesis=sampled_self_hypothesis,
+ sampled_opponent_hypothesis=sampled_opponent_hypothesis,
+ deadline=deadline,
+ )
+ if updated_opponent_range.count > 0:
+ self._advance_cache[cache_key] = updated_opponent_range
+ if len(self._advance_cache) > 256:
+ self._advance_cache.clear()
+ else:
+ updated_opponent_range = ExplicitRange((replace_weight(sampled_opponent_hypothesis, 1.0),))
updated = PublicBeliefState(
root_player=pre_public_belief.root_player,
acting_player=(
@@ -41,12 +63,12 @@
public_state=public_state,
round_number=int(public_state.round_number),
phase=str(public_state.phase),
- opponent_public_dice_count=tracker.opponent_public_dice_count_from_state(public_state),
- opponent_public_hand_count=tracker.opponent_public_hand_count_from_state(public_state),
+ opponent_public_dice_count=float(next_opponent_public_dice_count),
+ opponent_public_hand_count=float(next_opponent_public_hand_count),
self_range=ExplicitRange((sampled_self_hypothesis,)),
opponent_range=updated_opponent_range,
public_history=tracker.public_history(),
- revealed_opponent_card_definition_ids=tracker.revealed_opponent_card_definition_ids(),
+ revealed_opponent_card_definition_ids=next_revealed,
)
return _RangeUpdateResult(
updated_public_belief=updated,
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -58,57 +58,12 @@
policy_cache=policy_cache,
leaf_value_cache=leaf_value_cache,
)
- log_probs = _policy_log_probs(
- context=current_context,
- history=current_history,
- public_history=current_public_belief.public_history,
- opponent_deck_name=opponent_deck_name,
- model=model,
- encoder=encoder,
+ return _leaf_value(
+ public_belief=current_public_belief,
+ current_context=current_context,
+ budget=budget,
+ card_vocabulary=self.card_vocabulary,
device=device,
- cache=policy_cache,
+ search_state_value_fn=search_state_value_fn,
+ cache=leaf_value_cache,
)
- ranked = sorted(range(len(log_probs)), key=lambda index: float(log_probs[index]), reverse=True)
- candidate_indices = tuple(ranked[: max(1, min(len(ranked), budget.root_top_k))])
- branch_scores: list[float] = []
- for candidate_index in candidate_indices:
- if time.perf_counter() > deadline:
- break
- try:
- branch_value = self._evaluate_internal_self_candidate(
- current_context=current_context,
- current_sampled_state_json=current_sampled_state_json,
- current_public_belief=current_public_belief,
- current_history=current_history,
- root_player=root_player,
- budget=budget,
- depth_remaining=depth_remaining,
- model=model,
- encoder=encoder,
- device=device,
- opponent_deck_name=opponent_deck_name,
- search_state_value_fn=search_state_value_fn,
- deadline=deadline,
- updater=updater,
- selected_action_code=int(current_context.legal_low_level_codes[candidate_index]),
- env_config=getattr(env, "_config", None),
- matchup=getattr(env, "_matchup", None),
- current_tracker=current_tracker,
- policy_cache=policy_cache,
- leaf_value_cache=leaf_value_cache,
- )
- except _SearchTimeout:
- break
- if branch_value is not None:
- branch_scores.append(float(branch_value))
- if not branch_scores:
- return _leaf_value(
- public_belief=current_public_belief,
- current_context=current_context,
- budget=budget,
- card_vocabulary=self.card_vocabulary,
- device=device,
- search_state_value_fn=search_state_value_fn,
- cache=leaf_value_cache,
- )
- return max(branch_scores)
--- a/research/world_model/src/gitcg_world_model/sepot_search.py
+++ b/research/world_model/src/gitcg_world_model/sepot_search.py
@@ -5,18 +5,13 @@
budget: SePotPhaseBudget,
) -> tuple[float, ...]:
legal_count = float(len(current_context.legal_low_level_codes)) if current_context is not None else 0.0
- has_end = bool(
- current_context is not None
- and any(
- low_level_kind_for_code(int(action_code)) == OptionKind.ACTION_DECLARE_END
- for action_code in current_context.legal_low_level_codes
- )
- )
+ legal_kinds = _legal_option_kinds(current_context) if current_context is not None else ()
+ has_end = any(kind == OptionKind.ACTION_DECLARE_END for kind in legal_kinds)
productive = float(
sum(
1
- for action_code in (current_context.legal_low_level_codes if current_context is not None else ())
- if low_level_kind_for_code(int(action_code)) in {
+ for kind in legal_kinds
+ if kind in {
OptionKind.ACTION_USE_SKILL,
OptionKind.ACTION_PLAY_CARD,
OptionKind.ACTION_ELEMENTAL_TUNING,