[3378] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 | using HeuristicLab.Operators;
|
---|
| 25 | using HeuristicLab.Optimization.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// An operator which represents the main loop of an offspring selection genetic algorithm.
|
---|
| 32 | /// </summary>
|
---|
| 33 | [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public sealed class OffspringSelectionGeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
| 36 | #region Parameter properties
|
---|
| 37 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 38 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 39 | }
|
---|
| 40 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 41 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 42 | }
|
---|
[3659] | 43 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 44 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[3378] | 45 | }
|
---|
| 46 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 47 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 48 | }
|
---|
| 49 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 50 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 51 | }
|
---|
| 52 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 53 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 54 | }
|
---|
| 55 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 56 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 59 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
| 62 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 63 | }
|
---|
| 64 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 65 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 66 | }
|
---|
| 67 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 68 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 69 | }
|
---|
[3650] | 70 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 71 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
[3378] | 72 | }
|
---|
| 73 | public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
| 74 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
| 75 | }
|
---|
[3744] | 76 | public LookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 77 | get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 78 | }
|
---|
| 79 | public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
|
---|
| 80 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
|
---|
| 81 | }
|
---|
[3378] | 82 | public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
|
---|
| 83 | get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
|
---|
| 84 | }
|
---|
| 85 | public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
| 86 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
| 87 | }
|
---|
| 88 | public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
| 89 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
| 90 | }
|
---|
| 91 | #endregion
|
---|
| 92 |
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base() { }
|
---|
| 95 | public OffspringSelectionGeneticAlgorithmMainLoop()
|
---|
| 96 | : base() {
|
---|
| 97 | Initialize();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private void Initialize() {
|
---|
| 101 | #region Create parameters
|
---|
| 102 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 103 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
[3659] | 104 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
[3378] | 105 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
| 106 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 107 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 108 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 110 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
| 111 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 113 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
[3650] | 114 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
[3378] | 115 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
|
---|
[3744] | 116 | 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]."));
|
---|
| 117 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
|
---|
[3378] | 118 | Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
|
---|
| 120 | 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."));
|
---|
| 121 | #endregion
|
---|
| 122 |
|
---|
| 123 | #region Create operators
|
---|
| 124 | VariableCreator variableCreator = new VariableCreator();
|
---|
[3744] | 125 | Assigner comparisonFactorInitializer = new Assigner();
|
---|
[3650] | 126 | Placeholder analyzer1 = new Placeholder();
|
---|
| 127 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
[3672] | 128 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
[3650] | 129 | OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
|
---|
| 130 | IntCounter generationsCounter = new IntCounter();
|
---|
| 131 | Comparator maxGenerationsComparator = new Comparator();
|
---|
| 132 | Comparator maxSelectionPressureComparator = new Comparator();
|
---|
[3891] | 133 | Comparator maxEvaluatedSolutionsComparator = new Comparator();
|
---|
[3744] | 134 | Placeholder comparisonFactorModifier = new Placeholder();
|
---|
[3650] | 135 | Placeholder analyzer2 = new Placeholder();
|
---|
[3672] | 136 | ResultsCollector resultsCollector3 = new ResultsCollector();
|
---|
[3378] | 137 | ConditionalBranch conditionalBranch1 = new ConditionalBranch();
|
---|
| 138 | ConditionalBranch conditionalBranch2 = new ConditionalBranch();
|
---|
[3891] | 139 | ConditionalBranch conditionalBranch3 = new ConditionalBranch();
|
---|
[3378] | 140 |
|
---|
[3750] | 141 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
|
---|
[3446] | 142 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
[3379] | 143 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
|
---|
[3378] | 144 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
|
---|
| 145 |
|
---|
[3744] | 146 | comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
|
---|
| 147 | comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
| 148 | comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
|
---|
[3378] | 149 |
|
---|
[3650] | 150 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
| 151 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 152 |
|
---|
[3672] | 153 | resultsCollector1.CopyValue = new BoolValue(false);
|
---|
[3650] | 154 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
[3744] | 155 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Curent Comparison Factor", null, ComparisonFactorParameter.Name));
|
---|
[3650] | 156 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", null, "SelectionPressure"));
|
---|
| 157 | resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Success Ratio", null, "CurrentSuccessRatio"));
|
---|
| 158 | resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
[3378] | 159 |
|
---|
[3672] | 160 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
| 161 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 162 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
| 163 |
|
---|
[3744] | 164 | mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
|
---|
[3650] | 165 | mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 166 | mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
|
---|
| 167 | mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
| 168 | mainOperator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 169 | mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
|
---|
| 170 | mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
| 171 | mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
| 172 | mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 173 | mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 174 | mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
| 175 | mainOperator.QualityParameter.ActualName = QualityParameter.Name;
|
---|
| 176 | mainOperator.RandomParameter.ActualName = RandomParameter.Name;
|
---|
| 177 | mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
|
---|
| 178 | mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 179 | mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
[3426] | 180 |
|
---|
[3650] | 181 | generationsCounter.Increment = new IntValue(1);
|
---|
| 182 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
[3426] | 183 |
|
---|
[3650] | 184 | maxGenerationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 185 | maxGenerationsComparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 186 | maxGenerationsComparator.ResultParameter.ActualName = "TerminateMaximumGenerations";
|
---|
| 187 | maxGenerationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3378] | 188 |
|
---|
[3650] | 189 | maxSelectionPressureComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 190 | maxSelectionPressureComparator.LeftSideParameter.ActualName = "SelectionPressure";
|
---|
| 191 | maxSelectionPressureComparator.ResultParameter.ActualName = "TerminateSelectionPressure";
|
---|
| 192 | maxSelectionPressureComparator.RightSideParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
[3378] | 193 |
|
---|
[3891] | 194 | maxEvaluatedSolutionsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 195 | maxEvaluatedSolutionsComparator.LeftSideParameter.ActualName = "EvaluatedSolutions";
|
---|
| 196 | maxEvaluatedSolutionsComparator.ResultParameter.ActualName = "TerminateEvaluatedSolutions";
|
---|
| 197 | maxEvaluatedSolutionsComparator.RightSideParameter.ActualName = "MaximumEvaluatedSolutions";
|
---|
| 198 |
|
---|
[3744] | 199 | comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
|
---|
| 200 | comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
[3698] | 201 |
|
---|
[3650] | 202 | analyzer2.Name = "Analyzer (placeholder)";
|
---|
| 203 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
[3378] | 204 |
|
---|
[3672] | 205 | resultsCollector3.CopyValue = new BoolValue(true);
|
---|
| 206 | resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 207 | resultsCollector3.ResultsParameter.ActualName = ResultsParameter.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;
|
---|
[3672] | 224 | resultsCollector1.Successor = resultsCollector2;
|
---|
| 225 | resultsCollector2.Successor = mainOperator;
|
---|
[3650] | 226 | mainOperator.Successor = generationsCounter;
|
---|
| 227 | generationsCounter.Successor = maxGenerationsComparator;
|
---|
| 228 | maxGenerationsComparator.Successor = maxSelectionPressureComparator;
|
---|
[3891] | 229 | maxSelectionPressureComparator.Successor = maxEvaluatedSolutionsComparator;
|
---|
| 230 | maxEvaluatedSolutionsComparator.Successor = comparisonFactorModifier;
|
---|
[3744] | 231 | comparisonFactorModifier.Successor = analyzer2;
|
---|
[3672] | 232 | analyzer2.Successor = resultsCollector3;
|
---|
| 233 | resultsCollector3.Successor = conditionalBranch1;
|
---|
[3378] | 234 | conditionalBranch1.FalseBranch = conditionalBranch2;
|
---|
| 235 | conditionalBranch1.TrueBranch = null;
|
---|
| 236 | conditionalBranch1.Successor = null;
|
---|
[3891] | 237 | conditionalBranch2.FalseBranch = conditionalBranch3;
|
---|
[3378] | 238 | conditionalBranch2.TrueBranch = null;
|
---|
| 239 | conditionalBranch2.Successor = null;
|
---|
[3891] | 240 | conditionalBranch3.FalseBranch = mainOperator;
|
---|
| 241 | conditionalBranch3.TrueBranch = null;
|
---|
| 242 | conditionalBranch3.Successor = null;
|
---|
[3378] | 243 | #endregion
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|