[3378] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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 | }
|
---|
| 65 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 66 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 67 | }
|
---|
| 68 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 69 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 70 | }
|
---|
[3650] | 71 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 72 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3378] | 73 | }
|
---|
| 74 | public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
| 75 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
| 76 | }
|
---|
[3744] | 77 | public LookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 78 | get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 79 | }
|
---|
| 80 | public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
|
---|
| 81 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
|
---|
| 82 | }
|
---|
[3378] | 83 | public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
|
---|
| 84 | get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
|
---|
| 85 | }
|
---|
| 86 | public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
| 87 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
| 88 | }
|
---|
| 89 | public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
| 90 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
| 91 | }
|
---|
[5356] | 92 | public LookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 93 | get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 94 | }
|
---|
[3378] | 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | [StorableConstructor]
|
---|
[4722] | 98 | private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 99 | private OffspringSelectionGeneticAlgorithmMainLoop(OffspringSelectionGeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 100 | : base(original, cloner) {
|
---|
| 101 | }
|
---|
| 102 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 103 | return new OffspringSelectionGeneticAlgorithmMainLoop(this, cloner);
|
---|
| 104 | }
|
---|
[3378] | 105 | public OffspringSelectionGeneticAlgorithmMainLoop()
|
---|
| 106 | : base() {
|
---|
| 107 | Initialize();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | private void Initialize() {
|
---|
| 111 | #region Create parameters
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 113 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
[3659] | 114 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[3378] | 115 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 116 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 117 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 118 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
[5208] | 120 | 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] | 121 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
| 122 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 123 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
[3650] | 124 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
[3378] | 125 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
|
---|
[3744] | 126 | 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]."));
|
---|
| 127 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
|
---|
[3378] | 128 | Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
|
---|
| 129 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
|
---|
| 130 | 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] | 131 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
[3378] | 132 | #endregion
|
---|
| 133 |
|
---|
| 134 | #region Create operators
|
---|
| 135 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3744] | 136 | Assigner comparisonFactorInitializer = new Assigner();
|
---|
[3650] | 137 | Placeholder analyzer1 = new Placeholder();
|
---|
| 138 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 139 | OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
|
---|
| 140 | IntCounter generationsCounter = new IntCounter();
|
---|
| 141 | Comparator maxGenerationsComparator = new Comparator();
|
---|
| 142 | Comparator maxSelectionPressureComparator = new Comparator();
|
---|
[3891] | 143 | Comparator maxEvaluatedSolutionsComparator = new Comparator();
|
---|
[3744] | 144 | Placeholder comparisonFactorModifier = new Placeholder();
|
---|
[3650] | 145 | Placeholder analyzer2 = new Placeholder();
|
---|
[3378] | 146 | ConditionalBranch conditionalBranch1 = new ConditionalBranch();
|
---|
| 147 | ConditionalBranch conditionalBranch2 = new ConditionalBranch();
|
---|
[3891] | 148 | ConditionalBranch conditionalBranch3 = new ConditionalBranch();
|
---|
[3378] | 149 |
|
---|
[3750] | 150 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
|
---|
[3379] | 151 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
[3378] | 152 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
|
---|
| 153 |
|
---|
[3744] | 154 | comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
|
---|
| 155 | comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
| 156 | comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
|
---|
[3378] | 157 |
|
---|
[3650] | 158 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 159 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 160 |
|
---|
[3672] | 161 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[3650] | 162 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[6630] | 163 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Comparison Factor", null, ComparisonFactorParameter.Name));
|
---|
| 164 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", "Displays the rising selection pressure during a generation.", "SelectionPressure"));
|
---|
| 165 | 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] | 166 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3378] | 167 |
|
---|
[3744] | 168 | mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
[3650] | 169 | mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 170 | mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
|
---|
| 171 | mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[5356] | 172 | mainOperator.EvaluatedSolutionsParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
[3650] | 173 | mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 174 | mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 175 | mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
| 176 | mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 177 | mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 178 | mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
| 179 | mainOperator.QualityParameter.ActualName = QualityParameter.Name;
|
---|
| 180 | mainOperator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 181 | mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
|
---|
| 182 | mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 183 | mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
[3426] | 184 |
|
---|
[3650] | 185 | generationsCounter.Increment = new IntValue(1);
|
---|
| 186 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
[3426] | 187 |
|
---|
[3650] | 188 | maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 189 | maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 190 | maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
|
---|
| 191 | maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3378] | 192 |
|
---|
[3650] | 193 | maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 194 | maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
|
---|
| 195 | maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
|
---|
| 196 | maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
[3378] | 197 |
|
---|
[3891] | 198 | maxEvaluatedSolutionsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
[5356] | 199 | maxEvaluatedSolutionsComparator.LeftSideParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
[3891] | 200 | maxEvaluatedSolutionsComparator.ResultParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
| 201 | maxEvaluatedSolutionsComparator.RightSideParameter.ActualName = "MaximumEvaluatedSolutions";
|
---|
| 202 |
|
---|
[3744] | 203 | comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
|
---|
| 204 | comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
[3698] | 205 |
|
---|
[3650] | 206 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 207 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 208 |
|
---|
| 209 | conditionalBranch1.Name = "MaximumSelectionPressure reached?";
|
---|
| 210 | conditionalBranch1.ConditionParameter.ActualName = "TerminateSelectionPressure";
|
---|
| 211 |
|
---|
| 212 | conditionalBranch2.Name = "MaximumGenerations reached?";
|
---|
[3650] | 213 | conditionalBranch2.ConditionParameter.ActualName = "TerminateMaximumGenerations";
|
---|
[3891] | 214 |
|
---|
| 215 | conditionalBranch3.Name = "MaximumEvaluatedSolutions reached?";
|
---|
| 216 | conditionalBranch3.ConditionParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
[3378] | 217 | #endregion
|
---|
| 218 |
|
---|
| 219 | #region Create operator graph
|
---|
[3650] | 220 | OperatorGraph.InitialOperator = variableCreator;
|
---|
[3744] | 221 | variableCreator.Successor = comparisonFactorInitializer;
|
---|
| 222 | comparisonFactorInitializer.Successor = analyzer1;
|
---|
[3650] | 223 | analyzer1.Successor = resultsCollector1;
|
---|
[5356] | 224 | resultsCollector1.Successor = mainOperator;
|
---|
[3650] | 225 | mainOperator.Successor = generationsCounter;
|
---|
| 226 | generationsCounter.Successor = maxGenerationsComparator;
|
---|
| 227 | maxGenerationsComparator.Successor = maxSelectionPressureComparator;
|
---|
[3891] | 228 | maxSelectionPressureComparator.Successor = maxEvaluatedSolutionsComparator;
|
---|
| 229 | maxEvaluatedSolutionsComparator.Successor = comparisonFactorModifier;
|
---|
[3744] | 230 | comparisonFactorModifier.Successor = analyzer2;
|
---|
[5356] | 231 | analyzer2.Successor = conditionalBranch1;
|
---|
[3378] | 232 | conditionalBranch1.FalseBranch = conditionalBranch2;
|
---|
| 233 | conditionalBranch1.TrueBranch = null;
|
---|
| 234 | conditionalBranch1.Successor = null;
|
---|
[3891] | 235 | conditionalBranch2.FalseBranch = conditionalBranch3;
|
---|
[3378] | 236 | conditionalBranch2.TrueBranch = null;
|
---|
| 237 | conditionalBranch2.Successor = null;
|
---|
[3891] | 238 | conditionalBranch3.FalseBranch = mainOperator;
|
---|
| 239 | conditionalBranch3.TrueBranch = null;
|
---|
| 240 | conditionalBranch3.Successor = null;
|
---|
[3378] | 241 | #endregion
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | }
|
---|