- Timestamp:
- 03/08/09 12:48:18 (16 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/StandardGP.cs
r1053 r1287 36 36 37 37 namespace HeuristicLab.GP.StructureIdentification { 38 public class StandardGP : ItemBase, IEditable { 39 private IntData maxGenerations = new IntData(); 40 public int MaxGenerations { 41 get { return maxGenerations.Data; } 42 set { maxGenerations.Data = value; } 43 } 44 45 private IntData tournamentSize = new IntData(); 46 public int TournamentSize { 47 get { return tournamentSize.Data; } 48 set { tournamentSize.Data = value; } 49 } 50 private DoubleData mutationRate = new DoubleData(); 51 public double MutationRate { 52 get { return mutationRate.Data; } 53 set { mutationRate.Data = value; } 54 } 55 private IntData parents = new IntData(); 56 private IntData populationSize = new IntData(); 57 public int PopulationSize { 58 get { return populationSize.Data; } 38 public class StandardGP : AlgorithmBase, IEditable { 39 40 public virtual int MaxGenerations { 41 get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; } 42 set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; } 43 } 44 45 public virtual int TournamentSize { 46 get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; } 47 set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; } 48 } 49 50 public double FullTreeShakingFactor { 51 get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; } 52 set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; } 53 } 54 55 public double OnePointShakingFactor { 56 get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; } 57 set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; } 58 } 59 60 public int MinInitialTreeSize { 61 get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; } 62 set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; } 63 } 64 65 public override int MaxTreeSize { 66 get { 67 return base.MaxTreeSize; 68 } 59 69 set { 60 populationSize.Data = value; 61 parents.Data = value * 2; 62 } 63 } 64 65 private BoolData setSeedRandomly = new BoolData(); 66 public bool SetSeedRandomly { 67 get { return setSeedRandomly.Data; } 68 set { setSeedRandomly.Data = value; } 69 } 70 71 private IntData seed = new IntData(); 72 public int Seed { 73 get { return seed.Data; } 74 set { seed.Data = value; } 75 } 76 77 public IOperator ProblemInjector { 78 get { return algorithm.SubOperators[0]; } 70 base.MaxTreeSize = value; 71 MinInitialTreeSize = value / 2; 72 } 73 } 74 75 public override int PopulationSize { 76 get { 77 return base.PopulationSize; 78 } 79 79 set { 80 value.Name = "ProblemInjector"; 81 algorithm.RemoveSubOperator(0); 82 algorithm.AddSubOperator(value, 0); 83 } 84 } 85 86 private IntData elites = new IntData(); 87 public int Elites { 88 get { return elites.Data; } 89 set { elites.Data = value; } 90 } 91 92 private int maxTreeSize = 50; 93 private int maxTreeHeight = 8; 94 private double punishmentFactor = 10.0; 95 private bool useEstimatedTargetValue = false; 96 private double fullTreeShakingFactor = 0.1; 97 private double onepointShakingFactor = 1.0; 98 private IOperator algorithm; 99 private SequentialEngine.SequentialEngine engine; 100 101 public IEngine Engine { 102 get { return engine; } 103 } 104 105 public StandardGP() { 106 PopulationSize = 100; 80 base.PopulationSize = value; 81 Parents = 2 * value; 82 } 83 } 84 85 public StandardGP() 86 : base() { 87 PopulationSize = 10000; 107 88 MaxGenerations = 100; 108 89 TournamentSize = 7; 109 90 MutationRate = 0.15; 110 91 Elites = 1; 111 engine = new SequentialEngine.SequentialEngine(); 112 CombinedOperator algo = CreateAlgorithm(); 113 engine.OperatorGraph.AddOperator(algo); 114 engine.OperatorGraph.InitialOperator = algo; 115 } 116 117 private CombinedOperator CreateAlgorithm() { 118 CombinedOperator algo = new CombinedOperator(); 119 algo.Name = "StandardGP"; 120 SequentialProcessor seq = new SequentialProcessor(); 121 EmptyOperator problemInjectorPlaceholder = new EmptyOperator(); 122 RandomInjector randomInjector = new RandomInjector(); 123 randomInjector.GetVariable("SetSeedRandomly").Value = setSeedRandomly; 124 randomInjector.GetVariable("Seed").Value = seed; 125 randomInjector.Name = "Random Injector"; 126 VariableInjector globalInjector = CreateGlobalInjector(); 127 CombinedOperator initialization = CreateInialization(); 128 initialization.Name = "Initialization"; 129 FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector(); 130 CombinedOperator mainLoop = CreateMainLoop(); 131 mainLoop.Name = "Main loop"; 132 133 ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator(); 134 treeCreator.Name = "Tree generator"; 135 treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 136 treeCreator.GetVariableInfo("MinTreeSize").Local = true; 137 treeCreator.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData(3))); 138 MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator(); 139 evaluator.GetVariableInfo("MSE").ActualName = "Quality"; 140 evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart"; 141 evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd"; 142 evaluator.Name = "Evaluator"; 143 StandardCrossOver crossover = new StandardCrossOver(); 144 crossover.Name = "Crossover"; 145 crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 146 CombinedOperator manipulator = CreateManipulator(); 147 manipulator.Name = "Manipulator"; 92 MaxTreeSize = 100; 93 MaxTreeHeight = 10; 94 FullTreeShakingFactor = 0.1; 95 OnePointShakingFactor = 1.0; 96 PunishmentFactor = 10.0; 97 UseEstimatedTargetValue = false; 98 SetSeedRandomly = true; 99 } 100 101 protected internal override IOperator CreateProblemInjector() { 102 return new ProblemInjector(); 103 } 104 105 protected internal override IOperator CreateSelector() { 148 106 TournamentSelector selector = new TournamentSelector(); 149 107 selector.Name = "Selector"; … … 152 110 selector.RemoveVariable("GroupSize"); 153 111 selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize"; 154 155 seq.AddSubOperator(problemInjectorPlaceholder); 156 seq.AddSubOperator(randomInjector); 157 seq.AddSubOperator(globalInjector); 158 seq.AddSubOperator(funLibInjector); 159 seq.AddSubOperator(initialization); 160 seq.AddSubOperator(mainLoop); 161 162 initialization.AddSubOperator(treeCreator); 163 initialization.AddSubOperator(evaluator); 164 165 mainLoop.AddSubOperator(selector); 166 mainLoop.AddSubOperator(crossover); 167 mainLoop.AddSubOperator(manipulator); 168 mainLoop.AddSubOperator(evaluator); 169 algo.OperatorGraph.AddOperator(seq); 170 algo.OperatorGraph.InitialOperator = seq; 171 this.algorithm = seq; 172 return algo; 173 } 174 175 private VariableInjector CreateGlobalInjector() { 176 VariableInjector injector = new VariableInjector(); 177 injector.Name = "Global Injector"; 178 injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0))); 179 injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", maxGenerations)); 180 injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", mutationRate)); 181 injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", populationSize)); 182 injector.AddVariable(new HeuristicLab.Core.Variable("Parents", parents)); 183 injector.AddVariable(new HeuristicLab.Core.Variable("Elites", elites)); 184 injector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", tournamentSize)); 185 injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false))); 186 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData(maxTreeHeight))); 187 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData(maxTreeSize))); 188 injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0))); 189 injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0))); 190 injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(punishmentFactor))); 191 injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData(useEstimatedTargetValue))); 192 return injector; 193 } 194 195 private CombinedOperator CreateManipulator() { 112 return selector; 113 } 114 115 protected internal override IOperator CreateGlobalInjector() { 116 VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector(); 117 globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData())); 118 globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData())); 119 globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData())); 120 globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData())); 121 globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData())); 122 return globalInjector; 123 } 124 125 protected internal override IOperator CreateCrossover() { 126 StandardCrossOver crossover = new StandardCrossOver(); 127 crossover.Name = "Crossover"; 128 crossover.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 129 return crossover; 130 } 131 132 protected internal override IOperator CreateTreeCreator() { 133 ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator(); 134 treeCreator.Name = "Tree generator"; 135 treeCreator.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 136 treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize"; 137 return treeCreator; 138 } 139 140 protected internal override IOperator CreateFunctionLibraryInjector() { 141 return new FunctionLibraryInjector(); 142 } 143 144 protected internal override IOperator CreateManipulator() { 196 145 CombinedOperator manipulator = new CombinedOperator(); 146 manipulator.Name = "Manipulator"; 197 147 StochasticMultiBranch multibranch = new StochasticMultiBranch(); 198 148 FullTreeShaker fullTreeShaker = new FullTreeShaker(); 199 149 fullTreeShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 200 fullTreeShaker.GetVariableInfo("ShakingFactor").Local = true; 201 fullTreeShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(fullTreeShakingFactor))); 150 fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor"; 202 151 203 152 OnePointShaker onepointShaker = new OnePointShaker(); 204 153 onepointShaker.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; 205 onepointShaker.GetVariableInfo("ShakingFactor").Local = true; 206 onepointShaker.AddVariable(new HeuristicLab.Core.Variable("ShakingFactor", new DoubleData(onepointShakingFactor))); 154 onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor"; 207 155 ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation(); 208 156 changeNodeTypeManipulation.GetVariableInfo("OperatorLibrary").ActualName = "FunctionLibrary"; … … 222 170 223 171 DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]); 224 for (int i=0;i<manipulators.Length;i++) {172 for (int i = 0; i < manipulators.Length; i++) { 225 173 probabilities.Data[i] = 1.0; 226 174 multibranch.AddSubOperator(manipulators[i]); … … 234 182 } 235 183 236 private CombinedOperator CreateInialization() { 237 CombinedOperator init = new CombinedOperator(); 184 protected internal override IOperator CreateBestSolutionProcessor() { 185 SequentialProcessor bestSolutionProcessor = new SequentialProcessor(); 186 MeanSquaredErrorEvaluator testMseEvaluator = new MeanSquaredErrorEvaluator(); 187 testMseEvaluator.Name = "TestMeanSquaredErrorEvaluator"; 188 testMseEvaluator.GetVariableInfo("MSE").ActualName = "TestQuality"; 189 testMseEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart"; 190 testMseEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd"; 191 MeanAbsolutePercentageErrorEvaluator trainingMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator(); 192 trainingMapeEvaluator.Name = "TrainingMapeEvaluator"; 193 trainingMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TrainingMAPE"; 194 trainingMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart"; 195 trainingMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd"; 196 MeanAbsolutePercentageErrorEvaluator validationMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator(); 197 validationMapeEvaluator.Name = "ValidationMapeEvaluator"; 198 validationMapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE"; 199 validationMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 200 validationMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 201 MeanAbsolutePercentageErrorEvaluator testMapeEvaluator = new MeanAbsolutePercentageErrorEvaluator(); 202 testMapeEvaluator.Name = "TestMapeEvaluator"; 203 testMapeEvaluator.GetVariableInfo("MAPE").ActualName = "TestMAPE"; 204 testMapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart"; 205 testMapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd"; 206 MeanAbsolutePercentageOfRangeErrorEvaluator trainingMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator(); 207 trainingMapreEvaluator.Name = "TrainingMapreEvaluator"; 208 trainingMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TrainingMAPRE"; 209 trainingMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart"; 210 trainingMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd"; 211 MeanAbsolutePercentageOfRangeErrorEvaluator validationMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator(); 212 validationMapreEvaluator.Name = "ValidationMapreEvaluator"; 213 validationMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "ValidationMAPRE"; 214 validationMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 215 validationMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 216 MeanAbsolutePercentageOfRangeErrorEvaluator testMapreEvaluator = new MeanAbsolutePercentageOfRangeErrorEvaluator(); 217 testMapreEvaluator.Name = "TestMapreEvaluator"; 218 testMapreEvaluator.GetVariableInfo("MAPRE").ActualName = "TestMAPRE"; 219 testMapreEvaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart"; 220 testMapreEvaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd"; 221 CoefficientOfDeterminationEvaluator trainingR2Evaluator = new CoefficientOfDeterminationEvaluator(); 222 trainingR2Evaluator.Name = "TrainingR2Evaluator"; 223 trainingR2Evaluator.GetVariableInfo("R2").ActualName = "TrainingR2"; 224 trainingR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TrainingSamplesStart"; 225 trainingR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TrainingSamplesEnd"; 226 CoefficientOfDeterminationEvaluator validationR2Evaluator = new CoefficientOfDeterminationEvaluator(); 227 validationR2Evaluator.Name = "ValidationR2Evaluator"; 228 validationR2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2"; 229 validationR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 230 validationR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 231 CoefficientOfDeterminationEvaluator testR2Evaluator = new CoefficientOfDeterminationEvaluator(); 232 testR2Evaluator.Name = "TestR2Evaluator"; 233 testR2Evaluator.GetVariableInfo("R2").ActualName = "TestR2"; 234 testR2Evaluator.GetVariableInfo("SamplesStart").ActualName = "TestSamplesStart"; 235 testR2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "TestSamplesEnd"; 236 ProgrammableOperator progOperator = new ProgrammableOperator(); 237 progOperator.RemoveVariableInfo("Result"); 238 progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In)); 239 progOperator.Code = @" 240 int evalSolutions = EvaluatedSolutions.Data; 241 scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions))); 242 "; 243 bestSolutionProcessor.AddSubOperator(testMseEvaluator); 244 bestSolutionProcessor.AddSubOperator(trainingMapeEvaluator); 245 bestSolutionProcessor.AddSubOperator(validationMapeEvaluator); 246 bestSolutionProcessor.AddSubOperator(testMapeEvaluator); 247 bestSolutionProcessor.AddSubOperator(trainingMapreEvaluator); 248 bestSolutionProcessor.AddSubOperator(validationMapreEvaluator); 249 bestSolutionProcessor.AddSubOperator(testMapreEvaluator); 250 bestSolutionProcessor.AddSubOperator(trainingR2Evaluator); 251 bestSolutionProcessor.AddSubOperator(validationR2Evaluator); 252 bestSolutionProcessor.AddSubOperator(testR2Evaluator); 253 bestSolutionProcessor.AddSubOperator(progOperator); 254 return bestSolutionProcessor; 255 } 256 257 protected internal override IOperator CreateLoggingOperator() { 258 CombinedOperator loggingOperator = new CombinedOperator(); 259 loggingOperator.Name = "Logging"; 238 260 SequentialProcessor seq = new SequentialProcessor(); 239 SubScopesCreater subScopesCreater = new SubScopesCreater(); 240 subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize"; 241 UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor(); 242 SequentialProcessor individualSeq = new SequentialProcessor(); 243 OperatorExtractor treeCreater = new OperatorExtractor(); 244 treeCreater.Name = "Tree generator (extr.)"; 245 treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator"; 246 OperatorExtractor evaluator = new OperatorExtractor(); 247 evaluator.Name = "Evaluator (extr.)"; 248 evaluator.GetVariableInfo("Operator").ActualName = "Evaluator"; 249 MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator(); 250 validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality"; 251 validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 252 validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 253 Counter evalCounter = new Counter(); 254 evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions"; 255 256 seq.AddSubOperator(subScopesCreater); 257 seq.AddSubOperator(subScopesProc); 258 subScopesProc.AddSubOperator(individualSeq); 259 individualSeq.AddSubOperator(treeCreater); 260 individualSeq.AddSubOperator(evaluator); 261 individualSeq.AddSubOperator(validationEvaluator); 262 individualSeq.AddSubOperator(evalCounter); 263 264 init.OperatorGraph.AddOperator(seq); 265 init.OperatorGraph.InitialOperator = seq; 266 return init; 267 } 268 269 private CombinedOperator CreateMainLoop() { 270 CombinedOperator main = new CombinedOperator(); 271 SequentialProcessor seq = new SequentialProcessor(); 272 CombinedOperator childCreater = CreateChildCreater(); 273 childCreater.Name = "Create children"; 274 CombinedOperator replacement = CreateReplacement(); 275 replacement.Name = "Replacement"; 276 BestSolutionStorer solutionStorer = CreateBestSolutionStorer(); 277 BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator(); 278 BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator(); 279 validationQualityCalculator.Name = "ValidationQualityCalculator"; 280 validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality"; 281 validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality"; 282 validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality"; 261 283 262 DataCollector collector = new DataCollector(); 284 263 ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>(); … … 294 273 QualityLogger qualityLogger = new QualityLogger(); 295 274 QualityLogger validationQualityLogger = new QualityLogger(); 296 validationQuality Calculator.Name = "ValidationQualityLogger";275 validationQualityLogger.Name = "ValidationQualityLogger"; 297 276 validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality"; 298 277 validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog"; 299 Counter counter = new Counter(); 300 counter.GetVariableInfo("Value").ActualName = "Generations"; 301 LessThanComparator comparator = new LessThanComparator(); 302 comparator.GetVariableInfo("LeftSide").ActualName = "Generations"; 303 comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations"; 304 comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition"; 305 ConditionalBranch cond = new ConditionalBranch(); 306 cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition"; 307 308 seq.AddSubOperator(childCreater); 309 seq.AddSubOperator(replacement); 310 seq.AddSubOperator(solutionStorer); 311 seq.AddSubOperator(qualityCalculator); 312 seq.AddSubOperator(validationQualityCalculator); 278 313 279 seq.AddSubOperator(collector); 314 280 seq.AddSubOperator(lineChartInjector); 315 281 seq.AddSubOperator(qualityLogger); 316 282 seq.AddSubOperator(validationQualityLogger); 317 seq.AddSubOperator(counter); 318 seq.AddSubOperator(comparator); 319 seq.AddSubOperator(cond); 320 cond.AddSubOperator(seq); 321 322 main.OperatorGraph.AddOperator(seq); 323 main.OperatorGraph.InitialOperator = seq; 324 return main; 325 } 326 327 private BestSolutionStorer CreateBestSolutionStorer() { 328 BestSolutionStorer solutionStorer = new BestSolutionStorer(); 329 solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality"; 330 solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution"; 331 SequentialProcessor bestSolutionProcessor = new SequentialProcessor(); 332 MeanAbsolutePercentageErrorEvaluator mapeEvaluator = new MeanAbsolutePercentageErrorEvaluator(); 333 mapeEvaluator.Name = "ValidationMapeEvaluator"; 334 mapeEvaluator.GetVariableInfo("MAPE").ActualName = "ValidationMAPE"; 335 mapeEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 336 mapeEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 337 CoefficientOfDeterminationEvaluator r2Evaluator = new CoefficientOfDeterminationEvaluator(); 338 r2Evaluator.Name = "ValidationR2Evaluator"; 339 r2Evaluator.GetVariableInfo("R2").ActualName = "ValidationR2"; 340 r2Evaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 341 r2Evaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 342 ProgrammableOperator progOperator = new ProgrammableOperator(); 343 progOperator.RemoveVariableInfo("Result"); 344 progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In)); 345 progOperator.Code = @" 346 int evalSolutions = EvaluatedSolutions.Data; 347 scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions))); 348 "; 349 solutionStorer.AddSubOperator(bestSolutionProcessor); 350 bestSolutionProcessor.AddSubOperator(mapeEvaluator); 351 bestSolutionProcessor.AddSubOperator(r2Evaluator); 352 bestSolutionProcessor.AddSubOperator(progOperator); 353 return solutionStorer; 354 } 355 356 private CombinedOperator CreateReplacement() { 357 CombinedOperator replacement = new CombinedOperator(); 358 SequentialProcessor seq = new SequentialProcessor(); 359 SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor(); 360 SequentialProcessor selectedProc = new SequentialProcessor(); 361 LeftSelector leftSelector = new LeftSelector(); 362 leftSelector.GetVariableInfo("Selected").ActualName = "Elites"; 363 RightReducer rightReducer = new RightReducer(); 364 365 SequentialProcessor remainingProc = new SequentialProcessor(); 366 RightSelector rightSelector = new RightSelector(); 367 rightSelector.GetVariableInfo("Selected").ActualName = "Elites"; 368 LeftReducer leftReducer = new LeftReducer(); 369 MergingReducer mergingReducer = new MergingReducer(); 370 Sorter sorter = new Sorter(); 371 sorter.GetVariableInfo("Descending").ActualName = "Maximization"; 372 sorter.GetVariableInfo("Value").ActualName = "Quality"; 373 374 seq.AddSubOperator(seqScopeProc); 375 seqScopeProc.AddSubOperator(selectedProc); 376 selectedProc.AddSubOperator(leftSelector); 377 selectedProc.AddSubOperator(rightReducer); 378 379 seqScopeProc.AddSubOperator(remainingProc); 380 remainingProc.AddSubOperator(rightSelector); 381 remainingProc.AddSubOperator(leftReducer); 382 seq.AddSubOperator(mergingReducer); 383 seq.AddSubOperator(sorter); 384 replacement.OperatorGraph.AddOperator(seq); 385 replacement.OperatorGraph.InitialOperator = seq; 386 return replacement; 387 } 388 389 private CombinedOperator CreateChildCreater() { 390 CombinedOperator childCreater = new CombinedOperator(); 391 SequentialProcessor seq = new SequentialProcessor(); 392 OperatorExtractor selector = new OperatorExtractor(); 393 selector.Name = "Selector (extr.)"; 394 selector.GetVariableInfo("Operator").ActualName = "Selector"; 395 SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor(); 396 EmptyOperator emptyOpt = new EmptyOperator(); 397 SequentialProcessor selectedProc = new SequentialProcessor(); 398 OperatorExtractor crossover = new OperatorExtractor(); 399 crossover.Name = "Crossover (extr.)"; 400 crossover.GetVariableInfo("Operator").ActualName = "Crossover"; 401 UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor(); 402 SequentialProcessor individualSeqProc = new SequentialProcessor(); 403 StochasticBranch cond = new StochasticBranch(); 404 cond.GetVariableInfo("Probability").ActualName = "MutationRate"; 405 OperatorExtractor manipulator = new OperatorExtractor(); 406 manipulator.Name = "Manipulator (extr.)"; 407 manipulator.GetVariableInfo("Operator").ActualName = "Manipulator"; 408 OperatorExtractor evaluator = new OperatorExtractor(); 409 evaluator.Name = "Evaluator (extr.)"; 410 evaluator.GetVariableInfo("Operator").ActualName = "Evaluator"; 411 MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator(); 412 validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality"; 413 validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart"; 414 validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd"; 415 Counter evalCounter = new Counter(); 416 evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions"; 417 418 Sorter sorter = new Sorter(); 419 sorter.GetVariableInfo("Descending").ActualName = "Maximization"; 420 sorter.GetVariableInfo("Value").ActualName = "Quality"; 421 422 423 seq.AddSubOperator(selector); 424 seq.AddSubOperator(seqScopesProc); 425 seqScopesProc.AddSubOperator(emptyOpt); 426 seqScopesProc.AddSubOperator(selectedProc); 427 selectedProc.AddSubOperator(crossover); 428 selectedProc.AddSubOperator(individualProc); 429 individualProc.AddSubOperator(individualSeqProc); 430 individualSeqProc.AddSubOperator(cond); 431 cond.AddSubOperator(manipulator); 432 individualSeqProc.AddSubOperator(evaluator); 433 individualSeqProc.AddSubOperator(validationEvaluator); 434 individualSeqProc.AddSubOperator(evalCounter); 435 selectedProc.AddSubOperator(sorter); 436 437 childCreater.OperatorGraph.AddOperator(seq); 438 childCreater.OperatorGraph.InitialOperator = seq; 439 return childCreater; 440 } 441 442 public IEditor CreateEditor() { 283 284 loggingOperator.OperatorGraph.AddOperator(seq); 285 loggingOperator.OperatorGraph.InitialOperator = seq; 286 return loggingOperator; 287 } 288 289 public virtual IEditor CreateEditor() { 443 290 return new StandardGpEditor(this); 444 291 } … … 447 294 return new StandardGpEditor(this); 448 295 } 449 450 public override object Clone(IDictionary<Guid, object> clonedObjects) {451 StandardGP clone = new StandardGP();452 clonedObjects.Add(Guid, clone);453 clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);454 return clone;455 }456 #region SetReferences Method457 private void SetReferences() {458 // SGA459 CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;460 // SequentialProcessor in SGA461 algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;462 // RandomInjector463 RandomInjector ri = (RandomInjector)algorithm.SubOperators[1];464 setSeedRandomly = ri.GetVariable("SetSeedRandomly").GetValue<BoolData>();465 seed = ri.GetVariable("Seed").GetValue<IntData>();466 // VariableInjector467 VariableInjector vi = (VariableInjector)algorithm.SubOperators[2];468 populationSize = vi.GetVariable("PopulationSize").GetValue<IntData>();469 parents = vi.GetVariable("Parents").GetValue<IntData>();470 maxGenerations = vi.GetVariable("MaxGenerations").GetValue<IntData>();471 mutationRate = vi.GetVariable("MutationRate").GetValue<DoubleData>();472 elites = vi.GetVariable("Elites").GetValue<IntData>();473 }474 #endregion475 476 #region Persistence Methods477 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {478 XmlNode node = base.GetXmlNode(name, document, persistedObjects);479 node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));480 return node;481 }482 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {483 base.Populate(node, restoredObjects);484 engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);485 SetReferences();486 }487 #endregion488 296 } 489 297 }
Note: See TracChangeset
for help on using the changeset viewer.