From: <jen...@us...> - 2007-08-31 15:46:03
|
Revision: 122 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=122&view=rev Author: jenslehmann Date: 2007-08-31 07:21:57 -0700 (Fri, 31 Aug 2007) Log Message: ----------- fixed performance issue in genetic programming algorithm (2nd child was overwritten when performing crossover) Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/algorithms/gp/GP.java trunk/src/dl-learner/org/dllearner/algorithms/gp/Program.java Modified: trunk/src/dl-learner/org/dllearner/algorithms/gp/GP.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/gp/GP.java 2007-08-31 10:23:51 UTC (rev 121) +++ trunk/src/dl-learner/org/dllearner/algorithms/gp/GP.java 2007-08-31 14:21:57 UTC (rev 122) @@ -1,6 +1,25 @@ +/** + * Copyright (C) 2007, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + package org.dllearner.algorithms.gp; - import static org.dllearner.Config.GP.algorithmType; import static org.dllearner.Config.GP.crossoverProbability; import static org.dllearner.Config.GP.elitism; @@ -26,7 +45,6 @@ import org.dllearner.Config; import org.dllearner.LearningProblem; -import org.dllearner.Main; import org.dllearner.Score; import org.dllearner.algorithms.LearningAlgorithm; import org.dllearner.algorithms.hybridgp.Psi; @@ -34,7 +52,6 @@ import org.dllearner.dl.Top; import org.dllearner.utilities.Helper; - /** * This class implements the genetic programming (GP) algorithm and provides * methods to configure and run it. @@ -85,6 +102,8 @@ private Comparator<Program> fitnessComparator; private static Random rand = new Random(); + + private long startTime; private Score bestScore; private Concept bestConcept; @@ -105,7 +124,7 @@ this.learningProblem = learningProblem; } - public void start() { + public void start() { // falls refinement-Wahrscheinlichkeit größer 0, dann erzeuge psi psi = new Psi(learningProblem); @@ -180,6 +199,8 @@ } }; + startTime = System.nanoTime(); + // System.out.println("If Java does not allocate enough memory on your system\n" + // "use \"java -Xmx128m Start\" to start the program. This allocates\n" + // "a maximum of 128 MB of memory. 64 MB are sufficient so it is usually\n" + @@ -201,8 +222,9 @@ Program[] newIndividuals = new Program[numberOfNewIndividuals]; Program[] tmp = new Program[2]; - long startTime = System.currentTimeMillis(); + // long startTime = System.currentTimeMillis(); + int generation = 0; // MAIN LOOP @@ -259,6 +281,12 @@ newIndividuals[i] = tmp[0]; newIndividuals[i + 1] = tmp[1]; + + // Incrementing i here is a significant code change! (2007/08/31) + // This is done, because crossover uses two individuals as input and + // outputs two. Without the increment that second produced child is + // overwritten, which caused a significant loss in performance. + i++; // mutation } else if(rand >= crossoverBoundary && rand < mutationBoundary) { newIndividuals[i] = GPUtilities.mutation(learningProblem, individuals[selectedIndividuals[i]]); @@ -346,7 +374,7 @@ // fittestIndividual.optimize(); - long endTime = System.currentTimeMillis(); + long endTime = System.nanoTime(); // .currentTimeMillis(); // R�ckgabewert des Algorithmus speichern bestScore = fittestIndividual.getScore(); bestConcept = fittestIndividual.getTree(); @@ -381,7 +409,7 @@ System.out.println("generations: " + generation); System.out.println("fittest individual found after " + fittestIndividualGeneration + " generations"); - System.out.println("runtime in ms: " + (endTime - startTime)); + System.out.println("runtime in ms: " + Helper.prettyPrintNanoSeconds(endTime - startTime)); System.out.println("fitness evaluations: " + GPUtilities.fitnessEvaluations); if(Config.algorithm == Config.Algorithm.HYBRID_GP) { @@ -660,9 +688,14 @@ conceptLengthSum += p.getTree().getLength(); double conceptLengthAverage = conceptLengthSum/(double)individuals.length; System.out.println("average concept length: " + df.format(conceptLengthAverage)); - long algorithmTime = System.nanoTime() - Main.getAlgorithmStartTime(); + // long algorithmTime = System.nanoTime() - Main.getAlgorithmStartTime(); + long algorithmTime = System.nanoTime() - startTime; System.out.println("overall algorithm runtime: " + Helper.prettyPrintNanoSeconds(algorithmTime)); + // System.out.println("instance checks: " + learningProblem.getReasoningService().getNrOfInstanceChecks()); + // System.out.println("fitness evals: " + Program.fitnessEvaluations); + // System.out.println("nr. of individuals: " + individuals.length + " (" + numberOfSelectedIndividuals + " selected)"); + // für temporäre Performance-Werte // double some = 100*(psi.someTime/(double)algorithmTime); // System.out.println("some: " + df.format(some) + "%"); Modified: trunk/src/dl-learner/org/dllearner/algorithms/gp/Program.java =================================================================== --- trunk/src/dl-learner/org/dllearner/algorithms/gp/Program.java 2007-08-31 10:23:51 UTC (rev 121) +++ trunk/src/dl-learner/org/dllearner/algorithms/gp/Program.java 2007-08-31 14:21:57 UTC (rev 122) @@ -1,3 +1,23 @@ +/** + * Copyright (C) 2007, Jens Lehmann + * + * This file is part of DL-Learner. + * + * DL-Learner is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * DL-Learner is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + package org.dllearner.algorithms.gp; import org.dllearner.Score; @@ -6,25 +26,12 @@ /** * This class represents a program, i.e. an individual. * - * Um spaeter KAON2-Queries zu unterstuetzen, muesste man aus Program ein Interface - * oder eine abstrakte Klasse machen. Diese Klasse wird zu OwnProgram o.ä. und - * es wird eine zusaetzliche Klasse KAON2Program erstellt, die dann Queries an - * KAON2 stellt und im Konstruktor eine KAON2-DL entgegennimmt. Programmiert - * wird im GP-Algorithmus dann natuerlich nur gegen das Interface. Schwierig wird - * lediglich, dass KAON2 eine andere Struktur hat, also ev. die Utility-Methoden - * angepasst werden muessen. :-/ - * - * Neu: Das Ganze wurde jetzt ueber einen abstrahierten Reasoner und die Klasse - * LearningProblem erledigt. TODO: Eventuell ist etwas wie "ProblemSolution" ein - * besserer Name. Bei Program scheint unklar, warum das LearningProblem hier eine - * Rolle spielt. - * * @author Jens Lehmann * */ public class Program { - // private static int fitnessEvaluations = 0; + // public static int fitnessEvaluations = 0; private Concept hypothesis; @@ -39,7 +46,7 @@ // private LearningProblem learningProblem; private double fitness; - + /** * Create a new program. * @@ -60,7 +67,10 @@ // fitness = score.getScore() - hypothesis.getLength() * Config.percentPerLengthUnit; // => in getScore() ist jetzt schon der length penalty integriert fitness = score.getScore(); - + // fitnessEvaluations++; + + // System.out.println("new program: " + hypothesis); + /* // falls R�ckgabetyp spezifiziert ist, dann muss hier der Baum // entsprechend ver�ndert werden This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |