遺傳演算法AllOnesGA

來源:互聯網
上載者:User

標籤:problems   confirm   str   end   div   out   loop   package   cas   

package chapter2;/** * This is our main class used to run the genetic algorithm. *  * This case is one of the simplest problems we can solve: the objective is to * end up with an individual whose chromosome is all ones. *  * The simplicity of this problem makes the GeneticAlgorithm class‘ * "calcFitness" method very simple. We‘ll just count the number of ones in the * chromosome and use that as the fitness score. Similarly, the * "isTerminationConditionMet" method in the GeneticAlgorithm class for this * example is very simple: if the fitness score (ie, number of ones) is the same * as the length of the chromosome (ie, we‘re all ones), we‘re done! *  * @author bkanber * */public class AllOnesGA {    public static void main(String[] args) {        // Create GA object        GeneticAlgorithm ga = new GeneticAlgorithm(100, 0.001, 0.95, 2);        // Initialize population        Population population = ga.initPopulation(50);        // Evaluate population        ga.evalPopulation(population);        // Keep track of current generation        int generation = 1;        /**         * Start the evolution loop         *          * Every genetic algorithm problem has different criteria for finishing.         * In this case, we know what a perfect solution looks like (we don‘t         * always!), so our isTerminationConditionMet method is very         * straightforward: if there‘s a member of the population whose         * chromosome is all ones, we‘re done!         */        while (ga.isTerminationConditionMet(population) == false) {            // Print fittest individual from population            System.out.println("Best solution: " + population.getFittest(0).toString());            // Apply crossover            population = ga.crossoverPopulation(population);            // Apply mutation            population = ga.mutatePopulation(population);            // Evaluate population            ga.evalPopulation(population);            // Increment the current generation            generation++;        }        /**         * We‘re out of the loop now, which means we have a perfect solution on         * our hands. Let‘s print it out to confirm that it is actually all         * ones, as promised.         */        System.out.println("Found solution in " + generation + " generations");        System.out.println("Best solution: " + population.getFittest(0).toString());    }}

 

遺傳演算法AllOnesGA

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.