HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Agar.io-like game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
gamelikeagar

Problem

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  1) {
                    geneRecord.get(g).set(y, geneRecord.get(g).get(y) - gaussian);
                } else {
                    geneRecord.get(g).set(y, geneRecord.get(g).get(y) + gaussian);
                }

            }
        }
    }

Solution

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  1) { // Note: doesn't handle gy < -1
            grec.set(y, gy - gaussian);
        } else {
            grec.set(y, gy + gaussian);
        }
          :

Code Snippets

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);
        }
          :

Context

StackExchange Code Review Q#143153, answer score: 3

Revisions (0)

No revisions yet.