[1230] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[2222] | 24 | using System.Xml;
|
---|
[1230] | 25 | using HeuristicLab.Core;
|
---|
[2222] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Evolutionary;
|
---|
| 28 | using HeuristicLab.Logging;
|
---|
[1230] | 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Random;
|
---|
| 31 | using HeuristicLab.Selection;
|
---|
[2331] | 32 | using HeuristicLab.GP.Operators;
|
---|
[1230] | 33 |
|
---|
[2331] | 34 | namespace HeuristicLab.GP.Algorithms {
|
---|
| 35 | public abstract class AlgorithmBase : ItemBase {
|
---|
[1857] | 36 | public virtual string Name { get { return "GP"; } }
|
---|
| 37 | public virtual string Description { get { return "TODO"; } }
|
---|
| 38 |
|
---|
[1231] | 39 | public virtual double MutationRate {
|
---|
[1287] | 40 | get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
|
---|
| 41 | set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
|
---|
[1230] | 42 | }
|
---|
[1231] | 43 | public virtual int PopulationSize {
|
---|
[1287] | 44 | get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
|
---|
| 45 | set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
|
---|
[1230] | 46 | }
|
---|
| 47 |
|
---|
[2335] | 48 | public virtual int MaxGenerations {
|
---|
| 49 | get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
|
---|
| 50 | set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
|
---|
[1230] | 51 | }
|
---|
| 52 |
|
---|
[1231] | 53 | public virtual int Elites {
|
---|
[1287] | 54 | get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
|
---|
| 55 | set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
|
---|
[1230] | 56 | }
|
---|
| 57 |
|
---|
[1231] | 58 | public virtual int MaxTreeSize {
|
---|
[1287] | 59 | get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
|
---|
| 60 | set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
|
---|
[1230] | 61 | }
|
---|
| 62 |
|
---|
[2335] | 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 |
|
---|
[1231] | 68 | public virtual int MaxTreeHeight {
|
---|
[1287] | 69 | get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
|
---|
| 70 | set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
|
---|
[1230] | 71 | }
|
---|
| 72 |
|
---|
[1231] | 73 | public virtual int Parents {
|
---|
[1287] | 74 | get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
|
---|
| 75 | set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
|
---|
[1231] | 76 | }
|
---|
| 77 |
|
---|
[2335] | 78 | public virtual bool SetSeedRandomly {
|
---|
| 79 | get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
|
---|
| 80 | set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | public virtual int RandomSeed {
|
---|
| 84 | get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
|
---|
| 85 | set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public virtual IOperator ProblemInjector {
|
---|
| 89 | get { return GetInitializationOperator().SubOperators[0]; }
|
---|
| 90 | set {
|
---|
| 91 | value.Name = "ProblemInjector";
|
---|
| 92 | IOperator init = GetInitializationOperator();
|
---|
| 93 | init.RemoveSubOperator(0);
|
---|
| 94 | init.AddSubOperator(value, 0);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 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 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[1230] | 108 | private IOperator algorithm;
|
---|
| 109 |
|
---|
| 110 | private SequentialEngine.SequentialEngine engine;
|
---|
| 111 | public IEngine Engine {
|
---|
| 112 | get { return engine; }
|
---|
[1231] | 113 | protected set { engine = (SequentialEngine.SequentialEngine)value; }
|
---|
[1230] | 114 | }
|
---|
| 115 |
|
---|
| 116 | public AlgorithmBase() {
|
---|
| 117 | engine = new SequentialEngine.SequentialEngine();
|
---|
| 118 | CombinedOperator algo = CreateAlgorithm();
|
---|
| 119 | engine.OperatorGraph.AddOperator(algo);
|
---|
| 120 | engine.OperatorGraph.InitialOperator = algo;
|
---|
[1287] | 121 | SetSeedRandomly = true;
|
---|
| 122 | Elites = 1;
|
---|
| 123 | MutationRate = 0.15;
|
---|
| 124 | PopulationSize = 1000;
|
---|
[2335] | 125 | MaxGenerations = 100;
|
---|
[1287] | 126 | MaxTreeSize = 100;
|
---|
[2335] | 127 | MinTreeSize = 1;
|
---|
[1287] | 128 | MaxTreeHeight = 10;
|
---|
| 129 | Parents = 2000;
|
---|
[1230] | 130 | }
|
---|
| 131 |
|
---|
[1287] | 132 | protected internal virtual CombinedOperator CreateAlgorithm() {
|
---|
[1230] | 133 | CombinedOperator algo = new CombinedOperator();
|
---|
| 134 | algo.Name = "GP";
|
---|
| 135 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[2335] | 136 | IOperator init = CreateInitializationOperator();
|
---|
| 137 | init.AddSubOperator(CreateProblemInjector());
|
---|
| 138 | init.AddSubOperator(CreateFunctionLibraryInjector());
|
---|
| 139 | seq.AddSubOperator(init);
|
---|
[1230] | 140 |
|
---|
[2335] | 141 | IOperator initPopulation = CreateInitialPopulationGenerator();
|
---|
| 142 | initPopulation.AddSubOperator(CreateRandomSolutionGenerator());
|
---|
| 143 | initPopulation.AddSubOperator(CreateInitialPopulationEvaluator());
|
---|
| 144 | seq.AddSubOperator(initPopulation);
|
---|
[1230] | 145 |
|
---|
| 146 | IOperator mainLoop = CreateMainLoop();
|
---|
[2335] | 147 | mainLoop.AddSubOperator(CreateSelectionOperator());
|
---|
| 148 | mainLoop.AddSubOperator(CreateCrossoverOperator());
|
---|
| 149 | mainLoop.AddSubOperator(CreateManipulationOperator());
|
---|
| 150 | mainLoop.AddSubOperator(CreateEvaluationOperator());
|
---|
| 151 | mainLoop.AddSubOperator(CreateTerminationCondition());
|
---|
[1230] | 152 | seq.AddSubOperator(mainLoop);
|
---|
| 153 |
|
---|
[2335] | 154 | IOperator postProcess = CreatePostProcessingOperator();
|
---|
| 155 | seq.AddSubOperator(postProcess);
|
---|
[1230] | 156 |
|
---|
| 157 | algo.OperatorGraph.AddOperator(seq);
|
---|
| 158 | algo.OperatorGraph.InitialOperator = seq;
|
---|
| 159 | this.algorithm = seq;
|
---|
| 160 | return algo;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[2335] | 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());
|
---|
[2328] | 170 |
|
---|
[2335] | 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;
|
---|
[1287] | 184 | }
|
---|
[1230] | 185 |
|
---|
[2335] | 186 | protected virtual IOperator CreateRandomInjector() {
|
---|
| 187 | RandomInjector randomInjector = new RandomInjector();
|
---|
| 188 | randomInjector.Name = "Random Injector";
|
---|
| 189 | return randomInjector;
|
---|
[2331] | 190 | }
|
---|
[1230] | 191 |
|
---|
[2335] | 192 | protected virtual VariableInjector CreateGlobalInjector() {
|
---|
[1230] | 193 | VariableInjector injector = new VariableInjector();
|
---|
| 194 | injector.Name = "Global Injector";
|
---|
| 195 | injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
|
---|
[2335] | 196 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
|
---|
[1287] | 197 | injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
|
---|
| 198 | injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
|
---|
| 199 | injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
|
---|
[1230] | 200 | injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
[1287] | 201 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
|
---|
| 202 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
|
---|
[2335] | 203 | injector.AddVariable(new HeuristicLab.Core.Variable("MinTreeSize", new IntData()));
|
---|
[1230] | 204 | injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
|
---|
| 205 | injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
|
---|
[1287] | 206 | injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
|
---|
[1230] | 207 | return injector;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 |
|
---|
[2335] | 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";
|
---|
[1230] | 224 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 225 | SubScopesCreater subScopesCreater = new SubScopesCreater();
|
---|
| 226 | subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
|
---|
| 227 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
| 228 | SequentialProcessor individualSeq = new SequentialProcessor();
|
---|
| 229 | OperatorExtractor treeCreater = new OperatorExtractor();
|
---|
| 230 | treeCreater.Name = "Tree generator (extr.)";
|
---|
[2335] | 231 | treeCreater.GetVariableInfo("Operator").ActualName = "Solution generator";
|
---|
| 232 |
|
---|
[1230] | 233 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 234 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 235 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 236 | Counter evalCounter = new Counter();
|
---|
| 237 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
| 238 | Sorter sorter = new Sorter();
|
---|
| 239 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 240 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 241 |
|
---|
| 242 | seq.AddSubOperator(subScopesCreater);
|
---|
| 243 | seq.AddSubOperator(subScopesProc);
|
---|
| 244 | seq.AddSubOperator(sorter);
|
---|
| 245 |
|
---|
| 246 | subScopesProc.AddSubOperator(individualSeq);
|
---|
| 247 | individualSeq.AddSubOperator(treeCreater);
|
---|
| 248 | individualSeq.AddSubOperator(evaluator);
|
---|
| 249 | individualSeq.AddSubOperator(evalCounter);
|
---|
| 250 |
|
---|
[2335] | 251 | initPopulation.OperatorGraph.AddOperator(seq);
|
---|
| 252 | initPopulation.OperatorGraph.InitialOperator = seq;
|
---|
| 253 | return initPopulation;
|
---|
[1230] | 254 | }
|
---|
| 255 |
|
---|
[2335] | 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() {
|
---|
[1230] | 269 | CombinedOperator main = new CombinedOperator();
|
---|
[2335] | 270 | main.Name = "Main";
|
---|
[1230] | 271 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 272 | IOperator childCreater = CreateChildCreater();
|
---|
| 273 | IOperator replacement = CreateReplacement();
|
---|
| 274 |
|
---|
| 275 | BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
[1287] | 276 | IOperator loggingOperator = CreateLoggingOperator();
|
---|
[1230] | 277 | Counter counter = new Counter();
|
---|
| 278 | counter.GetVariableInfo("Value").ActualName = "Generations";
|
---|
| 279 |
|
---|
[2335] | 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);
|
---|
| 289 |
|
---|
[1230] | 290 | seq.AddSubOperator(childCreater);
|
---|
| 291 | seq.AddSubOperator(replacement);
|
---|
| 292 | seq.AddSubOperator(qualityCalculator);
|
---|
[2335] | 293 | seq.AddSubOperator(CreateGenerationStepHook());
|
---|
[1287] | 294 | seq.AddSubOperator(loggingOperator);
|
---|
[1230] | 295 | seq.AddSubOperator(counter);
|
---|
[2335] | 296 | seq.AddSubOperator(terminationCriterionExtractor);
|
---|
| 297 | seq.AddSubOperator(loop);
|
---|
[1230] | 298 |
|
---|
| 299 | main.OperatorGraph.AddOperator(seq);
|
---|
| 300 | main.OperatorGraph.InitialOperator = seq;
|
---|
| 301 | return main;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[2335] | 304 | protected virtual IOperator CreateChildCreater() {
|
---|
[1230] | 305 | CombinedOperator childCreater = new CombinedOperator();
|
---|
| 306 | childCreater.Name = "Create children";
|
---|
| 307 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 308 | OperatorExtractor selector = new OperatorExtractor();
|
---|
| 309 | selector.Name = "Selector (extr.)";
|
---|
| 310 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
[1335] | 311 |
|
---|
[1230] | 312 | SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
|
---|
| 313 | EmptyOperator emptyOpt = new EmptyOperator();
|
---|
| 314 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
[1335] | 315 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
| 316 | ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
|
---|
| 317 |
|
---|
[1230] | 318 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
| 319 | crossover.Name = "Crossover (extr.)";
|
---|
| 320 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
| 321 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
| 322 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
| 323 | StochasticBranch cond = new StochasticBranch();
|
---|
| 324 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 325 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
| 326 | manipulator.Name = "Manipulator (extr.)";
|
---|
| 327 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
| 328 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 329 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 330 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 331 | Counter evalCounter = new Counter();
|
---|
| 332 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
[1335] | 333 | SubScopesRemover parentRefRemover = new SubScopesRemover();
|
---|
[1230] | 334 |
|
---|
| 335 | Sorter sorter = new Sorter();
|
---|
| 336 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 337 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 338 |
|
---|
| 339 |
|
---|
| 340 | seq.AddSubOperator(selector);
|
---|
| 341 | seq.AddSubOperator(seqScopesProc);
|
---|
| 342 | seqScopesProc.AddSubOperator(emptyOpt);
|
---|
| 343 | seqScopesProc.AddSubOperator(selectedProc);
|
---|
[1335] | 344 | selectedProc.AddSubOperator(childInitializer);
|
---|
[1230] | 345 | selectedProc.AddSubOperator(individualProc);
|
---|
| 346 | individualProc.AddSubOperator(individualSeqProc);
|
---|
[1335] | 347 | individualSeqProc.AddSubOperator(crossover);
|
---|
[1230] | 348 | individualSeqProc.AddSubOperator(cond);
|
---|
| 349 | cond.AddSubOperator(manipulator);
|
---|
| 350 | individualSeqProc.AddSubOperator(evaluator);
|
---|
| 351 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
[1335] | 352 | individualSeqProc.AddSubOperator(parentRefRemover);
|
---|
[1230] | 353 | selectedProc.AddSubOperator(sorter);
|
---|
| 354 |
|
---|
| 355 | childCreater.OperatorGraph.AddOperator(seq);
|
---|
| 356 | childCreater.OperatorGraph.InitialOperator = seq;
|
---|
| 357 | return childCreater;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[2335] | 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();
|
---|
[2270] | 369 |
|
---|
[2335] | 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";
|
---|
[2041] | 378 |
|
---|
[2335] | 379 | seq.AddSubOperator(seqScopeProc);
|
---|
| 380 | seqScopeProc.AddSubOperator(selectedProc);
|
---|
| 381 | selectedProc.AddSubOperator(leftSelector);
|
---|
| 382 | selectedProc.AddSubOperator(rightReducer);
|
---|
[2328] | 383 |
|
---|
[2335] | 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;
|
---|
[1906] | 392 | }
|
---|
| 393 |
|
---|
[2335] | 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 |
|
---|
| 467 |
|
---|
[1230] | 468 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 469 | AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
|
---|
| 470 | clonedObjects.Add(Guid, clone);
|
---|
| 471 | clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
|
---|
| 472 | return clone;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[2335] | 475 | protected virtual IOperator GetVariableInjector() {
|
---|
| 476 | CombinedOperator init = (CombinedOperator)GetInitializationOperator();
|
---|
| 477 | return init.OperatorGraph.InitialOperator.SubOperators[1];
|
---|
[1230] | 478 | }
|
---|
| 479 |
|
---|
[2335] | 480 | protected virtual IOperator GetRandomInjector() {
|
---|
| 481 | CombinedOperator init = (CombinedOperator)GetInitializationOperator();
|
---|
| 482 | return init.OperatorGraph.InitialOperator.SubOperators[0];
|
---|
[1287] | 483 | }
|
---|
| 484 |
|
---|
[2335] | 485 | protected virtual IOperator GetInitializationOperator() {
|
---|
| 486 | return algorithm.SubOperators[0];
|
---|
| 487 | }
|
---|
| 488 |
|
---|
[1230] | 489 | #region Persistence Methods
|
---|
| 490 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 491 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 492 | node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
|
---|
| 493 | return node;
|
---|
| 494 | }
|
---|
| 495 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 496 | base.Populate(node, restoredObjects);
|
---|
| 497 | engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
| 498 | }
|
---|
| 499 | #endregion
|
---|
[1857] | 500 |
|
---|
[1922] | 501 |
|
---|
[1230] | 502 | }
|
---|
| 503 | }
|
---|