Source Code Cross Referenced for TestOverallPerformance.java in  » Development » jgap » org » jgap » perf » 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 » org.jgap.perf 
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 org.jgap.perf;
011:
012:        import java.util.*;
013:        import org.jgap.*;
014:        import org.jgap.impl.*;
015:
016:        /**
017:         * This class provides an implementation of an overall performance test.
018:         * To obtain this, the provided example has been modified slightly, regarding
019:         * the random number generator. We use a static number generator here which does
020:         * not deserve the name "random generator". With that we have a determined
021:         * calculation path that results in reproducable results.
022:         * By executing the example several times we get a performance measurement.
023:         * The measured time has to be compared to other results manually as with
024:         * different hardware equipment the numbers vary a lot.
025:         *
026:         * @author Klaus Meffert
027:         * @since 2.0
028:         */
029:        public class TestOverallPerformance {
030:            /** String containing the CVS revision. Read out via reflection!*/
031:            private final static String CVS_REVISION = "$Revision: 1.6 $";
032:
033:            /**
034:             * The total number of times we'll let the population evolve.
035:             */
036:            private static final int MAX_ALLOWED_EVOLUTIONS = 1000;
037:
038:            /**
039:             * Executes the genetic algorithm to determine the minimum number of
040:             * coins necessary to make up the given target amount of change. The
041:             * solution will then be written to System.out.
042:             *
043:             * @param a_targetChangeAmount the target amount of change for which this
044:             * method is attempting to produce the minimum number of coins
045:             *
046:             * @throws Exception
047:             *
048:             * @author Klaus Meffert
049:             * @since 2.0
050:             */
051:            public void makeChangeForAmount(int a_targetChangeAmount)
052:                    throws Exception {
053:                // Start with a DefaultConfiguration, which comes setup with the
054:                // most common settings.
055:                // -------------------------------------------------------------
056:                Configuration.reset();
057:                Configuration conf = new DefaultConfiguration();
058:                RandomGeneratorForTesting gen = new RandomGeneratorForTesting();
059:                gen.setNextDouble(0.5d);
060:                gen.setNextBoolean(true);
061:                gen.setNextInt(3);
062:                gen.setNextFloat(0.7f);
063:                gen.setNextLong(6);
064:                conf.setRandomGenerator(gen);
065:                // Set the fitness function we want to use, which is our
066:                // MinimizingMakeChangeFitnessFunction. We construct it with
067:                // the target amount of change passed in to this method.
068:                // ---------------------------------------------------------
069:                FitnessFunction myFunc = new TestOverallPerformanceFitnessFunc(
070:                        a_targetChangeAmount);
071:                conf.setFitnessFunction(myFunc);
072:                // Now we need to tell the Configuration object how we want our
073:                // Chromosomes to be setup. We do that by actually creating a
074:                // sample Chromosome and then setting it on the Configuration
075:                // object. As mentioned earlier, we want our Chromosomes to each
076:                // have four genes, one for each of the coin types. We want the
077:                // values (alleles) of those genes to be integers, which represent
078:                // how many coins of that type we have. We therefore use the
079:                // IntegerGene class to represent each of the genes. That class
080:                // also lets us specify a lower and upper bound, which we set
081:                // to sensible values for each coin type.
082:                // Here we use "fantasy" coins just to have more genes and bloat the time
083:                // consumed for test performance test!
084:                // --------------------------------------------------------------
085:                Gene[] sampleGenes = new Gene[10];
086:                sampleGenes[0] = new IntegerGene(conf, 0, 3); // Quarters
087:                sampleGenes[1] = new IntegerGene(conf, 0, 2); // Dimes
088:                sampleGenes[2] = new IntegerGene(conf, 0, 1); // Nickels
089:                sampleGenes[3] = new IntegerGene(conf, 0, 4); // Pennies
090:                sampleGenes[4] = new IntegerGene(conf, 0, 3); // A
091:                sampleGenes[5] = new IntegerGene(conf, 0, 1); // B
092:                sampleGenes[6] = new IntegerGene(conf, 0, 1); // C
093:                sampleGenes[7] = new IntegerGene(conf, 0, 2); // D
094:                sampleGenes[8] = new IntegerGene(conf, 0, 3); // E
095:                sampleGenes[9] = new IntegerGene(conf, 0, 1); // F
096:                Chromosome sampleChromosome = new Chromosome(conf, sampleGenes);
097:                conf.setSampleChromosome(sampleChromosome);
098:                // Finally, we need to tell the Configuration object how many
099:                // Chromosomes we want in our population. The more Chromosomes,
100:                // the larger number of potential solutions (which is good for
101:                // finding the answer), but the longer it will take to evolve
102:                // the population (which could be seen as bad). We'll just set
103:                // the population size to 10000 here. It is that big because of performance
104:                // test issues!
105:                // ------------------------------------------------------------
106:                conf.setPopulationSize(10000);
107:                // Create random initial population of Chromosomes.
108:                // ------------------------------------------------
109:                Genotype population = Genotype.randomInitialGenotype(conf);
110:                // Evolve the population. Since we don't know what the best answer
111:                // is going to be, we just evolve the max number of times.
112:                // ---------------------------------------------------------------
113:                for (int i = 0; i < MAX_ALLOWED_EVOLUTIONS; i++) {
114:                    population.evolve();
115:                }
116:                // Determine the best solution we found.
117:                // -------------------------------------
118:                population.getFittestChromosome();
119:            }
120:
121:            /**
122:             * Execute the performance test.
123:             *
124:             * @param args ignored
125:             * @throws Exception
126:             *
127:             * @author Klaus Meffert
128:             * @since 2.0
129:             */
130:            public static void main(String[] args) throws Exception {
131:                final int amount = 287;
132:                final int numRuns = 20;
133:                long starttime, timeMillis;
134:                System.out.println("Test started.");
135:                // get current time
136:                starttime = getCurrentMilliseconds();
137:                for (int i = 0; i < numRuns; i++) {
138:                    TestOverallPerformance runner = new TestOverallPerformance();
139:                    runner.makeChangeForAmount(amount);
140:                }
141:                // calculate time of run
142:                timeMillis = getCurrentMilliseconds() - starttime;
143:                System.out
144:                        .println("Overall time needed for executing performance test: "
145:                                + timeMillis + " [millisecs]");
146:            }
147:
148:            /**
149:             * @return current time in milliseconds
150:             */
151:            private static long getCurrentMilliseconds() {
152:                Calendar cal = Calendar.getInstance(TimeZone.getDefault());
153:                return cal.getTimeInMillis();
154:            }
155:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.