遺傳演算法Population

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.