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