Spaces:
Build error
Build error
Fix styles and deleted unused code of wordle_env
Browse filesFiles changed (3)
6 unmodified lines7891011121314156 unmodified lines)import osfrom . import wordle# Classic# ----------------------------------------current_dir = os.path.realpath(os.path.dirname(__file__))register(6 unmodified lines7891011126 unmodified lines)import osfrom . import wordleregister(
1 unmodified line234567891011121314111 unmodified lines12612712812913013139 unmodified lines1711721731741 unmodified lineKeep the state in a 1D int arrayindex[0] = remaining stepsRest of data is laid out as binary array[1..27] = whether char has been guessed or not[[status, status, status, status, status]for _ in "ABCD..."]where status has codes[1, 0, 0] - char is definitely not in this spot[0, 1, 0] - char is maybe in this spot[0, 0, 1] - char is definitely in this spot111 unmodified linesreturn maskdef update_mask(state: WordleState, word: str, goal_word: str) -> WordleState:"""return a copy of state that has been updated to new state39 unmodified linesstate[offset:offset + 3 * WORDLE_N] = [1, 0, 0] * WORDLE_Nprocessed_letters.append(c)return state, reward1 unmodified line23456789101112111 unmodified lines12412512612712812913039 unmodified lines1701711721 unmodified lineKeep the state in a 1D int arrayindex[0] = remaining steps[[status, status, status, status, status]for _ in "ABCD..."]where status has codes[0, 0, 0] - no information about the char[1, 0, 0] - char is definitely not in this spot[0, 1, 0] - char is maybe in this spot[0, 0, 1] - char is definitely in this spot111 unmodified linesreturn maskdef update_mask(state: WordleState, word: str, goal_word: str) -> WordleState:"""return a copy of state that has been updated to new state39 unmodified linesstate[offset:offset + 3 * WORDLE_N] = [1, 0, 0] * WORDLE_Nprocessed_letters.append(c)return state, reward
11 unmodified lines1213141516171819202 unmodified lines2324252627282930313233343536373839404142432 unmodified lines4647484950515253545556575859606112 unmodified lines747576777879808182838485868788899017 unmodified lines10810911011111211311411511611711811912012112212312412512612712812913013113211 unmodified linesimport randomdef _load_words(limit: Optional[int]=None, complete: Optional[bool]=False) -> List[str]:words = complete_vocabulary if complete else target_vocabularyreturn words if not limit else words[:limit]class WordleEnvBase(gym.Env):"""2 unmodified lines* 13k for full vocabState space is defined as:* 6 possibilities for turns (WORDLE_TURNS)* Each VALID_CHAR has a state of 0/1 for whether it's been guessed before* For each in VALID_CHARS [A-Z] can be in one of 3^WORDLE_N states: (No, Maybe, Yes)for full game, this is (3^5)^26Each state has 1 + 5*26 possibilitiesReward:Reward is 10 for guessing the right word, -10 for not guessing the right word after 6 guesses.Starting State:Random goal wordInitial state with turn 0, all chars Unvisited + Maybe"""def __init__(self, words: List[str],max_turns: int=6,allowable_words: Optional[int]=None,frequencies: Optional[List[float]]=None,mask_based_state_updates: bool=False):assert all(len(w) == WORDLE_N for w in words), f'Not all words of length {WORDLE_N}, {words}'self.words = wordsself.max_turns = max_turns2 unmodified linesif not self.allowable_words:self.allowable_words = len(self.words)self.frequencies = Noneif frequencies:assert len(words) == len(frequencies), f'{len(words), len(frequencies)}'self.frequencies = np.array(frequencies, dtype=np.float32) / sum(frequencies)self.action_space = spaces.Discrete(self.words_as_action_space())self.observation_space = spaces.MultiDiscrete(state.get_nvec(self.max_turns))self.done = Trueself.goal_word: Tuple = tuple(tuple([tuple([-1]) * WORDLE_N]) *len(WORDLE_CHARS))self.state: state.WordleState = Noneself.state_updater = state.update12 unmodified linesgoal_word = self.words[self.goal_word]# assert word in self.words, f'{word} not in words list'self.state, r = self.state_updater(state=self.state,word=word,goal_word=goal_word)reward = rif action == self.goal_word:self.done = True#reward = REWARDif state.remaining_steps(self.state) == self.max_turns-1:reward = 0#-10*REWARD # No reward for guessing off the batelse:#reward = REWARD*(self.state.remaining_steps() + 1) / self.max_turnsreward = REWARDelif state.remaining_steps(self.state) == 0:self.done = True17 unmodified linesdef words_as_action_space(self):return len(self.words)def encode_word(self, word):encoded_word = np.array([[0] * WORDLE_N] * len(WORDLE_CHARS),dtype=np.int32)for index, letter in enumerate(word):cint = WORDLE_CHARS.index(letter)encoded_word[cint][index] = 1return encoded_worddef decode_word(self, action):word = [''] * WORDLE_Nfor index, letter_vec in enumerate(action):if 1 in letter_vec:for i, j in enumerate(letter_vec):if j == 1:word[i] = WORDLE_CHARS[index]return ''.join(word)class WordleEnv100OneAction(WordleEnvBase):def __init__(self):11 unmodified lines12131415161718192021222 unmodified lines2526272829303132333435363738394041424344452 unmodified lines484950515253545556575812 unmodified lines717273747576777879808182838485868717 unmodified lines10510610710810911011 unmodified linesimport randomdef _load_words(limit: Optional[int] = None, complete: Optional[bool] = False) -> List[str]:words = complete_vocabulary if complete else target_vocabularyreturn words if not limit else words[:limit]class WordleEnvBase(gym.Env):"""2 unmodified lines* 13k for full vocabState space is defined as:* 6 possibilities for turns (WORDLE_TURNS)* For each in VALID_CHARS [A-Z] can be in one of 3^WORDLE_N states: (No, Maybe, Yes)for full game, this is (3^5)^26Each state has 1 + 5*26 possibilitiesReward:Reward is 10 for guessing the right word, -10 for not guessing the right word after 6 guesses.1 from every letter correctly guessed on each tryStarting State:Random goal wordInitial state with turn 0, all chars Unvisited"""def __init__(self, words: List[str],max_turns: int = 6,allowable_words: Optional[int] = None,mask_based_state_updates: bool = False):assert all(len(w) == WORDLE_N for w in words), f'Not all words of length {WORDLE_N}, {words}'self.words = wordsself.max_turns = max_turns2 unmodified linesif not self.allowable_words:self.allowable_words = len(self.words)self.action_space = spaces.Discrete(self.words_as_action_space())self.observation_space = spaces.MultiDiscrete(state.get_nvec(self.max_turns))self.done = Trueself.goal_word: int = -1self.state: state.WordleState = Noneself.state_updater = state.update12 unmodified linesgoal_word = self.words[self.goal_word]# assert word in self.words, f'{word} not in words list'self.state, r = self.state_updater(state=self.state,word=word,goal_word=goal_word)reward = rif action == self.goal_word:self.done = True# reward = REWARDif state.remaining_steps(self.state) == self.max_turns-1:reward = 0 # -10*REWARD # No reward for guessing off the batelse:# reward = REWARD*(self.state.remaining_steps() + 1) / self.max_turnsreward = REWARDelif state.remaining_steps(self.state) == 0:self.done = True17 unmodified linesdef words_as_action_space(self):return len(self.words)class WordleEnv100OneAction(WordleEnvBase):def __init__(self):
wordle_env/__init__.py-3
wordle_env/state.py+2-4
wordle_env/wordle.py+17-39