

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS DeepRacer 報酬関数の例
<a name="deepracer-reward-function-examples"></a>

以下に AWS DeepRacer の報酬関数のいくつかの例を示します。

**Topics**
+ [例 1: タイムトライアルでセンターラインに従う](#deepracer-reward-function-example-0)
+ [例 2: タイムトライアルで 2 つの境界内に留まる](#deepracer-reward-function-example-1)
+ [例 3: タイムトライアルでのジグザグ運転の防止](#deepracer-reward-function-example-2)
+ [例 4: 静止している障害物や走行中の車両に衝突することなく、1 つの車線に留まること。](#deepracer-reward-function-example-3)

## 例 1: タイムトライアルでセンターラインに従う
<a name="deepracer-reward-function-example-0"></a>

 この例では、エージェントがセンターラインからどれだけ離れているかを調べ、トラックの中央に近いと高い報酬を与え、エージェントがセンターラインに密接に従うように促します。

```
def reward_function(params):
    '''
    Example of rewarding the agent to follow center line
    '''
    
    # Read input parameters
    track_width = params['track_width']
    distance_from_center = params['distance_from_center']

    # Calculate 3 markers that are increasingly further away from the center line
    marker_1 = 0.1 * track_width
    marker_2 = 0.25 * track_width
    marker_3 = 0.5 * track_width

    # Give higher reward if the car is closer to center line and vice versa
    if distance_from_center <= marker_1:
        reward = 1
    elif distance_from_center <= marker_2:
        reward = 0.5
    elif distance_from_center <= marker_3:
        reward = 0.1
    else:
        reward = 1e-3  # likely crashed/ close to off track

    return reward
```

## 例 2: タイムトライアルで 2 つの境界内に留まる
<a name="deepracer-reward-function-example-1"></a>

 この例では、エージェントが境界内に留まる場合に高い報酬を与え、エージェントがラップを完了するための最良の経路を把握させます。プログラミングと理解は簡単ですが、収束に時間がかかる可能性があります。

```
def reward_function(params):
    '''
    Example of rewarding the agent to stay inside the two borders of the track
    '''
    
    # Read input parameters
    all_wheels_on_track = params['all_wheels_on_track']
    distance_from_center = params['distance_from_center']
    track_width = params['track_width']
    
    # Give a very low reward by default
    reward = 1e-3

    # Give a high reward if no wheels go off the track and 
    # the car is somewhere in between the track borders 
    if all_wheels_on_track and (0.5*track_width - distance_from_center) >= 0.05:
        reward = 1.0

    # Always return a float value
    return reward
```

## 例 3: タイムトライアルでのジグザグ運転の防止
<a name="deepracer-reward-function-example-2"></a>

 この例では、エージェントがセンターラインに従うようにインセンティブを与えますが、操作が大きすぎると報酬が低くなり、ジグザグ運転を防ぐのに役立ちます。エージェントはシミュレーターでスムーズに運転することを学習すれば、実際の車両にデプロイされたときに同じ動作を維持できる可能性があります。

```
def reward_function(params):
    '''
    Example of penalize steering, which helps mitigate zig-zag behaviors
    '''
    
    # Read input parameters
    distance_from_center = params['distance_from_center']
    track_width = params['track_width']
    abs_steering = abs(params['steering_angle']) # Only need the absolute steering angle

    # Calculate 3 marks that are farther and father away from the center line
    marker_1 = 0.1 * track_width
    marker_2 = 0.25 * track_width
    marker_3 = 0.5 * track_width

    # Give higher reward if the car is closer to center line and vice versa
    if distance_from_center <= marker_1:
        reward = 1.0
    elif distance_from_center <= marker_2:
        reward = 0.5
    elif distance_from_center <= marker_3:
        reward = 0.1
    else:
        reward = 1e-3  # likely crashed/ close to off track

    # Steering penality threshold, change the number based on your action space setting
    ABS_STEERING_THRESHOLD = 15 

    # Penalize reward if the car is steering too much
    if abs_steering > ABS_STEERING_THRESHOLD:
        reward *= 0.8

    return float(reward)
```

## 例 4: 静止している障害物や走行中の車両に衝突することなく、1 つの車線に留まること。
<a name="deepracer-reward-function-example-3"></a>

 

この報酬関数は、トラック境界の間に留まるエージェントに報酬を与え、前方のオブジェクトに近づきすぎたエージェントにペナルティを与えます。エージェントは、クラッシュを回避するために、車線から車線に移動することができます。報酬総額は、報酬とペナルティの加重合計です。この例では、衝突を回避するために、ペナルティをより重視しました。平均化の重みを変えて実験し、様々な行動の結果に対応できるようにトレーニングします。

 

```
import math
def reward_function(params):
    '''
    Example of rewarding the agent to stay inside two borders
    and penalizing getting too close to the objects in front
    '''
    all_wheels_on_track = params['all_wheels_on_track']
    distance_from_center = params['distance_from_center']
    track_width = params['track_width']
    objects_location = params['objects_location']
    agent_x = params['x']
    agent_y = params['y']
    _, next_object_index = params['closest_objects']
    objects_left_of_center = params['objects_left_of_center']
    is_left_of_center = params['is_left_of_center']
    # Initialize reward with a small number but not zero
    # because zero means off-track or crashed
    reward = 1e-3
    # Reward if the agent stays inside the two borders of the track
    if all_wheels_on_track and (0.5 * track_width - distance_from_center) >= 0.05:
        reward_lane = 1.0
    else:
        reward_lane = 1e-3
    # Penalize if the agent is too close to the next object
    reward_avoid = 1.0
    # Distance to the next object
    next_object_loc = objects_location[next_object_index]
    distance_closest_object = math.sqrt((agent_x - next_object_loc[0])**2 + (agent_y - next_object_loc[1])**2)
    # Decide if the agent and the next object is on the same lane
    is_same_lane = objects_left_of_center[next_object_index] == is_left_of_center
    if is_same_lane:
        if 0.5 <= distance_closest_object < 0.8:
            reward_avoid *= 0.5
        elif 0.3 <= distance_closest_object < 0.5:
            reward_avoid *= 0.2
        elif distance_closest_object < 0.3:
            reward_avoid = 1e-3  # Likely crashed
    # Calculate reward by putting different weights on
    # the two aspects above
    reward += 1.0 * reward_lane + 4.0 * reward_avoid
    return reward
```