[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()));
|
---|
| 72 | injector.AddVariable(new HeuristicLab.Core.Variable("SuccessRatioLimit", new DoubleData()));
|
---|
[1156] | 73 | return injector;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2335] | 76 | protected override IOperator CreateSelectionOperator() {
|
---|
[1287] | 77 | CombinedOperator selector = new CombinedOperator();
|
---|
| 78 | selector.Name = "Selector";
|
---|
[1156] | 79 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 80 | seq.Name = "Selector";
|
---|
| 81 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 82 | ProportionalSelector femaleSelector = new ProportionalSelector();
|
---|
| 83 | femaleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 84 | femaleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
[1156] | 85 |
|
---|
[1287] | 86 | RandomSelector maleSelector = new RandomSelector();
|
---|
| 87 | maleSelector.GetVariableInfo("Selected").ActualName = "Parents";
|
---|
| 88 | maleSelector.GetVariableValue<BoolData>("CopySelected", null, false).Data = true;
|
---|
| 89 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 90 | RightChildReducer rightChildReducer = new RightChildReducer();
|
---|
| 91 | SubScopesMixer mixer = new SubScopesMixer();
|
---|
[1156] | 92 |
|
---|
[1287] | 93 | seqSubScopesProc.AddSubOperator(femaleSelector);
|
---|
| 94 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1156] | 95 |
|
---|
[1287] | 96 | seq.AddSubOperator(maleSelector);
|
---|
| 97 | seq.AddSubOperator(seqSubScopesProc);
|
---|
| 98 | seq.AddSubOperator(rightChildReducer);
|
---|
| 99 | seq.AddSubOperator(mixer);
|
---|
[1156] | 100 |
|
---|
[1287] | 101 | selector.OperatorGraph.AddOperator(seq);
|
---|
| 102 | selector.OperatorGraph.InitialOperator = seq;
|
---|
| 103 | return selector;
|
---|
[1156] | 104 | }
|
---|
| 105 |
|
---|
[2335] | 106 | protected override IOperator CreateChildCreater() {
|
---|
[1156] | 107 | CombinedOperator childCreater = new CombinedOperator();
|
---|
[1287] | 108 | childCreater.Name = "Create children";
|
---|
[1347] | 109 | SequentialProcessor main = new SequentialProcessor();
|
---|
[1156] | 110 | SequentialProcessor seq = new SequentialProcessor();
|
---|
[1287] | 111 | SequentialProcessor offspringSelectionSeq = new SequentialProcessor();
|
---|
[1156] | 112 | OperatorExtractor selector = new OperatorExtractor();
|
---|
| 113 | selector.Name = "Selector (extr.)";
|
---|
| 114 | selector.GetVariableInfo("Operator").ActualName = "Selector";
|
---|
[1287] | 115 | SequentialSubScopesProcessor seqSubScopesProc = new SequentialSubScopesProcessor();
|
---|
| 116 | EmptyOperator emptyOp = new EmptyOperator();
|
---|
| 117 | OffspringSelector offspringSelector = new OffspringSelector();
|
---|
[1347] | 118 | ChildrenInitializer childInitializer = new ChildrenInitializer();
|
---|
| 119 | UniformSequentialSubScopesProcessor individualProc = new UniformSequentialSubScopesProcessor();
|
---|
| 120 | SequentialProcessor individualSeqProc = new SequentialProcessor();
|
---|
[1156] | 121 | OperatorExtractor crossover = new OperatorExtractor();
|
---|
| 122 | crossover.Name = "Crossover (extr.)";
|
---|
| 123 | crossover.GetVariableInfo("Operator").ActualName = "Crossover";
|
---|
| 124 | StochasticBranch cond = new StochasticBranch();
|
---|
| 125 | cond.GetVariableInfo("Probability").ActualName = "MutationRate";
|
---|
| 126 | OperatorExtractor manipulator = new OperatorExtractor();
|
---|
| 127 | manipulator.Name = "Manipulator (extr.)";
|
---|
| 128 | manipulator.GetVariableInfo("Operator").ActualName = "Manipulator";
|
---|
| 129 | OperatorExtractor evaluator = new OperatorExtractor();
|
---|
| 130 | evaluator.Name = "Evaluator (extr.)";
|
---|
| 131 | evaluator.GetVariableInfo("Operator").ActualName = "Evaluator";
|
---|
| 132 | Counter evalCounter = new Counter();
|
---|
| 133 | evalCounter.GetVariableInfo("Value").ActualName = "EvaluatedSolutions";
|
---|
[1347] | 134 | WeightedOffspringFitnessComparer offspringFitnessComparer = new WeightedOffspringFitnessComparer();
|
---|
| 135 | SubScopesRemover parentScopesRemover = new SubScopesRemover();
|
---|
[1156] | 136 |
|
---|
| 137 | Sorter sorter = new Sorter();
|
---|
| 138 | sorter.GetVariableInfo("Descending").ActualName = "Maximization";
|
---|
| 139 | sorter.GetVariableInfo("Value").ActualName = "Quality";
|
---|
| 140 |
|
---|
[1347] | 141 | main.AddSubOperator(seq);
|
---|
| 142 | seq.AddSubOperator(selector);
|
---|
| 143 | seq.AddSubOperator(seqSubScopesProc);
|
---|
[1287] | 144 | seqSubScopesProc.AddSubOperator(emptyOp);
|
---|
[1347] | 145 | seqSubScopesProc.AddSubOperator(offspringSelectionSeq);
|
---|
| 146 | seq.AddSubOperator(offspringSelector);
|
---|
| 147 | offspringSelector.AddSubOperator(seq);
|
---|
[1287] | 148 |
|
---|
[1347] | 149 | offspringSelectionSeq.AddSubOperator(childInitializer);
|
---|
| 150 | offspringSelectionSeq.AddSubOperator(individualProc);
|
---|
| 151 | offspringSelectionSeq.AddSubOperator(sorter);
|
---|
[1287] | 152 |
|
---|
[1156] | 153 | individualProc.AddSubOperator(individualSeqProc);
|
---|
[1347] | 154 | individualSeqProc.AddSubOperator(crossover);
|
---|
[1156] | 155 | individualSeqProc.AddSubOperator(cond);
|
---|
| 156 | cond.AddSubOperator(manipulator);
|
---|
| 157 | individualSeqProc.AddSubOperator(evaluator);
|
---|
| 158 | individualSeqProc.AddSubOperator(evalCounter);
|
---|
[1347] | 159 | individualSeqProc.AddSubOperator(offspringFitnessComparer);
|
---|
| 160 | individualSeqProc.AddSubOperator(parentScopesRemover);
|
---|
[1156] | 161 |
|
---|
[1287] | 162 | SequentialSubScopesProcessor seqSubScopesProc2 = new SequentialSubScopesProcessor();
|
---|
[1347] | 163 | main.AddSubOperator(seqSubScopesProc2);
|
---|
[1287] | 164 | seqSubScopesProc2.AddSubOperator(emptyOp);
|
---|
[2335] | 165 | seqSubScopesProc2.AddSubOperator(sorter);
|
---|
[1287] | 166 |
|
---|
[1347] | 167 | childCreater.OperatorGraph.AddOperator(main);
|
---|
| 168 | childCreater.OperatorGraph.InitialOperator = main;
|
---|
[1156] | 169 | return childCreater;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[2335] | 172 | protected override IOperator CreateTerminationCondition() {
|
---|
| 173 | CombinedOperator terminationCritertion = new CombinedOperator();
|
---|
| 174 | terminationCritertion.Name = "TerminationCondition";
|
---|
| 175 | GreaterThanComparator selPresComparator = new GreaterThanComparator();
|
---|
[1287] | 176 | selPresComparator.GetVariableInfo("LeftSide").ActualName = "SelectionPressure";
|
---|
| 177 | selPresComparator.GetVariableInfo("RightSide").ActualName = "SelectionPressureLimit";
|
---|
[2335] | 178 | selPresComparator.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
[1287] | 179 |
|
---|
[2385] | 180 | IOperator baseAndSelPresTerminationCriterion = AlgorithmBase.CombineTerminationCriterions(base.CreateTerminationCondition(), selPresComparator);
|
---|
[1287] | 181 |
|
---|
[2335] | 182 | GreaterThanComparator evalSolutionsComparer = new GreaterThanComparator();
|
---|
| 183 | evalSolutionsComparer.GetVariableInfo("LeftSide").ActualName = "EvaluatedSolutions";
|
---|
| 184 | evalSolutionsComparer.GetVariableInfo("RightSide").ActualName = "MaxEvaluatedSolutions";
|
---|
| 185 | evalSolutionsComparer.GetVariableInfo("Result").ActualName = "TerminationCriterion";
|
---|
[1287] | 186 |
|
---|
[2385] | 187 | IOperator combinedTerminationCritertion = AlgorithmBase.CombineTerminationCriterions(baseAndSelPresTerminationCriterion, evalSolutionsComparer);
|
---|
[1287] | 188 |
|
---|
[2335] | 189 | terminationCritertion.OperatorGraph.AddOperator(combinedTerminationCritertion);
|
---|
| 190 | terminationCritertion.OperatorGraph.InitialOperator = combinedTerminationCritertion;
|
---|
| 191 | return terminationCritertion;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | protected override IOperator CreateLoggingOperator() {
|
---|
[1287] | 195 | CombinedOperator loggingOperator = new CombinedOperator();
|
---|
| 196 | loggingOperator.Name = "Logging";
|
---|
| 197 | SequentialProcessor seq = new SequentialProcessor();
|
---|
| 198 |
|
---|
| 199 | DataCollector collector = new DataCollector();
|
---|
| 200 | ItemList<StringData> names = collector.GetVariable("VariableNames").GetValue<ItemList<StringData>>();
|
---|
| 201 | names.Add(new StringData("BestQuality"));
|
---|
| 202 | names.Add(new StringData("AverageQuality"));
|
---|
| 203 | names.Add(new StringData("WorstQuality"));
|
---|
| 204 | names.Add(new StringData("EvaluatedSolutions"));
|
---|
| 205 | names.Add(new StringData("SelectionPressure"));
|
---|
| 206 | QualityLogger qualityLogger = new QualityLogger();
|
---|
| 207 | seq.AddSubOperator(collector);
|
---|
| 208 | seq.AddSubOperator(qualityLogger);
|
---|
| 209 |
|
---|
| 210 | loggingOperator.OperatorGraph.AddOperator(seq);
|
---|
| 211 | loggingOperator.OperatorGraph.InitialOperator = seq;
|
---|
| 212 | return loggingOperator;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[2335] | 215 | public virtual IEditor CreateEditor() {
|
---|
[1287] | 216 | return new OffspringSelectionGpEditor(this);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[1156] | 219 | public override IView CreateView() {
|
---|
| 220 | return new OffspringSelectionGpEditor(this);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[1287] | 224 | OffspringSelectionGP clone = (OffspringSelectionGP)base.Clone(clonedObjects);
|
---|
| 225 | clone.SelectionPressureLimit = SelectionPressureLimit;
|
---|
| 226 | clone.SuccessRatioLimit = SuccessRatioLimit;
|
---|
| 227 | clone.ComparisonFactor = ComparisonFactor;
|
---|
[1156] | 228 | return clone;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|