Created
January 1, 2020 12:38
-
-
Save thunderInfy/3c0074dbb0f3fe99eca49816c8d79e60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Environment: | |
| #HELPFUL FUNCTIONS | |
| def get_new_state(self, state, action): | |
| ''' | |
| Get new state after applying action on this state | |
| Assumption: The car keeps on moving with the current velocity and then action is applied to | |
| change the velocity | |
| ''' | |
| new_state = state.copy() | |
| new_state[0] = state[0] - state[2] | |
| new_state[1] = state[1] + state[3] | |
| new_state[2] = state[2] + action[0] | |
| new_state[3] = state[3] + action[1] | |
| return new_state | |
| def select_randomly(self,NUMPY_ARR): | |
| ''' | |
| Returns a value uniform randomly from NUMPY_ARR | |
| Here NUMPY_ARR should be 1 dimensional | |
| ''' | |
| return np.random.choice(NUMPY_ARR) | |
| def set_zero(NUMPY_ARR): | |
| ''' | |
| Returns NUMPY_ARR after making zero all the elements in it | |
| ''' | |
| NUMPY_ARR[:] = 0 | |
| return NUMPY_ARR | |
| def is_finish_line_crossed(self, state, action): | |
| ''' | |
| Returns True if the car crosses the finish line | |
| False otherwise | |
| ''' | |
| new_state = self.get_new_state(state, action) | |
| old_cell, new_cell = state[0:2], new_state[0:2] | |
| ''' | |
| new_cell's row index will be less | |
| ''' | |
| rows = np.array(range(new_cell[0],old_cell[0]+1)) | |
| cols = np.array(range(old_cell[1],new_cell[1]+1)) | |
| fin = set([tuple(x) for x in self.data.finish_line]) | |
| row_col_matrix = [(x,y) for x in rows for y in cols] | |
| intersect = [x for x in row_col_matrix if x in fin] | |
| return len(intersect) > 0 | |
| def is_out_of_track(self, state, action): | |
| ''' | |
| Returns True if the car goes out of track if action is taken on state | |
| False otherwise | |
| ''' | |
| new_state = self.get_new_state(state, action) | |
| old_cell, new_cell = state[0:2], new_state[0:2] | |
| if new_cell[0] < 0 or new_cell[0] >= 100 or new_cell[1] < 0 or new_cell[1] >= 100: | |
| return True | |
| else: | |
| return self.data.racetrack[tuple(new_cell)] == -1 | |
| #CONSTRUCTOR | |
| def __init__(self, data, gen): | |
| ''' | |
| initialize step_count to be 0 | |
| ''' | |
| self.data = data | |
| self.gen = gen | |
| self.step_count = 0 | |
| #MEMBER FUNCTIONS | |
| def reset(self): | |
| self.data.episode = dict({'S':[],'A':[],'probs':[],'R':[None]}) | |
| self.step_count = 0 | |
| def start(self): | |
| ''' | |
| Makes the velocity of the car to be zero | |
| Returns the randomly selected start state. | |
| ''' | |
| state = np.zeros(4,dtype='int') | |
| state[0] = 99 | |
| state[1] = self.select_randomly(self.data.start_line[:,1]) | |
| ''' | |
| state[2] and state[3] are already zero | |
| ''' | |
| return state | |
| def step(self, state, action): | |
| ''' | |
| Returns the reward and new state when action is taken on state | |
| Checks the following 2 cases maintaining the order: | |
| 1. car finishes race by crossing the finish line | |
| 2. car goes out of track | |
| Ends the episode by returning reward as None and state as usual (which will be terminating) | |
| ''' | |
| self.data.episode['A'].append(action) | |
| reward = -1 | |
| if (self.is_finish_line_crossed(state, action)): | |
| new_state = self.get_new_state(state, action) | |
| self.data.episode['R'].append(reward) | |
| self.data.episode['S'].append(new_state) | |
| self.step_count += 1 | |
| return None, new_state | |
| elif (self.is_out_of_track(state, action)): | |
| new_state = self.start() | |
| else: | |
| new_state = self.get_new_state(state, action) | |
| self.data.episode['R'].append(reward) | |
| self.data.episode['S'].append(new_state) | |
| self.step_count += 1 | |
| return reward, new_state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment