patternpythonMinor
Udacity AI for Robotics translating Python code to C++
Viewed 0 times
udacitytranslatingroboticspythonforcode
Problem
I've been learning C++ for about a week now. My primary language is Python, so I thought I would find some code to translate to figure out how the pieces fit together. Please let me know any simplifications/best practices!
There is an assignment on a unit of Udacity's "Artificial Intelligence for Robotics" where you have a probability distribution for a 2D array of states, and given sensory input and movement, you calculate the probability of being in any of those states. The original Python code is first, and my C++ revision is below:
Edit: I'll attempt to give more detail on what the problem is, as well as add some comments to the original code.
There is a 4x5 map (not the CS sense of the word) of possible states, which you can think of as a checkerboard or similar with colors that correspond to the
Before we sense anything about our environment we have a belief that any position is equally likely, which initializes our probability distribution
By
The
There is an assignment on a unit of Udacity's "Artificial Intelligence for Robotics" where you have a probability distribution for a 2D array of states, and given sensory input and movement, you calculate the probability of being in any of those states. The original Python code is first, and my C++ revision is below:
Edit: I'll attempt to give more detail on what the problem is, as well as add some comments to the original code.
There is a 4x5 map (not the CS sense of the word) of possible states, which you can think of as a checkerboard or similar with colors that correspond to the
colors array. Our sensor is somewhere on the map, but we don't know where.Before we sense anything about our environment we have a belief that any position is equally likely, which initializes our probability distribution
p (probDist in the C++ code) to 0.05, or 1 in 20 squares. We will then update our belief by shifting, then sensing, in that order, 5 times.By
shift, I mean that the sensor attempts to move according to the #Motion comment at the top of the Python code. The sensor has a p_move (probMove in the C++ code) probability of successfully moving, otherwise it remains in the same location. What the shift function does is it convolves the current probability distribution with the probability distribution of successful movement. In other words, it figures out where it believes it will be after an attempted input motion.The
sense function is simply measuring the color of the square the sensor is on, aware of the fact that the sensor could be incorrect. The sensor_right (probSensorCorrect in the C++ code) amount is the probability of the sensor giving the correct color of the current square. The Law of Total Probability is then useSolution
This is quite an interesting piece of code.
There are however some things i would change.
-
Do not use
-
While it is a good representation of your physical world, I cannot see a real benefit for utilizing a nested 2D array. In most cases your code does not rely on the position within the grid and when it does you do boundchecks anyway. So i would suggest, that you simplify it to
-
Stick to your codestyle. Generally, when you already use braces for nesting, use them everywhere.
-
Initializing non array variables via
-
Separate operators like
-
Use the available functionality. Stuff like
-
For simple if-else assignments the ternary operator ? might be more suitable, e.g.
-
Declare variables where you use them, e.g. rowCal and colCalc in the shift function are only used inside the for loop-
-
Your sense function does not need to preserve the old array so directly overwrite it rather than assigning a temporary.
There are however some things i would change.
-
Do not use
namespace std; It is bad practice that will cost you in the long run, so avoid it early on.-
While it is a good representation of your physical world, I cannot see a real benefit for utilizing a nested 2D array. In most cases your code does not rely on the position within the grid and when it does you do boundchecks anyway. So i would suggest, that you simplify it to
std::array matrix;-
Stick to your codestyle. Generally, when you already use braces for nesting, use them everywhere.
-
Initializing non array variables via
double sum{0}; is really unorthodox. Stick with the simple double sum = 0.0;-
Separate operators like
== with spaces that makes the code much more readable.-
Use the available functionality. Stuff like
int rows = probDist.size(); is unneeded. Simply use probDist.size() in your code.-
For simple if-else assignments the ternary operator ? might be more suitable, e.g.
double probPiece = (measurement == colors[row][col]) ?
probSensorCorrect : 1.0 - probSensorCorrect;-
Declare variables where you use them, e.g. rowCal and colCalc in the shift function are only used inside the for loop-
-
Your sense function does not need to preserve the old array so directly overwrite it rather than assigning a temporary.
void sense(
std::array &probDist,
const std::array &colors,
const char measurement,
const double probSensorCorrect)
{
double sum = 0.0;
for (unsigned i = 0; i < probDist.size(); ++i) {
probDist[i] *= (measurement == colors[i]) ?
probSensorCorrect : 1.0 - probSensorCorrect;
sum += probDist[i];
}
for (auto &probability : probDist) {
probability /= sum;
}
}Code Snippets
std::array<double, 5 * 4> matrix;double probPiece = (measurement == colors[row][col]) ?
probSensorCorrect : 1.0 - probSensorCorrect;void sense(
std::array<double, 5 * 4> &probDist,
const std::array<char, 5 * 4> &colors,
const char measurement,
const double probSensorCorrect)
{
double sum = 0.0;
for (unsigned i = 0; i < probDist.size(); ++i) {
probDist[i] *= (measurement == colors[i]) ?
probSensorCorrect : 1.0 - probSensorCorrect;
sum += probDist[i];
}
for (auto &probability : probDist) {
probability /= sum;
}
}Context
StackExchange Code Review Q#145023, answer score: 2
Revisions (0)
No revisions yet.