Source Code Cross Referenced for jaicontrol.java in  » 6.0-JDK-Modules » Java-Advanced-Imaging » jaimoves » 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 » 6.0 JDK Modules » Java Advanced Imaging » jaimoves 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * jaicontrol.java
003:         *
004:         * Created on 29 kwiecie 2007, 11:46
005:         *
006:         * Algorytm selekcji klonalnej z sztucznych systemow immunologicznych
007:         *
008:         * To change this template, choose Tools | Template Manager
009:         * and open the template in the editor.
010:         */
011:
012:        package jaimoves;
013:
014:        import com.jme.math.Vector3f;
015:        import jaimoves.actor.Actor;
016:        import jaimoves.immun.ControlAntiGen;
017:        import java.io.IOException;
018:        import java.util.ArrayList;
019:        import jaimoves.immun.AntiGen;
020:        import jaimoves.immun.AntiBody;
021:        import java.util.Arrays;
022:        import java.util.Vector;
023:
024:        /**
025:         *
026:         * @author Romek Klis
027:         *
028:         */
029:        public class jaicontrol {
030:            private ArrayList<AntiBody> memory;
031:            private ArrayList<AntiBody> clones;
032:            double maxFit;
033:            private double minFit;
034:
035:            private int bitSize;
036:            private int maxMemory;
037:            private int pattern[];
038:
039:            private int cloneMulti;
040:            private int nextAntigen;
041:
042:            boolean cycleEnd;
043:
044:            private ControlAntiGen problem; // czyli moja pozycja rzadana
045:
046:            private void showFullData(ArrayList<AntiBody> a) {
047:                for (int i = 0; i < a.size(); i++) {
048:                    //        System.out.println(a.get(i).getKod().toString());
049:                    System.out.println(a.get(i).getFitting());
050:                }
051:            }
052:
053:            private void sort(ArrayList<AntiBody> a) {
054:                AntiBody A[] = new AntiBody[a.size()];
055:                a.toArray(A);
056:                Arrays.sort(A);
057:                a.clear();
058:                for (int i = 0; i < A.length; i++)
059:                    a.add(A[i]); // koniec sortowania
060:            } // uporzadkowane od najmniejszego do najwiekszego
061:
062:            private void setMaxFit(ArrayList<AntiBody> a) {
063:                maxFit = a.get(0).getFitting(); // wybieram najgorszy
064:                //       System.out.println("MaxFit : "+ maxFit);
065:            }
066:
067:            public AntiBody getCurrentAntibody() {
068:                AntiBody toReturn = null;
069:                if (nextAntigen == memory.size() + clones.size())
070:                    nextAntigen = 0;
071:                if (nextAntigen < memory.size()) {
072:                    toReturn = memory.get(nextAntigen);
073:                } else {
074:                    toReturn = clones.get(nextAntigen - memory.size());
075:                }
076:                return toReturn;
077:            }
078:
079:            public AntiBody getNextAntibody() {
080:                AntiBody toReturn = null;
081:                if (nextAntigen == memory.size() + clones.size()) {
082:                    nextAntigen = 0;
083:                    cycleEnd = true;
084:                }
085:                if (nextAntigen < memory.size()) {
086:                    toReturn = memory.get(nextAntigen);
087:                    nextAntigen++;
088:                    cycleEnd = false;
089:                } else {
090:                    toReturn = clones.get(nextAntigen - memory.size());
091:                    nextAntigen++;
092:                    cycleEnd = false;
093:                }
094:                return toReturn;
095:            }
096:
097:            private void normalizeFit(ArrayList<AntiBody> a) {
098:                for (int i = 0; i < a.size(); i++) {
099:                    double xFit = a.get(i).getFitting();
100:                    a.get(i).setFitting(xFit / maxFit);
101:                }
102:            }
103:
104:            private void clone(ArrayList<AntiBody> a, int multi,
105:                    int constHiperMnumber) {
106:                for (int i = 0; i < a.size(); i++) {
107:                    double fitting = a.get(i).getFitting() + 0.01f; // w najlepszym wypadku 100 klonow
108:                    int liczbaKlonow = 0;
109:                    if (fitting < 1)
110:                        liczbaKlonow = -(int) java.lang.Math.log10(fitting) * 20;
111:                    else
112:                        liczbaKlonow = (int) java.lang.Math.log10(fitting) * 20;
113:
114:                    if (liczbaKlonow == 0)
115:                        liczbaKlonow = 1;
116:                    if (liczbaKlonow > 5)
117:                        liczbaKlonow = 5;
118:                    //          System.out.println("Fitting "  + fitting);
119:                    //            System.out.println("Dla przeciwciala " + i + "Tworze " + liczbaKlonow + " Klonow");
120:                    int hmc = (int) (multi * fitting);
121:                    //        System.out.println("Ilosc hipermutacji "+ hmc);
122:                    for (int k = 0; k < liczbaKlonow; k++) {
123:                        // BARDZO WAZNA LINIJKA
124:                        // tworzymy klony w zaleznosci od fittingu
125:                        AntiBody clone = a.get(i).cloneOperation();
126:                        clone.hipermutate(hmc, 2);
127:                        clones.add(clone);
128:                    }
129:                }
130:            }
131:
132:            public void algorithmKillClonesStep() {
133:                clones.clear();
134:            }
135:
136:            public void algorithmCloneStep() {
137:                clone(memory, getCloneMulti(), 2);
138:            }
139:
140:            public void algorithmSuppresionStep(int minHamming) {
141:                supresja(minHamming);
142:            }
143:
144:            private void calculate(AntiGen problem) {
145:                //        for(int i =0; i < memory.size(); i++) {
146:                //          double newFit = problem.calcDistance((AntiBody) memory.get(i), 0.1f,8); // obliczemy odleglosc
147:                //        //        double newFit = i; // obliczemy odleglosc
148:                //       memory.get(i).setFitting(newFit);                                      // pamieci od celu
149:                //     }
150:                //       showFullData(memory);
151:                //        sort(memory);              // sortuje dla danej ekspozycji problemu
152:                //        setMaxFit(memory);         // znajduje najlepsze dopasowanie
153:                //        normalizeFit(memory);      // normalizuje tak aby byla miara porownania miedzy dwoma zbiorami memory i clones
154:                //        showFullData(memory);
155:                clone(memory, getCloneMulti(), 2); // wykonuje klony w zaleznosci od tego jak dobrze dana pamiec pasuje
156:                //        for(int i =0; i < clones.size(); i++) {
157:                //            double newFit = problem.calcDistance((AntiBody) clones.get(i), 0.1f,8); // obliczemy odleglosc
158:                //            clones.get(i).setFitting(newFit);                                      // klonow od celu
159:                //        }
160:                //        normalizeFit(clones); // normalizuje miary klonow
161:                //    System.out.println("Sortowanie klonow");
162:                //        sort(clones);              // sortuje dla danej ekspozycji problemu
163:                //        int cloneIndex = clones.size()-1;
164:                //        int memoryIndex = memory.size()-1;
165:                //        double bestCloneFitting = clones.get(cloneIndex).getFitting();
166:                //        double bestMemoryFitting = memory.get(memoryIndex).getFitting();
167:                //       if(bestCloneFitting < bestMemoryFitting) {
168:                //           memory.set(memoryIndex,clones.get(cloneIndex));   // ustawiamy nowy typ w pamieci
169:                //           memory.add(clones.get(cloneIndex));
170:                //       }
171:                //}
172:
173:                //       for(int i =0; i < memory.size(); i++) {
174:                //           double newFit = problem.calcDistance((AntiBody) memory.get(i), 0.1f,8); // obliczemy odleglosc
175:                //           memory.get(i).setFitting(newFit);                                      // pamieci od celu
176:                //       }
177:                //        minFit = memory.get(memory.size() - 1).getFitting();
178:                //      System.out.println("Najmniejszy fitting po : " + minFit);
179:                //        sort(memory);              // sortuje dla danej ekspozycji problemu
180:            }
181:
182:            private void supresja(int minDiff) {
183:
184:                //        int before = memory.size();
185:                if (memory.size() > getMaxMemory()) {
186:                    int HammingMatrix[][] = new int[memory.size()][memory
187:                            .size()]; // macierz 2 wymiarowa
188:
189:                    for (int i = 0; i < memory.size(); i++) {
190:                        for (int j = 0; j < memory.size(); j++) {
191:                            int distance = memory.get(i).hammingDist(
192:                                    memory.get(j));
193:                            HammingMatrix[i][j] = distance;
194:                        }
195:                    }
196:                    for (int i = memory.size() - 1; i >= 0; i--) {
197:                        for (int j = memory.size() - 1; j >= 0; j--) {
198:                            double fittingI = memory.get(i).getFitting();
199:                            double fittingJ = memory.get(j).getFitting();
200:                            if (HammingMatrix[i][j] < minDiff
201:                                    && fittingJ > fittingI) {
202:                                memory.remove(memory.get(j));
203:                                break;
204:                            }
205:                        }
206:                        if (memory.size() == getMaxMemory())
207:                            break;
208:                    }
209:                }
210:            }
211:
212:            // w naszym przypadku funkcje do zoptymalizowania
213:
214:            public jaicontrol(int memSize, int cloneMulti, int bitSetSize,
215:                    Vector3f WantedPos) {
216:
217:                problem = new ControlAntiGen(WantedPos);
218:                memory = new ArrayList();
219:                clones = new ArrayList();
220:                setBitSize(bitSetSize);
221:                setCloneMulti(cloneMulti);
222:
223:                for (int i = 0; i < memSize; i++)
224:                    memory.add(new AntiBody(getBitSize()));
225:                setMaxMemory(memSize);
226:                nextAntigen = 0;
227:                pattern = new int[10];
228:                pattern[0] = 12;
229:                pattern[1] = 12;
230:                pattern[2] = 12;
231:                pattern[3] = 12;
232:                pattern[4] = 12;
233:                pattern[5] = 12;
234:                pattern[6] = 14;
235:                pattern[7] = 14;
236:                pattern[8] = 14;
237:                pattern[9] = 14;
238:                //        for(int i =0; i < memSize; i++)
239:            }
240:
241:            public void showControl(AntiGen problem) {
242:                double A[] = problem.parserAntiBody(memory
243:                        .get(memory.size() - 1), 0.1f, 8);
244:                System.out.println("A: " + A[0] + "B: " + A[1] + "C: " + A[2]);
245:            }
246:
247:            public void applyControl(Actor actor) {
248:                AntiBody anti = getNextAntibody();
249:                double nastawy[] = this .getProblem().patternParserAntiBody(
250:                        anti, pattern, true);//parserAntiBody();
251:                actor.SetControls(nastawy);
252:            }
253:
254:            public static void main(String[] args) {
255:                jaicontrol crt = new jaicontrol(5, 10, 128, new Vector3f(0, 0,
256:                        4)); // tworzymy system kontroli
257:                AntiGen problem = new AntiGen();
258:                crt.calculate(problem);
259:                //    for(int i =0;crt.getMinFit() > 0.0001;i++) {
260:                for (int i = 0;; i++) {
261:                    crt.calculate(problem);
262:                    crt.showControl(problem);
263:                    crt.supresja(25);
264:                    problem.showAntigen();
265:                    crt.clones.clear();
266:                    if (i == 10) {
267:                        i = 0;
268:                        problem.shuffel();
269:                    }
270:                    ;
271:                }
272:            }
273:
274:            public double getMinFit() {
275:                return minFit;
276:            }
277:
278:            public int getBitSize() {
279:                return bitSize;
280:            }
281:
282:            public void setBitSize(int bitSize) {
283:                this .bitSize = bitSize;
284:            }
285:
286:            public int getMaxMemory() {
287:                return maxMemory;
288:            }
289:
290:            public void setMaxMemory(int maxMemory) {
291:                this .maxMemory = maxMemory;
292:            }
293:
294:            public int getCloneMulti() {
295:                return cloneMulti;
296:            }
297:
298:            public void setCloneMulti(int cloneMulti) {
299:                this .cloneMulti = cloneMulti;
300:            }
301:
302:            public ControlAntiGen getProblem() {
303:                return problem;
304:            }
305:
306:            public void setProblem(ControlAntiGen problem) {
307:                this .problem = problem;
308:            }
309:
310:            public boolean isCycleEnd() {
311:                return cycleEnd;
312:            }
313:
314:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.