[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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.DataAnalysis;
|
---|
| 24 | using HeuristicLab.Operators;
|
---|
| 25 | using HeuristicLab.Selection;
|
---|
| 26 | using HeuristicLab.Logging;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[1052] | 28 | using HeuristicLab.Operators.Programmable;
|
---|
[1856] | 29 | using HeuristicLab.Modeling;
|
---|
[2222] | 30 | using HeuristicLab.GP.Operators;
|
---|
[1050] | 31 |
|
---|
| 32 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[1287] | 33 | public class StandardGP : AlgorithmBase, IEditable {
|
---|
| 34 |
|
---|
[1857] | 35 | public override string Name { get { return "StandardGP"; } }
|
---|
| 36 |
|
---|
[1922] | 37 | public override int TargetVariable {
|
---|
| 38 | get { return ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data; }
|
---|
| 39 | set { ProblemInjector.GetVariableValue<IntData>("TargetVariable", null, false).Data = value; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public override Dataset Dataset {
|
---|
| 43 | get { return ProblemInjector.GetVariableValue<Dataset>("Dataset", null, false); }
|
---|
| 44 | set { ProblemInjector.GetVariable("Dataset").Value = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[1287] | 47 | public virtual int MaxGenerations {
|
---|
| 48 | get { return GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data; }
|
---|
| 49 | set { GetVariableInjector().GetVariable("MaxGenerations").GetValue<IntData>().Data = value; }
|
---|
[1050] | 50 | }
|
---|
[1051] | 51 |
|
---|
[1287] | 52 | public virtual int TournamentSize {
|
---|
| 53 | get { return GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data; }
|
---|
| 54 | set { GetVariableInjector().GetVariable("TournamentSize").GetValue<IntData>().Data = value; }
|
---|
[1050] | 55 | }
|
---|
[1287] | 56 |
|
---|
| 57 | public double FullTreeShakingFactor {
|
---|
| 58 | get { return GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data; }
|
---|
| 59 | set { GetVariableInjector().GetVariable("FullTreeShakingFactor").GetValue<DoubleData>().Data = value; }
|
---|
[1050] | 60 | }
|
---|
| 61 |
|
---|
[1287] | 62 | public double OnePointShakingFactor {
|
---|
| 63 | get { return GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data; }
|
---|
| 64 | set { GetVariableInjector().GetVariable("OnePointShakingFactor").GetValue<DoubleData>().Data = value; }
|
---|
[1051] | 65 | }
|
---|
| 66 |
|
---|
[1287] | 67 | public int MinInitialTreeSize {
|
---|
| 68 | get { return GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data; }
|
---|
| 69 | set { GetVariableInjector().GetVariable("MinInitialTreeSize").GetValue<IntData>().Data = value; }
|
---|
[1051] | 70 | }
|
---|
| 71 |
|
---|
[1287] | 72 | public override int MaxTreeSize {
|
---|
| 73 | get {
|
---|
| 74 | return base.MaxTreeSize;
|
---|
| 75 | }
|
---|
[1051] | 76 | set {
|
---|
[1287] | 77 | base.MaxTreeSize = value;
|
---|
| 78 | MinInitialTreeSize = value / 2;
|
---|
[1051] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[1287] | 82 | public override int PopulationSize {
|
---|
| 83 | get {
|
---|
| 84 | return base.PopulationSize;
|
---|
| 85 | }
|
---|
| 86 | set {
|
---|
| 87 | base.PopulationSize = value;
|
---|
| 88 | Parents = 2 * value;
|
---|
| 89 | }
|
---|
[1051] | 90 | }
|
---|
| 91 |
|
---|
[2161] | 92 | public override IOperator ProblemInjector {
|
---|
| 93 | get { return base.ProblemInjector.SubOperators[0]; }
|
---|
| 94 | set {
|
---|
| 95 | value.Name = "ProblemInjector";
|
---|
| 96 | base.ProblemInjector.RemoveSubOperator(0);
|
---|
| 97 | base.ProblemInjector.AddSubOperator(value, 0);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[1287] | 101 | public StandardGP()
|
---|
| 102 | : base() {
|
---|
| 103 | PopulationSize = 10000;
|
---|
[2130] | 104 | MaxGenerations = 500;
|
---|
[1050] | 105 | TournamentSize = 7;
|
---|
| 106 | MutationRate = 0.15;
|
---|
[1052] | 107 | Elites = 1;
|
---|
[1287] | 108 | MaxTreeSize = 100;
|
---|
| 109 | MaxTreeHeight = 10;
|
---|
| 110 | FullTreeShakingFactor = 0.1;
|
---|
| 111 | OnePointShakingFactor = 1.0;
|
---|
| 112 | UseEstimatedTargetValue = false;
|
---|
| 113 | SetSeedRandomly = true;
|
---|
[1050] | 114 | }
|
---|
| 115 |
|
---|
[1287] | 116 | protected internal override IOperator CreateProblemInjector() {
|
---|
[2328] | 117 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[2161] | 118 | var probInject = new ProblemInjector();
|
---|
| 119 | probInject.GetVariableInfo("MaxNumberOfTrainingSamples").Local = true;
|
---|
| 120 | probInject.AddVariable(new HeuristicLab.Core.Variable("MaxNumberOfTrainingSamples", new IntData(5000)));
|
---|
| 121 |
|
---|
| 122 | var shuffler = new DatasetShuffler();
|
---|
| 123 | shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";
|
---|
| 124 | shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";
|
---|
| 125 |
|
---|
| 126 | seq.AddSubOperator(probInject);
|
---|
| 127 | seq.AddSubOperator(shuffler);
|
---|
| 128 | return seq;
|
---|
[1287] | 129 | }
|
---|
[1050] | 130 |
|
---|
[1287] | 131 | protected internal override IOperator CreateSelector() {
|
---|
[1050] | 132 | TournamentSelector selector = new TournamentSelector();
|
---|
| 133 | selector.Name = "Selector";
|
---|
| 134 | selector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 135 | selector.GetVariableInfo("GroupSize").Local = false;
|
---|
| 136 | selector.RemoveVariable("GroupSize");
|
---|
| 137 | selector.GetVariableInfo("GroupSize").ActualName = "TournamentSize";
|
---|
[1287] | 138 | return selector;
|
---|
| 139 | }
|
---|
[1050] | 140 |
|
---|
[1287] | 141 | protected internal override IOperator CreateGlobalInjector() {
|
---|
| 142 | VariableInjector globalInjector = (VariableInjector)base.CreateGlobalInjector();
|
---|
| 143 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("TournamentSize", new IntData()));
|
---|
| 144 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("MaxGenerations", new IntData()));
|
---|
| 145 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("FullTreeShakingFactor", new DoubleData()));
|
---|
| 146 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("OnePointShakingFactor", new DoubleData()));
|
---|
| 147 | globalInjector.AddVariable(new HeuristicLab.Core.Variable("MinInitialTreeSize", new IntData()));
|
---|
| 148 | return globalInjector;
|
---|
| 149 | }
|
---|
[1050] | 150 |
|
---|
[1287] | 151 | protected internal override IOperator CreateCrossover() {
|
---|
| 152 | StandardCrossOver crossover = new StandardCrossOver();
|
---|
| 153 | crossover.Name = "Crossover";
|
---|
| 154 | return crossover;
|
---|
| 155 | }
|
---|
[1050] | 156 |
|
---|
[1287] | 157 | protected internal override IOperator CreateTreeCreator() {
|
---|
| 158 | ProbabilisticTreeCreator treeCreator = new ProbabilisticTreeCreator();
|
---|
| 159 | treeCreator.Name = "Tree generator";
|
---|
| 160 | treeCreator.GetVariableInfo("MinTreeSize").ActualName = "MinInitialTreeSize";
|
---|
| 161 | return treeCreator;
|
---|
[1050] | 162 | }
|
---|
| 163 |
|
---|
[1287] | 164 | protected internal override IOperator CreateFunctionLibraryInjector() {
|
---|
[1908] | 165 | FunctionLibraryInjector funLibInjector = new FunctionLibraryInjector();
|
---|
[1873] | 166 | funLibInjector.GetVariableValue<BoolData>("Xor", null, false).Data = false;
|
---|
| 167 | funLibInjector.GetVariableValue<BoolData>("Average", null, false).Data = false;
|
---|
| 168 | return funLibInjector;
|
---|
[1050] | 169 | }
|
---|
| 170 |
|
---|
[1287] | 171 | protected internal override IOperator CreateManipulator() {
|
---|
[1050] | 172 | CombinedOperator manipulator = new CombinedOperator();
|
---|
[1287] | 173 | manipulator.Name = "Manipulator";
|
---|
[1050] | 174 | StochasticMultiBranch multibranch = new StochasticMultiBranch();
|
---|
| 175 | FullTreeShaker fullTreeShaker = new FullTreeShaker();
|
---|
[1287] | 176 | fullTreeShaker.GetVariableInfo("ShakingFactor").ActualName = "FullTreeShakingFactor";
|
---|
[1050] | 177 |
|
---|
| 178 | OnePointShaker onepointShaker = new OnePointShaker();
|
---|
[1287] | 179 | onepointShaker.GetVariableInfo("ShakingFactor").ActualName = "OnePointShakingFactor";
|
---|
[1050] | 180 | ChangeNodeTypeManipulation changeNodeTypeManipulation = new ChangeNodeTypeManipulation();
|
---|
| 181 | CutOutNodeManipulation cutOutNodeManipulation = new CutOutNodeManipulation();
|
---|
| 182 | DeleteSubTreeManipulation deleteSubTreeManipulation = new DeleteSubTreeManipulation();
|
---|
| 183 | SubstituteSubTreeManipulation substituteSubTreeManipulation = new SubstituteSubTreeManipulation();
|
---|
| 184 |
|
---|
[1051] | 185 | IOperator[] manipulators = new IOperator[] {
|
---|
| 186 | onepointShaker, fullTreeShaker,
|
---|
| 187 | changeNodeTypeManipulation,
|
---|
| 188 | cutOutNodeManipulation,
|
---|
| 189 | deleteSubTreeManipulation,
|
---|
| 190 | substituteSubTreeManipulation};
|
---|
| 191 |
|
---|
| 192 | DoubleArrayData probabilities = new DoubleArrayData(new double[manipulators.Length]);
|
---|
[1287] | 193 | for (int i = 0; i < manipulators.Length; i++) {
|
---|
[1051] | 194 | probabilities.Data[i] = 1.0;
|
---|
| 195 | multibranch.AddSubOperator(manipulators[i]);
|
---|
| 196 | }
|
---|
| 197 | multibranch.GetVariableInfo("Probabilities").Local = true;
|
---|
| 198 | multibranch.AddVariable(new HeuristicLab.Core.Variable("Probabilities", probabilities));
|
---|
| 199 |
|
---|
[1050] | 200 | manipulator.OperatorGraph.AddOperator(multibranch);
|
---|
| 201 | manipulator.OperatorGraph.InitialOperator = multibranch;
|
---|
| 202 | return manipulator;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[1287] | 205 | protected internal override IOperator CreateLoggingOperator() {
|
---|
| 206 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
| 207 | loggingOperator.Name = "Logging";
|
---|
[1050] | 208 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 209 |
|
---|
[1050] | 210 | DataCollector collector = new DataCollector();
|
---|
| 211 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 212 | names.Add(new StringData("BestQuality"));
|
---|
| 213 | names.Add(new StringData("AverageQuality"));
|
---|
| 214 | names.Add(new StringData("WorstQuality"));
|
---|
[1052] | 215 | names.Add(new StringData("BestValidationQuality"));
|
---|
| 216 | names.Add(new StringData("AverageValidationQuality"));
|
---|
| 217 | names.Add(new StringData("WorstValidationQuality"));
|
---|
[1050] | 218 | LinechartInjector lineChartInjector = new LinechartInjector();
|
---|
| 219 | lineChartInjector.GetVariableInfo("Linechart").ActualName = "Quality Linechart";
|
---|
[1052] | 220 | lineChartInjector.GetVariable("NumberOfLines").GetValue<IntData>().Data = 6;
|
---|
[1050] | 221 | QualityLogger qualityLogger = new QualityLogger();
|
---|
[1052] | 222 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
[1287] | 223 | validationQualityLogger.Name = "ValidationQualityLogger";
|
---|
[1052] | 224 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 225 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
[1051] | 226 |
|
---|
| 227 | seq.AddSubOperator(collector);
|
---|
| 228 | seq.AddSubOperator(lineChartInjector);
|
---|
| 229 | seq.AddSubOperator(qualityLogger);
|
---|
[1052] | 230 | seq.AddSubOperator(validationQualityLogger);
|
---|
[1051] | 231 |
|
---|
[1287] | 232 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
| 233 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
| 234 | return loggingOperator;
|
---|
[1050] | 235 | }
|
---|
| 236 |
|
---|
[1287] | 237 | public virtual IEditor CreateEditor() {
|
---|
[1051] | 238 | return new StandardGpEditor(this);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[1052] | 241 | public override IView CreateView() {
|
---|
[1051] | 242 | return new StandardGpEditor(this);
|
---|
| 243 | }
|
---|
[1050] | 244 | }
|
---|
| 245 | }
|
---|