I'm trying to learn a bit on robotics and Java. In my attempt I have written a function that (should) take a uniformed array called q(0.2,0.2,0.2,0.2,0.2), multiply each value in this array by 0.6 or 0.2 based on value Z, compared to another array world (0,1,1,0,0) that represents hit or miss of value Z compared to array world and finally return a new uniformed array.
So: 1.) Loop through (0,1,1,0,0) 2.) if i!=Z than i*0.2, if i=Z than i*0.6 3.) sum all new values in q to a double called normalize 4.) normalize each value in q (value / normalize)
Below is the function:
public static final double pHit = 0.6;
public static final double pMiss = 0.2;
public static int[] world = {0,1,1,0,0};
public static List<Double> q = new ArrayList<Double>();
public static List<Double> sense(int Z){
double normalize = 0;
for(int i=0;i < q.size();i++){
if(Z == world[i]){
q.set(i, (q.get(i) * pHit));
}
else{
q.set(i, (q.get(i) * pMiss));
}
}
//Normalize
for(int i=0;i < q.size();i++){
normalize += q.get(i);
}
for(int i=0;i<q.size();i++){
q.set(i, q.get(i)/normalize);
}
return q;
}
If I set world to (0,1,0,0,0) and Z to 1 I get following results: 0.14285714285714288 0.4285714285714285 0.14285714285714288 0.14285714285714288 0.14285714285714288
This normalizes nicely (sum = 1).
But if I set world to (0,1,1,0,0) and Z to 1 I get a strange result: 0.1111111111111111 0.3333333333333332 0.3333333333333332 0.1111111111111111 0.1111111111111111
This "normalizes" to 0.9999999999999998 ?
Many thanks for any input!