udacity / sdc-issue-reports

29 stars 6 forks source link

Term 3, lesson 4, 17. Implement a Second Cost Function in C++ (solution) - Bad solution #1446

Closed DominikMa closed 5 years ago

DominikMa commented 5 years ago

The given solution only works for the given inputs. Obviously the outputs of the solution function

float inefficiency_cost(int target_speed, int intended_lane, int final_lane, vector<int> lane_speeds) {
    /*
    Cost becomes higher for trajectories with intended lane and final lane that have traffic slower than target_speed.
    */

    float speed_intended = lane_speeds[intended_lane];
    float speed_final = lane_speeds[final_lane];
    float cost = (2.0*target_speed - speed_intended - speed_final)/target_speed;
    return cost;
}

are not necessarily between 0 and 1.

With the inputs vector<int> lane_speeds = {6, 1, 8, 9}; we get

Costs for (intended_lane, final_lane):
----------------------------------------------------------
The cost is 0.2 for (3, 3)
The cost is 0.3 for (2, 3)
The cost is 0.4 for (2, 2)
The cost is 1.1 for (1, 2)
The cost is 1.8 for (1, 1)
The cost is 1.3 for (0, 1)
The cost is 0.8 for (0, 0)

where some cost values are greater then 1. This is the case if speed_intended + speed_final is less than target_speed.

With the inputs vector<int> lane_speeds = {6, 20, 8, 9}; we get

Costs for (intended_lane, final_lane):
----------------------------------------------------------
The cost is 0.2 for (3, 3)
The cost is 0.3 for (2, 3)
The cost is 0.4 for (2, 2)
The cost is -0.8 for (1, 2)
The cost is -2 for (1, 1)
The cost is -0.6 for (0, 1)
The cost is 0.8 for (0, 0)

where some cost values are less then 0. This is the case if speed_intended + speed_final is greater than 2.0*target_speed.

The simplest solution is to add a min and max call

float cost = min(max((2.0*target_speed - speed_intended - speed_final)/target_speed, 0.0), 1.0);
mvirgo commented 5 years ago

Hi there! With Waffle closing its doors next month, and the ability to leave more detailed feedback on each classroom page (see “Send Feedback” in the upper right if you are enrolled in the program), we are migrating away from our Waffle board. All issues and conversations here will be migrated onto our internal platforms.

Over the next week, I am leaving these notes in case anyone has further follow-ups they want to get in before the board closes, as well as migrating over the tickets. As of 4/19, I will close all the remaining open tickets, and archive the related Github repository.