Package de.mh4j.solver.genetic

Examples of de.mh4j.solver.genetic.Genome


            pointer2 = 1 - pointer1;
        }

        long fitnessSum = genePool.getFitnessSum();
        double probabilitySum = 0;
        Genome parent1 = null;
        for (Genome genome : genePool) {
            double selectionProbability = (fitnessSum == 0) ? 1 : genome.getFitness() / (double) fitnessSum;
            probabilitySum += selectionProbability;

            if (parent1 == null && probabilitySum >= pointer1) {
View Full Code Here


        return parents;
    }

    private Couple getCouple(GenePool<GenericGenomeType> genePool) {
        Genome parent1 = selectMate(genePool);
        Genome parent2 = selectMate(genePool);
        return new Couple(parent1, parent2);
    }
View Full Code Here

    public Collection<Couple> select(int numberOfPairs, GenePool<GenericGenomeType> genePool) {
        Collection<Couple> couples = new ArrayList<Couple>(numberOfPairs);

        Genome[] genomes = genePool.toArray();
        for (int i = 0; i < numberOfPairs; i++) {
            Genome parent1 = selectMate(genomes);
            Genome parent2 = selectMate(genomes);
            couples.add(new Couple(parent1, parent2));
        }
        return couples;
    }
View Full Code Here

    /**
     * TODO write javadoc
     */
    protected Genome selectMate(Genome[] genomes) {
        Genome bestIndividual = null;
        int numberOfIndividualsToDrawForTournament = 1 + random.nextInt(genomes.length - 1);
        for (int i = 0; i < numberOfIndividualsToDrawForTournament; i++) {
            int indexOfIndividual = random.nextInt(genomes.length);
            Genome tmpIndividual = genomes[indexOfIndividual];
            if (bestIndividual == null) {
                bestIndividual = tmpIndividual;
            }
            else {
                if (bestIndividual.getFitness() < tmpIndividual.getFitness()) {
                    bestIndividual = tmpIndividual;
                }
            }
        }
        return bestIndividual;
View Full Code Here

        selector.select(population, 5);

        assert population.getSize() == 5;

        for (int i = 1; i <= 5; i++) {
            Genome genome = allGenomes.get(i - 1);
            assert population.contains(genome) == false : "Genomes 1-5 should not have survived";
        }

        for (int i = 6; i <= 10; i++) {
            Genome genome = allGenomes.get(i - 1);
            assert population.contains(genome) == true : "Genomes 6-10 should have survived";
        }
    }
View Full Code Here

    private List<Genome> createPopulation() {
        int totalNumberOfGenomes = 10;
        List<Genome> allGenomes = new ArrayList<>(totalNumberOfGenomes);

        Genome genome1 = mock(Genome.class);
        when(genome1.getFitness()).thenReturn(1000);
        allGenomes.add(genome1);

        Genome genome2 = mock(Genome.class);
        when(genome2.getFitness()).thenReturn(2000);
        allGenomes.add(genome2);

        Genome genome3 = mock(Genome.class);
        when(genome3.getFitness()).thenReturn(3000);
        allGenomes.add(genome3);

        Genome genome4 = mock(Genome.class);
        when(genome4.getFitness()).thenReturn(4000);
        allGenomes.add(genome4);

        Genome genome5 = mock(Genome.class);
        when(genome5.getFitness()).thenReturn(5000);
        allGenomes.add(genome5);

        Genome genome6 = mock(Genome.class);
        when(genome6.getFitness()).thenReturn(6000);
        allGenomes.add(genome6);

        Genome genome7 = mock(Genome.class);
        when(genome7.getFitness()).thenReturn(7000);
        allGenomes.add(genome7);

        Genome genome8 = mock(Genome.class);
        when(genome8.getFitness()).thenReturn(8000);
        allGenomes.add(genome8);

        Genome genome9 = mock(Genome.class);
        when(genome9.getFitness()).thenReturn(9000);
        allGenomes.add(genome9);

        Genome genome10 = mock(Genome.class);
        when(genome10.getFitness()).thenReturn(10000);
        allGenomes.add(genome10);

        for (int i = 0; i < totalNumberOfGenomes; i++) {
            for (int j = 0; j < totalNumberOfGenomes; j++) {
                makeGenomeMockComparable(allGenomes.get(i), allGenomes.get(j));
View Full Code Here

        int totalNumberOfGenomes = 10;
        int tooOld = currentGeneration - maxAge - 1;
        oldGenomes = new ArrayList<>(totalNumberOfGenomes / 2);
        newGenomes = new ArrayList<>(totalNumberOfGenomes / 2);

        Genome genome1 = mock(Genome.class);
        when(genome1.getFitness()).thenReturn(1000);
        when(genome1.getBirthGeneration()).thenReturn(tooOld);
        oldGenomes.add(genome1);

        Genome genome2 = mock(Genome.class);
        when(genome2.getFitness()).thenReturn(2000);
        when(genome2.getBirthGeneration()).thenReturn(tooOld);
        oldGenomes.add(genome2);

        Genome genome3 = mock(Genome.class);
        when(genome3.getFitness()).thenReturn(3000);
        when(genome3.getBirthGeneration()).thenReturn(tooOld);
        oldGenomes.add(genome3);

        Genome genome4 = mock(Genome.class);
        when(genome4.getFitness()).thenReturn(4000);
        when(genome4.getBirthGeneration()).thenReturn(tooOld);
        oldGenomes.add(genome4);

        Genome genome5 = mock(Genome.class);
        when(genome5.getFitness()).thenReturn(5000);
        when(genome5.getBirthGeneration()).thenReturn(tooOld);
        oldGenomes.add(genome5);

        Genome genome6 = mock(Genome.class);
        when(genome6.getFitness()).thenReturn(6000);
        when(genome6.getBirthGeneration()).thenReturn(currentGeneration);
        newGenomes.add(genome6);

        Genome genome7 = mock(Genome.class);
        when(genome7.getFitness()).thenReturn(7000);
        when(genome7.getBirthGeneration()).thenReturn(currentGeneration);
        newGenomes.add(genome7);

        Genome genome8 = mock(Genome.class);
        when(genome8.getFitness()).thenReturn(8000);
        when(genome8.getBirthGeneration()).thenReturn(currentGeneration);
        newGenomes.add(genome8);

        Genome genome9 = mock(Genome.class);
        when(genome9.getFitness()).thenReturn(9000);
        when(genome9.getBirthGeneration()).thenReturn(currentGeneration);
        newGenomes.add(genome9);

        Genome genome10 = mock(Genome.class);
        when(genome10.getFitness()).thenReturn(10000);
        when(genome10.getBirthGeneration()).thenReturn(currentGeneration);
        newGenomes.add(genome10);

        List<Genome> allGenomes = new ArrayList<>(totalNumberOfGenomes);
        allGenomes.addAll(oldGenomes);
        allGenomes.addAll(newGenomes);
View Full Code Here

    protected GenePool<Genome> genePool;
    protected Genome[] genomes;

    public void setUp() {
        Genome genome1 = mock(Genome.class);
        when(genome1.getFitness()).thenReturn(1000);
        Genome genome2 = mock(Genome.class);
        when(genome2.getFitness()).thenReturn(2000);
        Genome genome3 = mock(Genome.class);
        when(genome3.getFitness()).thenReturn(3000);
        Genome genome4 = mock(Genome.class);
        when(genome4.getFitness()).thenReturn(4000);
        Genome genome5 = mock(Genome.class);
        when(genome5.getFitness()).thenReturn(5000);
        Genome genome6 = mock(Genome.class);
        when(genome6.getFitness()).thenReturn(6000);
        Genome genome7 = mock(Genome.class);
        when(genome7.getFitness()).thenReturn(7000);
        Genome genome8 = mock(Genome.class);
        when(genome8.getFitness()).thenReturn(8000);
        Genome genome9 = mock(Genome.class);
        when(genome9.getFitness()).thenReturn(9000);
        Genome genome10 = mock(Genome.class);
        when(genome10.getFitness()).thenReturn(10000);

        genomes = new Genome[] { genome1, genome2, genome3, genome4, genome5, //
                genome6, genome7, genome8, genome9, genome10 };

        genePool = new GenePool<>();
View Full Code Here

    public void testAddGenome_WithNewGenome() throws Exception {
        int currentSize = genePool.getSize();
        int currentGeneration = genePool.getCurrentGeneration();
        long oldFitnessSum = genePool.getFitnessSum();

        Genome newGenome = mock(Genome.class);
        int newGenomeFitness = 42;
        when(newGenome.compareTo(newGenome)).thenReturn(Genome.EQUAL);
        when(newGenome.getFitness()).thenReturn(newGenomeFitness);

        genePool.addGenome(newGenome);

        assert genePool.contains(newGenome) : "Genepool should contain the added genome";
        assert genePool.getSize() == currentSize + 1 : "A newly added genome should increase the size of the genepool";
View Full Code Here

        verify(newGenome).setBirthGeneration(currentGeneration);
    }

    @Test
    public void testAddGenome_WhenNewGenomeHasSameFitnessAsExistingOne_ThenAddIt() throws Exception {
        Genome sameFitnessGenome = mock(Genome.class);
        when(sameFitnessGenome.getFitness()).thenReturn(genome1Fitness);
        makeGenomeMockComparable(sameFitnessGenome, genome1);
        makeGenomeMockComparable(sameFitnessGenome, genome2);
        makeGenomeMockComparable(sameFitnessGenome, genome3);

        genePool.addGenome(sameFitnessGenome);
View Full Code Here

TOP

Related Classes of de.mh4j.solver.genetic.Genome

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.