# Revised algorithm of choice

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 0

int 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()
{
}
```

## Comment viewing options

Leonardo's serial port is a bit different. I found this piece of code in examples sketches for Leonardo. Maybe this is needed to make the program work:

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}

Strange to talk in English to you guys ;-)

I changed that code...yeah I remember that the Leonardo is not my first choice...I rather use another one but for now I stuck with it.

After adding the while loop (not changing the BAUD rate) the code did not spit out anything. Changing the BAUD rate to 9600 gave me a "action alpha_2 chosen" right after the upload was done. Changing the BAUD rate back to 19200 was resulting in a funny behavior. After uploading nothing happened and after pushing RESET and short blinking of the LED it cam up with "action alpha_2 chosen" that time...

That should be ok. The code will be only one time executed because it's in the setup routine. You must open the serial monitor again or press reset. It just choses an action according how you set the probabilities.

Ok, i guess i can live with that :-) Thanks Markus \o

I think RobotFreak is correct. I just uploaded the code again to my Mega and it works. Is it working now, Lumi?

It's the same than in the code.

I do not get any error message and when putting some code in the "loop" then it's executed...so the sketch is running but gives me no serial output to the monitor.

I just tried that sketch with an Arduino Leonardo and did not get any output. It seems something is not running right. Any advise how to debug it? The entrophy examples are running ok.

Baud rate setting on serial monitor?