# Simple trial-and-error algorithm - yet

I am experimenting these days with trial-and-error algorithms. Below you find the code for a simple trial-and-error algorithm which learns to add two integers a and b, where 1 a,b 5. The summands a and b and the result c are randomly generated numbers. We assume that the robot doens't know about the commutative property, so we have 225 possible false results to consider (9 results can be false per operation and there are 25 possible operations). The false results and the according summands are stored in an array (for instance 5 + 1 = 10). Every time the random generator choses values for a, b and c, the values will be compared with the already as false tagged operations in the array. If the operation is already stored in the array, the random generator choses new integers for a, b and c. After a while all false operations are stored in the array and the program only shows correct equations.

You can download the program to your board, open the serial monitor and play with it. It will improve your mental math as well. The algorithm can be used for all kind of robot approaches like Maze solvers etc.

And yes, this

======
( .. )
)..(
( .. )
======

is an ASCII hourglass, 'cause sometimes it takes a while till the random function generates integers which are not already in the array.

I'll worker further on the algorithm and try to make it more clever, combining it with a variable structure stochastic learning automaton. The program should not only sort out false equations, it should analyse and find pattern why they were tagged as false. If for istance 10 as false tagged equations have a result which is smaller than one of the two summands, it could have something to do with it :)

```/********************************************
*      Simple trial-and-error algorithm     *
*                                           *
*        Learns to add two integers         *
*                                           *
*         Written by M. Bindhammer          *
*********************************************/

#include <Entropy.h>

int a;
int b;
int c;
int i=0;
int j=1;
int k=2;
int false_operations[700];

void setup()
{
Serial.begin(9600);
Entropy.Initialize();
}

void loop()
{
main:
int m=0;
int n=1;
int o=2;
a=Entropy.random(1,6);
b=Entropy.random(1,6);
c=Entropy.random(1,11);
for (int l=0; l<i/3; l++)
{
if (false_operations[m]==a&&false_operations[n]==b&&false_operations[o]==c)
{
Serial.println("======");
Serial.println("(....)");
Serial.println(" )..( ");
Serial.println("(    )");
Serial.println("======");
Serial.println(" ");
Serial.println("======");
Serial.println("( .. )");
Serial.println(" )..( ");
Serial.println("( .. )");
Serial.println("======");
Serial.println(" ");
Serial.println("======");
Serial.println("(    )");
Serial.println(" )..( ");
Serial.println("(....)");
Serial.println("======");
Serial.println(" ");
goto main;
}
m=m+3;
n=n+3;
o=o+3;
}
Serial.print(a);
Serial.print(" + ");
Serial.print(b);
Serial.print(" = ");
Serial.println(c);
Serial.println(" ");
Serial.println("send y if equation is true");
Serial.println("send n if equation is false");
Serial.println(" ");
check_input:
char ser = Serial.read();
if(ser == 'y')
{
}
else if(ser == 'n')
{
false_operations[i] = a;
false_operations[j] = b;
false_operations[k] = c;
i=i+3;
j=j+3;
k=k+3;
}
else
{
goto check_input;
}
}

```

## Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Instead of storing wrong calculations you can store wrong paths the robot took in a maze. It will not repeat it for the next round. It has no reinforcement scheme in the moment to analyze false calculations or false paths. It is not a calculator btw. The numbers for the summands and the sum are randomly generated.

I am working on a program which simulates how a child learns adding two numbers. It uses the principle "Substitute unknown by known".

Yes I got it after I wrote the comment :-) but did not want to change the comment tho...

How is your diving trip? Is your little one already diving too :-))) (kidding)

Only snorkeling till now. We'll get on another island soon for two days so I hope I have a few hours for scuba diving :)

And your little daughter? Also swimming with you or is she afraid of water?

She likes water very much. She can not swim alone, so I hold her.

As I see this is a simple calculator. I am probably too tired to see how I can use it for a robot as a learning algorithm. I got the principle but I guess I need to check this further to fully understand it's behavior.