# Agar.io-like game

I've created a game (basically an Agar.io clone), where a human player is placed against AI controlled bots powered by a genetic algorithm and neural networks.

The problem is that I think that my algorithm isn't efficient. I have 10 bots ranked by their fitness function, which is time survived. Their gene consists of real numbers between -1 and 1.

From lowest to highest fitness, I take n bots up to 5 bots and take the current weight value and add it by a Gaussian number multiplied by ($10^{-n}$). I had trouble performing crossover with floating point numbers, thus I only did mutation like this.

Obviously, my AI isn't very intelligent.

How could I improve my algorithm?

if (generation != 1) {

for (int g = 0; g < geneRecord.size - 5; g++) {

Random randomno = new Random();

for (int y = 0; y < geneRecord.get(g).size; y++) {

float gaussian = (float) (((randomno.nextGaussian()) * Math.pow(10, -(g+1))));

if (geneRecord.get(g).get(y) + gaussian > 1) {
geneRecord.get(g).set(y, geneRecord.get(g).get(y) - gaussian);
} else {
geneRecord.get(g).set(y, geneRecord.get(g).get(y) + gaussian);
}

}
}
}

• How large are geneRecord.size and geneRecord.get(g).size? Your description of your algorithm is confusing. I don't know what "I take n bots up to 5 bots" means when your loop goes from 0 .. geneRecord.size - 5. Where are the "bots"? Please explain more clearly what your code is doing. – JS1 Oct 4 '16 at 1:11
• Sorry if I wasn't clear, basically I have 10 bots. geneRecord is an Array that represents the genes of each bot, which mean geneRecord has a size of 10. geneRecord.get(g).size is currently 64. Basically it takes the first (weakest) 5 bots and modifies their weights in the neural network by a value. This value gets smaller as g gets larger. – Jaden Wang Oct 4 '16 at 1:17

It's hard to optimize without the full code, but for this segment of the code, you can start by precomputing a few vars.

Random randomno = new Random();

for (int g = 0; g < geneRecord.size - 5; g++) {
var grec = geneRecord.get(g);

float weight = Math.pow(10, -(g+1)));

for (int y = 0; y < grec.size; y++) {
float gy = grec.get(y);

float gaussian = (float) (randomno.nextGaussian() * weight);
if (gy + gaussian > 1) { // Note: doesn't handle gy < -1
grec.set(y, gy - gaussian);
} else {
grec.set(y, gy + gaussian);
}
: