Package com.heatonresearch.aifh.genetic.genome

Examples of com.heatonresearch.aifh.genetic.genome.IntegerArrayGenome


            parents[0].getData()[i - 1] = i;
        }

        // Create an array to hold the offspring.
        IntegerArrayGenome[] offspring = new IntegerArrayGenome[1];
        offspring[0] = new IntegerArrayGenome(5);

        // Perform the operation
        opp.performOperation(rnd, parents, 0, offspring, 0);

        // Display the results
View Full Code Here


     * {@inheritDoc}
     */
    @Override
    public double calculateScore(MLMethod phenotype) {
        double result = 0.0;
        IntegerArrayGenome genome = (IntegerArrayGenome) phenotype;
        int[] path = genome.getData();

        for (int i = 0; i < cities.length - 1; i++) {
            City city1 = cities[path[i]];
            City city2 = cities[path[i + 1]];

View Full Code Here

    /**
     * Generate a random path through cities.
     */
    private IntegerArrayGenome randomGenome() {
        IntegerArrayGenome result = new IntegerArrayGenome(cities.length);
        final int organism[] = result.getData();
        final boolean taken[] = new boolean[cities.length];

        for (int i = 0; i < organism.length - 1; i++) {
            int icandidate;
            do {
View Full Code Here

        Population result = new BasicPopulation(POPULATION_SIZE, null);

        BasicSpecies defaultSpecies = new BasicSpecies();
        defaultSpecies.setPopulation(result);
        for (int i = 0; i < POPULATION_SIZE; i++) {
            final IntegerArrayGenome genome = randomGenome();
            defaultSpecies.add(genome);
        }
        result.setGenomeFactory(new IntegerArrayGenomeFactory(cities.length));
        result.getSpecies().add(defaultSpecies);
View Full Code Here

            lastSolution = thisSolution;
        }

        System.out.println("Good solution found:");
        IntegerArrayGenome best = (IntegerArrayGenome) genetic.getBestGenome();
        displaySolution(best);
        genetic.finishTraining();

    }
View Full Code Here

            throw new AIFHError("This speciation does not support: " + genome1.getClass().getName());
        }
    }

    private double scoreInt(final Genome genome1, final Genome genome2) {
        IntegerArrayGenome intGenome1 = (IntegerArrayGenome) genome1;
        IntegerArrayGenome intGenome2 = (IntegerArrayGenome) genome2;
        double sum = 0;
        for (int i = 0; i < intGenome1.size(); i++) {
            double diff = intGenome1.getData()[i] - intGenome2.getData()[i];
            sum += diff * diff;
        }
        return Math.sqrt(sum);
    }
View Full Code Here

        Population pop = new BasicPopulation();
        Species species = pop.createSpecies();

        // Create 1000 genomes, assign the score to be the index number.
        for (int i = 0; i < 1000; i++) {
            Genome genome = new IntegerArrayGenome(1);
            genome.setScore(i);
            genome.setAdjustedScore(i);
            pop.getSpecies().get(0).add(genome);
        }

        GenerateRandom rnd = new MersenneTwisterGenerateRandom();

        // Create a trainer with a very simple score function.  We do not care
        // about the calculation of the score, as they will never be calculated.
        // We only care that we are maximizing.
        EvolutionaryAlgorithm train = new BasicEA(pop, new ScoreFunction() {
            @Override
            public double calculateScore(MLMethod method) {
                return 0;
            }

            @Override
            public boolean shouldMinimize() {
                return false;
            }
        });

        // Perform the test for round counts between 1 and 10.
        for (int roundCount = 1; roundCount <= 10; roundCount++) {
            TournamentSelection selection = new TournamentSelection(train, roundCount);
            int sum = 0;
            int count = 0;
            for (int i = 0; i < 100000; i++) {
                int genomeID = selection.performSelection(rnd, species);
                Genome genome = species.getMembers().get(genomeID);
                sum += genome.getAdjustedScore();
                count++;
            }
            sum /= count;
            System.out.println("Rounds: " + roundCount + ", Avg Score: " + sum);
        }
View Full Code Here

     */
    @Override
    public void performOperation(GenerateRandom rnd, Genome[] parents, int parentIndex,
                                 Genome[] offspring, int offspringIndex) {

        IntegerArrayGenome mother = (IntegerArrayGenome) parents[parentIndex];
        IntegerArrayGenome father = (IntegerArrayGenome) parents[parentIndex + 1];
        IntegerArrayGenome offspring1 = (IntegerArrayGenome) this.owner.getPopulation().getGenomeFactory().factor();
        IntegerArrayGenome offspring2 = (IntegerArrayGenome) this.owner.getPopulation().getGenomeFactory().factor();

        offspring[offspringIndex] = offspring1;
        offspring[offspringIndex + 1] = offspring2;

        final int geneLength = mother.size();

        // the chromosome must be cut at two positions, determine them
        final int cutpoint1 = rnd.nextInt(geneLength - this.cutLength);
        final int cutpoint2 = cutpoint1 + this.cutLength;

        // keep track of which genes have been taken in each of the two
        // offspring, defaults to false.
        final Set<Integer> taken1 = new HashSet<Integer>();
        final Set<Integer> taken2 = new HashSet<Integer>();

        // handle cut section
        for (int i = 0; i < geneLength; i++) {
            if (!((i < cutpoint1) || (i > cutpoint2))) {
                offspring1.copy(father, i, i);
                offspring2.copy(mother, i, i);
                taken1.add(father.getData()[i]);
                taken2.add(mother.getData()[i]);
            }
        }

        // handle outer sections
        for (int i = 0; i < geneLength; i++) {
            if ((i < cutpoint1) || (i > cutpoint2)) {

                offspring1.getData()[i] = SpliceNoRepeat.getNotTaken(mother, taken1);
                offspring2.getData()[i] = SpliceNoRepeat.getNotTaken(father, taken2);

            }
        }
    }
View Full Code Here

TOP

Related Classes of com.heatonresearch.aifh.genetic.genome.IntegerArrayGenome

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.