- Timestamp:
- 09/04/09 16:23:45 (15 years ago)
- Location:
- branches/gp-algorithms-refactoring-#720/sources/HeuristicLab.GP.Algorithms/3.2
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gp-algorithms-refactoring-#720/sources/HeuristicLab.GP.Algorithms/3.2/AlgorithmBase.cs
r2331 r2335 46 46 } 47 47 48 public virtual int MaxGenerations { 49 get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; } 50 set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; } 51 } 52 53 public virtual int Elites { 54 get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; } 55 set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; } 56 } 57 58 public virtual int MaxTreeSize { 59 get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; } 60 set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; } 61 } 62 63 public virtual int MinTreeSize { 64 get { return GetVariableInjector().GetVariable("MinTreeSize").GetValue<IntData>().Data; } 65 set { GetVariableInjector().GetVariable("MinTreeSize").GetValue<IntData>().Data = value; } 66 } 67 68 public virtual int MaxTreeHeight { 69 get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; } 70 set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; } 71 } 72 73 public virtual int Parents { 74 get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; } 75 set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; } 76 } 77 48 78 public virtual bool SetSeedRandomly { 49 79 get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; } … … 57 87 58 88 public virtual IOperator ProblemInjector { 59 get { return algorithm.SubOperators[1]; }89 get { return GetInitializationOperator().SubOperators[0]; } 60 90 set { 61 91 value.Name = "ProblemInjector"; 62 algorithm.RemoveSubOperator(1); 63 algorithm.AddSubOperator(value, 1); 92 IOperator init = GetInitializationOperator(); 93 init.RemoveSubOperator(0); 94 init.AddSubOperator(value, 0); 64 95 } 65 96 } 66 97 67 public virtual int Elites { 68 get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; } 69 set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; } 70 } 71 72 public virtual int MaxTreeSize { 73 get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; } 74 set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; } 75 } 76 77 public virtual int MaxTreeHeight { 78 get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; } 79 set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; } 80 } 81 82 public virtual int Parents { 83 get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; } 84 set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; } 98 public virtual IOperator FunctionLibraryInjector { 99 get { return GetInitializationOperator().SubOperators[1]; } 100 set { 101 value.Name = "FunctionLibraryInjector"; 102 IOperator init = GetInitializationOperator(); 103 init.RemoveSubOperator(1); 104 init.AddSubOperator(value, 1); 105 } 85 106 } 86 107 … … 102 123 MutationRate = 0.15; 103 124 PopulationSize = 1000; 125 MaxGenerations = 100; 104 126 MaxTreeSize = 100; 127 MinTreeSize = 1; 105 128 MaxTreeHeight = 10; 106 129 Parents = 2000; … … 111 134 algo.Name = "GP"; 112 135 SequentialProcessor seq = new SequentialProcessor(); 113 IOperator problemInjector = CreateProblemInjector(); 114 115 RandomInjector randomInjector = new RandomInjector(); 116 randomInjector.Name = "Random Injector"; 117 118 IOperator globalInjector = CreateGlobalInjector(); 119 IOperator initialization = CreateInitialization(); 120 IOperator funLibInjector = CreateFunctionLibraryInjector(); 136 IOperator init = CreateInitializationOperator(); 137 init.AddSubOperator(CreateProblemInjector()); 138 init.AddSubOperator(CreateFunctionLibraryInjector()); 139 seq.AddSubOperator(init); 140 141 IOperator initPopulation = CreateInitialPopulationGenerator(); 142 initPopulation.AddSubOperator(CreateRandomSolutionGenerator()); 143 initPopulation.AddSubOperator(CreateInitialPopulationEvaluator()); 144 seq.AddSubOperator(initPopulation); 121 145 122 146 IOperator mainLoop = CreateMainLoop(); 123 mainLoop.Name = "Main loop"; 124 125 IOperator treeCreator = CreateTreeCreator(); 126 127 IOperator crossover = CreateCrossover(); 128 IOperator manipulator = CreateManipulator(); 129 130 IOperator selector = CreateSelector(); 131 LeftReducer cleanUp = new LeftReducer(); 132 133 seq.AddSubOperator(randomInjector); 134 seq.AddSubOperator(problemInjector); 135 seq.AddSubOperator(globalInjector); 136 seq.AddSubOperator(funLibInjector); 137 seq.AddSubOperator(initialization); 147 mainLoop.AddSubOperator(CreateSelectionOperator()); 148 mainLoop.AddSubOperator(CreateCrossoverOperator()); 149 mainLoop.AddSubOperator(CreateManipulationOperator()); 150 mainLoop.AddSubOperator(CreateEvaluationOperator()); 151 mainLoop.AddSubOperator(CreateTerminationCondition()); 138 152 seq.AddSubOperator(mainLoop); 139 seq.AddSubOperator(cleanUp); 140 141 initialization.AddSubOperator(treeCreator); 142 143 mainLoop.AddSubOperator(selector); 144 mainLoop.AddSubOperator(crossover); 145 mainLoop.AddSubOperator(manipulator); 146 mainLoop.AddSubOperator(evaluator); 153 154 IOperator postProcess = CreatePostProcessingOperator(); 155 seq.AddSubOperator(postProcess); 156 147 157 algo.OperatorGraph.AddOperator(seq); 148 158 algo.OperatorGraph.InitialOperator = seq; … … 151 161 } 152 162 153 protected internal virtual IOperator CreateProblemInjector() { 154 return new EmptyOperator(); 155 } 156 157 protected virtual IOperator CreateSelector() { 158 return new EmptyOperator(); 159 } 160 161 protected virtual IOperator CreateCrossover() { 162 return new StandardCrossOver(); 163 } 164 165 protected internal abstract IOperator CreateTreeCreator(); 166 167 protected internal abstract IOperator CreateFunctionLibraryInjector(); 168 169 protected internal virtual IOperator CreateGlobalInjector() { 163 #region global init 164 protected virtual IOperator CreateInitializationOperator() { 165 CombinedOperator init = new CombinedOperator(); 166 init.Name = "Initialization"; 167 SequentialProcessor seq = new SequentialProcessor(); 168 seq.AddSubOperator(CreateRandomInjector()); 169 seq.AddSubOperator(CreateGlobalInjector()); 170 171 OperatorExtractor probInjectorExtractor = new OperatorExtractor(); 172 probInjectorExtractor.Name = "ProblemInjector"; 173 probInjectorExtractor.GetVariableInfo("Operator").ActualName = "ProblemInjector"; 174 seq.AddSubOperator(probInjectorExtractor); 175 176 OperatorExtractor funLibInjectorExtractor = new OperatorExtractor(); 177 funLibInjectorExtractor.Name = "FunctionLibraryInjector"; 178 funLibInjectorExtractor.GetVariableInfo("Operator").ActualName = "FunctionLibraryInjector"; 179 seq.AddSubOperator(funLibInjectorExtractor); 180 181 init.OperatorGraph.AddOperator(seq); 182 init.OperatorGraph.InitialOperator = seq; 183 return init; 184 } 185 186 protected virtual IOperator CreateRandomInjector() { 187 RandomInjector randomInjector = new RandomInjector(); 188 randomInjector.Name = "Random Injector"; 189 return randomInjector; 190 } 191 192 protected virtual VariableInjector CreateGlobalInjector() { 170 193 VariableInjector injector = new VariableInjector(); 171 194 injector.Name = "Global Injector"; 172 195 injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0))); 196 injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData())); 173 197 injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData())); 174 198 injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData())); … … 177 201 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData())); 178 202 injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData())); 203 injector.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData())); 179 204 injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0))); 180 205 injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0))); 181 206 injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData())); 182 injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));183 injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(10.0)));184 207 return injector; 185 208 } 186 209 187 protected internal abstract IOperator CreateManipulator(); 188 189 protected internal virtual IOperator CreateInitialization() { 190 CombinedOperator init = new CombinedOperator(); 191 init.Name = "Initialization"; 210 211 protected virtual IOperator CreateProblemInjector() { 212 return new EmptyOperator(); 213 } 214 215 protected virtual IOperator CreateFunctionLibraryInjector() { 216 return new EmptyOperator(); 217 } 218 #endregion 219 220 #region population init 221 private IOperator CreateInitialPopulationGenerator() { 222 CombinedOperator initPopulation = new CombinedOperator(); 223 initPopulation.Name = "Init population"; 192 224 SequentialProcessor seq = new SequentialProcessor(); 193 225 SubScopesCreater subScopesCreater = new SubScopesCreater(); … … 197 229 OperatorExtractor treeCreater = new OperatorExtractor(); 198 230 treeCreater.Name = "Tree generator (extr.)"; 199 treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator"; 231 treeCreater.GetVariableInfo("Operator").ActualName = "Solution generator"; 232 200 233 OperatorExtractor evaluator = new OperatorExtractor(); 201 234 evaluator.Name = "Evaluator (extr.)"; 202 235 evaluator.GetVariableInfo("Operator").ActualName = "Evaluator"; 203 MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();204 validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";205 validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";206 validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";207 236 Counter evalCounter = new Counter(); 208 237 evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions"; … … 218 247 individualSeq.AddSubOperator(treeCreater); 219 248 individualSeq.AddSubOperator(evaluator); 220 individualSeq.AddSubOperator(validationEvaluator);221 249 individualSeq.AddSubOperator(evalCounter); 222 250 223 init.OperatorGraph.AddOperator(seq); 224 init.OperatorGraph.InitialOperator = seq; 225 return init; 226 } 227 228 protected internal virtual IOperator CreateMainLoop() { 251 initPopulation.OperatorGraph.AddOperator(seq); 252 initPopulation.OperatorGraph.InitialOperator = seq; 253 return initPopulation; 254 } 255 256 protected virtual IOperator CreateRandomSolutionGenerator() { 257 ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator(); 258 treeCreator.Name = "Solution generator"; 259 return treeCreator; 260 } 261 262 protected virtual IOperator CreateInitialPopulationEvaluator() { 263 return new EmptyOperator(); 264 } 265 #endregion 266 267 #region mainloop 268 protected virtual IOperator CreateMainLoop() { 229 269 CombinedOperator main = new CombinedOperator(); 270 main.Name = "Main"; 230 271 SequentialProcessor seq = new SequentialProcessor(); 231 272 IOperator childCreater = CreateChildCreater(); 232 273 IOperator replacement = CreateReplacement(); 233 274 234 BestSolutionStorer solutionStorer = new BestSolutionStorer();235 solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";236 solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";237 solutionStorer.AddSubOperator(CreateBestSolutionProcessor());238 239 275 BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator(); 240 BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();241 validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";242 validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";243 validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";244 validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";245 validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";246 276 IOperator loggingOperator = CreateLoggingOperator(); 247 277 Counter counter = new Counter(); 248 278 counter.GetVariableInfo("Value").ActualName = "Generations"; 249 IOperator loopCondition = CreateLoopCondition(seq); 279 280 OperatorExtractor terminationCriterionExtractor = new OperatorExtractor(); 281 terminationCriterionExtractor.Name = "TerminationCondition (extr.)"; 282 terminationCriterionExtractor.GetVariableInfo("Operator").ActualName = "TerminationCondition"; 283 284 ConditionalBranch loop = new ConditionalBranch(); 285 loop.Name = "Main loop"; 286 loop.GetVariableInfo("Condition").ActualName = "TerminationCriterion"; 287 loop.AddSubOperator(new EmptyOperator()); 288 loop.AddSubOperator(seq); 250 289 251 290 seq.AddSubOperator(childCreater); 252 291 seq.AddSubOperator(replacement); 253 seq.AddSubOperator(solutionStorer);254 292 seq.AddSubOperator(qualityCalculator); 255 seq.AddSubOperator( validationQualityCalculator);293 seq.AddSubOperator(CreateGenerationStepHook()); 256 294 seq.AddSubOperator(loggingOperator); 257 295 seq.AddSubOperator(counter); 258 seq.AddSubOperator(loopCondition); 296 seq.AddSubOperator(terminationCriterionExtractor); 297 seq.AddSubOperator(loop); 259 298 260 299 main.OperatorGraph.AddOperator(seq); … … 263 302 } 264 303 265 protected internal virtual IOperator CreateLoggingOperator() { 266 return new EmptyOperator(); 267 } 268 269 protected internal virtual IOperator CreateLoopCondition(IOperator loop) { 270 SequentialProcessor seq = new SequentialProcessor(); 271 seq.Name = "Loop Condition"; 272 LessThanComparator comparator = new LessThanComparator(); 273 comparator.GetVariableInfo("LeftSide").ActualName = "Generations"; 274 comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations"; 275 comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition"; 276 ConditionalBranch cond = new ConditionalBranch(); 277 cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition"; 278 279 seq.AddSubOperator(comparator); 280 seq.AddSubOperator(cond); 281 282 cond.AddSubOperator(loop); 283 return seq; 284 } 285 286 protected internal virtual IOperator CreateBestSolutionProcessor() { 287 SequentialProcessor seq = new SequentialProcessor(); 288 ProgrammableOperator progOperator = new ProgrammableOperator(); 289 progOperator.RemoveVariableInfo("Result"); 290 progOperator.AddVariableInfo(new HeuristicLab.Core.VariableInfo("EvaluatedSolutions", "", typeof(IntData), VariableKind.In)); 291 progOperator.Code = @" 292 int evalSolutions = EvaluatedSolutions.Data; 293 scope.AddVariable(new Variable(""EvaluatedSolutions"", new IntData(evalSolutions))); 294 "; 295 seq.AddSubOperator(progOperator); 296 return seq; 297 } 298 299 protected internal virtual IOperator CreateReplacement() { 300 CombinedOperator replacement = new CombinedOperator(); 301 replacement.Name = "Replacement"; 302 SequentialProcessor seq = new SequentialProcessor(); 303 SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor(); 304 SequentialProcessor selectedProc = new SequentialProcessor(); 305 LeftSelector leftSelector = new LeftSelector(); 306 leftSelector.GetVariableInfo("Selected").ActualName = "Elites"; 307 RightReducer rightReducer = new RightReducer(); 308 309 SequentialProcessor remainingProc = new SequentialProcessor(); 310 RightSelector rightSelector = new RightSelector(); 311 rightSelector.GetVariableInfo("Selected").ActualName = "Elites"; 312 LeftReducer leftReducer = new LeftReducer(); 313 MergingReducer mergingReducer = new MergingReducer(); 314 Sorter sorter = new Sorter(); 315 sorter.GetVariableInfo("Descending").ActualName = "Maximization"; 316 sorter.GetVariableInfo("Value").ActualName = "Quality"; 317 318 seq.AddSubOperator(seqScopeProc); 319 seqScopeProc.AddSubOperator(selectedProc); 320 selectedProc.AddSubOperator(leftSelector); 321 selectedProc.AddSubOperator(rightReducer); 322 323 seqScopeProc.AddSubOperator(remainingProc); 324 remainingProc.AddSubOperator(rightSelector); 325 remainingProc.AddSubOperator(leftReducer); 326 seq.AddSubOperator(mergingReducer); 327 seq.AddSubOperator(sorter); 328 replacement.OperatorGraph.AddOperator(seq); 329 replacement.OperatorGraph.InitialOperator = seq; 330 return replacement; 331 } 332 333 protected internal virtual IOperator CreateChildCreater() { 304 protected virtual IOperator CreateChildCreater() { 334 305 CombinedOperator childCreater = new CombinedOperator(); 335 306 childCreater.Name = "Create children"; … … 358 329 evaluator.Name = "Evaluator (extr.)"; 359 330 evaluator.GetVariableInfo("Operator").ActualName = "Evaluator"; 360 MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();361 validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";362 validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";363 validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";364 331 Counter evalCounter = new Counter(); 365 332 evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions"; … … 382 349 cond.AddSubOperator(manipulator); 383 350 individualSeqProc.AddSubOperator(evaluator); 384 individualSeqProc.AddSubOperator(validationEvaluator);385 351 individualSeqProc.AddSubOperator(evalCounter); 386 352 individualSeqProc.AddSubOperator(parentRefRemover); … … 392 358 } 393 359 394 protected internal virtual IAnalyzerModel CreateGPModel(IScope bestModelScope) { 395 IAnalyzerModel model = new AnalyzerModel(); 396 model.Predictor = bestModelScope.GetVariableValue<IPredictor>("Predictor", true); 397 Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true); 398 model.Dataset = ds; 399 model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data); 400 model.TrainingSamplesStart = bestModelScope.GetVariableValue<IntData>("TrainingSamplesStart", true).Data; 401 model.TrainingSamplesEnd = bestModelScope.GetVariableValue<IntData>("TrainingSamplesEnd", true).Data; 402 model.ValidationSamplesStart = bestModelScope.GetVariableValue<IntData>("ValidationSamplesStart", true).Data; 403 model.ValidationSamplesEnd = bestModelScope.GetVariableValue<IntData>("ValidationSamplesEnd", true).Data; 404 model.TestSamplesStart = bestModelScope.GetVariableValue<IntData>("TestSamplesStart", true).Data; 405 model.TestSamplesEnd = bestModelScope.GetVariableValue<IntData>("TestSamplesEnd", true).Data; 406 407 model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TrainingMSE", false).Data; 408 model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationMSE", false).Data; 409 model.TestMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("TestMSE", false).Data; 410 model.TrainingCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TrainingR2", false).Data; 411 model.ValidationCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("ValidationR2", false).Data; 412 model.TestCoefficientOfDetermination = bestModelScope.GetVariableValue<DoubleData>("TestR2", false).Data; 413 model.TrainingMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPE", false).Data; 414 model.ValidationMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPE", false).Data; 415 model.TestMeanAbsolutePercentageError = bestModelScope.GetVariableValue<DoubleData>("TestMAPE", false).Data; 416 model.TrainingMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TrainingMAPRE", false).Data; 417 model.ValidationMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("ValidationMAPRE", false).Data; 418 model.TestMeanAbsolutePercentageOfRangeError = bestModelScope.GetVariableValue<DoubleData>("TestMAPRE", false).Data; 419 model.TrainingVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TrainingVAF", false).Data; 420 model.ValidationVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("ValidationVAF", false).Data; 421 model.TestVarianceAccountedFor = bestModelScope.GetVariableValue<DoubleData>("TestVAF", false).Data; 422 423 ItemList evaluationImpacts = bestModelScope.GetVariableValue<ItemList>("VariableEvaluationImpacts", false); 424 ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>("VariableQualityImpacts", false); 425 foreach (ItemList row in evaluationImpacts) { 426 string variableName = ((StringData)row[0]).Data; 427 double impact = ((DoubleData)row[1]).Data; 428 model.SetVariableEvaluationImpact(variableName, impact); 429 model.AddInputVariable(variableName); 430 } 431 foreach (ItemList row in qualityImpacts) { 432 string variableName = ((StringData)row[0]).Data; 433 double impact = ((DoubleData)row[1]).Data; 434 model.SetVariableQualityImpact(variableName, impact); 435 model.AddInputVariable(variableName); 436 } 437 438 return model; 439 } 360 protected virtual IOperator CreateReplacement() { 361 CombinedOperator replacement = new CombinedOperator(); 362 replacement.Name = "Replacement"; 363 SequentialProcessor seq = new SequentialProcessor(); 364 SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor(); 365 SequentialProcessor selectedProc = new SequentialProcessor(); 366 LeftSelector leftSelector = new LeftSelector(); 367 leftSelector.GetVariableInfo("Selected").ActualName = "Elites"; 368 RightReducer rightReducer = new RightReducer(); 369 370 SequentialProcessor remainingProc = new SequentialProcessor(); 371 RightSelector rightSelector = new RightSelector(); 372 rightSelector.GetVariableInfo("Selected").ActualName = "Elites"; 373 LeftReducer leftReducer = new LeftReducer(); 374 MergingReducer mergingReducer = new MergingReducer(); 375 Sorter sorter = new Sorter(); 376 sorter.GetVariableInfo("Descending").ActualName = "Maximization"; 377 sorter.GetVariableInfo("Value").ActualName = "Quality"; 378 379 seq.AddSubOperator(seqScopeProc); 380 seqScopeProc.AddSubOperator(selectedProc); 381 selectedProc.AddSubOperator(leftSelector); 382 selectedProc.AddSubOperator(rightReducer); 383 384 seqScopeProc.AddSubOperator(remainingProc); 385 remainingProc.AddSubOperator(rightSelector); 386 remainingProc.AddSubOperator(leftReducer); 387 seq.AddSubOperator(mergingReducer); 388 seq.AddSubOperator(sorter); 389 replacement.OperatorGraph.AddOperator(seq); 390 replacement.OperatorGraph.InitialOperator = seq; 391 return replacement; 392 } 393 394 protected virtual IOperator CreateLoggingOperator() { 395 CombinedOperator loggingOperator = new CombinedOperator(); 396 loggingOperator.Name = "Logging"; 397 SequentialProcessor seq = new SequentialProcessor(); 398 399 DataCollector collector = new DataCollector(); 400 ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>(); 401 names.Add(new StringData("BestQuality")); 402 names.Add(new StringData("AverageQuality")); 403 names.Add(new StringData("WorstQuality")); 404 LinechartInjector lineChartInjector = new LinechartInjector(); 405 lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart"; 406 lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 3; 407 QualityLogger qualityLogger = new QualityLogger(); 408 409 seq.AddSubOperator(collector); 410 seq.AddSubOperator(lineChartInjector); 411 seq.AddSubOperator(qualityLogger); 412 413 loggingOperator.OperatorGraph.AddOperator(seq); 414 loggingOperator.OperatorGraph.InitialOperator = seq; 415 return loggingOperator; 416 } 417 418 protected virtual IOperator CreateGenerationStepHook() { 419 return new EmptyOperator(); 420 } 421 422 protected virtual IOperator CreateTerminationCondition() { 423 CombinedOperator terminationCriterion = new CombinedOperator(); 424 terminationCriterion.Name = "TerminationCondition"; 425 SequentialProcessor seq = new SequentialProcessor(); 426 GreaterThanComparator comparator = new GreaterThanComparator(); 427 comparator.GetVariableInfo("LeftSide").ActualName = "Generations"; 428 comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations"; 429 comparator.GetVariableInfo("Result").ActualName = "TerminationCriterion"; 430 431 seq.AddSubOperator(comparator); 432 terminationCriterion.OperatorGraph.AddOperator(seq); 433 terminationCriterion.OperatorGraph.InitialOperator = seq; 434 return terminationCriterion; 435 } 436 437 protected virtual IOperator CreateEvaluationOperator() { 438 return new EmptyOperator(); 439 } 440 441 protected virtual IOperator CreateManipulationOperator() { 442 ChangeNodeTypeManipulation manipulator = new ChangeNodeTypeManipulation(); 443 manipulator.Name = "Manipulator"; 444 return manipulator; 445 } 446 447 protected virtual IOperator CreateCrossoverOperator() { 448 StandardCrossOver crossover = new StandardCrossOver(); 449 crossover.Name = "Crossover"; 450 return crossover; 451 } 452 453 protected virtual IOperator CreateSelectionOperator() { 454 TournamentSelector selector = new TournamentSelector(); 455 selector.GetVariableInfo("Selected").ActualName = "Parents"; 456 selector.GetVariable("GroupSize").Value = new IntData(7); 457 selector.Name = "Selector"; 458 return selector; 459 } 460 461 #endregion 462 463 protected virtual IOperator CreatePostProcessingOperator() { 464 return new EmptyOperator(); 465 } 466 440 467 441 468 public override object Clone(IDictionary<Guid, object> clonedObjects) { … … 446 473 } 447 474 448 protected VariableInjector GetVariableInjector() {449 CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;450 // SequentialProcessor in GP451 algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;452 return (VariableInjector)algorithm.SubOperators[2]; 453 }454 455 protected RandomInjector GetRandomInjector() {456 CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;457 // SequentialProcessor in GP 458 algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;459 return (RandomInjector)algorithm.SubOperators[0];475 protected virtual IOperator GetVariableInjector() { 476 CombinedOperator init = (CombinedOperator)GetInitializationOperator(); 477 return init.OperatorGraph.InitialOperator.SubOperators[1]; 478 } 479 480 protected virtual IOperator GetRandomInjector() { 481 CombinedOperator init = (CombinedOperator)GetInitializationOperator(); 482 return init.OperatorGraph.InitialOperator.SubOperators[0]; 483 } 484 485 protected virtual IOperator GetInitializationOperator() { 486 return algorithm.SubOperators[0]; 460 487 } 461 488 -
branches/gp-algorithms-refactoring-#720/sources/HeuristicLab.GP.Algorithms/3.2/HeuristicLab.GP.Algorithms-3.2.csproj
r2331 r2335 70 70 <RequiredTargetFramework>3.5</RequiredTargetFramework> 71 71 </Reference> 72 <Reference Include="System.Drawing" /> 72 73 <Reference Include="System.Windows.Forms" /> 73 74 <Reference Include="System.Xml.Linq"> … … 83 84 <Compile Include="AlgorithmBase.cs" /> 84 85 <Compile Include="HeuristicLabGpAlgorithmsPlugin.cs" /> 85 <Compile Include="OffspringSelectionGP.cs" /> 86 <Compile Include="OffspringSelectionGP.cs"> 87 <SubType>Code</SubType> 88 </Compile> 86 89 <Compile Include="OffSpringSelectionGpEditor.cs"> 87 90 <SubType>UserControl</SubType> -
branches/gp-algorithms-refactoring-#720/sources/HeuristicLab.GP.Algorithms/3.2/OffspringSelectionGP.cs
r2331 r2335 31 31 32 32 namespace HeuristicLab.GP.Algorithms { 33 public class OffspringSelectionGP : StandardGP{33 public class OffspringSelectionGP : AlgorithmBase, IEditable { 34 34 public override string Name { get { return "OffspringSelectionGP"; } } 35 35 … … 52 52 get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; } 53 53 set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; } 54 }55 56 public override int MaxGenerations {57 get { throw new NotSupportedException(); }58 set { /* ignore */ }59 }60 61 public override int TournamentSize {62 get { throw new NotSupportedException(); }63 set { /* ignore */ }64 54 } 65 55 … … 68 58 PopulationSize = 1000; 69 59 Parents = 20; 60 MaxGenerations = 10000; // something large to make sure we either stop because of max-evaluated soltions or selection pressure limit 70 61 MaxEvaluatedSolutions = 5000000; 71 62 SelectionPressureLimit = 400; … … 74 65 } 75 66 76 protected internal override IOperator CreateGlobalInjector() { 77 VariableInjector injector = (VariableInjector)base.CreateGlobalInjector(); 78 injector.RemoveVariable("TournamentSize"); 79 injector.RemoveVariable("MaxGenerations"); 67 protected override VariableInjector CreateGlobalInjector() { 68 VariableInjector injector = base.CreateGlobalInjector(); 80 69 injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData())); 81 70 injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData())); … … 85 74 } 86 75 87 protected internal override IOperator CreateSelector() {76 protected override IOperator CreateSelectionOperator() { 88 77 CombinedOperator selector = new CombinedOperator(); 89 78 selector.Name = "Selector"; … … 115 104 } 116 105 117 protected internaloverride IOperator CreateChildCreater() {106 protected override IOperator CreateChildCreater() { 118 107 CombinedOperator childCreater = new CombinedOperator(); 119 108 childCreater.Name = "Create children"; … … 150 139 sorter.GetVariableInfo("Value").ActualName = "Quality"; 151 140 152 UniformSequentialSubScopesProcessor validationEvaluator = new UniformSequentialSubScopesProcessor();153 MeanSquaredErrorEvaluator validationQualityEvaluator = new MeanSquaredErrorEvaluator();154 validationQualityEvaluator.Name = "ValidationMeanSquaredErrorEvaluator";155 validationQualityEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";156 validationQualityEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";157 validationQualityEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";158 159 141 main.AddSubOperator(seq); 160 142 seq.AddSubOperator(selector); … … 181 163 main.AddSubOperator(seqSubScopesProc2); 182 164 seqSubScopesProc2.AddSubOperator(emptyOp); 183 184 SequentialProcessor newGenProc = new SequentialProcessor(); 185 newGenProc.AddSubOperator(sorter); 186 newGenProc.AddSubOperator(validationEvaluator); 187 seqSubScopesProc2.AddSubOperator(newGenProc); 188 189 validationEvaluator.AddSubOperator(validationQualityEvaluator); 165 seqSubScopesProc2.AddSubOperator(sorter); 190 166 191 167 childCreater.OperatorGraph.AddOperator(main); … … 194 170 } 195 171 196 protected internal override IOperator CreateLoopCondition(IOperator loop) { 197 SequentialProcessor seq = new SequentialProcessor(); 198 seq.Name = "Loop Condition"; 199 LessThanComparator generationsComparator = new LessThanComparator(); 200 generationsComparator.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions"; 201 generationsComparator.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions"; 202 generationsComparator.GetVariableInfo("Result").ActualName = "EvaluatedSolutionsCondition"; 203 204 LessThanComparator selPresComparator = new LessThanComparator(); 172 protected override IOperator CreateTerminationCondition() { 173 CombinedOperator terminationCritertion = new CombinedOperator(); 174 terminationCritertion.Name = "TerminationCondition"; 175 GreaterThanComparator selPresComparator = new GreaterThanComparator(); 205 176 selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure"; 206 177 selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit"; 207 selPresComparator.GetVariableInfo("Result").ActualName = "SelectionPressureCondition"; 208 209 ConditionalBranch generationsCond = new ConditionalBranch(); 210 generationsCond.GetVariableInfo("Condition").ActualName = "EvaluatedSolutionsCondition"; 211 212 ConditionalBranch selPresCond = new ConditionalBranch(); 213 selPresCond.GetVariableInfo("Condition").ActualName = "SelectionPressureCondition"; 214 215 seq.AddSubOperator(generationsComparator); 216 seq.AddSubOperator(selPresComparator); 217 seq.AddSubOperator(generationsCond); 218 generationsCond.AddSubOperator(selPresCond); 219 selPresCond.AddSubOperator(loop); 178 selPresComparator.GetVariableInfo("Result").ActualName = "TerminationCriterion"; 179 180 IOperator baseAndSelPresTerminationCriterion = CombineTerminationCriterions(base.CreateTerminationCondition(), selPresComparator); 181 182 GreaterThanComparator evalSolutionsComparer = new GreaterThanComparator(); 183 evalSolutionsComparer.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions"; 184 evalSolutionsComparer.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions"; 185 evalSolutionsComparer.GetVariableInfo("Result").ActualName = "TerminationCriterion"; 186 187 IOperator combinedTerminationCritertion = CombineTerminationCriterions(baseAndSelPresTerminationCriterion, evalSolutionsComparer); 188 189 terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCritertion); 190 terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCritertion; 191 return terminationCritertion; 192 } 193 194 private IOperator CombineTerminationCriterions(IOperator criterion1, IOperator criterion2) { 195 ConditionalBranch branch = new ConditionalBranch(); 196 branch.GetVariableInfo("Condition").ActualName = "TerminationCriterion"; 197 branch.AddSubOperator(new EmptyOperator()); 198 branch.AddSubOperator(criterion2); 199 200 SequentialProcessor seq = new SequentialProcessor(); 201 seq.AddSubOperator(criterion1); 202 seq.AddSubOperator(branch); 220 203 221 204 return seq; 222 205 } 223 206 224 protected internaloverride IOperator CreateLoggingOperator() {207 protected override IOperator CreateLoggingOperator() { 225 208 CombinedOperator loggingOperator = new CombinedOperator(); 226 209 loggingOperator.Name = "Logging"; … … 232 215 names.Add(new StringData("AverageQuality")); 233 216 names.Add(new StringData("WorstQuality")); 234 names.Add(new StringData("BestValidationQuality"));235 names.Add(new StringData("AverageValidationQuality"));236 names.Add(new StringData("WorstValidationQuality"));237 217 names.Add(new StringData("EvaluatedSolutions")); 238 218 names.Add(new StringData("SelectionPressure")); 239 219 QualityLogger qualityLogger = new QualityLogger(); 240 QualityLogger validationQualityLogger = new QualityLogger();241 validationQualityLogger.Name = "ValidationQualityLogger";242 validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";243 validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";244 245 220 seq.AddSubOperator(collector); 246 221 seq.AddSubOperator(qualityLogger); 247 seq.AddSubOperator(validationQualityLogger);248 222 249 223 loggingOperator.OperatorGraph.AddOperator(seq); … … 252 226 } 253 227 254 255 public override IEditor CreateEditor() { 228 public virtual IEditor CreateEditor() { 256 229 return new OffspringSelectionGpEditor(this); 257 230 } -
branches/gp-algorithms-refactoring-#720/sources/HeuristicLab.GP.Algorithms/3.2/StandardGP.cs
r2331 r2335 31 31 32 32 public override string Name { get { return "StandardGP"; } } 33 34 public override int TargetVariable {35 get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }36 set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }37 }38 39 public override Dataset Dataset {40 get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }41 set { ProblemInjector.GetVariable("Dataset").Value = value; }42 }43 44 public virtual int MaxGenerations {45 get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }46 set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }47 }48 49 33 public virtual int TournamentSize { 50 34 get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; } … … 62 46 } 63 47 64 public int MinInitialTreeSize {65 get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; }66 set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; }67 }68 69 public override int MaxTreeSize {70 get {71 return base.MaxTreeSize;72 }73 set {74 base.MaxTreeSize = value;75 MinInitialTreeSize = value / 2;76 }77 }78 79 48 public override int PopulationSize { 80 49 get { … … 84 53 base.PopulationSize = value; 85 54 Parents = 2 * value; 86 }87 }88 89 public override IOperator ProblemInjector {90 get { return base.ProblemInjector.SubOperators[0]; }91 set {92 value.Name = "ProblemInjector";93 base.ProblemInjector.RemoveSubOperator(0);94 base.ProblemInjector.AddSubOperator(value, 0);95 55 } 96 56 } … … 107 67 FullTreeShakingFactor = 0.1; 108 68 OnePointShakingFactor = 1.0; 109 UseEstimatedTargetValue = false;110 69 SetSeedRandomly = true; 111 70 } 112 71 113 protected internal override IOperator CreateProblemInjector() { 114 SequentialProcessor seq = new SequentialProcessor(); 115 var probInject = new ProblemInjector(); 116 probInject.GetVariableInfo("MaxNumberOfTrainingSamples").Local = true; 117 probInject.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(5000))); 118 119 var shuffler = new DatasetShuffler(); 120 shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart"; 121 shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd"; 122 123 seq.AddSubOperator(probInject); 124 seq.AddSubOperator(shuffler); 125 return seq; 126 } 127 128 protected internal override IOperator CreateSelector() { 72 protected override IOperator CreateSelectionOperator() { 129 73 TournamentSelector selector = new TournamentSelector(); 130 74 selector.Name = "Selector"; … … 136 80 } 137 81 138 protected internal override IOperator CreateGlobalInjector() {139 VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();82 protected override VariableInjector CreateGlobalInjector() { 83 VariableInjector globalInjector = base.CreateGlobalInjector(); 140 84 globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData())); 141 globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));142 85 globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData())); 143 86 globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData())); 144 globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData()));145 87 return globalInjector; 146 88 } 147 89 148 protected internal override IOperator CreateCrossover() { 149 StandardCrossOver crossover = new StandardCrossOver(); 150 crossover.Name = "Crossover"; 151 return crossover; 152 } 153 154 protected internal override IOperator CreateTreeCreator() { 155 ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator(); 156 treeCreator.Name = "Tree generator"; 157 treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize"; 158 return treeCreator; 159 } 160 161 protected internal override IOperator CreateFunctionLibraryInjector() { 162 FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector(); 163 funLibInjector.GetVariableValue<BoolData>("Xor", null, false).Data = false; 164 funLibInjector.GetVariableValue<BoolData>("Average", null, false).Data = false; 165 return funLibInjector; 166 } 167 168 protected internal override IOperator CreateManipulator() { 90 protected override IOperator CreateManipulationOperator() { 169 91 CombinedOperator manipulator = new CombinedOperator(); 170 92 manipulator.Name = "Manipulator"; … … 200 122 } 201 123 202 protected internal override IOperator CreateLoggingOperator() {203 CombinedOperator loggingOperator = new CombinedOperator();204 loggingOperator.Name = "Logging";205 SequentialProcessor seq = new SequentialProcessor();206 207 DataCollector collector = new DataCollector();208 ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();209 names.Add(new StringData("BestQuality"));210 names.Add(new StringData("AverageQuality"));211 names.Add(new StringData("WorstQuality"));212 names.Add(new StringData("BestValidationQuality"));213 names.Add(new StringData("AverageValidationQuality"));214 names.Add(new StringData("WorstValidationQuality"));215 LinechartInjector lineChartInjector = new LinechartInjector();216 lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";217 lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;218 QualityLogger qualityLogger = new QualityLogger();219 QualityLogger validationQualityLogger = new QualityLogger();220 validationQualityLogger.Name = "ValidationQualityLogger";221 validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";222 validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";223 224 seq.AddSubOperator(collector);225 seq.AddSubOperator(lineChartInjector);226 seq.AddSubOperator(qualityLogger);227 seq.AddSubOperator(validationQualityLogger);228 229 loggingOperator.OperatorGraph.AddOperator(seq);230 loggingOperator.OperatorGraph.InitialOperator = seq;231 return loggingOperator;232 }233 234 124 public virtual IEditor CreateEditor() { 235 125 return new StandardGpEditor(this);
Note: See TracChangeset
for help on using the changeset viewer.