All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.romualdrousseau.shuju.genetic.GeneticPool Maven / Gradle / Ivy

Go to download

Collection of various algorithms implemented in Java covering domains such as mathematics and data science.

There is a newer version: 1.29.7
Show newest version
package com.github.romualdrousseau.shuju.genetic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class GeneticPool {

    public GeneticPool() {
        this.pool = new ArrayList();
    }

    public void newPool() {
        this.pool.clear();
    }

    public void addOne(Individual one) {
        this.pool.add(one);
    }

    public void sample() {
        this.sample((int) Math.floor(Math.random() * this.pool.size()));
    }

    public void sample(int sampleCount) {
        sampleCount = Math.max(1, sampleCount);

        Collections.sort(this.pool, Collections.reverseOrder(new Comparator() {
            public int compare(Individual a, Individual b) {
                float d = a.getFitness() - b.getFitness();
                return (d < 0) ? -1 : ((d > 0) ? 1 : 0);
            }
        }));

        if (this.pool.size() > sampleCount) {
            for (int i = this.pool.size() - 1; i >= sampleCount; i--) {
                this.pool.remove(i);
            }
        }
    }

    public void normalize() {
        float sum = 0;
        for (int i = 0; i < this.pool.size(); i++) {
            Individual individual = this.pool.get(i);
            sum += individual.getFitness();
        }

        for (int i = 0; i < this.pool.size(); i++) {
            Individual individual = this.pool.get(i);
            individual.setFitness(individual.getFitness() / sum);
        }
    }

    public Individual selectParent() {
        float r = (float) Math.random();

        int bestIndex = 0;
        while (r > 0) {
            r -= pool.get(bestIndex).getFitness();
            bestIndex++;
        }
        bestIndex--;

        return pool.get(bestIndex);
    }

    public Individual spawn() {
        return this.selectParent().clone().mutate();
    }

    private ArrayList pool;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy