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©;
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;
}
}