Skip to content

Commit

Permalink
Add hashing to environment (Farama-Foundation#94)
Browse files Browse the repository at this point in the history
Why:

Hashing an environment makes it possible to verify if two initial states
are identical. This is necessary if we want to create groups of initial
states that are mutually exclusive.

How:

Compute the hash of the grid encoding with the initial agent position and
direction.

Note:

I'm not sure if the RNG state may eventually cause divergences in the
episodes and whether that should be accounted for in the hashing. It's
difficult to measure however the divergence without playing, and taking
into account the RNG state will likely give different hashing for
practically identical states. That's why my feeling is the RNG state should not
be part of the hashing.
  • Loading branch information
bouthilx authored May 20, 2020
1 parent cf9a376 commit 1a29011
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions gym_minigrid/minigrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import hashlib
import gym
from enum import IntEnum
import numpy as np
Expand Down Expand Up @@ -733,6 +734,18 @@ def seed(self, seed=1337):
self.np_random, _ = seeding.np_random(seed)
return [seed]

def hash(self, size=16):
"""Compute a hash that uniquely identifies the current state of the environment.
:param size: Size of the hashing
"""
sample_hash = hashlib.sha256()

to_encode = [self.grid.encode(), self.agent_pos, self.agent_dir]
for item in to_encode:
sample_hash.update(str(item).encode('utf8'))

return sample_hash.hexdigest()[:size]

@property
def steps_remaining(self):
return self.max_steps - self.step_count
Expand Down

0 comments on commit 1a29011

Please sign in to comment.