[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;
|
---|
[1230] | 27 | using HeuristicLab.DataAnalysis;
|
---|
[2222] | 28 | using HeuristicLab.Evolutionary;
|
---|
| 29 | using HeuristicLab.GP.Interfaces;
|
---|
| 30 | using HeuristicLab.Logging;
|
---|
| 31 | using HeuristicLab.Modeling;
|
---|
[1230] | 32 | using HeuristicLab.Operators;
|
---|
| 33 | using HeuristicLab.Random;
|
---|
| 34 | using HeuristicLab.Selection;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[1857] | 37 | public abstract class AlgorithmBase : ItemBase, IAlgorithm, IStochasticAlgorithm {
|
---|
| 38 | public virtual string Name { get { return "GP"; } }
|
---|
| 39 | public virtual string Description { get { return "TODO"; } }
|
---|
| 40 |
|
---|
[1922] | 41 | public abstract Dataset Dataset { get; set; }
|
---|
| 42 | public abstract int TargetVariable { get; set; }
|
---|
| 43 |
|
---|
[1231] | 44 | public virtual double MutationRate {
|
---|
[1287] | 45 | get { return GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data; }
|
---|
| 46 | set { GetVariableInjector().GetVariable("MutationRate").GetValue<DoubleData>().Data = value; }
|
---|
[1230] | 47 | }
|
---|
[1231] | 48 | public virtual int PopulationSize {
|
---|
[1287] | 49 | get { return GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data; }
|
---|
| 50 | set { GetVariableInjector().GetVariable("PopulationSize").GetValue<IntData>().Data = value; }
|
---|
[1230] | 51 | }
|
---|
| 52 |
|
---|
[1231] | 53 | public virtual bool SetSeedRandomly {
|
---|
[1287] | 54 | get { return GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data; }
|
---|
| 55 | set { GetRandomInjector().GetVariable("SetSeedRandomly").GetValue<BoolData>().Data = value; }
|
---|
[1230] | 56 | }
|
---|
| 57 |
|
---|
[1857] | 58 | public virtual int RandomSeed {
|
---|
[1287] | 59 | get { return GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data; }
|
---|
| 60 | set { GetRandomInjector().GetVariable("Seed").GetValue<IntData>().Data = value; }
|
---|
[1230] | 61 | }
|
---|
| 62 |
|
---|
[1231] | 63 | public virtual IOperator ProblemInjector {
|
---|
[2161] | 64 | get { return algorithm.SubOperators[1]; }
|
---|
[1230] | 65 | set {
|
---|
| 66 | value.Name = "ProblemInjector";
|
---|
[2161] | 67 | algorithm.RemoveSubOperator(1);
|
---|
| 68 | algorithm.AddSubOperator(value, 1);
|
---|
[1230] | 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[2223] | 72 | private IModel model;
|
---|
[1906] | 73 | public virtual IModel Model {
|
---|
| 74 | get {
|
---|
| 75 | if (!engine.Terminated) throw new InvalidOperationException("The algorithm is still running. Wait until the algorithm is terminated to retrieve the result.");
|
---|
[2223] | 76 | if (model == null) {
|
---|
| 77 | IScope bestModelScope = engine.GlobalScope.GetVariableValue<IScope>("BestValidationSolution", false);
|
---|
| 78 | model = CreateGPModel(bestModelScope);
|
---|
| 79 | }
|
---|
| 80 | return model;
|
---|
[1906] | 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[1231] | 84 | public virtual int Elites {
|
---|
[1287] | 85 | get { return GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data; }
|
---|
| 86 | set { GetVariableInjector().GetVariable("Elites").GetValue<IntData>().Data = value; }
|
---|
[1230] | 87 | }
|
---|
| 88 |
|
---|
[1231] | 89 | public virtual int MaxTreeSize {
|
---|
[1287] | 90 | get { return GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data; }
|
---|
| 91 | set { GetVariableInjector().GetVariable("MaxTreeSize").GetValue<IntData>().Data = value; }
|
---|
[1230] | 92 | }
|
---|
| 93 |
|
---|
[1231] | 94 | public virtual int MaxTreeHeight {
|
---|
[1287] | 95 | get { return GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data; }
|
---|
| 96 | set { GetVariableInjector().GetVariable("MaxTreeHeight").GetValue<IntData>().Data = value; }
|
---|
[1230] | 97 | }
|
---|
| 98 |
|
---|
[1231] | 99 | public virtual int Parents {
|
---|
[1287] | 100 | get { return GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data; }
|
---|
| 101 | set { GetVariableInjector().GetVariable("Parents").GetValue<IntData>().Data = value; }
|
---|
[1231] | 102 | }
|
---|
| 103 |
|
---|
[1287] | 104 | public virtual double PunishmentFactor {
|
---|
| 105 | get { return GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data; }
|
---|
| 106 | set { GetVariableInjector().GetVariable("PunishmentFactor").GetValue<DoubleData>().Data = value; }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public virtual bool UseEstimatedTargetValue {
|
---|
| 110 | get { return GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data; }
|
---|
| 111 | set { GetVariableInjector().GetVariable("UseEstimatedTargetValue").GetValue<BoolData>().Data = value; }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[1230] | 114 | private IOperator algorithm;
|
---|
| 115 |
|
---|
| 116 | private SequentialEngine.SequentialEngine engine;
|
---|
| 117 | public IEngine Engine {
|
---|
| 118 | get { return engine; }
|
---|
[1231] | 119 | protected set { engine = (SequentialEngine.SequentialEngine)value; }
|
---|
[1230] | 120 | }
|
---|
| 121 |
|
---|
| 122 | public AlgorithmBase() {
|
---|
| 123 | engine = new SequentialEngine.SequentialEngine();
|
---|
| 124 | CombinedOperator algo = CreateAlgorithm();
|
---|
| 125 | engine.OperatorGraph.AddOperator(algo);
|
---|
| 126 | engine.OperatorGraph.InitialOperator = algo;
|
---|
[1287] | 127 | SetSeedRandomly = true;
|
---|
| 128 | Elites = 1;
|
---|
| 129 | MutationRate = 0.15;
|
---|
| 130 | PopulationSize = 1000;
|
---|
| 131 | MaxTreeSize = 100;
|
---|
| 132 | MaxTreeHeight = 10;
|
---|
| 133 | Parents = 2000;
|
---|
| 134 | PunishmentFactor = 10;
|
---|
| 135 | UseEstimatedTargetValue = false;
|
---|
[1230] | 136 | }
|
---|
| 137 |
|
---|
[1287] | 138 | protected internal virtual CombinedOperator CreateAlgorithm() {
|
---|
[1230] | 139 | CombinedOperator algo = new CombinedOperator();
|
---|
| 140 | algo.Name = "GP";
|
---|
| 141 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 142 | IOperator problemInjector = CreateProblemInjector();
|
---|
[1230] | 143 |
|
---|
| 144 | RandomInjector randomInjector = new RandomInjector();
|
---|
| 145 | randomInjector.Name = "Random Injector";
|
---|
| 146 |
|
---|
| 147 | IOperator globalInjector = CreateGlobalInjector();
|
---|
| 148 | IOperator initialization = CreateInitialization();
|
---|
[1287] | 149 | IOperator funLibInjector = CreateFunctionLibraryInjector();
|
---|
[1873] | 150 |
|
---|
[1230] | 151 | IOperator mainLoop = CreateMainLoop();
|
---|
| 152 | mainLoop.Name = "Main loop";
|
---|
| 153 |
|
---|
| 154 | IOperator treeCreator = CreateTreeCreator();
|
---|
[1287] | 155 |
|
---|
[1230] | 156 | MeanSquaredErrorEvaluator evaluator = new MeanSquaredErrorEvaluator();
|
---|
| 157 | evaluator.GetVariableInfo("MSE").ActualName = "Quality";
|
---|
[2161] | 158 | evaluator.GetVariableInfo("SamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
| 159 | evaluator.GetVariableInfo("SamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
[1230] | 160 | evaluator.Name = "Evaluator";
|
---|
[1287] | 161 |
|
---|
[1230] | 162 | IOperator crossover = CreateCrossover();
|
---|
| 163 | IOperator manipulator = CreateManipulator();
|
---|
| 164 |
|
---|
| 165 | IOperator selector = CreateSelector();
|
---|
| 166 | LeftReducer cleanUp = new LeftReducer();
|
---|
| 167 |
|
---|
[2161] | 168 | seq.AddSubOperator(randomInjector);
|
---|
[1287] | 169 | seq.AddSubOperator(problemInjector);
|
---|
[1230] | 170 | seq.AddSubOperator(globalInjector);
|
---|
| 171 | seq.AddSubOperator(funLibInjector);
|
---|
| 172 | seq.AddSubOperator(initialization);
|
---|
| 173 | seq.AddSubOperator(mainLoop);
|
---|
| 174 | seq.AddSubOperator(cleanUp);
|
---|
| 175 |
|
---|
| 176 | initialization.AddSubOperator(treeCreator);
|
---|
| 177 | initialization.AddSubOperator(evaluator);
|
---|
| 178 |
|
---|
| 179 | mainLoop.AddSubOperator(selector);
|
---|
| 180 | mainLoop.AddSubOperator(crossover);
|
---|
| 181 | mainLoop.AddSubOperator(manipulator);
|
---|
| 182 | mainLoop.AddSubOperator(evaluator);
|
---|
| 183 | algo.OperatorGraph.AddOperator(seq);
|
---|
| 184 | algo.OperatorGraph.InitialOperator = seq;
|
---|
| 185 | this.algorithm = seq;
|
---|
| 186 | return algo;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[1287] | 189 | protected internal virtual IOperator CreateProblemInjector() {
|
---|
| 190 | return new EmptyOperator();
|
---|
| 191 | }
|
---|
[1230] | 192 |
|
---|
[1287] | 193 | protected internal abstract IOperator CreateSelector();
|
---|
[1230] | 194 |
|
---|
[1287] | 195 | protected internal abstract IOperator CreateCrossover();
|
---|
[1230] | 196 |
|
---|
[1287] | 197 | protected internal abstract IOperator CreateTreeCreator();
|
---|
[1230] | 198 |
|
---|
[1287] | 199 | protected internal abstract IOperator CreateFunctionLibraryInjector();
|
---|
| 200 |
|
---|
| 201 | protected internal virtual IOperator CreateGlobalInjector() {
|
---|
[1230] | 202 | VariableInjector injector = new VariableInjector();
|
---|
| 203 | injector.Name = "Global Injector";
|
---|
| 204 | injector.AddVariable(new HeuristicLab.Core.Variable("Generations", new IntData(0)));
|
---|
[1287] | 205 | injector.AddVariable(new HeuristicLab.Core.Variable("MutationRate", new DoubleData()));
|
---|
| 206 | injector.AddVariable(new HeuristicLab.Core.Variable("PopulationSize", new IntData()));
|
---|
| 207 | injector.AddVariable(new HeuristicLab.Core.Variable("Elites", new IntData()));
|
---|
[1230] | 208 | injector.AddVariable(new HeuristicLab.Core.Variable("Maximization", new BoolData(false)));
|
---|
[1287] | 209 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeHeight", new IntData()));
|
---|
| 210 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxTreeSize", new IntData()));
|
---|
[1230] | 211 | injector.AddVariable(new HeuristicLab.Core.Variable("EvaluatedSolutions", new IntData(0)));
|
---|
| 212 | injector.AddVariable(new HeuristicLab.Core.Variable("TotalEvaluatedNodes", new DoubleData(0)));
|
---|
[1287] | 213 | injector.AddVariable(new HeuristicLab.Core.Variable("Parents", new IntData()));
|
---|
| 214 | injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData()));
|
---|
| 215 | injector.AddVariable(new HeuristicLab.Core.Variable("UseEstimatedTargetValue", new BoolData()));
|
---|
[2034] | 216 | injector.AddVariable(new HeuristicLab.Core.Variable("TreeEvaluator", new HL2TreeEvaluator()));
|
---|
[1230] | 217 | return injector;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[1287] | 220 | protected internal abstract IOperator CreateManipulator();
|
---|
[1230] | 221 |
|
---|
[1287] | 222 | protected internal virtual IOperator CreateInitialization() {
|
---|
[1230] | 223 | CombinedOperator init = new CombinedOperator();
|
---|
| 224 | init.Name = "Initialization";
|
---|
| 225 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 226 | SubScopesCreater subScopesCreater = new SubScopesCreater();
|
---|
| 227 | subScopesCreater.GetVariableInfo("SubScopes").ActualName = "PopulationSize";
|
---|
| 228 | UniformSequentialSubScopesProcessor subScopesProc = new UniformSequentialSubScopesProcessor();
|
---|
| 229 | SequentialProcessor individualSeq = new SequentialProcessor();
|
---|
| 230 | OperatorExtractor treeCreater = new OperatorExtractor();
|
---|
| 231 | treeCreater.Name = "Tree generator (extr.)";
|
---|
| 232 | treeCreater.GetVariableInfo("Operator").ActualName = "Tree generator";
|
---|
| 233 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 234 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 235 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 236 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
| 237 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
| 238 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
| 239 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
| 240 | Counter evalCounter = new Counter();
|
---|
| 241 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
| 242 | Sorter sorter = new Sorter();
|
---|
| 243 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 244 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 245 |
|
---|
| 246 | seq.AddSubOperator(subScopesCreater);
|
---|
| 247 | seq.AddSubOperator(subScopesProc);
|
---|
| 248 | seq.AddSubOperator(sorter);
|
---|
| 249 |
|
---|
| 250 | subScopesProc.AddSubOperator(individualSeq);
|
---|
| 251 | individualSeq.AddSubOperator(treeCreater);
|
---|
| 252 | individualSeq.AddSubOperator(evaluator);
|
---|
| 253 | individualSeq.AddSubOperator(validationEvaluator);
|
---|
| 254 | individualSeq.AddSubOperator(evalCounter);
|
---|
| 255 |
|
---|
| 256 | init.OperatorGraph.AddOperator(seq);
|
---|
| 257 | init.OperatorGraph.InitialOperator = seq;
|
---|
| 258 | return init;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[1287] | 261 | protected internal virtual IOperator CreateMainLoop() {
|
---|
[1230] | 262 | CombinedOperator main = new CombinedOperator();
|
---|
| 263 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 264 | IOperator childCreater = CreateChildCreater();
|
---|
| 265 | IOperator replacement = CreateReplacement();
|
---|
| 266 |
|
---|
| 267 | BestSolutionStorer solutionStorer = new BestSolutionStorer();
|
---|
| 268 | solutionStorer.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 269 | solutionStorer.GetVariableInfo("BestSolution").ActualName = "BestValidationSolution";
|
---|
| 270 | solutionStorer.AddSubOperator(CreateBestSolutionProcessor());
|
---|
| 271 |
|
---|
| 272 | BestAverageWorstQualityCalculator qualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
| 273 | BestAverageWorstQualityCalculator validationQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
[1287] | 274 | validationQualityCalculator.Name = "BestAverageWorstValidationQualityCalculator";
|
---|
| 275 | validationQualityCalculator.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
[1230] | 276 | validationQualityCalculator.GetVariableInfo("BestQuality").ActualName = "BestValidationQuality";
|
---|
| 277 | validationQualityCalculator.GetVariableInfo("AverageQuality").ActualName = "AverageValidationQuality";
|
---|
| 278 | validationQualityCalculator.GetVariableInfo("WorstQuality").ActualName = "WorstValidationQuality";
|
---|
[1287] | 279 | IOperator loggingOperator = CreateLoggingOperator();
|
---|
[1230] | 280 | Counter counter = new Counter();
|
---|
| 281 | counter.GetVariableInfo("Value").ActualName = "Generations";
|
---|
[1287] | 282 | IOperator loopCondition = CreateLoopCondition(seq);
|
---|
[1230] | 283 |
|
---|
| 284 | seq.AddSubOperator(childCreater);
|
---|
| 285 | seq.AddSubOperator(replacement);
|
---|
| 286 | seq.AddSubOperator(solutionStorer);
|
---|
| 287 | seq.AddSubOperator(qualityCalculator);
|
---|
| 288 | seq.AddSubOperator(validationQualityCalculator);
|
---|
[1287] | 289 | seq.AddSubOperator(loggingOperator);
|
---|
[1230] | 290 | seq.AddSubOperator(counter);
|
---|
[1287] | 291 | seq.AddSubOperator(loopCondition);
|
---|
[1230] | 292 |
|
---|
| 293 | main.OperatorGraph.AddOperator(seq);
|
---|
| 294 | main.OperatorGraph.InitialOperator = seq;
|
---|
| 295 | return main;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[1287] | 298 | protected internal virtual IOperator CreateLoggingOperator() {
|
---|
[1230] | 299 | return new EmptyOperator();
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[1287] | 302 | protected internal virtual IOperator CreateLoopCondition(IOperator loop) {
|
---|
| 303 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 304 | seq.Name = "Loop Condition";
|
---|
| 305 | LessThanComparator comparator = new LessThanComparator();
|
---|
| 306 | comparator.GetVariableInfo("LeftSide").ActualName = "Generations";
|
---|
| 307 | comparator.GetVariableInfo("RightSide").ActualName = "MaxGenerations";
|
---|
| 308 | comparator.GetVariableInfo("Result").ActualName = "GenerationsCondition";
|
---|
| 309 | ConditionalBranch cond = new ConditionalBranch();
|
---|
| 310 | cond.GetVariableInfo("Condition").ActualName = "GenerationsCondition";
|
---|
| 311 |
|
---|
| 312 | seq.AddSubOperator(comparator);
|
---|
| 313 | seq.AddSubOperator(cond);
|
---|
| 314 |
|
---|
| 315 | cond.AddSubOperator(loop);
|
---|
| 316 | return seq;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | protected internal virtual IOperator CreateBestSolutionProcessor() {
|
---|
| 320 | return new EmptyOperator();
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | protected internal virtual IOperator CreateReplacement() {
|
---|
[1230] | 324 | CombinedOperator replacement = new CombinedOperator();
|
---|
| 325 | replacement.Name = "Replacement";
|
---|
| 326 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 327 | SequentialSubScopesProcessor seqScopeProc = new SequentialSubScopesProcessor();
|
---|
| 328 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
| 329 | LeftSelector leftSelector = new LeftSelector();
|
---|
| 330 | leftSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
| 331 | RightReducer rightReducer = new RightReducer();
|
---|
| 332 |
|
---|
| 333 | SequentialProcessor remainingProc = new SequentialProcessor();
|
---|
| 334 | RightSelector rightSelector = new RightSelector();
|
---|
| 335 | rightSelector.GetVariableInfo("Selected").ActualName = "Elites";
|
---|
| 336 | LeftReducer leftReducer = new LeftReducer();
|
---|
| 337 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 338 | Sorter sorter = new Sorter();
|
---|
| 339 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 340 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 341 |
|
---|
| 342 | seq.AddSubOperator(seqScopeProc);
|
---|
| 343 | seqScopeProc.AddSubOperator(selectedProc);
|
---|
| 344 | selectedProc.AddSubOperator(leftSelector);
|
---|
| 345 | selectedProc.AddSubOperator(rightReducer);
|
---|
| 346 |
|
---|
| 347 | seqScopeProc.AddSubOperator(remainingProc);
|
---|
| 348 | remainingProc.AddSubOperator(rightSelector);
|
---|
| 349 | remainingProc.AddSubOperator(leftReducer);
|
---|
| 350 | seq.AddSubOperator(mergingReducer);
|
---|
| 351 | seq.AddSubOperator(sorter);
|
---|
| 352 | replacement.OperatorGraph.AddOperator(seq);
|
---|
| 353 | replacement.OperatorGraph.InitialOperator = seq;
|
---|
| 354 | return replacement;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[1287] | 357 | protected internal virtual IOperator CreateChildCreater() {
|
---|
[1230] | 358 | CombinedOperator childCreater = new CombinedOperator();
|
---|
| 359 | childCreater.Name = "Create children";
|
---|
| 360 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 361 | OperatorExtractor selector = new OperatorExtractor();
|
---|
| 362 | selector.Name = "Selector (extr.)";
|
---|
| 363 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
[1335] | 364 |
|
---|
[1230] | 365 | SequentialSubScopesProcessor seqScopesProc = new SequentialSubScopesProcessor();
|
---|
| 366 | EmptyOperator emptyOpt = new EmptyOperator();
|
---|
| 367 | SequentialProcessor selectedProc = new SequentialProcessor();
|
---|
[1335] | 368 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
| 369 | ((IntData)childInitializer.GetVariable("ParentsPerChild").Value).Data = 2;
|
---|
| 370 |
|
---|
[1230] | 371 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
| 372 | crossover.Name = "Crossover (extr.)";
|
---|
| 373 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
| 374 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
| 375 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
| 376 | StochasticBranch cond = new StochasticBranch();
|
---|
| 377 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 378 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
| 379 | manipulator.Name = "Manipulator (extr.)";
|
---|
| 380 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
| 381 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 382 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 383 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 384 | MeanSquaredErrorEvaluator validationEvaluator = new MeanSquaredErrorEvaluator();
|
---|
| 385 | validationEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
| 386 | validationEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
| 387 | validationEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
| 388 | Counter evalCounter = new Counter();
|
---|
| 389 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
[1335] | 390 | SubScopesRemover parentRefRemover = new SubScopesRemover();
|
---|
[1230] | 391 |
|
---|
| 392 | Sorter sorter = new Sorter();
|
---|
| 393 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 394 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | seq.AddSubOperator(selector);
|
---|
| 398 | seq.AddSubOperator(seqScopesProc);
|
---|
| 399 | seqScopesProc.AddSubOperator(emptyOpt);
|
---|
| 400 | seqScopesProc.AddSubOperator(selectedProc);
|
---|
[1335] | 401 | selectedProc.AddSubOperator(childInitializer);
|
---|
[1230] | 402 | selectedProc.AddSubOperator(individualProc);
|
---|
| 403 | individualProc.AddSubOperator(individualSeqProc);
|
---|
[1335] | 404 | individualSeqProc.AddSubOperator(crossover);
|
---|
[1230] | 405 | individualSeqProc.AddSubOperator(cond);
|
---|
| 406 | cond.AddSubOperator(manipulator);
|
---|
| 407 | individualSeqProc.AddSubOperator(evaluator);
|
---|
| 408 | individualSeqProc.AddSubOperator(validationEvaluator);
|
---|
| 409 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
[1335] | 410 | individualSeqProc.AddSubOperator(parentRefRemover);
|
---|
[1230] | 411 | selectedProc.AddSubOperator(sorter);
|
---|
| 412 |
|
---|
| 413 | childCreater.OperatorGraph.AddOperator(seq);
|
---|
| 414 | childCreater.OperatorGraph.InitialOperator = seq;
|
---|
| 415 | return childCreater;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[1906] | 418 | protected internal virtual Model CreateGPModel(IScope bestModelScope) {
|
---|
[2057] | 419 | Engine.GlobalScope.AddSubScope(bestModelScope);
|
---|
[1906] | 420 | Model model = new Model();
|
---|
| 421 | Dataset ds = bestModelScope.GetVariableValue<Dataset>("Dataset", true);
|
---|
[2222] | 422 | model.Data = bestModelScope.GetVariableValue<IGeneticProgrammingModel>("FunctionTree", false);
|
---|
[1906] | 423 | model.Dataset = ds;
|
---|
| 424 | model.TargetVariable = ds.GetVariableName(bestModelScope.GetVariableValue<IntData>("TargetVariable", true).Data);
|
---|
| 425 | model.TrainingMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("Quality", false).Data;
|
---|
| 426 | model.ValidationMeanSquaredError = bestModelScope.GetVariableValue<DoubleData>("ValidationQuality", false).Data;
|
---|
[2041] | 427 | // calculate and set variable impacts
|
---|
| 428 | VariableEvaluationImpactCalculator evaluationImpactCalculator = new VariableEvaluationImpactCalculator();
|
---|
[2242] | 429 | evaluationImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
| 430 | evaluationImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
[2041] | 431 | VariableQualityImpactCalculator qualityImpactCalculator = new VariableQualityImpactCalculator();
|
---|
[2242] | 432 | qualityImpactCalculator.GetVariableInfo("TrainingSamplesStart").ActualName = "ActualTrainingSamplesStart";
|
---|
| 433 | qualityImpactCalculator.GetVariableInfo("TrainingSamplesEnd").ActualName = "ActualTrainingSamplesEnd";
|
---|
| 434 |
|
---|
[2041] | 435 |
|
---|
| 436 | evaluationImpactCalculator.Apply(bestModelScope);
|
---|
| 437 | qualityImpactCalculator.Apply(bestModelScope);
|
---|
| 438 |
|
---|
| 439 | ItemList evaluationImpacts = bestModelScope.GetVariableValue<ItemList>("VariableEvaluationImpacts", false);
|
---|
| 440 | ItemList qualityImpacts = bestModelScope.GetVariableValue<ItemList>("VariableQualityImpacts", false);
|
---|
| 441 | foreach (ItemList row in evaluationImpacts) {
|
---|
| 442 | string variableName = ((StringData)row[0]).Data;
|
---|
[2051] | 443 | double impact = ((DoubleData)row[1]).Data;
|
---|
[2041] | 444 | model.SetVariableEvaluationImpact(variableName, impact);
|
---|
[2223] | 445 | model.AddInputVariables(variableName);
|
---|
[2041] | 446 | }
|
---|
| 447 | foreach (ItemList row in qualityImpacts) {
|
---|
| 448 | string variableName = ((StringData)row[0]).Data;
|
---|
[2051] | 449 | double impact = ((DoubleData)row[1]).Data;
|
---|
[2041] | 450 | model.SetVariableQualityImpact(variableName, impact);
|
---|
[2223] | 451 | model.AddInputVariables(variableName);
|
---|
[2041] | 452 | }
|
---|
[2057] | 453 | Engine.GlobalScope.RemoveSubScope(bestModelScope);
|
---|
[1906] | 454 | return model;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[1230] | 457 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 458 | AlgorithmBase clone = (AlgorithmBase)base.Clone(clonedObjects);
|
---|
| 459 | clonedObjects.Add(Guid, clone);
|
---|
| 460 | clone.engine = (SequentialEngine.SequentialEngine)Auxiliary.Clone(Engine, clonedObjects);
|
---|
| 461 | return clone;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[1287] | 464 | protected VariableInjector GetVariableInjector() {
|
---|
[1230] | 465 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
[1287] | 466 | // SequentialProcessor in GP
|
---|
[1230] | 467 | algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
[1287] | 468 | return (VariableInjector)algorithm.SubOperators[2];
|
---|
[1230] | 469 | }
|
---|
| 470 |
|
---|
[1287] | 471 | protected RandomInjector GetRandomInjector() {
|
---|
| 472 | CombinedOperator co1 = (CombinedOperator)Engine.OperatorGraph.InitialOperator;
|
---|
| 473 | // SequentialProcessor in GP
|
---|
| 474 | algorithm = (SequentialProcessor)co1.OperatorGraph.InitialOperator;
|
---|
[2161] | 475 | return (RandomInjector)algorithm.SubOperators[0];
|
---|
[1287] | 476 | }
|
---|
| 477 |
|
---|
[1230] | 478 | #region Persistence Methods
|
---|
| 479 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 480 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 481 | node.AppendChild(PersistenceManager.Persist("Engine", Engine, document, persistedObjects));
|
---|
| 482 | return node;
|
---|
| 483 | }
|
---|
| 484 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 485 | base.Populate(node, restoredObjects);
|
---|
| 486 | engine = (SequentialEngine.SequentialEngine)PersistenceManager.Restore(node.SelectSingleNode("Engine"), restoredObjects);
|
---|
| 487 | }
|
---|
| 488 | #endregion
|
---|
[1857] | 489 |
|
---|
[1922] | 490 |
|
---|
[1230] | 491 | }
|
---|
| 492 | }
|
---|