Saturday, April 23, 2011

Mr. Genetic Algorithm, please give me strings with 5 characters.

Mr. Genetic Algorithm: But how do I do that?

Me: I'll explain:

Given a string s, produce a fitness function need to determine fitness for a string with an n number of characters.

1 / |n - len(s)|

Or another way of writing it:

1 / abs(n - len(s))

Examples:

1) s = "abcd", n = 5

fitness(s) = 1 / |5 - 4|
           = 1 / 1
           = 1

2) s = "ab", n = 5

fitness(s) = 1 / |5 - 2|
           = 1 / 3

3) s = "abcdefghij", n = 5

fitness(s) = 1 / |5 - 10|
           = 1 / |-5|
           = 1 / 5

Now out of these 3 solutions, which is the fittest?

Number (1) is the fittest because it has the highest fitness score (1 > 1/3 > 1/5).

No comments:

Post a Comment