Today I thought of a simple way of how to implement
genetic algorithm. I'll need it for new formula generation. The idea is to randomly generate left and right side, then to check do they yield the same result with the same input variables. If they do, we put equal sign between them and there is a new furmula. To do this, we need to systematically generate possible candidates for left and right side.
The first thought I had was to cycle combinations of expressions like in following example (if we have expressions e1, e2 and e3 and super-expression that holds these 3):
e1,e1,e1
e1,e1,e2
e1,e1,e3
e1,e2,e1
e1,e2,e2
e1,e2,e3
e1,e3,e1
e1,e3,e2
...
This is easy, but it gets complicated to track combinations with multiple level expression depth and recursion. The genetic algorithm would be in that we first try combinations of those expressions which was more successful in finding new formulas (i.e. when a new formula is found to be true, expressions that built it get indexed up in future expression picking order).
Simpler way would be by utilizing random numbers, so the pattern of upper noted expression would be:
e1 if random(percent of e1) | e2 if random(percent of e2) | e3 if random(pecent of e3),
e1 if random(percent of e1) | e2 if random(percent of e2) | e3 if random(pecent of e3),
e1 if random(percent of e1) | e2 if random(percent of e2) | e3 if random(pecent of e3)
With each call we would get a randomly generated triple of expressions without need to track each combination and to cycle through all possible combination. We just pass percents of success (the more the percent is, the more possibility of picking that option) and each time we get a different randomly generated expression (well, it is possible to get the same expression twice or more times, but those are solvable details).
I think that random-approach is a lot simpler than cycling through all possible combinations. More, imagine if we had multiple levels of expressions with recursions and stuff. All that is solved easily with random-approach.
I just wanted to share this with you, such an complex combinatorial task, yet so easily solved with random expression picking.