Source Code Cross Referenced for ClientEvolveStrategy.java in  » Development » jgap » examples » grid » mathProblemDistributed » 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.grid.mathProblemDistributed 
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.grid.mathProblemDistributed;
011:
012:        import org.homedns.dade.jcgrid.client.*;
013:        import org.jgap.*;
014:        import org.jgap.gp.*;
015:        import org.jgap.gp.impl.*;
016:        import org.jgap.distr.grid.*;
017:        import org.jgap.distr.grid.gp.*;
018:        import org.jgap.gp.function.Subtract;
019:        import org.jgap.gp.function.Add3;
020:        import org.jgap.gp.impl.GPConfiguration;
021:        import org.jgap.gp.function.Divide;
022:        import org.jgap.gp.function.Multiply3;
023:        import org.jgap.gp.function.Pow;
024:        import org.jgap.gp.function.Exp;
025:        import org.jgap.gp.terminal.Variable;
026:        import org.jgap.gp.function.Sine;
027:        import org.jgap.gp.function.Multiply;
028:        import org.jgap.gp.function.Add;
029:        import org.jgap.gp.impl.*;
030:        import org.jgap.gp.terminal.*;
031:        import org.apache.log4j.*;
032:
033:        /**
034:         * Sample implementation of a strategy for evolving a generation on the client.
035:         *
036:         * @author Klaus Meffert
037:         * @since 3.2
038:         */
039:        public class ClientEvolveStrategy extends GPProblem implements 
040:                IClientEvolveStrategyGP {
041:            /** String containing the CVS revision. Read out via reflection!*/
042:            public final static String CVS_REVISION = "$Revision: 1.9 $";
043:
044:            private static Logger log = Logger
045:                    .getLogger(ClientEvolveStrategy.class);
046:
047:            //  private GPConfiguration m_config;
048:
049:            private IClientFeedbackGP m_clientFeedback;
050:
051:            private final int m_maxEvolutions = 3;
052:
053:            private GPPopulation m_pop;
054:
055:            private static Float[] x = new Float[20];
056:
057:            private static float[] y = new float[20];
058:
059:            /**@todo pass config*/
060:            //  public ClientEvolveStrategy(GPConfiguration a_conf)
061:            //      throws InvalidConfigurationException {
062:            //    super(a_conf);
063:            //  }
064:            /**
065:             * Default constructor is necessary here as it will be called dynamically!
066:             * Don't declare any other constructor as it will not be called!
067:             */
068:            public ClientEvolveStrategy() {
069:                super ();
070:            }
071:
072:            /**
073:             * Called at the very beginning and only once before distributed evolution
074:             * starts.
075:             *
076:             * @param a_gc GridClient
077:             * @param a_config Configuration
078:             * @param a_clientFeedback IClientFeedback
079:             * @throws Exception
080:             *
081:             * @author Klaus Meffert
082:             * @since 3.2
083:             */
084:            public void initialize(GridClient a_gc, GPConfiguration a_config,
085:                    IClientFeedbackGP a_clientFeedback) throws Exception {
086:                log.info("Initializing...");
087:                super .setGPConfiguration(a_config);
088:                m_clientFeedback = a_clientFeedback;
089:                // Start with a randomly initialized population.
090:                // ---------------------------------------------
091:                GPGenotype gen = create();
092:                m_pop = gen.getGPPopulation();
093:            }
094:
095:            public void afterWorkRequestsSent() throws Exception {
096:                // Don't clear population here!
097:                if (false) {
098:                    // Important: clear population, otherwise it would grow
099:                    // endlessly.
100:                    // ----------------------------------------------------
101:                    if (m_pop != null) {
102:                        m_pop.clear();
103:                        log.warn("Population cleared");
104:                    }
105:                }
106:            }
107:
108:            public boolean isEvolutionFinished(int a_evolutionsDone) {
109:                if (m_pop != null) {
110:                    // Check if best solution is satisfying.
111:                    // -------------------------------------
112:                    IGPProgram fittest = m_pop.determineFittestProgram();
113:                    if (fittest.getFitnessValue() < 20) {
114:                        return true;
115:                    }
116:                }
117:                // Do the complete evolution cycle 3 times.
118:                // ----------------------------------------
119:                if (a_evolutionsDone > m_maxEvolutions) {
120:                    return true;
121:                } else {
122:                    return false;
123:                }
124:            }
125:
126:            /**
127:             * Called after evolution has finished.
128:             */
129:            public void onFinished() {
130:                IGPProgram best = m_pop.determineFittestProgram();
131:                m_clientFeedback.info("Best solution evolved: "
132:                        + best.getFitnessValue());
133:            }
134:
135:            public void evolve() throws Exception {
136:                // Nothing to do here in this example as evolution takes
137:                // place on behalf of the workers .
138:                // -----------------------------------------------------
139:            }
140:
141:            public JGAPRequestGP[] generateWorkRequests(
142:                    JGAPRequestGP m_workReq,
143:                    IRequestSplitStrategyGP m_splitStrategy, Object data)
144:                    throws Exception {
145:                JGAPRequestGP[] workList;
146:                if (m_pop == null || m_pop.isFirstEmpty()) {
147:                    log.warn("Initial population is empty!");
148:                }
149:                m_workReq.setPopulation(m_pop);
150:                m_workReq.setConfiguration(getGPConfiguration());
151:                if (getGPConfiguration().getJGAPFactory() == null) {
152:                    throw new IllegalStateException(
153:                            "JGAPFactory must not be null!");
154:                }
155:                workList = m_splitStrategy.split(m_workReq);
156:                return workList;
157:            }
158:
159:            /**
160:             * Merge the received results as a basis for the next evolution.
161:             *
162:             * @param a_result JGAPResult
163:             * @throws Exception
164:             * @since 3.2
165:             */
166:            public void resultReceived(JGAPResultGP a_result) throws Exception {
167:                // This is a very simplistic implementation!
168:                // -----------------------------------------
169:                GPPopulation pop = a_result.getPopulation();
170:                if (pop == null || pop.isFirstEmpty()) {
171:                    IGPProgram best = a_result.getFittest();
172:                    if (best == null) {
173:                        log.error("Empty result received");
174:                    } else {
175:                        m_pop.addFittestProgram(best);
176:                    }
177:                } else {
178:                    m_pop = pop;
179:                }
180:            }
181:
182:            public GPGenotype create() throws InvalidConfigurationException {
183:                GPConfiguration conf = getGPConfiguration();
184:                Class[] types = { CommandGene.FloatClass };
185:                Class[][] argTypes = { {} };
186:                Variable vx;
187:                CommandGene[][] nodeSets = { {
188:                        vx = Variable.create(conf, "X", CommandGene.FloatClass),
189:                        new Add(conf, CommandGene.FloatClass),
190:                        new Add3(conf, CommandGene.FloatClass),
191:                        new Subtract(conf, CommandGene.FloatClass),
192:                        new Multiply(conf, CommandGene.FloatClass),
193:                        new Multiply3(conf, CommandGene.FloatClass),
194:                        new Divide(conf, CommandGene.FloatClass),
195:                        new Sine(conf, CommandGene.FloatClass),
196:                        new Exp(conf, CommandGene.FloatClass),
197:                        new Pow(conf, CommandGene.FloatClass),
198:                        new Terminal(conf, CommandGene.FloatClass, 2.0d, 10.0d,
199:                                true), } };
200:                // Create genotype with initial population.
201:                // ----------------------------------------
202:                GPGenotype result = GPGenotype.randomInitialGenotype(conf,
203:                        types, argTypes, nodeSets, 20, true);
204:                result.putVariable(vx);
205:                conf.putVariable(vx);
206:                return result;
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.