Source Code Cross Referenced for ChromosomeInit.java in  » Development » jgap » examples » chromInit » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jgap » examples.chromInit 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of JGAP.
003:         *
004:         * JGAP offers a dual license model containing the LGPL as well as the MPL.
005:         *
006:         * For licensing information please see the file license.txt included with JGAP
007:         * or have a look at the top of class org.jgap.Chromosome which representatively
008:         * includes the JGAP license policy applicable for any file delivered with JGAP.
009:         */
010:        package examples.chromInit;
011:
012:        import org.jgap.*;
013:        import org.jgap.impl.*;
014:
015:        /**
016:         * Simple test class that demonstrates how to initialize chromosomes with
017:         * different numbers of Genes.
018:         *
019:         * @author Klaus Meffert
020:         * @since 2.4
021:         */
022:        public class ChromosomeInit {
023:            /** String containing the CVS revision. Read out via reflection!*/
024:            private static final String CVS_REVISION = "$Revision: 1.7 $";
025:
026:            public static void main(String[] args) {
027:                int numEvolutions = 500;
028:                // Create configuration by using DefaultConfiguration and removing
029:                // CrossoverOperator. Removal necessary because that operator does not
030:                // work with chromosomes of different gene size!
031:                Configuration gaConf = new DefaultConfiguration();
032:                // The next statement is dirty and to be avoided outside this example!
033:                gaConf.getGeneticOperators().remove(0);
034:                gaConf.setPreservFittestIndividual(true);
035:                gaConf.setKeepPopulationSizeConstant(false);
036:                try {
037:                    int chromeSize;
038:                    if (args.length == 0) {
039:                        chromeSize = 7;
040:                    } else {
041:                        chromeSize = Integer.parseInt(args[0]);
042:                    }
043:                    if (chromeSize > 15) {
044:                        System.err
045:                                .println("This example does not handle "
046:                                        + "Chromosomes greater than 15 bits in length.");
047:                        System.exit(-1);
048:                    }
049:                    IChromosome sampleChromosome = new Chromosome(gaConf,
050:                            new BooleanGene(gaConf), chromeSize);
051:                    gaConf.setSampleChromosome(sampleChromosome);
052:                    gaConf.setPopulationSize(20);
053:                    gaConf.setFitnessFunction(new MaxFunction());
054:                    // Completely initialize the population with custom code.
055:                    // Notice that we assign the double number of Genes to
056:                    // each other Chromosome.
057:                    // ------------------------------------------------------
058:                    int populationSize = gaConf.getPopulationSize();
059:                    Population pop = new Population(gaConf, populationSize);
060:                    for (int i = 0; i < populationSize; i++) {
061:                        int mult;
062:                        // Every second Chromosome has double the number of Genes.
063:                        // -------------------------------------------------------
064:                        if (i % 2 == 0) {
065:                            mult = 1;
066:                        } else {
067:                            mult = 2;
068:                        }
069:                        Gene[] sampleGenes = sampleChromosome.getGenes();
070:                        Gene[] newGenes = new Gene[sampleGenes.length * mult];
071:                        RandomGenerator generator = gaConf.getRandomGenerator();
072:                        for (int j = 0; j < newGenes.length; j = j + mult) {
073:                            // We use the newGene() method on each of the genes in the
074:                            // sample Chromosome to generate our new Gene instances for
075:                            // the Chromosome we're returning. This guarantees that the
076:                            // new Genes are setup with all of the correct internal state
077:                            // for the respective gene position they're going to inhabit.
078:                            // ----------------------------------------------------------
079:                            newGenes[j] = sampleGenes[j / mult].newGene();
080:                            // Set the gene's value (allele) to a random value.
081:                            // ------------------------------------------------
082:                            newGenes[j].setToRandomValue(generator);
083:                            if (mult > 1) {
084:                                newGenes[j + 1] = sampleGenes[j / 2].newGene();
085:                                // Set the gene's value (allele) to a random value.
086:                                // ------------------------------------------------
087:                                newGenes[j + 1].setToRandomValue(generator);
088:                            }
089:                        }
090:                        IChromosome chrom = Chromosome
091:                                .randomInitialChromosome(gaConf);
092:                        chrom.setGenes(newGenes);
093:                        pop.addChromosome(chrom);
094:                    }
095:                    // Now we need to construct the Genotype. This could otherwise be
096:                    // accomplished more easily by writing
097:                    // "Genotype genotype = Genotype.randomInitialGenotype(...)"
098:                    Genotype genotype = new Genotype(gaConf, pop);
099:                    int progress = 0;
100:                    int percentEvolution = numEvolutions / 100;
101:                    for (int i = 0; i < numEvolutions; i++) {
102:                        genotype.evolve();
103:                        // Print progress.
104:                        // ---------------
105:                        if (percentEvolution > 0 && i % percentEvolution == 0) {
106:                            progress++;
107:                            IChromosome fittest = genotype
108:                                    .getFittestChromosome();
109:                            double fitness = fittest.getFitnessValue();
110:                            System.out.println("Fittest Chromosome has value "
111:                                    + fitness);
112:                        }
113:                    }
114:                    IChromosome fittest = genotype.getFittestChromosome();
115:                    System.out.println("Fittest Chromosome has value "
116:                            + fittest.getFitnessValue());
117:                } catch (InvalidConfigurationException e) {
118:                    e.printStackTrace();
119:                    System.exit(-2);
120:                }
121:            }
122:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.