[3378] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3378] | 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 |
|
---|
[4722] | 22 | using HeuristicLab.Common;
|
---|
[3378] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// An operator which represents the main loop of an offspring selection genetic algorithm.
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public sealed class OffspringSelectionGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
| 37 | #region Parameter properties
|
---|
| 38 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 39 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 40 | }
|
---|
| 41 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 42 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 43 | }
|
---|
[3659] | 44 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 45 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[3378] | 46 | }
|
---|
| 47 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 48 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 49 | }
|
---|
| 50 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 51 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 52 | }
|
---|
| 53 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 54 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 55 | }
|
---|
| 56 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 57 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 58 | }
|
---|
| 59 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 60 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 61 | }
|
---|
| 62 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
| 63 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 64 | }
|
---|
[9569] | 65 | public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 66 | get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 67 | }
|
---|
[3378] | 68 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 69 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 70 | }
|
---|
| 71 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 72 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 73 | }
|
---|
[3650] | 74 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 75 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3378] | 76 | }
|
---|
| 77 | public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
| 78 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
| 79 | }
|
---|
[3744] | 80 | public LookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 81 | get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 82 | }
|
---|
| 83 | public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
|
---|
| 84 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
|
---|
| 85 | }
|
---|
[3378] | 86 | public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
|
---|
| 87 | get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
|
---|
| 88 | }
|
---|
| 89 | public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
| 90 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
| 91 | }
|
---|
| 92 | public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
| 93 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
| 94 | }
|
---|
[5356] | 95 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 96 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 97 | }
|
---|
[10643] | 98 | public IValueLookupParameter<BoolValue> FillPopulationWithParentsParameter {
|
---|
| 99 | get { return (IValueLookupParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
|
---|
| 100 | }
|
---|
[3378] | 101 | #endregion
|
---|
| 102 |
|
---|
| 103 | [StorableConstructor]
|
---|
[4722] | 104 | private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 105 | private OffspringSelectionGeneticAlgorithmMainLoop(OffspringSelectionGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 106 | : base(original, cloner) {
|
---|
| 107 | }
|
---|
| 108 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 109 | return new OffspringSelectionGeneticAlgorithmMainLoop(this, cloner);
|
---|
| 110 | }
|
---|
[3378] | 111 | public OffspringSelectionGeneticAlgorithmMainLoop()
|
---|
| 112 | : base() {
|
---|
| 113 | Initialize();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[9569] | 116 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 117 | private void AfterDeserialization() {
|
---|
[9592] | 118 | // BackwardsCompatibility3.3
|
---|
[9591] | 119 | #region Backwards compatible code, remove with 3.4
|
---|
[9569] | 120 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 121 | Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
| 122 | }
|
---|
[10643] | 123 | if (!Parameters.ContainsKey("FillPopulationWithParents"))
|
---|
| 124 | Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded."));
|
---|
[9591] | 125 | #endregion
|
---|
[9569] | 126 | }
|
---|
| 127 |
|
---|
[3378] | 128 | private void Initialize() {
|
---|
| 129 | #region Create parameters
|
---|
| 130 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 131 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
[3659] | 132 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[3378] | 133 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 134 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 135 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 136 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 137 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
[5208] | 138 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
|
---|
[3378] | 139 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
[9569] | 140 | Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
[3378] | 141 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 142 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
[3650] | 143 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
[3378] | 144 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
|
---|
[3744] | 145 | Parameters.Add(new LookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1]."));
|
---|
| 146 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
|
---|
[3378] | 147 | Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
|
---|
| 148 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
|
---|
| 149 | Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation."));
|
---|
[5356] | 150 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
[10643] | 151 | Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded."));
|
---|
[3378] | 152 | #endregion
|
---|
| 153 |
|
---|
| 154 | #region Create operators
|
---|
| 155 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3744] | 156 | Assigner comparisonFactorInitializer = new Assigner();
|
---|
[3650] | 157 | Placeholder analyzer1 = new Placeholder();
|
---|
| 158 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 159 | OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
|
---|
| 160 | IntCounter generationsCounter = new IntCounter();
|
---|
| 161 | Comparator maxGenerationsComparator = new Comparator();
|
---|
| 162 | Comparator maxSelectionPressureComparator = new Comparator();
|
---|
[3891] | 163 | Comparator maxEvaluatedSolutionsComparator = new Comparator();
|
---|
[3744] | 164 | Placeholder comparisonFactorModifier = new Placeholder();
|
---|
[3650] | 165 | Placeholder analyzer2 = new Placeholder();
|
---|
[3378] | 166 | ConditionalBranch conditionalBranch1 = new ConditionalBranch();
|
---|
| 167 | ConditionalBranch conditionalBranch2 = new ConditionalBranch();
|
---|
[3891] | 168 | ConditionalBranch conditionalBranch3 = new ConditionalBranch();
|
---|
[3378] | 169 |
|
---|
[3750] | 170 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
|
---|
[3379] | 171 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
[3378] | 172 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
|
---|
| 173 |
|
---|
[3744] | 174 | comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
|
---|
| 175 | comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
| 176 | comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
|
---|
[3378] | 177 |
|
---|
[3650] | 178 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 179 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 180 |
|
---|
[3672] | 181 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[3650] | 182 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[6630] | 183 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Comparison Factor", null, ComparisonFactorParameter.Name));
|
---|
| 184 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", "Displays the rising selection pressure during a generation.", "SelectionPressure"));
|
---|
| 185 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", "Indicates how many successful children were already found during a generation (relative to the population size).", "CurrentSuccessRatio"));
|
---|
[3650] | 186 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3378] | 187 |
|
---|
[3744] | 188 | mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
[3650] | 189 | mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 190 | mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
|
---|
| 191 | mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[9569] | 192 | mainOperator.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
[5356] | 193 | mainOperator.EvaluatedSolutionsParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
[3650] | 194 | mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 195 | mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 196 | mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
| 197 | mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 198 | mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 199 | mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
| 200 | mainOperator.QualityParameter.ActualName = QualityParameter.Name;
|
---|
| 201 | mainOperator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 202 | mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
|
---|
| 203 | mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 204 | mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
[10643] | 205 | mainOperator.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
|
---|
[3426] | 206 |
|
---|
[3650] | 207 | generationsCounter.Increment = new IntValue(1);
|
---|
| 208 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
[3426] | 209 |
|
---|
[3650] | 210 | maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 211 | maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 212 | maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
|
---|
| 213 | maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3378] | 214 |
|
---|
[3650] | 215 | maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 216 | maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
|
---|
| 217 | maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
|
---|
| 218 | maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
[3378] | 219 |
|
---|
[3891] | 220 | maxEvaluatedSolutionsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
[5356] | 221 | maxEvaluatedSolutionsComparator.LeftSideParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
[3891] | 222 | maxEvaluatedSolutionsComparator.ResultParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
| 223 | maxEvaluatedSolutionsComparator.RightSideParameter.ActualName = "MaximumEvaluatedSolutions";
|
---|
| 224 |
|
---|
[3744] | 225 | comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
|
---|
| 226 | comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
[3698] | 227 |
|
---|
[3650] | 228 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 229 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 230 |
|
---|
| 231 | conditionalBranch1.Name = "MaximumSelectionPressure reached?";
|
---|
| 232 | conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
|
---|
| 233 |
|
---|
| 234 | conditionalBranch2.Name = "MaximumGenerations reached?";
|
---|
[3650] | 235 | conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
|
---|
[3891] | 236 |
|
---|
| 237 | conditionalBranch3.Name = "MaximumEvaluatedSolutions reached?";
|
---|
| 238 | conditionalBranch3.ConditionParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
[3378] | 239 | #endregion
|
---|
| 240 |
|
---|
| 241 | #region Create operator graph
|
---|
[3650] | 242 | OperatorGraph.InitialOperator = variableCreator;
|
---|
[3744] | 243 | variableCreator.Successor = comparisonFactorInitializer;
|
---|
| 244 | comparisonFactorInitializer.Successor = analyzer1;
|
---|
[3650] | 245 | analyzer1.Successor = resultsCollector1;
|
---|
[5356] | 246 | resultsCollector1.Successor = mainOperator;
|
---|
[3650] | 247 | mainOperator.Successor = generationsCounter;
|
---|
| 248 | generationsCounter.Successor = maxGenerationsComparator;
|
---|
| 249 | maxGenerationsComparator.Successor = maxSelectionPressureComparator;
|
---|
[3891] | 250 | maxSelectionPressureComparator.Successor = maxEvaluatedSolutionsComparator;
|
---|
| 251 | maxEvaluatedSolutionsComparator.Successor = comparisonFactorModifier;
|
---|
[3744] | 252 | comparisonFactorModifier.Successor = analyzer2;
|
---|
[5356] | 253 | analyzer2.Successor = conditionalBranch1;
|
---|
[3378] | 254 | conditionalBranch1.FalseBranch = conditionalBranch2;
|
---|
| 255 | conditionalBranch1.TrueBranch = null;
|
---|
| 256 | conditionalBranch1.Successor = null;
|
---|
[3891] | 257 | conditionalBranch2.FalseBranch = conditionalBranch3;
|
---|
[3378] | 258 | conditionalBranch2.TrueBranch = null;
|
---|
| 259 | conditionalBranch2.Successor = null;
|
---|
[3891] | 260 | conditionalBranch3.FalseBranch = mainOperator;
|
---|
| 261 | conditionalBranch3.TrueBranch = null;
|
---|
| 262 | conditionalBranch3.Successor = null;
|
---|
[3378] | 263 | #endregion
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | }
|
---|