def initSimulation(car, state): # global obstacels # global all_sprites obstacles.empty() all_sprites.empty() all_sprites.add(car) car.reInit(lane_idx=random.randint(0, 2)) initObstacles(CONST.OBS_L_TO_R, CONST.OBS_R_TO_L) # initial state assumes the agent # has been stationary for a short period car.updateSensors(obstacles)
def initSimulation(car, state, random_start=False, random_obs=False): global obstacels global all_sprites obstacles.empty() all_sprites.empty() all_sprites.add(car) # This is a super hacky way to try and bias away from initialising the car in 'breaking' state # when filling replay buffer # random_start = random.randint(0,len(CONST.ACTION_NAMES)-1) # random_start = random.randint(0,len(CONST.ACTION_NAMES)-1) if random_start == 3 else random_start obs_x = [100, 350, 500] obs_lanes = [1, 2, 3] if random_obs: obs_x = np.random.uniform(low=200, high=CONST.SCREEN_WIDTH * 0.8, size=3) obs_lanes = np.random.randint(low=1, high=4, size=3) rand_x = random.uniform(0, CONST.SCREEN_WIDTH * 0.75) rand_y = random.randint(1, 3) valid = False while not valid: valid = not ( (rand_y == 1 and abs(rand_x - obs_x[1 - 1]) < 1.5 * CONST.CAR_SAFE_BUBBLE) or (rand_y == 2 and abs(rand_x - obs_x[2 - 1]) < 1.5 * CONST.CAR_SAFE_BUBBLE) or (rand_y == 3 and abs(rand_x - obs_x[3 - 1]) < 1.5 * CONST.CAR_SAFE_BUBBLE)) rand_x = random.uniform(0, CONST.SCREEN_WIDTH * 0.75) rand_y = random.randint(1, 3) if random_start: car.reInit(rand_x, rand_y) else: car.reInit(-50, 2) #initObstacles(CONST.OBS_L_TO_R,CONST.OBS_R_TO_L) initStaticObstacles(xPos=obs_x, lanes=obs_lanes) # Initial state assumes the agent # Has been stationary for a short period car.updateSensors(obstacles)
def initSimulation(car, state, filling_buffer=False, x_dist=0, lane=2): global obstacels global all_sprites obstacles.empty() all_sprites.empty() all_sprites.add(car) state.reset() # This is a super hacky way to try and bias away from initialising the car in 'breaking' state # when filling replay buffer # random_start = random.randint(0,len(CONST.ACTION_NAMES)-1) # random_start = random.randint(0,len(CONST.ACTION_NAMES)-1) if random_start == 3 else random_start obs_x = [220] obs_lanes = [2] # rand_x = random.uniform(0, CONST.SCREEN_WIDTH*0.75) # rand_y = random.randint(1,3) # # valid = False # while not valid: # valid = not ((rand_y == 1 and abs(rand_x - obs_x[1-1]) < 1.5*CONST.CAR_SAFE_BUBBLE) or # (rand_y == 2 and abs(rand_x - obs_x[2-1]) < 1.5*CONST.CAR_SAFE_BUBBLE) or # (rand_y == 3 and abs(rand_x - obs_x[3-1]) < 1.5*CONST.CAR_SAFE_BUBBLE)) # rand_x = random.uniform(0, CONST.SCREEN_WIDTH*0.75) # rand_y = random.randint(1,3) # # if filling_buffer: # car.reInit(rand_x, rand_y) # else: lane = 2 x_dist = 0 if filling_buffer: lane = random.randint(1, 3) x_dist = int(random.uniform(0, 0.5) * CONST.SCREEN_WIDTH) while (lane == 2 and abs(x_dist - obs_x[0]) < CONST.CAR_SAFE_BUBBLE): x_dist = int(random.uniform(0, 0.5) * CONST.SCREEN_WIDTH) car.reInit(x_dist, lane) #initObstacles(CONST.OBS_L_TO_R,CONST.OBS_R_TO_L) initStaticObstacles(xPos=obs_x, lanes=obs_lanes) # Initial state assumes the agent # Has been stationary for a short period car.updateSensors(obstacles)
CONST.ACTION_NAMES[action_idx]) else: action_idx = np.argmax(qMatrix) __console_string += "selected action: {0} -- ".format( CONST.ACTION_NAMES[action_idx]) ##### Take action ##### # car.updateAction(2) # apply action selected above car.updateAction(action_idx) # apply action selected above all_sprites.update() # Pygame updating sprites __console_string += "speed: {0} -- ".format(car.speed) collisions = pygame.sprite.spritecollide( car, obstacles, False) # Check for agent obstacle collisions ##### Observe new state (s') ##### car.updateSensors(obstacles) # Sensor update state.update(car.sensor_data) # Update state with new data ##### GET maxQ' from DCNN ##### next_qMatrix = dqnn.getQMat(state.state) reward = car.reward # Check for terminal states and override # Reward to teminal values if necessary # if car.tail_gaiting: # reward = CONST.REWARDS['tail_gate'] # pigs_fly = True # if car.lane_idx == 0: # reward = CONST.REWARDS['on_sholder'] # pigs_fly = True