001 package org.jaga.hooks;
002
003
004 import org.jaga.util.FittestIndividualResult;
005 import org.jaga.masterAlgorithm.SimpleGA;
006 import org.jaga.definitions.*;
007
008
009
010 /**
011 * TODO: Complete these comments.
012 *
013 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
014 *
015 * <p><u>Company:</u> University College London and JAGA.Org
016 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
017 * </p>
018 *
019 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
020 * This program is free software; you can redistribute it and/or modify
021 * it under the terms of the GNU General Public License as published by
022 * the Free Software Foundation, ONLY if you include a note of the original
023 * author(s) in any redistributed/modified copy.<br/>
024 * This program is distributed in the hope that it will be useful,
025 * but WITHOUT ANY WARRANTY; without even the implied warranty of
026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
027 * GNU General Public License for more details.<br/>
028 * You should have received a copy of the GNU General Public License
029 * along with this program; if not, write to the Free Software
030 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031 * or see http://www.gnu.org/licenses/gpl.html</p>
032 *
033 * @author Greg Paperin (greg@jaga.org)
034 *
035 * @version JAGA public release 1.0 beta
036 */
037
038 public class DebugHook extends SimpleGAHook {
039
040 public DebugHook() {
041 }
042
043 private void printPopulation(Population pop, int age) {
044 System.out.println("Population in generation " + age + " has "
045 + pop.getSize() + " members:");
046 for (int i = 0; i < pop.getSize(); i++) {
047 System.out.println(" " + i + ") " + pop.getMember(i));
048 }
049 }
050
051 private void printResult(GAResult result, int age) {
052 if (!(result instanceof FittestIndividualResult)) {
053 return;
054 }
055 System.out.println("Best result (in generation " + age + "):");
056 System.out.println(((FittestIndividualResult) result).
057 getFittestIndividual());
058 }
059
060 protected void printIndividuals(Individual[] inds) {
061 for (int i = 0; i < inds.length; i++) {
062 System.out.println(" " + i + ": " + inds[i]);
063 }
064 }
065
066 public void initialisationDone(SimpleGA caller, Population pop, int age,
067 GAResult result, GAParameterSet params) {
068 System.out.println("\nINITIALISATION DONE.");
069 printPopulation(pop, age);
070 printResult(result, age);
071 System.out.println("--------------------------------------------------");
072 }
073
074 public void foundNewResult(SimpleGA caller, Population pop, int age,
075 GAResult result, GAParameterSet params) {
076 System.out.println("\nFOUND NEW RESULT.");
077 printPopulation(pop, age);
078 printResult(result, age);
079 System.out.println("--------------------------------------------------");
080 }
081
082 public void generationChanged(SimpleGA caller, Population pop, int age,
083 GAResult result, GAParameterSet paramss) {
084 System.out.println("\nNEXT GENERATION.");
085 printPopulation(pop, age);
086 printResult(result, age);
087 System.out.println("--------------------------------------------------");
088 }
089
090 public void terminationConditionApplies(SimpleGA caller, Population pop,
091 int age,
092 GAResult result,
093 GAParameterSet params) {
094 System.out.println("\nTERMINATION APPLIED.");
095 printPopulation(pop, age);
096 printResult(result, age);
097 System.out.println("--------------------------------------------------");
098 }
099
100 public void selectedForReproduction(SimpleGA caller,
101 Individual[] selectedParents,
102 Population pop, int age,
103 GAResult result,
104 GAParameterSet params) {
105 System.out.println("\nPARENTS SELECTED.");
106 printPopulation(pop, age);
107 printResult(result, age);
108 System.out.println("Parents:");
109 printIndividuals(selectedParents);
110 System.out.println("--------------------------------------------------");
111 }
112
113 public void reproduced(SimpleGA caller, Individual[] children,
114 Individual[] parents,
115 Population pop, int age, GAResult result,
116 GAParameterSet params) {
117 System.out.println("\nCHILDREN PRODUCED.");
118 printPopulation(pop, age);
119 printResult(result, age);
120 System.out.println("Parents:");
121 printIndividuals(parents);
122 System.out.println("Children:");
123 printIndividuals(children);
124 System.out.println("--------------------------------------------------");
125 }
126
127 public void fitnessCalculated(SimpleGA caller, Individual updatedIndividual,
128 Population pop, int age,
129 GAParameterSet params) {
130 System.out.println("\nFITNESS CALCULATED.");
131 System.out.println("Updated individual: " + updatedIndividual);
132 System.out.println("--------------------------------------------------");
133 }
134
135 }