MajorityPlayerA¶
Role: Player A that encodes segment-wise majority information from a flattened binary field into a deterministic communication vector.
Location: Q_Sea_Battle.majority_player_a.MajorityPlayerA
Derived constraints¶
- Let field_size be
self.game_layout.field_size(int, not specified), then \(n2 = \text{field\_size}^2\) (int). - Let comms_size be
self.game_layout.comms_size(int, not specified), then \(m = \text{comms\_size}\) (int). - Segment length is \(\text{segment\_len} = n2 // m\) (int); the implementation assumes \(m\) divides \(n2\) (comment: enforced by
GameLayout).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| game_layout | GameLayout, constraints: not specified, shape: N/A | Game configuration for this player; stored/used via the PlayerA base class and accessed as self.game_layout. |
Preconditions
game_layoutis aGameLayoutinstance (not validated here).- Additional constraints are not specified in this class.
Postconditions
- The instance is initialized by delegating to
PlayerA.__init__(game_layout).
Errors
- Not specified (any exceptions would come from
PlayerA.__init__or invalidgame_layoutusage elsewhere).
Example
from Q_Sea_Battle.game_layout import GameLayout
from Q_Sea_Battle.majority_player_a import MajorityPlayerA
layout = GameLayout(field_size=4, comms_size=4) # exact signature not specified here
player = MajorityPlayerA(layout)
Public Methods¶
decide(field, supp=None)¶
Encode majority statistics in the communication vector.
Parameters
- field: np.ndarray, dtype: any (converted to int), constraints: interpreted as 0/1 values by documentation but not validated, shape: any (flattened internally to 1D via
.ravel()). - supp: Optional[Any], constraints: unused, shape: N/A.
Returns
- np.ndarray, dtype int, constraints: values in {0, 1}, shape (m,), where \(m = \text{comms\_size}\).
Behavior
- Converts
fieldtoflat_field = np.asarray(field, dtype=int).ravel(). - Computes \(n2 = \text{field\_size}^2\) and \(m = \text{comms\_size}\) from
self.game_layout. - Sets
segment_len = n2 // m. - For each segment \(i \in [0, m)\), slices
flat_field[start:end]wherestart = i * segment_lenandend = start + segment_len, then setscomm[i] = 1ifones >= zeroselse0, withones = int(segment.sum())andzeros = segment_len - ones. - Returns the resulting
comm.
Preconditions
self.game_layout.field_sizeandself.game_layout.comms_sizeexist and are usable as integers.- The implementation assumes \(m\) divides \(n2\).
fieldmust contain at leastn2elements after flattening; otherwise segment slices may be shorter thansegment_len, affecting majority computation (not checked).
Postconditions
- Returns a newly allocated vector
commof lengthm.
Errors
- May raise exceptions from
np.asarray(..., dtype=int)for non-coercible inputs. - May raise
ZeroDivisionErrorifm == 0(not prevented here). - May raise attribute errors if
self.game_layoutis missing required attributes.
Example
import numpy as np
from Q_Sea_Battle.majority_player_a import MajorityPlayerA
from Q_Sea_Battle.game_layout import GameLayout
layout = GameLayout(field_size=4, comms_size=4) # exact signature not specified here
player = MajorityPlayerA(layout)
field = np.array([
1, 0, 1, 0,
0, 0, 1, 1,
1, 1, 0, 0,
0, 1, 0, 1,
])
comm = player.decide(field)
Data & State¶
- Inherited state from
PlayerA(not defined in this module). - Uses
self.game_layout(type: GameLayout, constraints: not specified, shape: N/A) as established by the base class. - No additional attributes are defined by
MajorityPlayerA.
Planned (design-spec)¶
- Not specified (no design notes provided).
Deviations¶
- No deviations identified (no design notes provided to compare against code).
Notes for Contributors¶
- The method
decidecomputesn2fromfield_sizerather than from the actual length offield; iffielddoes not match the expected size, behavior is not validated and may silently produce incorrect segment statistics. - Majority tie-breaking is implemented as
1whenones >= zeros.
Related¶
Q_Sea_Battle.players_base.PlayerA(base class; behavior not documented here).Q_Sea_Battle.game_layout.GameLayout(providesfield_sizeandcomms_size).
Changelog¶
- 0.1: Initial implementation (module docstring version).