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