[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 HeuristicLab.Core;
|
---|
[2222] | 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Evolutionary;
|
---|
| 27 | using HeuristicLab.Logging;
|
---|
[1156] | 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Selection;
|
---|
[1287] | 30 | using HeuristicLab.Selection.OffspringSelection;
|
---|
[1156] | 31 |
|
---|
[2331] | 32 | namespace HeuristicLab.GP.Algorithms {
|
---|
[2335] | 33 | public class OffspringSelectionGP : AlgorithmBase, IEditable {
|
---|
[1857] | 34 | public override string Name { get { return "OffspringSelectionGP"; } }
|
---|
[1156] | 35 |
|
---|
[1287] | 36 | public virtual int MaxEvaluatedSolutions {
|
---|
| 37 | get { return GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data; }
|
---|
| 38 | set { GetVariableInjector().GetVariable("MaxEvaluatedSolutions").GetValue<IntData>().Data = value; }
|
---|
[1156] | 39 | }
|
---|
| 40 |
|
---|
[1287] | 41 | public virtual double SelectionPressureLimit {
|
---|
| 42 | get { return GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data; }
|
---|
| 43 | set { GetVariableInjector().GetVariable("SelectionPressureLimit").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 44 | }
|
---|
| 45 |
|
---|
[1287] | 46 | public virtual double ComparisonFactor {
|
---|
| 47 | get { return GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data; }
|
---|
| 48 | set { GetVariableInjector().GetVariable("ComparisonFactor").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 49 | }
|
---|
| 50 |
|
---|
[1287] | 51 | public virtual double SuccessRatioLimit {
|
---|
| 52 | get { return GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data; }
|
---|
| 53 | set { GetVariableInjector().GetVariable("SuccessRatioLimit").GetValue<DoubleData>().Data = value; }
|
---|
[1156] | 54 | }
|
---|
| 55 |
|
---|
[1287] | 56 | public OffspringSelectionGP()
|
---|
| 57 | : base() {
|
---|
[1156] | 58 | PopulationSize = 1000;
|
---|
[1287] | 59 | Parents = 20;
|
---|
[2335] | 60 | MaxGenerations = 10000; // something large to make sure we either stop because of max-evaluated soltions or selection pressure limit
|
---|
[2130] | 61 | MaxEvaluatedSolutions = 5000000;
|
---|
| 62 | SelectionPressureLimit = 400;
|
---|
[1287] | 63 | ComparisonFactor = 1.0;
|
---|
| 64 | SuccessRatioLimit = 1.0;
|
---|
[1156] | 65 | }
|
---|
| 66 |
|
---|
[2335] | 67 | protected override VariableInjector CreateGlobalInjector() {
|
---|
| 68 | VariableInjector injector = base.CreateGlobalInjector();
|
---|
[1287] | 69 | injector.AddVariable(new HeuristicLab.Core.Variable("MaxEvaluatedSolutions", new IntData()));
|
---|
| 70 | injector.AddVariable(new HeuristicLab.Core.Variable("ComparisonFactor", new DoubleData()));
|
---|
| 71 | injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressureLimit", new DoubleData()));
|
---|
[2683] | 72 | injector.AddVariable(new HeuristicLab.Core.Variable("SelectionPressure", new DoubleData(1)));
|
---|
[1287] | 73 | injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
|
---|
[1156] | 74 | return injector;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[2335] | 77 | protected override IOperator CreateSelectionOperator() {
|
---|
[1287] | 78 | CombinedOperator selector = new CombinedOperator();
|
---|
| 79 | selector.Name = "Selector";
|
---|
[1156] | 80 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 81 | seq.Name = "Selector";
|
---|
| 82 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 83 | ProportionalSelector femaleSelector = new ProportionalSelector();
|
---|
| 84 | femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 85 | femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
[1156] | 86 |
|
---|
[1287] | 87 | RandomSelector maleSelector = new RandomSelector();
|
---|
| 88 | maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 89 | maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
| 90 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 91 | RightChildReducer rightChildReducer = new RightChildReducer();
|
---|
| 92 | SubScopesMixer mixer = new SubScopesMixer();
|
---|
[1156] | 93 |
|
---|
[1287] | 94 | seqSubScopesProc.AddSubOperator(femaleSelector);
|
---|
| 95 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1156] | 96 |
|
---|
[1287] | 97 | seq.AddSubOperator(maleSelector);
|
---|
| 98 | seq.AddSubOperator(seqSubScopesProc);
|
---|
| 99 | seq.AddSubOperator(rightChildReducer);
|
---|
| 100 | seq.AddSubOperator(mixer);
|
---|
[1156] | 101 |
|
---|
[1287] | 102 | selector.OperatorGraph.AddOperator(seq);
|
---|
| 103 | selector.OperatorGraph.InitialOperator = seq;
|
---|
| 104 | return selector;
|
---|
[1156] | 105 | }
|
---|
| 106 |
|
---|
[2335] | 107 | protected override IOperator CreateChildCreater() {
|
---|
[1156] | 108 | CombinedOperator childCreater = new CombinedOperator();
|
---|
[1287] | 109 | childCreater.Name = "Create children";
|
---|
[1347] | 110 | SequentialProcessor main = new SequentialProcessor();
|
---|
[1156] | 111 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 112 | SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
|
---|
[1156] | 113 | OperatorExtractor selector = new OperatorExtractor();
|
---|
| 114 | selector.Name = "Selector (extr.)";
|
---|
| 115 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
[1287] | 116 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 117 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 118 | OffspringSelector offspringSelector = new OffspringSelector();
|
---|
[1347] | 119 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
| 120 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
| 121 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
[1156] | 122 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
| 123 | crossover.Name = "Crossover (extr.)";
|
---|
| 124 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
| 125 | StochasticBranch cond = new StochasticBranch();
|
---|
| 126 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 127 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
| 128 | manipulator.Name = "Manipulator (extr.)";
|
---|
| 129 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
| 130 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 131 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 132 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 133 | Counter evalCounter = new Counter();
|
---|
| 134 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
[1347] | 135 | WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
|
---|
| 136 | SubScopesRemover parentScopesRemover = new SubScopesRemover();
|
---|
[1156] | 137 |
|
---|
| 138 | Sorter sorter = new Sorter();
|
---|
| 139 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 140 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 141 |
|
---|
[1347] | 142 | main.AddSubOperator(seq);
|
---|
| 143 | seq.AddSubOperator(selector);
|
---|
| 144 | seq.AddSubOperator(seqSubScopesProc);
|
---|
[1287] | 145 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1347] | 146 | seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
|
---|
| 147 | seq.AddSubOperator(offspringSelector);
|
---|
| 148 | offspringSelector.AddSubOperator(seq);
|
---|
[1287] | 149 |
|
---|
[1347] | 150 | offspringSelectionSeq.AddSubOperator(childInitializer);
|
---|
| 151 | offspringSelectionSeq.AddSubOperator(individualProc);
|
---|
| 152 | offspringSelectionSeq.AddSubOperator(sorter);
|
---|
[1287] | 153 |
|
---|
[1156] | 154 | individualProc.AddSubOperator(individualSeqProc);
|
---|
[1347] | 155 | individualSeqProc.AddSubOperator(crossover);
|
---|
[1156] | 156 | individualSeqProc.AddSubOperator(cond);
|
---|
| 157 | cond.AddSubOperator(manipulator);
|
---|
| 158 | individualSeqProc.AddSubOperator(evaluator);
|
---|
| 159 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
[1347] | 160 | individualSeqProc.AddSubOperator(offspringFitnessComparer);
|
---|
| 161 | individualSeqProc.AddSubOperator(parentScopesRemover);
|
---|
[1156] | 162 |
|
---|
[1287] | 163 | SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
|
---|
[1347] | 164 | main.AddSubOperator(seqSubScopesProc2);
|
---|
[1287] | 165 | seqSubScopesProc2.AddSubOperator(emptyOp);
|
---|
[2335] | 166 | seqSubScopesProc2.AddSubOperator(sorter);
|
---|
[1287] | 167 |
|
---|
[1347] | 168 | childCreater.OperatorGraph.AddOperator(main);
|
---|
| 169 | childCreater.OperatorGraph.InitialOperator = main;
|
---|
[1156] | 170 | return childCreater;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[2335] | 173 | protected override IOperator CreateTerminationCondition() {
|
---|
| 174 | CombinedOperator terminationCritertion = new CombinedOperator();
|
---|
| 175 | terminationCritertion.Name = "TerminationCondition";
|
---|
| 176 | GreaterThanComparator selPresComparator = new GreaterThanComparator();
|
---|
[1287] | 177 | selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
|
---|
| 178 | selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
|
---|
[2335] | 179 | selPresComparator.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
[1287] | 180 |
|
---|
[2385] | 181 | IOperator baseAndSelPresTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), selPresComparator);
|
---|
[1287] | 182 |
|
---|
[2335] | 183 | GreaterThanComparator evalSolutionsComparer = new GreaterThanComparator();
|
---|
| 184 | evalSolutionsComparer.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
|
---|
| 185 | evalSolutionsComparer.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
|
---|
| 186 | evalSolutionsComparer.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
[1287] | 187 |
|
---|
[2385] | 188 | IOperator combinedTerminationCritertion = AlgorithmBase.CombineTerminationCriterions(baseAndSelPresTerminationCriterion, evalSolutionsComparer);
|
---|
[1287] | 189 |
|
---|
[2335] | 190 | terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCritertion);
|
---|
| 191 | terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCritertion;
|
---|
| 192 | return terminationCritertion;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | protected override IOperator CreateLoggingOperator() {
|
---|
[1287] | 196 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
| 197 | loggingOperator.Name = "Logging";
|
---|
| 198 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 199 |
|
---|
| 200 | DataCollector collector = new DataCollector();
|
---|
| 201 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 202 | names.Add(new StringData("BestQuality"));
|
---|
| 203 | names.Add(new StringData("AverageQuality"));
|
---|
| 204 | names.Add(new StringData("WorstQuality"));
|
---|
| 205 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
| 206 | names.Add(new StringData("SelectionPressure"));
|
---|
| 207 | QualityLogger qualityLogger = new QualityLogger();
|
---|
| 208 | seq.AddSubOperator(collector);
|
---|
| 209 | seq.AddSubOperator(qualityLogger);
|
---|
| 210 |
|
---|
| 211 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
| 212 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
| 213 | return loggingOperator;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[2335] | 216 | public virtual IEditor CreateEditor() {
|
---|
[1287] | 217 | return new OffspringSelectionGpEditor(this);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[1156] | 220 | public override IView CreateView() {
|
---|
| 221 | return new OffspringSelectionGpEditor(this);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[1287] | 225 | OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
|
---|
| 226 | clone.SelectionPressureLimit = SelectionPressureLimit;
|
---|
| 227 | clone.SuccessRatioLimit = SuccessRatioLimit;
|
---|
| 228 | clone.ComparisonFactor = ComparisonFactor;
|
---|
[1156] | 229 | return clone;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|