Source Code Cross Referenced for MainClass.java in  » Development » jgap » examples » equalDistribution » 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.equalDistribution 
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 licencing 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.equalDistribution;
011:
012:        import java.util.*;
013:        import org.jgap.*;
014:        import org.jgap.impl.*;
015:
016:        /**
017:         * Given 64 vents with different weights. Try to make 8 group with each
018:         * groups having (nearly) the same weight as the other groups.<p>
019:         * Here, each vent has a similar weight with a small deviation. But the
020:         * deviation could also be significant (then, good solutions are harder to
021:         * find).<p>
022:         * The proposed way of solving this problem is quite easy in its structure.
023:         * There is potential for optimzations!
024:         *
025:         * @author Klaus Meffert
026:         * @since 3.2
027:         */
028:        public class MainClass {
029:            /** String containing the CVS revision. Read out via reflection!*/
030:            private final static String CVS_REVISION = "$Revision: 1.1 $";
031:
032:            /**
033:             * Holds the available vents, each with a specific weight
034:             */
035:            private Vent[] m_vents;
036:
037:            private int m_numEvolutions;
038:
039:            /**
040:             * Constructor.
041:             *
042:             * @throws Exception
043:             *
044:             * @author Klaus Meffert
045:             * @since 3.2
046:             */
047:            public MainClass() throws Exception {
048:                makeVents();
049:                Genotype genotype = configureJGAP();
050:                doEvolution(genotype);
051:            }
052:
053:            /**
054:             * Sets up the configuration for the problem.
055:             *
056:             * @throws Exception
057:             *
058:             * @author Klaus Meffert
059:             * @since 3.2
060:             */
061:            protected Genotype configureJGAP() throws Exception {
062:                m_numEvolutions = 50;
063:                Configuration gaConf = new DefaultConfiguration();
064:                gaConf.resetProperty(Configuration.PROPERTY_FITEVAL_INST);
065:                gaConf.setFitnessEvaluator(new DeltaFitnessEvaluator());
066:                // Just use a swapping operator instead of mutation and others.
067:                // ------------------------------------------------------------
068:                gaConf.getGeneticOperators().clear();
069:                SwappingMutationOperator swapper = new SwappingMutationOperator(
070:                        gaConf);
071:                gaConf.addGeneticOperator(swapper);
072:                // Setup some other parameters.
073:                // ----------------------------
074:                gaConf.setPreservFittestIndividual(true);
075:                gaConf.setKeepPopulationSizeConstant(false);
076:                // Set number of individuals (=tries) per generation.
077:                // --------------------------------------------------
078:                gaConf.setPopulationSize(50);
079:                int chromeSize = m_vents.length;
080:                Genotype genotype = null;
081:                try {
082:                    // Setup the structure with which to evolve the
083:                    // solution of the problem.
084:                    // --------------------------------------------
085:                    IChromosome sampleChromosome = new Chromosome(gaConf,
086:                            new IntegerGene(gaConf), chromeSize);
087:                    gaConf.setSampleChromosome(sampleChromosome);
088:                    // Setup the important fitness function!
089:                    // -------------------------------------
090:                    gaConf
091:                            .setFitnessFunction(new SampleFitnessFunction(
092:                                    m_vents));
093:                    //
094:                    genotype = Genotype.randomInitialGenotype(gaConf);
095:                    // Now ensure that each number from 1..64 (representing the
096:                    // indices of the vents) is represented by exactly one gene.
097:                    // --> Suboptimal here, as randomized initialization becomes
098:                    //     obsolete (other solution would be more complicated).
099:                    // ---------------------------------------------------------
100:                    List chromosomes = genotype.getPopulation()
101:                            .getChromosomes();
102:                    for (int i = 0; i < chromosomes.size(); i++) {
103:                        IChromosome chrom = (IChromosome) chromosomes.get(i);
104:                        for (int j = 0; j < chrom.size(); j++) {
105:                            Gene gene = (Gene) chrom.getGene(j);
106:                            gene.setAllele(new Integer(j));
107:                        }
108:                    }
109:                } catch (InvalidConfigurationException e) {
110:                    e.printStackTrace();
111:                    System.exit(-2);
112:                }
113:                return genotype;
114:            }
115:
116:            /**
117:             * Does the evolution until finished.
118:             *
119:             * @author Klaus Meffert
120:             * @since 3.2
121:             */
122:            public void doEvolution(Genotype genotype) {
123:                int progress = 0;
124:                int percentEvolution = m_numEvolutions / 100;
125:                for (int i = 0; i < m_numEvolutions; i++) {
126:                    genotype.evolve();
127:                    // Print progress.
128:                    // ---------------
129:                    if (percentEvolution > 0 && i % percentEvolution == 0) {
130:                        progress++;
131:                        IChromosome fittest = genotype.getFittestChromosome();
132:                        double fitness = fittest.getFitnessValue();
133:                        System.out
134:                                .println("Currently best solution has fitness "
135:                                        + fitness);
136:                        printSolution(fittest);
137:                    }
138:                }
139:                // Print summary.
140:                // --------------
141:                IChromosome fittest = genotype.getFittestChromosome();
142:                System.out.println("Best solution has fitness "
143:                        + fittest.getFitnessValue());
144:                printSolution(fittest);
145:            }
146:
147:            /**
148:             * @param a_solution a solution to print to the console
149:             *
150:             * @author Klaus Meffert
151:             * @since 3.2
152:             */
153:            public void printSolution(IChromosome a_solution) {
154:                double groupWeights = 0.0d;
155:                for (int i = 0; i < 8; i++) {
156:                    System.out.println("\nGroup " + i);
157:                    System.out.println("-------");
158:                    double groupWeight = 0.0d;
159:                    for (int j = 0; j < 8; j++) {
160:                        IntegerGene ventIndex = (IntegerGene) a_solution
161:                                .getGene((i * 8 + j));
162:                        Vent vent = (Vent) m_vents[ventIndex.intValue()];
163:                        double weight = vent.getWeight();
164:                        groupWeight += weight;
165:                        System.out.println("  Vent at index "
166:                                + ventIndex.intValue() + " with weight "
167:                                + weight);
168:                    }
169:                    groupWeights += groupWeight;
170:                    System.out.println("  --> Group weight: " + groupWeight);
171:                }
172:                System.out.println("\n Average group weight: " + groupWeights
173:                        / 8);
174:            }
175:
176:            /**
177:             * Create vents with different weights.
178:             *
179:             * @author Klaus Meffert
180:             * @since 3.2
181:             */
182:            public void makeVents() {
183:                m_vents = new Vent[64];
184:                for (int i = 0; i < m_vents.length; i++) {
185:                    // Set a weight between 290 and 310
186:                    double weight = 290 + Math.random() * 20;
187:                    Vent vent = new Vent(weight);
188:                    m_vents[i] = vent;
189:                }
190:            }
191:
192:            /**
193:             * Start the example
194:             * @param args ignored
195:             *
196:             * @author Klaus Meffert
197:             * @since 3.2
198:             */
199:            public static void main(String[] args) {
200:                try {
201:                    new MainClass();
202:                } catch (Throwable t) {
203:                    t.printStackTrace();
204:                    System.exit(1);
205:                }
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.