Advanced workshops/reInvent2019-400/customize/deepracer_racetrack_env_lidar_3cars.py [610:1240]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                ]
            
#             print(self.bot_cars[0].shapely_lane.length)
#             print(self.bot_cars[1].shapely_lane.length)

            self.bot_reset_callback = []
            for bot in self.bot_cars:
                self.bot_reset_callback.append(rospy.Subscriber('/clock', clock, bot.update_bot_sim))
#             for bot in self.bot_cars:
#                 rospy.Subscriber('/clock', clock, bot.update_bot_sim)

    def setup_simtrace_data_upload_to_s3(self):
        if node_type == SIMULATION_WORKER:
            #start timer to upload SIM_TRACE data to s3 when simapp exits
            #There is not enough time to upload data to S3 when robomaker shuts down
            #Set up timer to upload data to S3 300 seconds before the robomaker quits
            # - 300 seocnds is randomly chosen number based on various jobs launched that
            #   provides enough time to upload data to S3
            global simapp_data_upload_timer
            session = boto3.session.Session()
            robomaker_client = session.client('robomaker', region_name=self.aws_region)
            result = robomaker_client.describe_simulation_job(
                job=self.simulation_job_arn
            )
            logger.info("robomaker job description: {}".format(result))
            self.simapp_upload_duration = result['maxJobDurationInSeconds'] - SIMAPP_DATA_UPLOAD_TIME_TO_S3
            start = 0
            if self.job_type == TRAINING_JOB:
                logger.info("simapp training job started_at={}".format(result['lastStartedAt']))
                start = result['lastStartedAt']
                self.simtrace_s3_bucket = rospy.get_param('SAGEMAKER_SHARED_S3_BUCKET')
                self.simtrace_s3_key = os.path.join(rospy.get_param('SAGEMAKER_SHARED_S3_PREFIX'), TRAINING_SIMTRACE_DATA_S3_OBJECT_KEY)
            else:
                logger.info("simapp evaluation job started_at={}".format(result['lastUpdatedAt']))
                start = result['lastUpdatedAt']
                self.simtrace_s3_bucket = rospy.get_param('MODEL_S3_BUCKET')
                self.simtrace_s3_key = os.path.join(rospy.get_param('MODEL_S3_PREFIX'), EVALUATION_SIMTRACE_DATA_S3_OBJECT_KEY)
            end = start + datetime.timedelta(seconds=self.simapp_upload_duration)
            now = datetime.datetime.now(tz=end.tzinfo) # use tzinfo as robomaker
            self.simapp_data_upload_time = (end - now).total_seconds()
            logger.info("simapp job started_at={} now={} end={} upload_data_in={} sec".format(start, now, end, self.simapp_data_upload_time))
            simapp_data_upload_timer = threading.Timer(self.simapp_data_upload_time, simapp_data_upload_timer_expiry)
            simapp_data_upload_timer.daemon = True
            simapp_data_upload_timer.start()
            logger.info("Timer with {} seconds is {}".format(self.simapp_data_upload_time, simapp_data_upload_timer.is_alive()))

            #setup to upload SIM_TRACE_DATA to S3
            self.simtrace_data = DeepRacerRacetrackSimTraceData(self.simtrace_s3_bucket, self.simtrace_s3_key)

    def reset(self):
        if node_type == SAGEMAKER_TRAINING_WORKER:
            return self.observation_space.sample()

        # Simulation is done - so RoboMaker will start to shut down the app.
        # Till RoboMaker shuts down the app, do nothing more else metrics may show unexpected data.
        if (node_type == SIMULATION_WORKER) and self.is_simulation_done:
            while True:
                time.sleep(1)

        self.steering_angle = 0
        self.speed = 0
        self.action_taken = 0
        self.prev_progress = 0
        self.prev_point = Point(0, 0)
        self.prev_point_2 = Point(0, 0)
        self.next_state = None
        self.reward = None
        self.reward_in_episode = 0
        self.done = False
        # Reset the car and record the simulation start time
        if self.allow_servo_step_signals:
            self.send_action(0, 0)

        self.racecar_reset()
        self.steps = 0
        self.simulation_start_time = time.time()
        self.infer_reward_state(0, 0)

        return self.next_state

    def set_next_state(self):
#         # Make sure the first image is the starting image
#         image_data = self.image_queue.get(block=True, timeout=None)
#         # Read the image and resize to get the state
#         image = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
#         image = image.resize(TRAINING_IMAGE_SIZE, resample=2)
#         self.next_state = np.array(image)
        
        next_states = {}
        
        data = rospy.wait_for_message('/scan', LaserScan)
        next_states['LIDAR'] = np.array(data.ranges)
        
        # Make sure the first image is the starting image
        image_data = self.image_queue_left_camera.get(block=True, timeout=None)
        # Read the image and resize to get the state
        imgL = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
        imgL = imgL.resize(TRAINING_IMAGE_SIZE, resample=2)
#         next_states['left_camera'] = np.array(imgL)
        
        image_data = self.image_queue_right_camera.get(block=True, timeout=None)
        imgR = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
        imgR = imgR.resize(TRAINING_IMAGE_SIZE, resample=2)

        imgL = imgL.convert('L')
        imgR = imgR.convert('L')
        imgConcat = np.stack((imgL, imgR), axis=2)
        next_states['STEREO_CAMERAS'] = np.array(imgConcat)

        self.next_state = next_states

    def racecar_reset(self):
        try:
            for joint in EFFORT_JOINTS:
                self.clear_forces_client(joint)
            prev_index, next_index = self.find_prev_next_waypoints(self.start_ndist)
            self.reset_car_client(self.start_ndist, next_index)
            # First clear the queue so that we set the state to the start image
#             _ = self.image_queue.get(block=True, timeout=None)
            _ = self.image_queue_left_camera.get(block=True, timeout=None)
            _ = self.image_queue_right_camera.get(block=True, timeout=None)
            self.set_next_state()

        except Exception as ex:
            utils.json_format_logger("Unable to reset the car: {}".format(ex),
                         **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION,
                                                         utils.SIMAPP_EVENT_ERROR_CODE_500))

    def set_allow_servo_step_signals(self, allow_servo_step_signals):
        self.allow_servo_step_signals = allow_servo_step_signals

    def step(self, action):
        if node_type == SAGEMAKER_TRAINING_WORKER:
            return self.observation_space.sample(), 0, False, {}

        # Initialize next state, reward, done flag
        self.next_state = None
        self.reward = None
        self.done = False

        # Send this action to Gazebo and increment the step count
        # if self.job_type == EVALUATION_JOB:
        noise_fraction = 0.1
        delta_steering = float(action[0]) * noise_fraction
        self.steering_angle = np.random.uniform(float(action[0])-delta_steering, float(action[0])+delta_steering)
        delta_speed = float(action[1]) * noise_fraction
        self.speed = max(0, np.random.uniform(float(action[1])-delta_speed, float(action[1])+delta_speed))
        # else:
        #     self.steering_angle = float(action[0])
        #     self.speed = float(action[1])

        if self.allow_servo_step_signals:
            self.send_action(self.steering_angle, self.speed)
        self.steps += 1

        # Compute the next state and reward
        self.infer_reward_state(self.steering_angle, self.speed)
        return self.next_state, self.reward, self.done, {}

#     def callback_image(self, data):
#         try:
#             self.image_queue.put_nowait(data)
#         except queue.Full:
#             pass
#         except Exception as ex:
#             utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
#                        **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))
            
    def callback_image_left_camera(self, data):
        try:
            self.image_queue_left_camera.put_nowait(data)
        except queue.Full:
            pass
        except Exception as ex:
            utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))
            
    def callback_image_right_camera(self, data):
        try:
            self.image_queue_right_camera.put_nowait(data)
        except queue.Full:
            pass
        except Exception as ex:
            utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))

    def send_action(self, steering_angle, speed):
        # Simple v/r to computes the desired rpm
        wheel_rpm = speed/WHEEL_RADIUS

        for _, pub in self.velocity_pub_dict.items():
            pub.publish(wheel_rpm)

        for _, pub in self.steering_pub_dict.items():
            pub.publish(steering_angle)

    def infer_reward_state(self, steering_angle, speed):
        try:
            self.set_next_state()
        except Exception as ex:
            utils.json_format_logger("Unable to retrieve image from queue: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))

        # Read model state from Gazebo
        model_state = self.get_model_state('racecar', '')
        model_orientation = Rotation.from_quat([
            model_state.pose.orientation.x,
            model_state.pose.orientation.y,
            model_state.pose.orientation.z,
            model_state.pose.orientation.w])
        model_location = np.array([
            model_state.pose.position.x,
            model_state.pose.position.y,
            model_state.pose.position.z]) + \
            model_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        model_point = Point(model_location[0], model_location[1])
        model_heading = model_orientation.as_euler('zyx')[0]

        # Read the wheel locations from Gazebo
        left_rear_wheel_state = self.get_link_state('racecar::left_rear_wheel', '')
        left_front_wheel_state = self.get_link_state('racecar::left_front_wheel', '')
        right_rear_wheel_state = self.get_link_state('racecar::right_rear_wheel', '')
        right_front_wheel_state = self.get_link_state('racecar::right_front_wheel', '')
        wheel_points = [
            Point(left_rear_wheel_state.link_state.pose.position.x,
                  left_rear_wheel_state.link_state.pose.position.y),
            Point(left_front_wheel_state.link_state.pose.position.x,
                  left_front_wheel_state.link_state.pose.position.y),
            Point(right_rear_wheel_state.link_state.pose.position.x,
                  right_rear_wheel_state.link_state.pose.position.y),
            Point(right_front_wheel_state.link_state.pose.position.x,
                  right_front_wheel_state.link_state.pose.position.y)
        ]

        # Project the current location onto the center line and find nearest points
        current_ndist = self.center_line.project(model_point, normalized=True)
        prev_index, next_index = self.find_prev_next_waypoints(current_ndist)
        distance_from_prev = model_point.distance(Point(self.center_line.coords[prev_index]))
        distance_from_next = model_point.distance(Point(self.center_line.coords[next_index]))
        closest_waypoint_index = (prev_index, next_index)[distance_from_next < distance_from_prev]

        # Compute distance from center and road width
        nearest_point_center = self.center_line.interpolate(current_ndist, normalized=True)
        nearest_point_inner = self.inner_border.interpolate(self.inner_border.project(nearest_point_center))
        nearest_point_outer = self.outer_border.interpolate(self.outer_border.project(nearest_point_center))
        distance_from_center = nearest_point_center.distance(model_point)
        distance_from_inner = nearest_point_inner.distance(model_point)
        distance_from_outer = nearest_point_outer.distance(model_point)
        track_width = nearest_point_inner.distance(nearest_point_outer)
        is_left_of_center = (distance_from_outer < distance_from_inner) if self.reverse_dir \
            else (distance_from_inner < distance_from_outer)
        
        # Compute the distance to the closest bot car
        is_crashed = False
        dist_closest_bot_car = 1e3 # large number
        closest_bot = -1           # index of closest bot car
        for kk in range(len(self.bot_cars)):
            dist_to_bot_car = np.sqrt((model_state.pose.position.x - self.bot_cars[kk].car_model_state.pose.position.x) ** 2
                                      + (model_state.pose.position.y - self.bot_cars[kk].car_model_state.pose.position.y) ** 2)
            if dist_to_bot_car < dist_closest_bot_car:
                dist_closest_bot_car = dist_to_bot_car
                closest_bot = kk
                
#         if self.bot_cars[closest_bot].shapely_lane.length < 16:
#             is_left_lane_bot = False if self.bot_cars[closest_bot].reverse_dir else True
#         else:
#             is_left_lane_bot = True if self.bot_cars[closest_bot].reverse_dir else False
                
        botcar = self.bot_cars[closest_bot].car_model_state
        botcar_orientation = Rotation.from_quat([
            botcar.pose.orientation.x,
            botcar.pose.orientation.y,
            botcar.pose.orientation.z,
            botcar.pose.orientation.w])
        botcar_heading = botcar_orientation.as_euler('zyx')[0]
        
        botcar_location = np.array([
            botcar.pose.position.x,
            botcar.pose.position.y,
            botcar.pose.position.z]) + \
            botcar_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        botcar_point = Point(botcar_location[0], botcar_location[1])
        botcar_current_ndist = self.center_line.project(botcar_point, normalized=True)
        botcar_prev_index, botcar_next_index = self.find_prev_next_waypoints(botcar_current_ndist)
        
        if next_index <= botcar_next_index:    # corner case: last few waypoints
            flag_model_car_behind = True
        else:
            flag_model_car_behind = False
            
        if flag_model_car_behind:
            behind_car_heading = model_heading * 180.0 / math.pi
            angle_to_front_car = math.atan2(botcar_point.y - model_point.y, botcar_point.x - model_point.x)
            angle_to_front_car = angle_to_front_car * 180.0 / math.pi
        else:
            behind_car_heading = botcar_heading * 180.0 / math.pi
            angle_to_front_car = math.atan2(model_point.y - botcar_point.y, model_point.x - botcar_point.x)
            angle_to_front_car = angle_to_front_car * 180.0 / math.pi
            
        abs_diff = abs(angle_to_front_car - behind_car_heading)
        if abs_diff > 180:
            abs_diff = 360 - abs_diff
            
        if abs_diff < self.safe_angle:
            flag_unsafe = True
        else:
            flag_unsafe = False
        
        if flag_unsafe and flag_model_car_behind and dist_closest_bot_car < 0.4:
            is_crashed = True
        elif flag_unsafe and not flag_model_car_behind and dist_closest_bot_car < 0.3:
            is_crashed = True
        elif not flag_unsafe and dist_closest_bot_car < 0.25:
            is_crashed = True

        # SAHIKA: Quantize the shapely projections to 2 digits
        current_ndist = np.around(current_ndist, decimals=2)
        self.start_ndist = np.around(self.start_ndist, decimals=2)

        # Convert current progress to be [0,100] starting at the initial waypoint
        if self.reverse_dir:
            current_progress = self.start_ndist - current_ndist
        else:
            current_progress = current_ndist - self.start_ndist
        
        # SAHIKA: adding the error margin because due to quantization errors some close points results in negative error
        #if current_progress < 0.0: current_progress = current_progress + 1.0
        if current_progress < 0.0: 
            if abs(current_progress) < 0.011:
                current_progress = 0.0
            else:
                current_progress = current_progress + 1.0
            
        
        current_progress = 100 * current_progress
        
        if current_progress < self.prev_progress:
            # Either: (1) we wrapped around and have finished the track,
            delta1 = current_progress + 100 - self.prev_progress
            # or (2) for some reason the car went backwards (this should be rare)
            delta2 = self.prev_progress - current_progress
            current_progress = (self.prev_progress, 100)[delta1 < delta2]

        # Car is off track if all wheels are outside the borders
        wheel_on_track = [self.road_poly.contains(p) for p in wheel_points]
        all_wheels_on_track = all(wheel_on_track)
        any_wheels_on_track = any(wheel_on_track)
        
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        # SAHIKA 
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        learner_car_position = Point(model_state.pose.position.x, model_state.pose.position.y)
        learner_car_progress = self.center_line.project(learner_car_position)
        learner_car_lane = self.center_line.contains(learner_car_position)
        
        bot_car_progress = list()
        bot_car_lane_match = list()
        for kk in range(len(self.bot_cars)):
            bot_car_position = Point(self.bot_cars[kk].car_model_state.pose.position.x, self.bot_cars[kk].car_model_state.pose.position.y)
            bot_car_progress.append(self.center_line.project(bot_car_position) - learner_car_progress)
            
            bot_car_lane = self.center_line.contains(bot_car_position)
            if learner_car_lane == bot_car_lane:
                bot_car_lane_match.append(True)
            else:
                bot_car_lane_match.append(False)
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        bot_car_progress = np.array(bot_car_progress)
        if all(bot_car_progress < 0):
            ahead_bot_index = np.argmin(bot_car_progress)
            ahead_bot_lane_match = bot_car_lane_match[ahead_bot_index]
        else:
            ahead_bot_index = np.where(bot_car_progress > 0, bot_car_progress, np.inf).argmin()
            ahead_bot_lane_match = bot_car_lane_match[ahead_bot_index]
            
        THRESHOLD1, THRESHOLD2 = 5, 50

        is_bot_in_left_camera = False
        _, thr1 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,0], THRESHOLD1, 1, cv2.THRESH_BINARY)
        _, thr2 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,0], THRESHOLD2, 1, cv2.THRESH_BINARY)
        thr = cv2.bitwise_xor(thr1, thr2)
        thr = cv2.convertScaleAbs(thr)
        contours, hierarchy = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        bot_size_in_left_camera = float('nan')
        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            if w * h > 70:
                is_bot_in_left_camera = True
                bot_size_in_left_camera = w*h
                break
                
        is_bot_in_right_camera = False
        _, thr1 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,1], THRESHOLD1, 1, cv2.THRESH_BINARY)
        _, thr2 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,1], THRESHOLD2, 1, cv2.THRESH_BINARY)
        thr = cv2.bitwise_xor(thr1, thr2)
        thr = cv2.convertScaleAbs(thr)
        contours, hierarchy = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        bot_size_in_right_camera = float('nan')
        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            if w * h > 70:
                is_bot_in_right_camera = True
                bot_size_in_right_camera = w*h
                break
                
        is_bot_in_camera = is_bot_in_left_camera or is_bot_in_right_camera
        
        # Compute the reward
        reward = 0.0
        if (not is_crashed) and any_wheels_on_track:
            done = False
            params = {
                'all_wheels_on_track': all_wheels_on_track,
                'x': model_point.x,
                'y': model_point.y,
                'heading': model_heading * 180.0 / math.pi,
                'distance_from_center': distance_from_center,
                'progress': current_progress,
                'steps': self.steps,
                'speed': speed,
                'steering_angle': steering_angle * 180.0 / math.pi,
                'track_width': track_width,
                'waypoints': list(self.center_line.coords),
                'closest_waypoints': [prev_index, next_index],
                'is_left_of_center': is_left_of_center,
                'is_reversed': self.reverse_dir,
                'is_crashed': is_crashed,
                'dist_closest_bot': dist_closest_bot_car,
                'flag_unsafe': flag_unsafe,
                'bot_car_progress': bot_car_progress,
                'bot_car_lane_match': ahead_bot_lane_match,
                'is_bot_in_camera': is_bot_in_camera,
                'bot_size_in_left_camera': bot_size_in_left_camera,
                'bot_size_in_right_camera': bot_size_in_right_camera
            }
            try:
                reward = float(self.reward_function(params))
            except Exception as e:
                utils.json_format_logger("Exception {} in customer reward function. Job failed!".format(e),
                                         **utils.build_user_error_dict(utils.SIMAPP_SIMULATION_WORKER_EXCEPTION,
                                                                       utils.SIMAPP_EVENT_ERROR_CODE_400))
                traceback.print_exc()
                utils.simapp_exit_gracefully()
        else:
            done = True
            reward = CRASHED

        # Reset if the car position hasn't changed in the last 2 steps
        prev_pnt_dist = min(model_point.distance(self.prev_point), model_point.distance(self.prev_point_2))

        if prev_pnt_dist <= 0.0001 and self.steps % NUM_STEPS_TO_CHECK_STUCK == 0:
            done = True
            reward = CRASHED  # stuck

        # Simulation jobs are done when progress reaches 100
        if current_progress >= 100:
            done = True
#             reward = 1e3

        # Keep data from the previous step around
        self.prev_point_2 = self.prev_point
        self.prev_point = model_point
        self.prev_progress = current_progress

        # Set the reward and done flag
        self.reward = reward
        self.reward_in_episode += reward
        self.done = done

        # Trace logs to help us debug and visualize the training runs
        # btown TODO: This should be written to S3, not to CWL.
        logger.info('SIM_TRACE_LOG:%d,%d,%.4f,%.4f,%.4f,%.2f,%.2f,%d,%.4f,%s,%s,%.4f,%d,%.2f,%s,%s,%.2f\n' % (
            self.episodes, self.steps, model_location[0], model_location[1], model_heading,
            self.steering_angle,
            self.speed,
            self.action_taken,
            self.reward,
            self.done,
            all_wheels_on_track,
            current_progress,
            closest_waypoint_index,
            self.track_length,
            time.time(),
            is_crashed,
            dist_closest_bot_car))

        #build json record of the reward metrics
        reward_metrics = OrderedDict()
        reward_metrics['episode'] = self.episodes
        reward_metrics['steps'] = self.steps
        reward_metrics['X'] = model_location[0]
        reward_metrics['Y'] = model_location[1]
        reward_metrics['yaw'] = model_heading
        reward_metrics['steer'] = self.steering_angle
        reward_metrics['throttle'] = self.speed
        reward_metrics['action'] = self.action_taken
        reward_metrics['reward'] = self.reward
        reward_metrics['done'] = self.done
        reward_metrics['all_wheels_on_track'] = all_wheels_on_track
        reward_metrics['progress'] = current_progress
        reward_metrics['closest_waypoint'] = closest_waypoint_index
        reward_metrics['track_len'] = self.track_length
        reward_metrics['tstamp'] = time.time()
        # self.simtrace_data.write_simtrace_data(reward_metrics)

        # Terminate this episode when ready
        if done and node_type == SIMULATION_WORKER:
            self.finish_episode(current_progress)

    def find_prev_next_waypoints(self, ndist):
        if self.reverse_dir:
            next_index = bisect.bisect_left(self.center_dists, ndist) - 1
            prev_index = next_index + 1
            if next_index == -1: next_index = len(self.center_dists) - 1
        else:
            next_index = bisect.bisect_right(self.center_dists, ndist)
            prev_index = next_index - 1
            if next_index == len(self.center_dists): next_index = 0
        return prev_index, next_index

    def stop_car(self):
        self.steering_angle = 0
        self.speed = 0
        self.action_taken = 0
        self.send_action(0, 0)
        self.racecar_reset()

    def finish_episode(self, progress):
        # Increment episode count, update start position and direction
        self.episodes += 1            
#         if self.change_start:
#             if self.job_type == TRAINING_JOB:
#                 self.start_ndist = (self.start_ndist + ROUND_ROBIN_ADVANCE_DIST) % 1.0
#             else:
#                 self.start_ndist = (self.start_ndist + 2*ROUND_ROBIN_ADVANCE_DIST) % 1.0
        if self.change_start:
#             if self.checkpoint_num < 20 or progress > 40 or self.job_type == EVALUATION_JOB:
            self.start_ndist_index = (self.start_ndist_index + 1) % len(self.start_ndist_map)
            self.start_ndist = self.start_ndist_map[self.start_ndist_index]
        if self.alternate_dir:
            self.reverse_dir = not self.reverse_dir

        # Reset the car
        self.stop_car()

        # upload SIM_TRACE data to S3
        # self.simtrace_data.upload_to_s3(self.episodes)

        # Update metrics based on job type
        if self.job_type == TRAINING_JOB:
            self.update_training_metrics(progress)
            self.write_metrics_to_s3()
            if self.is_training_done():
                self.cancel_simulation_job()
        elif self.job_type == EVALUATION_JOB:
            self.number_of_trials += 1
            self.update_eval_metrics(progress)
            self.write_metrics_to_s3()

    def set_checkpoint_num(self, current_checkpoint):
        self.checkpoint_num = current_checkpoint

    def update_eval_metrics(self, progress):
        eval_metric = {}
        eval_metric['checkpoint_num'] = self.checkpoint_num
        eval_metric['completion_percentage'] = int(progress)
        eval_metric['metric_time'] = int(round(time.time() * 1000))
        eval_metric['start_time'] = int(round(self.simulation_start_time * 1000))
        eval_metric['elapsed_time_in_milliseconds'] = int(round((time.time() - self.simulation_start_time) * 1000))
        eval_metric['trial'] = int(self.number_of_trials)
        self.metrics.append(eval_metric)

    def update_training_metrics(self, progress):
        training_metric = {}
        training_metric['checkpoint_num'] = self.checkpoint_num
        training_metric['reward_score'] = int(round(self.reward_in_episode))
        training_metric['metric_time'] = int(round(time.time() * 1000))
        training_metric['start_time'] = int(round(self.simulation_start_time * 1000))
        training_metric['elapsed_time_in_milliseconds'] = int(round((time.time() - self.simulation_start_time) * 1000))
        training_metric['episode'] = int(self.episodes)
        training_metric['completion_percentage'] = int(progress)
        self.metrics.append(training_metric)

    def write_metrics_to_s3(self):
        session = boto3.session.Session()
        s3_client = session.client('s3', region_name=self.aws_region)
        metrics_body = json.dumps({'metrics': self.metrics})
        s3_client.put_object(
            Bucket=self.metrics_s3_bucket,
            Key=self.metrics_s3_object_key,
            Body=bytes(metrics_body, encoding='utf-8')
        )

    def is_training_done(self):
        if ((self.target_number_of_episodes > 0) and (self.target_number_of_episodes == self.episodes)) or \
           ((isinstance(self.target_reward_score, (int, float))) and (self.target_reward_score <= self.reward_in_episode)):
            self.is_simulation_done = True
        return self.is_simulation_done

    def cancel_simulation_job(self):
        logger.info ("cancel_simulation_job: make sure to shutdown simapp first")
        simapp_shutdown()

        session = boto3.session.Session()
        robomaker_client = session.client('robomaker', region_name=self.aws_region)
        robomaker_client.cancel_simulation_job(
            job=self.simulation_job_arn
        )

class DeepRacerRacetrackCustomActionSpaceEnv(DeepRacerRacetrackEnv):
    def __init__(self):
        DeepRacerRacetrackEnv.__init__(self)
        try:
            # Try loading the custom model metadata (may or may not be present)
            with open('custom_files/model_metadata.json', 'r') as f:
                model_metadata = json.load(f)
                self.json_actions = model_metadata['action_space']
            logger.info("Loaded action space from file: {}".format(self.json_actions))
        except Exception as ex:
            # Failed to load, fall back on the default action space
            from markov.defaults import model_metadata
            self.json_actions = model_metadata['action_space']
            logger.info("Exception {} on loading custom action space, using default: {}".format(ex, self.json_actions))
        self.action_space = spaces.Discrete(len(self.json_actions))

    def step(self, action):
        self.steering_angle = float(self.json_actions[action]['steering_angle']) * math.pi / 180.0
        self.speed = float(self.json_actions[action]['speed'])
        self.action_taken = action
        return super().step([self.steering_angle, self.speed])
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



Advanced workshops/reInvent2019-400/customize/deepracer_racetrack_env_lidar_5cars.py [612:1242]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                ]
            
#             print(self.bot_cars[0].shapely_lane.length)
#             print(self.bot_cars[1].shapely_lane.length)

            self.bot_reset_callback = []
            for bot in self.bot_cars:
                self.bot_reset_callback.append(rospy.Subscriber('/clock', clock, bot.update_bot_sim))
#             for bot in self.bot_cars:
#                 rospy.Subscriber('/clock', clock, bot.update_bot_sim)

    def setup_simtrace_data_upload_to_s3(self):
        if node_type == SIMULATION_WORKER:
            #start timer to upload SIM_TRACE data to s3 when simapp exits
            #There is not enough time to upload data to S3 when robomaker shuts down
            #Set up timer to upload data to S3 300 seconds before the robomaker quits
            # - 300 seocnds is randomly chosen number based on various jobs launched that
            #   provides enough time to upload data to S3
            global simapp_data_upload_timer
            session = boto3.session.Session()
            robomaker_client = session.client('robomaker', region_name=self.aws_region)
            result = robomaker_client.describe_simulation_job(
                job=self.simulation_job_arn
            )
            logger.info("robomaker job description: {}".format(result))
            self.simapp_upload_duration = result['maxJobDurationInSeconds'] - SIMAPP_DATA_UPLOAD_TIME_TO_S3
            start = 0
            if self.job_type == TRAINING_JOB:
                logger.info("simapp training job started_at={}".format(result['lastStartedAt']))
                start = result['lastStartedAt']
                self.simtrace_s3_bucket = rospy.get_param('SAGEMAKER_SHARED_S3_BUCKET')
                self.simtrace_s3_key = os.path.join(rospy.get_param('SAGEMAKER_SHARED_S3_PREFIX'), TRAINING_SIMTRACE_DATA_S3_OBJECT_KEY)
            else:
                logger.info("simapp evaluation job started_at={}".format(result['lastUpdatedAt']))
                start = result['lastUpdatedAt']
                self.simtrace_s3_bucket = rospy.get_param('MODEL_S3_BUCKET')
                self.simtrace_s3_key = os.path.join(rospy.get_param('MODEL_S3_PREFIX'), EVALUATION_SIMTRACE_DATA_S3_OBJECT_KEY)
            end = start + datetime.timedelta(seconds=self.simapp_upload_duration)
            now = datetime.datetime.now(tz=end.tzinfo) # use tzinfo as robomaker
            self.simapp_data_upload_time = (end - now).total_seconds()
            logger.info("simapp job started_at={} now={} end={} upload_data_in={} sec".format(start, now, end, self.simapp_data_upload_time))
            simapp_data_upload_timer = threading.Timer(self.simapp_data_upload_time, simapp_data_upload_timer_expiry)
            simapp_data_upload_timer.daemon = True
            simapp_data_upload_timer.start()
            logger.info("Timer with {} seconds is {}".format(self.simapp_data_upload_time, simapp_data_upload_timer.is_alive()))

            #setup to upload SIM_TRACE_DATA to S3
            self.simtrace_data = DeepRacerRacetrackSimTraceData(self.simtrace_s3_bucket, self.simtrace_s3_key)

    def reset(self):
        if node_type == SAGEMAKER_TRAINING_WORKER:
            return self.observation_space.sample()

        # Simulation is done - so RoboMaker will start to shut down the app.
        # Till RoboMaker shuts down the app, do nothing more else metrics may show unexpected data.
        if (node_type == SIMULATION_WORKER) and self.is_simulation_done:
            while True:
                time.sleep(1)

        self.steering_angle = 0
        self.speed = 0
        self.action_taken = 0
        self.prev_progress = 0
        self.prev_point = Point(0, 0)
        self.prev_point_2 = Point(0, 0)
        self.next_state = None
        self.reward = None
        self.reward_in_episode = 0
        self.done = False
        # Reset the car and record the simulation start time
        if self.allow_servo_step_signals:
            self.send_action(0, 0)

        self.racecar_reset()
        self.steps = 0
        self.simulation_start_time = time.time()
        self.infer_reward_state(0, 0)

        return self.next_state

    def set_next_state(self):
#         # Make sure the first image is the starting image
#         image_data = self.image_queue.get(block=True, timeout=None)
#         # Read the image and resize to get the state
#         image = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
#         image = image.resize(TRAINING_IMAGE_SIZE, resample=2)
#         self.next_state = np.array(image)
        
        next_states = {}
        
        data = rospy.wait_for_message('/scan', LaserScan)
        next_states['LIDAR'] = np.array(data.ranges)
        
        # Make sure the first image is the starting image
        image_data = self.image_queue_left_camera.get(block=True, timeout=None)
        # Read the image and resize to get the state
        imgL = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
        imgL = imgL.resize(TRAINING_IMAGE_SIZE, resample=2)
#         next_states['left_camera'] = np.array(imgL)
        
        image_data = self.image_queue_right_camera.get(block=True, timeout=None)
        imgR = Image.frombytes('RGB', (image_data.width, image_data.height), image_data.data, 'raw', 'RGB', 0, 1)
        imgR = imgR.resize(TRAINING_IMAGE_SIZE, resample=2)

        imgL = imgL.convert('L')
        imgR = imgR.convert('L')
        imgConcat = np.stack((imgL, imgR), axis=2)
        next_states['STEREO_CAMERAS'] = np.array(imgConcat)

        self.next_state = next_states

    def racecar_reset(self):
        try:
            for joint in EFFORT_JOINTS:
                self.clear_forces_client(joint)
            prev_index, next_index = self.find_prev_next_waypoints(self.start_ndist)
            self.reset_car_client(self.start_ndist, next_index)
            # First clear the queue so that we set the state to the start image
#             _ = self.image_queue.get(block=True, timeout=None)
            _ = self.image_queue_left_camera.get(block=True, timeout=None)
            _ = self.image_queue_right_camera.get(block=True, timeout=None)
            self.set_next_state()

        except Exception as ex:
            utils.json_format_logger("Unable to reset the car: {}".format(ex),
                         **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION,
                                                         utils.SIMAPP_EVENT_ERROR_CODE_500))

    def set_allow_servo_step_signals(self, allow_servo_step_signals):
        self.allow_servo_step_signals = allow_servo_step_signals

    def step(self, action):
        if node_type == SAGEMAKER_TRAINING_WORKER:
            return self.observation_space.sample(), 0, False, {}

        # Initialize next state, reward, done flag
        self.next_state = None
        self.reward = None
        self.done = False

        # Send this action to Gazebo and increment the step count
        # if self.job_type == EVALUATION_JOB:
        noise_fraction = 0.1
        delta_steering = float(action[0]) * noise_fraction
        self.steering_angle = np.random.uniform(float(action[0])-delta_steering, float(action[0])+delta_steering)
        delta_speed = float(action[1]) * noise_fraction
        self.speed = max(0, np.random.uniform(float(action[1])-delta_speed, float(action[1])+delta_speed))
        # else:
        #     self.steering_angle = float(action[0])
        #     self.speed = float(action[1])

        if self.allow_servo_step_signals:
            self.send_action(self.steering_angle, self.speed)
        self.steps += 1

        # Compute the next state and reward
        self.infer_reward_state(self.steering_angle, self.speed)
        return self.next_state, self.reward, self.done, {}

#     def callback_image(self, data):
#         try:
#             self.image_queue.put_nowait(data)
#         except queue.Full:
#             pass
#         except Exception as ex:
#             utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
#                        **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))
            
    def callback_image_left_camera(self, data):
        try:
            self.image_queue_left_camera.put_nowait(data)
        except queue.Full:
            pass
        except Exception as ex:
            utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))
            
    def callback_image_right_camera(self, data):
        try:
            self.image_queue_right_camera.put_nowait(data)
        except queue.Full:
            pass
        except Exception as ex:
            utils.json_format_logger("Error retrieving frame from gazebo: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))

    def send_action(self, steering_angle, speed):
        # Simple v/r to computes the desired rpm
        wheel_rpm = speed/WHEEL_RADIUS

        for _, pub in self.velocity_pub_dict.items():
            pub.publish(wheel_rpm)

        for _, pub in self.steering_pub_dict.items():
            pub.publish(steering_angle)

    def infer_reward_state(self, steering_angle, speed):
        try:
            self.set_next_state()
        except Exception as ex:
            utils.json_format_logger("Unable to retrieve image from queue: {}".format(ex),
                       **utils.build_system_error_dict(utils.SIMAPP_ENVIRONMENT_EXCEPTION, utils.SIMAPP_EVENT_ERROR_CODE_500))

        # Read model state from Gazebo
        model_state = self.get_model_state('racecar', '')
        model_orientation = Rotation.from_quat([
            model_state.pose.orientation.x,
            model_state.pose.orientation.y,
            model_state.pose.orientation.z,
            model_state.pose.orientation.w])
        model_location = np.array([
            model_state.pose.position.x,
            model_state.pose.position.y,
            model_state.pose.position.z]) + \
            model_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        model_point = Point(model_location[0], model_location[1])
        model_heading = model_orientation.as_euler('zyx')[0]

        # Read the wheel locations from Gazebo
        left_rear_wheel_state = self.get_link_state('racecar::left_rear_wheel', '')
        left_front_wheel_state = self.get_link_state('racecar::left_front_wheel', '')
        right_rear_wheel_state = self.get_link_state('racecar::right_rear_wheel', '')
        right_front_wheel_state = self.get_link_state('racecar::right_front_wheel', '')
        wheel_points = [
            Point(left_rear_wheel_state.link_state.pose.position.x,
                  left_rear_wheel_state.link_state.pose.position.y),
            Point(left_front_wheel_state.link_state.pose.position.x,
                  left_front_wheel_state.link_state.pose.position.y),
            Point(right_rear_wheel_state.link_state.pose.position.x,
                  right_rear_wheel_state.link_state.pose.position.y),
            Point(right_front_wheel_state.link_state.pose.position.x,
                  right_front_wheel_state.link_state.pose.position.y)
        ]

        # Project the current location onto the center line and find nearest points
        current_ndist = self.center_line.project(model_point, normalized=True)
        prev_index, next_index = self.find_prev_next_waypoints(current_ndist)
        distance_from_prev = model_point.distance(Point(self.center_line.coords[prev_index]))
        distance_from_next = model_point.distance(Point(self.center_line.coords[next_index]))
        closest_waypoint_index = (prev_index, next_index)[distance_from_next < distance_from_prev]

        # Compute distance from center and road width
        nearest_point_center = self.center_line.interpolate(current_ndist, normalized=True)
        nearest_point_inner = self.inner_border.interpolate(self.inner_border.project(nearest_point_center))
        nearest_point_outer = self.outer_border.interpolate(self.outer_border.project(nearest_point_center))
        distance_from_center = nearest_point_center.distance(model_point)
        distance_from_inner = nearest_point_inner.distance(model_point)
        distance_from_outer = nearest_point_outer.distance(model_point)
        track_width = nearest_point_inner.distance(nearest_point_outer)
        is_left_of_center = (distance_from_outer < distance_from_inner) if self.reverse_dir \
            else (distance_from_inner < distance_from_outer)
        
        # Compute the distance to the closest bot car
        is_crashed = False
        dist_closest_bot_car = 1e3 # large number
        closest_bot = -1           # index of closest bot car
        for kk in range(len(self.bot_cars)):
            dist_to_bot_car = np.sqrt((model_state.pose.position.x - self.bot_cars[kk].car_model_state.pose.position.x) ** 2
                                      + (model_state.pose.position.y - self.bot_cars[kk].car_model_state.pose.position.y) ** 2)
            if dist_to_bot_car < dist_closest_bot_car:
                dist_closest_bot_car = dist_to_bot_car
                closest_bot = kk
                
#         if self.bot_cars[closest_bot].shapely_lane.length < 16:
#             is_left_lane_bot = False if self.bot_cars[closest_bot].reverse_dir else True
#         else:
#             is_left_lane_bot = True if self.bot_cars[closest_bot].reverse_dir else False
                
        botcar = self.bot_cars[closest_bot].car_model_state
        botcar_orientation = Rotation.from_quat([
            botcar.pose.orientation.x,
            botcar.pose.orientation.y,
            botcar.pose.orientation.z,
            botcar.pose.orientation.w])
        botcar_heading = botcar_orientation.as_euler('zyx')[0]
        
        botcar_location = np.array([
            botcar.pose.position.x,
            botcar.pose.position.y,
            botcar.pose.position.z]) + \
            botcar_orientation.apply(RELATIVE_POSITION_OF_FRONT_OF_CAR)
        botcar_point = Point(botcar_location[0], botcar_location[1])
        botcar_current_ndist = self.center_line.project(botcar_point, normalized=True)
        botcar_prev_index, botcar_next_index = self.find_prev_next_waypoints(botcar_current_ndist)
        
        if next_index <= botcar_next_index:    # corner case: last few waypoints
            flag_model_car_behind = True
        else:
            flag_model_car_behind = False
            
        if flag_model_car_behind:
            behind_car_heading = model_heading * 180.0 / math.pi
            angle_to_front_car = math.atan2(botcar_point.y - model_point.y, botcar_point.x - model_point.x)
            angle_to_front_car = angle_to_front_car * 180.0 / math.pi
        else:
            behind_car_heading = botcar_heading * 180.0 / math.pi
            angle_to_front_car = math.atan2(model_point.y - botcar_point.y, model_point.x - botcar_point.x)
            angle_to_front_car = angle_to_front_car * 180.0 / math.pi
            
        abs_diff = abs(angle_to_front_car - behind_car_heading)
        if abs_diff > 180:
            abs_diff = 360 - abs_diff
            
        if abs_diff < self.safe_angle:
            flag_unsafe = True
        else:
            flag_unsafe = False
        
        if flag_unsafe and flag_model_car_behind and dist_closest_bot_car < 0.4:
            is_crashed = True
        elif flag_unsafe and not flag_model_car_behind and dist_closest_bot_car < 0.3:
            is_crashed = True
        elif not flag_unsafe and dist_closest_bot_car < 0.25:
            is_crashed = True

        # SAHIKA: Quantize the shapely projections to 2 digits
        current_ndist = np.around(current_ndist, decimals=2)
        self.start_ndist = np.around(self.start_ndist, decimals=2)

        # Convert current progress to be [0,100] starting at the initial waypoint
        if self.reverse_dir:
            current_progress = self.start_ndist - current_ndist
        else:
            current_progress = current_ndist - self.start_ndist
        
        # SAHIKA: adding the error margin because due to quantization errors some close points results in negative error
        #if current_progress < 0.0: current_progress = current_progress + 1.0
        if current_progress < 0.0: 
            if abs(current_progress) < 0.011:
                current_progress = 0.0
            else:
                current_progress = current_progress + 1.0
            
        
        current_progress = 100 * current_progress
        
        if current_progress < self.prev_progress:
            # Either: (1) we wrapped around and have finished the track,
            delta1 = current_progress + 100 - self.prev_progress
            # or (2) for some reason the car went backwards (this should be rare)
            delta2 = self.prev_progress - current_progress
            current_progress = (self.prev_progress, 100)[delta1 < delta2]

        # Car is off track if all wheels are outside the borders
        wheel_on_track = [self.road_poly.contains(p) for p in wheel_points]
        all_wheels_on_track = all(wheel_on_track)
        any_wheels_on_track = any(wheel_on_track)
        
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        # SAHIKA 
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        learner_car_position = Point(model_state.pose.position.x, model_state.pose.position.y)
        learner_car_progress = self.center_line.project(learner_car_position)
        learner_car_lane = self.center_line.contains(learner_car_position)
        
        bot_car_progress = list()
        bot_car_lane_match = list()
        for kk in range(len(self.bot_cars)):
            bot_car_position = Point(self.bot_cars[kk].car_model_state.pose.position.x, self.bot_cars[kk].car_model_state.pose.position.y)
            bot_car_progress.append(self.center_line.project(bot_car_position) - learner_car_progress)
            
            bot_car_lane = self.center_line.contains(bot_car_position)
            if learner_car_lane == bot_car_lane:
                bot_car_lane_match.append(True)
            else:
                bot_car_lane_match.append(False)
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        
        bot_car_progress = np.array(bot_car_progress)
        if all(bot_car_progress < 0):
            ahead_bot_index = np.argmin(bot_car_progress)
            ahead_bot_lane_match = bot_car_lane_match[ahead_bot_index]
        else:
            ahead_bot_index = np.where(bot_car_progress > 0, bot_car_progress, np.inf).argmin()
            ahead_bot_lane_match = bot_car_lane_match[ahead_bot_index]
            
        THRESHOLD1, THRESHOLD2 = 5, 50

        is_bot_in_left_camera = False
        _, thr1 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,0], THRESHOLD1, 1, cv2.THRESH_BINARY)
        _, thr2 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,0], THRESHOLD2, 1, cv2.THRESH_BINARY)
        thr = cv2.bitwise_xor(thr1, thr2)
        thr = cv2.convertScaleAbs(thr)
        contours, hierarchy = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        bot_size_in_left_camera = float('nan')
        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            if w * h > 70:
                is_bot_in_left_camera = True
                bot_size_in_left_camera = w*h
                break
                
        is_bot_in_right_camera = False
        _, thr1 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,1], THRESHOLD1, 1, cv2.THRESH_BINARY)
        _, thr2 = cv2.threshold(self.next_state['STEREO_CAMERAS'][:,:,1], THRESHOLD2, 1, cv2.THRESH_BINARY)
        thr = cv2.bitwise_xor(thr1, thr2)
        thr = cv2.convertScaleAbs(thr)
        contours, hierarchy = cv2.findContours(thr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        bot_size_in_right_camera = float('nan')
        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            if w * h > 70:
                is_bot_in_right_camera = True
                bot_size_in_right_camera = w*h
                break
                
        is_bot_in_camera = is_bot_in_left_camera or is_bot_in_right_camera
        
        # Compute the reward
        reward = 0.0
        if (not is_crashed) and any_wheels_on_track:
            done = False
            params = {
                'all_wheels_on_track': all_wheels_on_track,
                'x': model_point.x,
                'y': model_point.y,
                'heading': model_heading * 180.0 / math.pi,
                'distance_from_center': distance_from_center,
                'progress': current_progress,
                'steps': self.steps,
                'speed': speed,
                'steering_angle': steering_angle * 180.0 / math.pi,
                'track_width': track_width,
                'waypoints': list(self.center_line.coords),
                'closest_waypoints': [prev_index, next_index],
                'is_left_of_center': is_left_of_center,
                'is_reversed': self.reverse_dir,
                'is_crashed': is_crashed,
                'dist_closest_bot': dist_closest_bot_car,
                'flag_unsafe': flag_unsafe,
                'bot_car_progress': bot_car_progress,
                'bot_car_lane_match': ahead_bot_lane_match,
                'is_bot_in_camera': is_bot_in_camera,
                'bot_size_in_left_camera': bot_size_in_left_camera,
                'bot_size_in_right_camera': bot_size_in_right_camera
            }
            try:
                reward = float(self.reward_function(params))
            except Exception as e:
                utils.json_format_logger("Exception {} in customer reward function. Job failed!".format(e),
                                         **utils.build_user_error_dict(utils.SIMAPP_SIMULATION_WORKER_EXCEPTION,
                                                                       utils.SIMAPP_EVENT_ERROR_CODE_400))
                traceback.print_exc()
                utils.simapp_exit_gracefully()
        else:
            done = True
            reward = CRASHED

        # Reset if the car position hasn't changed in the last 2 steps
        prev_pnt_dist = min(model_point.distance(self.prev_point), model_point.distance(self.prev_point_2))

        if prev_pnt_dist <= 0.0001 and self.steps % NUM_STEPS_TO_CHECK_STUCK == 0:
            done = True
            reward = CRASHED  # stuck

        # Simulation jobs are done when progress reaches 100
        if current_progress >= 100:
            done = True
#             reward = 1e3

        # Keep data from the previous step around
        self.prev_point_2 = self.prev_point
        self.prev_point = model_point
        self.prev_progress = current_progress

        # Set the reward and done flag
        self.reward = reward
        self.reward_in_episode += reward
        self.done = done

        # Trace logs to help us debug and visualize the training runs
        # btown TODO: This should be written to S3, not to CWL.
        logger.info('SIM_TRACE_LOG:%d,%d,%.4f,%.4f,%.4f,%.2f,%.2f,%d,%.4f,%s,%s,%.4f,%d,%.2f,%s,%s,%.2f\n' % (
            self.episodes, self.steps, model_location[0], model_location[1], model_heading,
            self.steering_angle,
            self.speed,
            self.action_taken,
            self.reward,
            self.done,
            all_wheels_on_track,
            current_progress,
            closest_waypoint_index,
            self.track_length,
            time.time(),
            is_crashed,
            dist_closest_bot_car))

        #build json record of the reward metrics
        reward_metrics = OrderedDict()
        reward_metrics['episode'] = self.episodes
        reward_metrics['steps'] = self.steps
        reward_metrics['X'] = model_location[0]
        reward_metrics['Y'] = model_location[1]
        reward_metrics['yaw'] = model_heading
        reward_metrics['steer'] = self.steering_angle
        reward_metrics['throttle'] = self.speed
        reward_metrics['action'] = self.action_taken
        reward_metrics['reward'] = self.reward
        reward_metrics['done'] = self.done
        reward_metrics['all_wheels_on_track'] = all_wheels_on_track
        reward_metrics['progress'] = current_progress
        reward_metrics['closest_waypoint'] = closest_waypoint_index
        reward_metrics['track_len'] = self.track_length
        reward_metrics['tstamp'] = time.time()
        # self.simtrace_data.write_simtrace_data(reward_metrics)

        # Terminate this episode when ready
        if done and node_type == SIMULATION_WORKER:
            self.finish_episode(current_progress)

    def find_prev_next_waypoints(self, ndist):
        if self.reverse_dir:
            next_index = bisect.bisect_left(self.center_dists, ndist) - 1
            prev_index = next_index + 1
            if next_index == -1: next_index = len(self.center_dists) - 1
        else:
            next_index = bisect.bisect_right(self.center_dists, ndist)
            prev_index = next_index - 1
            if next_index == len(self.center_dists): next_index = 0
        return prev_index, next_index

    def stop_car(self):
        self.steering_angle = 0
        self.speed = 0
        self.action_taken = 0
        self.send_action(0, 0)
        self.racecar_reset()

    def finish_episode(self, progress):
        # Increment episode count, update start position and direction
        self.episodes += 1            
#         if self.change_start:
#             if self.job_type == TRAINING_JOB:
#                 self.start_ndist = (self.start_ndist + ROUND_ROBIN_ADVANCE_DIST) % 1.0
#             else:
#                 self.start_ndist = (self.start_ndist + 2*ROUND_ROBIN_ADVANCE_DIST) % 1.0
        if self.change_start:
#             if self.checkpoint_num < 20 or progress > 40 or self.job_type == EVALUATION_JOB:
            self.start_ndist_index = (self.start_ndist_index + 1) % len(self.start_ndist_map)
            self.start_ndist = self.start_ndist_map[self.start_ndist_index]
        if self.alternate_dir:
            self.reverse_dir = not self.reverse_dir

        # Reset the car
        self.stop_car()

        # upload SIM_TRACE data to S3
        # self.simtrace_data.upload_to_s3(self.episodes)

        # Update metrics based on job type
        if self.job_type == TRAINING_JOB:
            self.update_training_metrics(progress)
            self.write_metrics_to_s3()
            if self.is_training_done():
                self.cancel_simulation_job()
        elif self.job_type == EVALUATION_JOB:
            self.number_of_trials += 1
            self.update_eval_metrics(progress)
            self.write_metrics_to_s3()

    def set_checkpoint_num(self, current_checkpoint):
        self.checkpoint_num = current_checkpoint

    def update_eval_metrics(self, progress):
        eval_metric = {}
        eval_metric['checkpoint_num'] = self.checkpoint_num
        eval_metric['completion_percentage'] = int(progress)
        eval_metric['metric_time'] = int(round(time.time() * 1000))
        eval_metric['start_time'] = int(round(self.simulation_start_time * 1000))
        eval_metric['elapsed_time_in_milliseconds'] = int(round((time.time() - self.simulation_start_time) * 1000))
        eval_metric['trial'] = int(self.number_of_trials)
        self.metrics.append(eval_metric)

    def update_training_metrics(self, progress):
        training_metric = {}
        training_metric['checkpoint_num'] = self.checkpoint_num
        training_metric['reward_score'] = int(round(self.reward_in_episode))
        training_metric['metric_time'] = int(round(time.time() * 1000))
        training_metric['start_time'] = int(round(self.simulation_start_time * 1000))
        training_metric['elapsed_time_in_milliseconds'] = int(round((time.time() - self.simulation_start_time) * 1000))
        training_metric['episode'] = int(self.episodes)
        training_metric['completion_percentage'] = int(progress)
        self.metrics.append(training_metric)

    def write_metrics_to_s3(self):
        session = boto3.session.Session()
        s3_client = session.client('s3', region_name=self.aws_region)
        metrics_body = json.dumps({'metrics': self.metrics})
        s3_client.put_object(
            Bucket=self.metrics_s3_bucket,
            Key=self.metrics_s3_object_key,
            Body=bytes(metrics_body, encoding='utf-8')
        )

    def is_training_done(self):
        if ((self.target_number_of_episodes > 0) and (self.target_number_of_episodes == self.episodes)) or \
           ((isinstance(self.target_reward_score, (int, float))) and (self.target_reward_score <= self.reward_in_episode)):
            self.is_simulation_done = True
        return self.is_simulation_done

    def cancel_simulation_job(self):
        logger.info ("cancel_simulation_job: make sure to shutdown simapp first")
        simapp_shutdown()

        session = boto3.session.Session()
        robomaker_client = session.client('robomaker', region_name=self.aws_region)
        robomaker_client.cancel_simulation_job(
            job=self.simulation_job_arn
        )

class DeepRacerRacetrackCustomActionSpaceEnv(DeepRacerRacetrackEnv):
    def __init__(self):
        DeepRacerRacetrackEnv.__init__(self)
        try:
            # Try loading the custom model metadata (may or may not be present)
            with open('custom_files/model_metadata.json', 'r') as f:
                model_metadata = json.load(f)
                self.json_actions = model_metadata['action_space']
            logger.info("Loaded action space from file: {}".format(self.json_actions))
        except Exception as ex:
            # Failed to load, fall back on the default action space
            from markov.defaults import model_metadata
            self.json_actions = model_metadata['action_space']
            logger.info("Exception {} on loading custom action space, using default: {}".format(ex, self.json_actions))
        self.action_space = spaces.Discrete(len(self.json_actions))

    def step(self, action):
        self.steering_angle = float(self.json_actions[action]['steering_angle']) * math.pi / 180.0
        self.speed = float(self.json_actions[action]['speed'])
        self.action_taken = action
        return super().step([self.steering_angle, self.speed])
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



