[1156] | 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;
|
---|
| 35 | using HeuristicLab.Operators.Programmable;
|
---|
[1287] | 36 | using HeuristicLab.Selection.OffspringSelection;
|
---|
[1347] | 37 | using HeuristicLab.Evolutionary;
|
---|
[1156] | 38 |
|
---|
| 39 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
[1287] | 40 | public class OffspringSelectionGP : StandardGP {
|
---|
[1857] | 41 | public override string Name { get { return "OffspringSelectionGP"; } }
|
---|
[1156] | 42 |
|
---|
[1287] | 43 | public virtual int MaxEvaluatedSolutions {
|
---|
| 44 | get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
|
---|
| 45 | set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
|
---|
[1156] | 46 | }
|
---|
| 47 |
|
---|
[1287] | 48 | public virtual double SelectionPressureLimit {
|
---|
| 49 | get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
|
---|
| 50 | set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 51 | }
|
---|
| 52 |
|
---|
[1287] | 53 | public virtual double ComparisonFactor {
|
---|
| 54 | get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
|
---|
| 55 | set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 56 | }
|
---|
| 57 |
|
---|
[1287] | 58 | public virtual double SuccessRatioLimit {
|
---|
| 59 | get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
|
---|
| 60 | set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 61 | }
|
---|
| 62 |
|
---|
[1287] | 63 | public override int MaxGenerations {
|
---|
| 64 | get { throw new NotSupportedException(); }
|
---|
| 65 | set { /* ignore */ }
|
---|
[1156] | 66 | }
|
---|
| 67 |
|
---|
[1287] | 68 | public override int TournamentSize {
|
---|
| 69 | get { throw new NotSupportedException(); }
|
---|
| 70 | set { /* ignore */ }
|
---|
[1156] | 71 | }
|
---|
| 72 |
|
---|
[1287] | 73 | public OffspringSelectionGP()
|
---|
| 74 | : base() {
|
---|
[1156] | 75 | PopulationSize = 1000;
|
---|
[1287] | 76 | Parents = 20;
|
---|
| 77 | MaxEvaluatedSolutions = 1000000;
|
---|
| 78 | SelectionPressureLimit = 300;
|
---|
| 79 | ComparisonFactor = 1.0;
|
---|
| 80 | SuccessRatioLimit = 1.0;
|
---|
[1156] | 81 | }
|
---|
| 82 |
|
---|
[1287] | 83 | protected internal override IOperator CreateGlobalInjector() {
|
---|
| 84 | VariableInjector injector = (VariableInjector)base.CreateGlobalInjector();
|
---|
| 85 | injector.RemoveVariable("TournamentSize");
|
---|
| 86 | injector.RemoveVariable("MaxGenerations");
|
---|
| 87 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData()));
|
---|
| 88 | injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData()));
|
---|
| 89 | injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressureLimit", new DoubleData()));
|
---|
| 90 | injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
|
---|
[1156] | 91 | return injector;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[1287] | 94 | protected internal override IOperator CreateSelector() {
|
---|
| 95 | CombinedOperator selector = new CombinedOperator();
|
---|
| 96 | selector.Name = "Selector";
|
---|
[1156] | 97 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 98 | seq.Name = "Selector";
|
---|
| 99 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 100 | ProportionalSelector femaleSelector = new ProportionalSelector();
|
---|
| 101 | femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 102 | femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
[1156] | 103 |
|
---|
[1287] | 104 | RandomSelector maleSelector = new RandomSelector();
|
---|
| 105 | maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 106 | maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
| 107 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 108 | RightChildReducer rightChildReducer = new RightChildReducer();
|
---|
| 109 | SubScopesMixer mixer = new SubScopesMixer();
|
---|
[1156] | 110 |
|
---|
[1287] | 111 | seqSubScopesProc.AddSubOperator(femaleSelector);
|
---|
| 112 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1156] | 113 |
|
---|
[1287] | 114 | seq.AddSubOperator(maleSelector);
|
---|
| 115 | seq.AddSubOperator(seqSubScopesProc);
|
---|
| 116 | seq.AddSubOperator(rightChildReducer);
|
---|
| 117 | seq.AddSubOperator(mixer);
|
---|
[1156] | 118 |
|
---|
[1287] | 119 | selector.OperatorGraph.AddOperator(seq);
|
---|
| 120 | selector.OperatorGraph.InitialOperator = seq;
|
---|
| 121 | return selector;
|
---|
[1156] | 122 | }
|
---|
| 123 |
|
---|
[1287] | 124 | protected internal override IOperator CreateChildCreater() {
|
---|
[1156] | 125 | CombinedOperator childCreater = new CombinedOperator();
|
---|
[1287] | 126 | childCreater.Name = "Create children";
|
---|
[1347] | 127 | SequentialProcessor main = new SequentialProcessor();
|
---|
[1156] | 128 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 129 | SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
|
---|
[1156] | 130 | OperatorExtractor selector = new OperatorExtractor();
|
---|
| 131 | selector.Name = "Selector (extr.)";
|
---|
| 132 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
[1287] | 133 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 134 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 135 | OffspringSelector offspringSelector = new OffspringSelector();
|
---|
[1347] | 136 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
| 137 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
| 138 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
[1156] | 139 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
| 140 | crossover.Name = "Crossover (extr.)";
|
---|
| 141 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
| 142 | StochasticBranch cond = new StochasticBranch();
|
---|
| 143 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 144 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
| 145 | manipulator.Name = "Manipulator (extr.)";
|
---|
| 146 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
| 147 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 148 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 149 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 150 | Counter evalCounter = new Counter();
|
---|
| 151 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
[1347] | 152 | WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
|
---|
| 153 | SubScopesRemover parentScopesRemover = new SubScopesRemover();
|
---|
[1156] | 154 |
|
---|
| 155 | Sorter sorter = new Sorter();
|
---|
| 156 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 157 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 158 |
|
---|
[1287] | 159 | UniformSequentialSubScopesProcessor validationEvaluator = new UniformSequentialSubScopesProcessor();
|
---|
| 160 | MeanSquaredErrorEvaluator validationQualityEvaluator = new MeanSquaredErrorEvaluator();
|
---|
| 161 | validationQualityEvaluator.Name = "ValidationMeanSquaredErrorEvaluator";
|
---|
| 162 | validationQualityEvaluator.GetVariableInfo("MSE").ActualName = "ValidationQuality";
|
---|
| 163 | validationQualityEvaluator.GetVariableInfo("SamplesStart").ActualName = "ValidationSamplesStart";
|
---|
| 164 | validationQualityEvaluator.GetVariableInfo("SamplesEnd").ActualName = "ValidationSamplesEnd";
|
---|
[1156] | 165 |
|
---|
[1347] | 166 | main.AddSubOperator(seq);
|
---|
| 167 | seq.AddSubOperator(selector);
|
---|
| 168 | seq.AddSubOperator(seqSubScopesProc);
|
---|
[1287] | 169 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1347] | 170 | seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
|
---|
| 171 | seq.AddSubOperator(offspringSelector);
|
---|
| 172 | offspringSelector.AddSubOperator(seq);
|
---|
[1287] | 173 |
|
---|
[1347] | 174 | offspringSelectionSeq.AddSubOperator(childInitializer);
|
---|
| 175 | offspringSelectionSeq.AddSubOperator(individualProc);
|
---|
| 176 | offspringSelectionSeq.AddSubOperator(sorter);
|
---|
[1287] | 177 |
|
---|
[1156] | 178 | individualProc.AddSubOperator(individualSeqProc);
|
---|
[1347] | 179 | individualSeqProc.AddSubOperator(crossover);
|
---|
[1156] | 180 | individualSeqProc.AddSubOperator(cond);
|
---|
| 181 | cond.AddSubOperator(manipulator);
|
---|
| 182 | individualSeqProc.AddSubOperator(evaluator);
|
---|
| 183 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
[1347] | 184 | individualSeqProc.AddSubOperator(offspringFitnessComparer);
|
---|
| 185 | individualSeqProc.AddSubOperator(parentScopesRemover);
|
---|
[1156] | 186 |
|
---|
[1287] | 187 | SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
|
---|
[1347] | 188 | main.AddSubOperator(seqSubScopesProc2);
|
---|
[1287] | 189 | seqSubScopesProc2.AddSubOperator(emptyOp);
|
---|
| 190 |
|
---|
| 191 | SequentialProcessor newGenProc = new SequentialProcessor();
|
---|
| 192 | newGenProc.AddSubOperator(sorter);
|
---|
| 193 | newGenProc.AddSubOperator(validationEvaluator);
|
---|
| 194 | seqSubScopesProc2.AddSubOperator(newGenProc);
|
---|
| 195 |
|
---|
| 196 | validationEvaluator.AddSubOperator(validationQualityEvaluator);
|
---|
| 197 |
|
---|
[1347] | 198 | childCreater.OperatorGraph.AddOperator(main);
|
---|
| 199 | childCreater.OperatorGraph.InitialOperator = main;
|
---|
[1156] | 200 | return childCreater;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[1287] | 203 | protected internal override IOperator CreateLoopCondition(IOperator loop) {
|
---|
| 204 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 205 | seq.Name = "Loop Condition";
|
---|
| 206 | LessThanComparator generationsComparator = new LessThanComparator();
|
---|
| 207 | generationsComparator.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
|
---|
| 208 | generationsComparator.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
|
---|
| 209 | generationsComparator.GetVariableInfo("Result").ActualName = "EvaluatedSolutionsCondition";
|
---|
| 210 |
|
---|
| 211 | LessThanComparator selPresComparator = new LessThanComparator();
|
---|
| 212 | selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
|
---|
| 213 | selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
|
---|
| 214 | selPresComparator.GetVariableInfo("Result").ActualName = "SelectionPressureCondition";
|
---|
| 215 |
|
---|
| 216 | ConditionalBranch generationsCond = new ConditionalBranch();
|
---|
| 217 | generationsCond.GetVariableInfo("Condition").ActualName = "EvaluatedSolutionsCondition";
|
---|
| 218 |
|
---|
| 219 | ConditionalBranch selPresCond = new ConditionalBranch();
|
---|
| 220 | selPresCond.GetVariableInfo("Condition").ActualName = "SelectionPressureCondition";
|
---|
| 221 |
|
---|
| 222 | seq.AddSubOperator(generationsComparator);
|
---|
| 223 | seq.AddSubOperator(selPresComparator);
|
---|
| 224 | seq.AddSubOperator(generationsCond);
|
---|
| 225 | generationsCond.AddSubOperator(selPresCond);
|
---|
| 226 | selPresCond.AddSubOperator(loop);
|
---|
| 227 |
|
---|
| 228 | return seq;
|
---|
[1156] | 229 | }
|
---|
| 230 |
|
---|
[1287] | 231 | protected internal override IOperator CreateLoggingOperator() {
|
---|
| 232 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
| 233 | loggingOperator.Name = "Logging";
|
---|
| 234 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 235 |
|
---|
| 236 | DataCollector collector = new DataCollector();
|
---|
| 237 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 238 | names.Add(new StringData("BestQuality"));
|
---|
| 239 | names.Add(new StringData("AverageQuality"));
|
---|
| 240 | names.Add(new StringData("WorstQuality"));
|
---|
| 241 | names.Add(new StringData("BestValidationQuality"));
|
---|
| 242 | names.Add(new StringData("AverageValidationQuality"));
|
---|
| 243 | names.Add(new StringData("WorstValidationQuality"));
|
---|
| 244 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
| 245 | names.Add(new StringData("SelectionPressure"));
|
---|
| 246 | QualityLogger qualityLogger = new QualityLogger();
|
---|
| 247 | QualityLogger validationQualityLogger = new QualityLogger();
|
---|
| 248 | validationQualityLogger.Name = "ValidationQualityLogger";
|
---|
| 249 | validationQualityLogger.GetVariableInfo("Quality").ActualName = "ValidationQuality";
|
---|
| 250 | validationQualityLogger.GetVariableInfo("QualityLog").ActualName = "ValidationQualityLog";
|
---|
| 251 |
|
---|
| 252 | seq.AddSubOperator(collector);
|
---|
| 253 | seq.AddSubOperator(qualityLogger);
|
---|
| 254 | seq.AddSubOperator(validationQualityLogger);
|
---|
| 255 |
|
---|
| 256 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
| 257 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
| 258 | return loggingOperator;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | public override IEditor CreateEditor() {
|
---|
| 263 | return new OffspringSelectionGpEditor(this);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[1156] | 266 | public override IView CreateView() {
|
---|
| 267 | return new OffspringSelectionGpEditor(this);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[1287] | 271 | OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
|
---|
| 272 | clone.SelectionPressureLimit = SelectionPressureLimit;
|
---|
| 273 | clone.SuccessRatioLimit = SuccessRatioLimit;
|
---|
| 274 | clone.ComparisonFactor = ComparisonFactor;
|
---|
[1156] | 275 | return clone;
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|