Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Genetic Algorithm in Java

Suppose you have an altered chess board:

1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1

where the 0's are spaces where nothing can be placed.

Now suppose you have four tetris shapes (each a 4x4 2D array)

Using a genetic algorithm, I need to come up with the best fitness (# spaces filled/total # of spaces)

Pseudo code for the genetic algorithm:

------------
initialize population randomly;
Evaluate;
while (best fitness < 1)
{
Evaluate;
Discard bottom 2 solutions;
Add a new solution as crossover of top 2 solutions;
Add a new solutation as a mutation of the top solution;
}
------------

Here is the code I have so far, I'm having trouble staying in bounds with my arrays:

http://www.codetoad.com/forum/17_25391.asp
[950 byte] By [behrk2] at [2007-11-11 7:21:27]