MajorityPlayerB¶
Role: Player B strategy that maps the active one-hot gun index into one of \(m\) contiguous index segments and returns the corresponding communication bit.
Location: Q_Sea_Battle.majority_player_b.MajorityPlayerB
Derived constraints¶
- Let \(n2\) be the flattened grid size (gun vector length) and \(m\) be the communication length (comm vector length). The class docstring states the layout is configured such that \(m\) divides \(n2\), so segment length is \(segment\_len = n2 // m\).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| game_layout | GameLayout, constraints: not specified, shape: scalar object |
Game configuration for this player. |
Preconditions
game_layoutis aGameLayoutinstance (specific validation not specified in this module).- Base class
PlayerBconstructor accepts the providedgame_layout(details not specified in this module).
Postconditions
- The instance is initialized via
super().__init__(game_layout).
Errors
- Not specified in this module.
Example
from Q_Sea_Battle.game_layout import GameLayout
from Q_Sea_Battle.majority_player_b import MajorityPlayerB
layout = GameLayout(...) # Not specified here
player_b = MajorityPlayerB(layout)
Public Methods¶
decide(gun, comm, supp=None)¶
Decide whether to shoot based on the segment-selected comm bit.
| Parameter | Type | Description |
|---|---|---|
| gun | np.ndarray, dtype: convertible to int, constraints: intended one-hot (not validated), shape: (n2,) after ravel() |
Flattened one-hot gun vector of length \(n2\); the active index is computed via np.argmax. |
| comm | np.ndarray, dtype: convertible to int, constraints: values not specified, shape: (m,) after ravel() |
Communication vector from Player A of length \(m\); the selected bit is returned as the decision. |
| supp | Optional[Any], constraints: ignored, shape: scalar object |
Optional supporting information; not used. |
Returns
int, constraints: derived fromcomm[segment_index]after conversion toint(typically in{0,1}ifcommis in{0,1}), shape: scalar.
Preconditions
gunandcommare array-like and convertible vianp.asarray(..., dtype=int).- \(m = comm.size\) must be non-zero to avoid division by zero when computing
segment_len = n2 // m(not explicitly checked). - Intended:
gunis a valid one-hot vector (not validated); behavior followsnp.argmaxeven if not one-hot.
Postconditions
- Returns the element
comm[segment_index]wheresegment_index = gun_index // (n2 // m)with a defensive clamp tom - 1ifsegment_index >= m.
Errors
ZeroDivisionErrorifcomm.size == 0(vian2 // m).IndexErrorifcomm.size == 0(attemptingcomm[segment_index]), or other indexing issues ifcommcannot be indexed as 1D afterravel().- Other NumPy conversion errors are not specified.
Example
import numpy as np
from Q_Sea_Battle.majority_player_b import MajorityPlayerB
from Q_Sea_Battle.game_layout import GameLayout
layout = GameLayout(...) # Not specified here
b = MajorityPlayerB(layout)
gun = np.array([0, 0, 1, 0, 0, 0], dtype=int) # n2 = 6, gun_index = 2
comm = np.array([1, 0, 1], dtype=int) # m = 3, segment_len = 2
# segments: [0-1]->comm[0], [2-3]->comm[1], [4-5]->comm[2]
decision = b.decide(gun, comm)
Data & State¶
- Inherits state from
PlayerB(state fields not specified in this module). - No additional instance attributes are defined in this module.
Planned (design-spec)¶
- Not specified.
Deviations¶
- The class docstring states the implementation assumes \(m\) divides \(n2\); the
decidemethod includes a defensive clamp for cases where the derivedsegment_index >= m, which is an accommodation for \(m\) not exactly dividing \(n2\) (or other inconsistencies).
Notes for Contributors¶
decidecoerces bothgunandcommtointand flattens them viaravel(). If future changes require preserving original shapes or dtypes, update this conversion logic and corresponding documentation.- Consider adding explicit validation for
comm.size > 0and for one-hot correctness ofgunif stricter behavior is desired.
Related¶
Q_Sea_Battle.players_base.PlayerBQ_Sea_Battle.game_layout.GameLayout
Changelog¶
- Not specified.