TrainableAssistedPlayers¶
Role: Factory/wrapper that provides a coordinated (A, B) PR-assisted player pair.
Location: Q_Sea_Battle.trainable_assisted_players.TrainableAssistedPlayers
Derived constraints¶
The instance coordinates two trainable models and two player wrappers; the shared dimensions are derived from game_layout.field_size and game_layout.comms_size. Define \(n2 = \text{field\_size}^2\), where field_size is the side length of the square field, and comms_size is the number of communication bits/logits.
Constructor¶
| Parameter | Type | Description |
|---|---|---|
game_layout |
Any, must provide attributes field_size: int and comms_size: int, scalar |
Game-layout-like object used to parameterize default models and to construct player wrappers. |
p_rule |
float, unconstrained, scalar |
Unused by current linear models; retained for compatibility with other/older configurations. |
num_iterations |
Optional[int], unconstrained, scalar |
Unused by current linear models; retained for compatibility with other/older configurations. |
hidden_dim |
int, unconstrained, scalar |
Unused by current linear models; retained for compatibility with other/older configurations. |
L_meas |
Optional[int], unconstrained, scalar |
Unused by current linear models; retained for compatibility with other/older configurations. |
model_a |
Optional[LinTrainableAssistedModelA], scalar |
Optional pre-constructed model for player A; if None, a default LinTrainableAssistedModelA is constructed from game_layout. |
model_b |
Optional[LinTrainableAssistedModelB], scalar |
Optional pre-constructed model for player B; if None, a default LinTrainableAssistedModelB is constructed from game_layout. |
Preconditions
game_layoutprovidesfield_sizeandcomms_sizeattributes readable viagetattr.- If
model_a is Noneormodel_b is None,int(getattr(game_layout, "field_size"))andint(getattr(game_layout, "comms_size"))must succeed.
Postconditions
self.game_layoutis set togame_layout.self.exploreis initialized toFalse.self._playerAandself._playerBare initialized toNone(lazy wrapper construction).self.has_previs set toTrue.self.previousis initialized toNone.self.model_aandself.model_bare set either from provided models or from newly constructed defaults usingsr_mode="sample"andseed=123.
Errors
- Raises
AttributeErrororTypeErrorifgame_layoutlacks required attributes or they cannot be converted viaint(...)when constructing default models. - Any exception raised by
LinTrainableAssistedModelA(...)orLinTrainableAssistedModelB(...)during default construction is propagated.
Example
Construct and obtain wrapper players
from Q_Sea_Battle.trainable_assisted_players import TrainableAssistedPlayers
# game_layout must provide: field_size, comms_size
players_factory = TrainableAssistedPlayers(game_layout)
player_a, player_b = players_factory.players()
players_factory.set_explore(True)
players_factory.reset()
Public Methods¶
check_model_correspondence()¶
Check that model A and model B appear dimensionally compatible by comparing field_size and comms_size when available.
Returns
bool, scalar:Trueif basic dimensions match (or cannot be checked due to missing attributes or other errors), otherwiseFalse.
Errors
- No exceptions are intended to propagate; exceptions encountered while reading model attributes are caught and result in returning
True.
players()¶
Return the (player A, player B) wrappers, constructing them lazily and caching them so state persists across calls until reset().
Returns
Tuple[TrainableAssistedPlayerA, TrainableAssistedPlayerB], shape(2,): A tuple(player_a, player_b).
Side effects
- On first call (or if a cached wrapper is missing), constructs
TrainableAssistedPlayerA(game_layout, model_a=self.model_a)and/orTrainableAssistedPlayerB(game_layout, model_b=self.model_b). - Sets
player_a.exploreandplayer_b.exploretoself.explore. - Sets
player_a.parentandplayer_b.parenttoself.
Errors
- Propagates any exception thrown by the
TrainableAssistedPlayerA/TrainableAssistedPlayerBconstructors or attribute assignments.
reset()¶
Reset per-game state by clearing cached previous tensors and forwarding reset to any already-instantiated player wrappers.
Returns
None.
Side effects
- If instantiated, calls
self._playerA.reset()andself._playerB.reset(). - Sets
self.previous = None.
Errors
- Propagates any exception thrown by the underlying wrapper
reset()methods.
set_explore(flag)¶
Enable or disable exploration for both players.
Parameters
flag:bool, scalar: IfTrue, players may sample actions and store log-probabilities (when supported); ifFalse, they act greedily.
Returns
None.
Side effects
- Sets
self.explore = bool(flag). - If wrappers are instantiated, updates
self._playerA.exploreandself._playerB.exploreto match.
Errors
- Propagates any exception thrown by setting
exploreon instantiated wrappers.
Data & State¶
has_log_probs:bool, scalar: Class attribute set toTrue.game_layout:Any, must providefield_size: intandcomms_size: int, scalar: Stored reference to the game layout.model_a:LinTrainableAssistedModelA, scalar: Internal trainable model used by player A.model_b:LinTrainableAssistedModelB, scalar: Internal trainable model used by player B.explore:bool, scalar: Shared exploration flag propagated to wrapper players._playerA:Optional[TrainableAssistedPlayerA], scalar: Cached wrapper instance for player A, created lazily._playerB:Optional[TrainableAssistedPlayerB], scalar: Cached wrapper instance for player B, created lazily.has_prev:bool, scalar: Set toTrue; semantics are not specified in this module beyond indicating previous-state support.previous:Any | None, expected structure(measurements_per_layer, outcomes_per_layer)orNone, scalar: Storage written by player A and consumed by player B; expected to be a 2-tuple of Python lists of tensors where each tensor is expected to have shape(B, n2).
Definition of n2 and tensor shape expectation
This module uses the convention \(n2 = \text{field\_size}^2\). The module docstring states each tensor in previous is expected to have shape (B, n2) where B is the batch dimension; tensor dtype and framework are not specified here.
Planned (design-spec)¶
Not specified.
Deviations¶
Not specified.
Notes for Contributors¶
- The constructor includes parameters (
p_rule,num_iterations,hidden_dim,L_meas) that are currently unused by the default linear models; keep them in place if backward/forward compatibility with configuration code is required. players()setsparenton wrapper players; any wrapper implementation changes should preserve this linkage if other components depend on it.check_model_correspondence()is intentionally permissive: if model attributes are missing or inaccessible it returnsTrue.
Related¶
Q_Sea_Battle.trainable_assisted_player_a.TrainableAssistedPlayerAQ_Sea_Battle.trainable_assisted_player_b.TrainableAssistedPlayerBQ_Sea_Battle.lin_trainable_assisted_model_a.LinTrainableAssistedModelAQ_Sea_Battle.lin_trainable_assisted_model_b.LinTrainableAssistedModelB
Changelog¶
- Not specified.