In the last two days I have reworked the algorithm of choice completely. The code is now much shorter and a great number of actions can be easily added. I have listed the code sketch below.
The algorithm of choice works as follows: First an array with according probabilities will be created. Then the probability values of the array will be sorted by a selection sorting algorithm from max to min. In a third step the probability values will be assigned to their corresponding actions and in the same FOR loop selected/not selected depending of their current probability value. I have replaced the random() function by the Entropy.random() function which claims to be a true random generator. The function is a bit slow though.
In a next step I will add speech recognition. I'll tell the robot only if an action was good or bad and the robot will learn after a while to chose only good tagged actions. This is considered as a kind of supervised learning.
Any suggestions to improve the code further are very welcome.
//********************************************* //********** Algorithm of choice V.2 ********** //*********************************************#include <Entropy.h> //see http://arduino.cc/forum/index.php/topic,108380.0.html
float t_1=3.0; //define probabilities
float y_1=17.0;
float t_2=1.0;
float y_2=17.0;
float t_3=2.0;
float y_3=17.0;
float t_4=1.0;
float y_4=17.0;
float t_5=11.0;
float y_5=17.0;
float p_1=t_1/y_1;
float p_2=t_2/y_2;
float p_3=t_3/y_3;
float p_4=t_4/y_4;
float p_5=t_5/y_5;long randomnumber;
float maxprob[] = {p_1,p_2,p_3,p_4,p_5, 3}; //create according array
//the number ‘3’ must be always kept in the array to prevent wrong ranking
//in case probabilities are equal to 0int r=5; //number of desired actions
void setup()
{
Serial.begin(19200);
Entropy.Initialize();//-------- Selection sorting algorithm --------
float temp;
int mini; //variable used to hold the assumed minimum element
int i;
int j;
for(i=0; i<r; i++) //outer FOR loop
{
mini=i; //first pass of FOR loop assumes 0th element as minimum,
//second pass assumes 1st element as minimum and so on
for(j=0; j<r; j++) //inner FOR loop
{
if(maxprob[mini]>maxprob[j]) //compares the minimum element with all
//other members using inner FOR loop
{
temp=maxprob[j]; //exchanges the elements
maxprob[j]=maxprob[mini];
maxprob[mini]=temp;
}
}
}//-------------- Choice algorithm -------------
int k=0; //identifier which action was chosen
for (i=0; i<r; i++)
{
if (maxprob[i]==p_1&&maxprob[i+1]!=p_1)
{
randomnumber=Entropy.random(1,y_1+1);
if (randomnumber<=t_1)
{
Serial.println(“action alpha_1 chosen”);
Serial.println(" “);
k=1;
break;
}
}
if (maxprob[i]==p_2&&maxprob[i+1]!=p_2)
{
randomnumber=Entropy.random(1,y_2+1);
if (randomnumber<=t_2)
{
Serial.println(“action alpha_2 chosen”);
Serial.println(” “);
k=2;
break;
}
}
if (maxprob[i]==p_3&&maxprob[i+1]!=p_3)
{
randomnumber=Entropy.random(1,y_3+1);
if (randomnumber<=t_3)
{
Serial.println(“action alpha_3 chosen”);
Serial.println(” “);
k=3;
break;
}
}
if (maxprob[i]==p_4&&maxprob[i+1]!=p_4)
{
randomnumber=Entropy.random(1,y_4+1);
if (randomnumber<=t_4)
{
Serial.println(“action alpha_4 chosen”);
Serial.println(” “);
k=4;
break;
}
}
if (maxprob[i]==p_5&&maxprob[i+1]!=p_5)
{
randomnumber=Entropy.random(1,y_5+1);
if (randomnumber<=t_5)
{
Serial.println(“action alpha_5 chosen”);
Serial.println(” ");
k=5;
break;
}
}
// Add here statements from p_6 to p_… if desired
}if (k==0)
{
if (maxprob[r-1]==p_1)
{
Serial.println(“action alpha_1 chosen”);
Serial.println(" “);
k=1;
}
else if (maxprob[r-1]==p_2)
{
Serial.println(“action alpha_2 chosen”);
Serial.println(” “);
k=2;
}
else if (maxprob[r-1]==p_3)
{
Serial.println(“action alpha_3 chosen”);
Serial.println(” “);
k=3;
}
else if (maxprob[r-1]==p_4)
{
Serial.println(“action alpha_4 chosen”);
Serial.println(” “);
k=4;
}
else if (maxprob[r-1]==p_5)
{
Serial.println(“action alpha_5 chosen”);
Serial.println(” ");
k=5;
}
// Add here statements from p_6 to p_… if desired
}}
void loop()
{
}