码迷,mamicode.com
首页 > 编程语言 > 详细

遗传算法Population

时间:2017-09-23 13:39:49      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:sel   pack   tis   cti   tor   shu   group   util   evel   

package chapter2;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;


/**
 * A population is an abstraction of a collection of individuals. The population
 * class is generally used to perform group-level operations on its individuals,
 * such as finding the strongest individuals, collecting stats on the population
 * as a whole, and selecting individuals to mutate or crossover.
 * 
 * @author bkanber
 *
 */
public class Population {
    private Individual population[];
    private double populationFitness = -1;

    /**
     * Initializes blank population of individuals
     * 
     * @param populationSize
     *            The number of individuals in the population
     */
    public Population(int populationSize) {
        // Initial population
        this.population = new Individual[populationSize];
    }

    /**
     * Initializes population of individuals
     * 
     * @param populationSize
     *            The number of individuals in the population
     * @param chromosomeLength
     *            The size of each individual‘s chromosome
     */
    public Population(int populationSize, int chromosomeLength) {
        // Initialize the population as an array of individuals
        this.population = new Individual[populationSize];

        // Create each individual in turn
        for (int individualCount = 0; individualCount < populationSize; individualCount++) {
            // Create an individual, initializing its chromosome to the given
            // length
            Individual individual = new Individual(chromosomeLength);
            // Add individual to population
            this.population[individualCount] = individual;
        }
    }

    /**
     * Get individuals from the population
     * 
     * @return individuals Individuals in population
     */
    public Individual[] getIndividuals() {
        return this.population;
    }

    /**
     * Find an individual in the population by its fitness
     * 
     * This method lets you select an individual in order of its fitness. This
     * can be used to find the single strongest individual (eg, if you‘re
     * testing for a solution), but it can also be used to find weak individuals
     * (if you‘re looking to cull the population) or some of the strongest
     * individuals (if you‘re using "elitism").
     * 
     * @param offset
     *            The offset of the individual you want, sorted by fitness. 0 is
     *            the strongest, population.length - 1 is the weakest.
     * @return individual Individual at offset
     */
    public Individual getFittest(int offset) {
        // Order population by fitness
        Arrays.sort(this.population, new Comparator<Individual>() {
            @Override
            public int compare(Individual o1, Individual o2) {
                if (o1.getFitness() > o2.getFitness()) {
                    return -1;
                } else if (o1.getFitness() < o2.getFitness()) {
                    return 1;
                }
                return 0;
            }
        });

        // Return the fittest individual
        return this.population[offset];
    }

    /**
     * Set population‘s group fitness
     * 
     * @param fitness
     *            The population‘s total fitness
     */
    public void setPopulationFitness(double fitness) {
        this.populationFitness = fitness;
    }

    /**
     * Get population‘s group fitness
     * 
     * @return populationFitness The population‘s total fitness
     */
    public double getPopulationFitness() {
        return this.populationFitness;
    }

    /**
     * Get population‘s size
     * 
     * @return size The population‘s size
     */
    public int size() {
        return this.population.length;
    }

    /**
     * Set individual at offset
     * 
     * @param individual
     * @param offset
     * @return individual
     */
    public Individual setIndividual(int offset, Individual individual) {
        return population[offset] = individual;
    }

    /**
     * Get individual at offset
     * 
     * @param offset
     * @return individual
     */
    public Individual getIndividual(int offset) {
        return population[offset];
    }
    
    /**
     * Shuffles the population in-place
     * 
     * @param void
     * @return void
     */
    public void shuffle() {
        Random rnd = new Random();
        for (int i = population.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            Individual a = population[index];
            population[index] = population[i];
            population[i] = a;
        }
    }
}

 

遗传算法Population

标签:sel   pack   tis   cti   tor   shu   group   util   evel   

原文地址:http://www.cnblogs.com/Michael2397/p/7580679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!