1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| from enum import Enum import numpy as np
class State():
def __init__(self, row=-1, column=-1): self.row = row self.column = column
def __repr__(self): return "<State: [{}, {}]>".format(self.row, self.column)
def clone(self): return State(self.row, self.column)
def __hash__(self): return hash((self.row, self.column))
def __eq__(self, other): return self.row == other.row and self.column == other.column
class Action(Enum): UP = 1 DOWN = -1 LEFT = 2 RIGHT = -2
class Environment():
def __init__(self, grid, move_prob=0.8): self.grid = grid self.agent_state = State()
self.default_reward = -0.04
self.move_prob = move_prob self.reset()
@property def row_length(self): return len(self.grid)
@property def column_length(self): return len(self.grid[0])
@property def actions(self): return [Action.UP, Action.DOWN, Action.LEFT, Action.RIGHT]
@property def states(self): states = [] for row in range(self.row_length): for column in range(self.column_length): if self.grid[row][column] != 9: states.append(State(row, column)) return states
def transit_func(self, state, action): transition_probs = {} if not self.can_action_at(state): return transition_probs opposite_direction = Action(action.value * -1)
for a in self.actions: prob = 0 if a == action: prob = self.move_prob elif a != opposite_direction: prob = (1 - self.move_prob) / 2
next_state = self._move(state, a) if next_state not in transition_probs: transition_probs[next_state] = prob else: transition_probs[next_state] += prob
return transition_probs
def can_action_at(self, state): if self.grid[state.row][state.column] == 0: return True else: return False
def _move(self, state, action): if not self.can_action_at(state): raise Exception("Can't move from here!") next_state = state.clone() if action == Action.UP: next_state.row -= 1 elif action == Action.DOWN: next_state.row += 1 elif action == Action.LEFT: next_state.column -= 1 elif action == Action.RIGHT: next_state.column += 1
if not (0 <= next_state.row < self.row_length): next_state = state if not (0 <= next_state.column < self.column_length): next_state = state if self.grid[next_state.row][next_state.column] == 9: next_state = state return next_state
def reward_func(self, state): reward = self.default_reward done = False
attribute = self.grid[state.row][state.column] if attribute == 1: reward = 1 done = True elif attribute == -1: reward = -1 done = True
return reward, done
def reset(self): self.agent_state = State(self.row_length - 1, 0) return self.agent_state
def step(self, action): next_state, reward, done = self.transit(self.agent_state, action) if next_state is not None: self.agent_state = next_state
return next_state, reward, done
def transit(self, state, action): transition_probs = self.transit_func(state, action) if len(transition_probs) == 0: return None, None, True
next_states = [] probs = [] for s in transition_probs: next_states.append(s) probs.append(transition_probs[s])
next_state = np.random.choice(next_states, p=probs) reward, done = self.reward_func(next_state) return next_state, reward, done
|