Source Code Cross Referenced for TurnToFood.java in  » Development » jgap » examples » gp » anttrail » 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.gp.anttrail 
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.gp.anttrail;
011:
012:        import org.jgap.*;
013:        import org.jgap.gp.*;
014:        import org.jgap.gp.impl.*;
015:
016:        /**
017:         * Look ahead, right and left (in this order) and turns to food in case such was
018:         * detected. Does nothing in the other case.<p>
019:         * This command is not part of the classic ant problem.
020:         *
021:         * @author Klaus Meffert
022:         * @since 3.01
023:         */
024:        public class TurnToFood extends AntCommand implements  IMutateable {
025:            /** String containing the CVS revision. Read out via reflection!*/
026:            private final static String CVS_REVISION = "$Revision: 1.2 $";
027:
028:            /**
029:             * Constructor.
030:             *
031:             * @param a_conf the configuration to use
032:             *
033:             * @throws InvalidConfigurationException
034:             *
035:             * @author Klaus Meffert
036:             * @since 3.01
037:             */
038:            public TurnToFood(final GPConfiguration a_conf)
039:                    throws InvalidConfigurationException {
040:                super (a_conf, 0, CommandGene.VoidClass);
041:            }
042:
043:            public CommandGene applyMutation(int index, double a_percentage)
044:                    throws InvalidConfigurationException {
045:                CommandGene mutant;
046:                if (a_percentage < 0.33d) {
047:                    mutant = new Right(getGPConfiguration());
048:                } else if (a_percentage < 0.67d) {
049:                    mutant = new Left(getGPConfiguration());
050:                } else {
051:                    mutant = new Move(getGPConfiguration());
052:                }
053:                return mutant;
054:            }
055:
056:            public void execute_void(ProgramChromosome a_chrom, int a_n,
057:                    Object[] a_args) {
058:                AntMap map = getMap(a_chrom);
059:                int x = map.getPosX();
060:                int y = map.getPosY();
061:                int orient = map.getOrientation();
062:                // Look ahead for food.
063:                // --------------------
064:                boolean found = false;
065:                switch (orient) {
066:                case AntMap.O_DOWN:
067:                    if (y < map.getHeight() - 1) {
068:                        if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
069:                            found = true;
070:                            map.setPosY(y + 1);
071:                        }
072:                    }
073:                    break;
074:                case AntMap.O_LEFT:
075:                    if (x >= 1) {
076:                        if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
077:                            found = true;
078:                            map.setPosX(x - 1);
079:                        }
080:                    }
081:                    break;
082:                case AntMap.O_RIGHT:
083:                    if (x < map.getWidth() - 1) {
084:                        if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
085:                            found = true;
086:                            map.setPosX(x + 1);
087:                        }
088:                    }
089:                    break;
090:                case AntMap.O_UP:
091:                    if (y >= 1) {
092:                        if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
093:                            found = true;
094:                            map.setPosY(y - 1);
095:                        }
096:                    }
097:                    break;
098:                }
099:                if (!found) {
100:                    // Look right for food.
101:                    // --------------------
102:                    switch (orient) {
103:                    case AntMap.O_RIGHT:
104:                        if (y < map.getHeight() - 1) {
105:                            if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
106:                                found = true;
107:                                map.setPosY(y + 1);
108:                                map.setOrientation(AntMap.O_DOWN);
109:                            }
110:                        }
111:                        break;
112:                    case AntMap.O_DOWN:
113:                        if (x >= 1) {
114:                            if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
115:                                found = true;
116:                                map.setPosX(x - 1);
117:                                map.setOrientation(AntMap.O_LEFT);
118:                            }
119:                        }
120:                        break;
121:                    case AntMap.O_UP:
122:                        if (x < map.getWidth() - 1) {
123:                            if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
124:                                found = true;
125:                                map.setPosX(x + 1);
126:                                map.setOrientation(AntMap.O_RIGHT);
127:                            }
128:                        }
129:                        break;
130:                    case AntMap.O_LEFT:
131:                        if (y >= 1) {
132:                            if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
133:                                found = true;
134:                                map.setPosY(y - 1);
135:                                map.setOrientation(AntMap.O_UP);
136:                            }
137:                        }
138:                        break;
139:                    }
140:                    if (!found) {
141:                        // Look left for food.
142:                        // -------------------
143:                        switch (orient) {
144:                        case AntMap.O_LEFT:
145:                            if (y < map.getHeight() - 1) {
146:                                if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
147:                                    found = true;
148:                                    map.setPosY(y + 1);
149:                                    map.setOrientation(AntMap.O_DOWN);
150:                                }
151:                            }
152:                            break;
153:                        case AntMap.O_UP:
154:                            if (x >= 1) {
155:                                if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
156:                                    found = true;
157:                                    map.setPosX(x - 1);
158:                                    map.setOrientation(AntMap.O_LEFT);
159:                                }
160:                            }
161:                            break;
162:                        case AntMap.O_DOWN:
163:                            if (x < map.getWidth() - 1) {
164:                                if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
165:                                    found = true;
166:                                    map.setPosX(x + 1);
167:                                    map.setOrientation(AntMap.O_RIGHT);
168:                                }
169:                            }
170:                            break;
171:                        case AntMap.O_RIGHT:
172:                            if (y >= 1) {
173:                                if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
174:                                    found = true;
175:                                    map.setPosY(y - 1);
176:                                    map.setOrientation(AntMap.O_UP);
177:                                }
178:                            }
179:                            break;
180:                        }
181:                    }
182:                }
183:                if (found) {
184:                    map.IncrementMoveCounter();
185:                }
186:            }
187:
188:            public String toString() {
189:                return "turn-to-food";
190:            }
191:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.