GameEnv¶
Role: Core environment for generating game state (field, gun), providing player inputs, evaluating rewards, and applying communication channel noise. Location:
Q_Sea_Battle.game_env.GameEnv
Derived constraints¶
- Let
field_size = nfromself.game_layout.field_size; thenn2 = n * nand the flattenedfieldandgunare shape(n2,). fieldvalues are integers in{0,1}and shape(n, n).gunis a one-hot integer array in{0,1}with exactly one1and shape(n, n).- Let
comms_size = m; thencommvectors passed toapply_channel_noiseare intended to have shape(m,)with values in{0,1}(length is not validated by the implementation).
Constructor¶
| Parameter | Type | Description |
|---|---|---|
| game_layout | Optional[GameLayout], nullable, shape N/A | Optional game configuration; if None, a default GameLayout() is constructed. |
Preconditions
- None.
Postconditions
self.game_layoutis set to the providedGameLayoutor a new default instance.self.fieldisNone.self.gunisNone.
Errors
- Not specified.
Example
from Q_Sea_Battle.game_env import GameEnv
env = GameEnv()
env.reset()
field_flat, gun_flat = env.provide()
r = env.evaluate(1)
Public Methods¶
reset¶
Signature: reset(self) -> None
Reset the environment state for a new game by creating a new random field and a new random one-hot gun position.
Arguments
- None.
Returns
None, shape N/A.
Preconditions
self.game_layout.field_sizeis expected to be usable as an integernsuch that arrays of shape(n, n)and lengthn2 = n * ncan be created.self.game_layout.enemy_probabilityis expected to be usable as a float probability for Bernoulli sampling.
Postconditions
self.field:np.ndarray, dtype int, values {0,1}, shape (field_size, field_size).self.gun:np.ndarray, dtype int, values {0,1}, shape (field_size, field_size), with exactly one element equal to1.
Errors
- Not specified.
Example
env.reset()
provide¶
Signature: provide(self) -> Tuple[np.ndarray, np.ndarray]
Provide inputs to the players by returning the current field and gun arrays in flattened form (copies).
Arguments
- None.
Returns
Tuple[np.ndarray, np.ndarray]:(field, gun)where each isnp.ndarray, dtype int, values {0,1}, shape (n2,)withn2 = field_size * field_size.
Preconditions
reset()has been called successfully so thatself.fieldandself.gunare notNone.
Postconditions
- Returns copies of internal arrays; mutating the returned arrays does not mutate
self.field/self.gun.
Errors
RuntimeError: Ifself.field is Noneorself.gun is None(environment not reset).
Example
env.reset()
field_flat, gun_flat = env.provide()
evaluate¶
Signature: evaluate(self, shoot: int) -> float
Evaluate the reward for a shooting decision.
Arguments
shoot:int, values {0,1}, shape N/A. (The implementation casts viaint(shoot); other values are not validated.)
Returns
float, values {0.0, 1.0}, shape N/A:1.0if the decision matches the true cell value at the gun position, otherwise0.0.
Preconditions
reset()has been called successfully so thatself.fieldandself.gunare notNone.self.guncontains exactly one1so that exactly one field cell is selected.
Postconditions
- No mutation of
self.fieldorself.gunis performed.
Errors
RuntimeError: Ifself.field is Noneorself.gun is None(environment not reset).RuntimeError: Ifself.gundoes not contain exactly one1(i.e., selected cell count is not 1).
Example
env.reset()
reward = env.evaluate(0)
apply_channel_noise¶
Signature: apply_channel_noise(self, comm: np.ndarray) -> np.ndarray
Apply independent bit-flip noise to a communication vector with per-bit flip probability channel_noise.
Arguments
comm:np.ndarray, dtype int convertible, intended values {0,1}, intended shape (comms_size,)wherecomms_size = m. The implementation converts usingnp.asarray(comm, dtype=int)and does not validate values or length.
Returns
np.ndarray, dtype int, shape comm.shape: Noisy communication vector.- If
channel_noise <= 0.0, returns an unchanged copy ofcomm(after conversion todtype int). - If
channel_noise >= 1.0, returns1 - comm(bitwise flip for 0/1 semantics). - Otherwise flips each entry independently with probability
channel_noise.
Preconditions
self.game_layout.channel_noiseis expected to be usable as a floatc.
Postconditions
- Returns a new array (copy) for
c <= 0.0and for0.0 < c < 1.0. - For
c >= 1.0, the expression1 - commproduces a new array.
Errors
- Not specified.
Example
env.reset()
comm = np.array([0, 1, 1, 0], dtype=int)
noisy = env.apply_channel_noise(comm)
Data & State¶
game_layout:GameLayout, non-null, shape N/A. Configuration object used to readfield_size,enemy_probability, andchannel_noise.field:Optional[np.ndarray], nullable. When set:np.ndarray, dtype int, values {0,1}, shape (field_size, field_size).gun:Optional[np.ndarray], nullable. When set:np.ndarray, dtype int, values {0,1}, shape (field_size, field_size), intended to be one-hot with exactly one1.
Planned (design-spec)¶
- Not specified.
Deviations¶
- No design notes were provided; no deviations can be assessed.
Notes for Contributors¶
provide()returns flattened copies; if new methods return views instead, explicitly document mutation and aliasing behavior.apply_channel_noise()assumes 0/1 semantics but does not validatecommcontents; if validation is added, document the resulting errors and constraints.
Related¶
Q_Sea_Battle.game_layout.GameLayout(configuration dependency; imported asfrom .game_layout import GameLayout).
Changelog¶
- Version: 0.1 (module docstring).