def cost_speed(vehicle_info, predictions, trajectory):
    """ Computes cost of driving at a given speed.

    Args:
        vehicle_info: A VehicleInfo tuple.
        predictions: A dict of predictions for vehicles.
        trajectory: A Trajectory tuple.
     Returns:
         A cost in [0, 1].
     """
    # Cost of the car stopping.
    STOP_COST = 0.7
    # How many km/h to drive at bellow speed limit.
    BUFFER_SPEED = 5.0
    vehicle_info.target_speed = vehicle_info.speed_limit - BUFFER_SPEED
    if vehicle_info.next_speed < vehicle_info.target_speed:
        # Cost linearly decreases the closer we drive to target speed.
        return (STOP_COST *
                (vehicle_info.target_speed - vehicle_info.next_speed) /
                vehicle_info.target_speed)
    elif (vehicle_info.next_speed >= vehicle_info.target_speed
          and vehicle_info.next_speed < vehicle_info.speed_limit):
        # Cost linearly increases if we drive above target speed.
        return (vehicle_info.next_speed -
                vehicle_info.target_speed) / BUFFER_SPEED
    else:
        # Cost is always 1 if we drive above speed limit.
        return 1