source: branches/TerminationCriteria/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithmMainLoop.cs @ 12357

Last change on this file since 12357 was 12357, checked in by pfleck, 5 years ago

#2027 Removed obsolete manual termination criteria operators.

File size: 14.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Termination;
30
31namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
32  /// <summary>
33  /// An operator which represents the main loop of an offspring selection genetic algorithm.
34  /// </summary>
35  [Item("OffspringSelectionGeneticAlgorithmMainLoop", "An operator which represents the main loop of an offspring selection genetic algorithm.")]
36  [StorableClass]
37  public sealed class OffspringSelectionGeneticAlgorithmMainLoop : AlgorithmOperator {
38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
42    public ValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
44    }
45    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
46      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    public ValueLookupParameter<IOperator> SelectorParameter {
49      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
50    }
51    public ValueLookupParameter<IOperator> CrossoverParameter {
52      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
53    }
54    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
55      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
56    }
57    public ValueLookupParameter<IOperator> MutatorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
59    }
60    public ValueLookupParameter<IOperator> EvaluatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
62    }
63    public ValueLookupParameter<IntValue> ElitesParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
65    }
66    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
67      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
68    }
69    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
70      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
71    }
72    public ValueLookupParameter<VariableCollection> ResultsParameter {
73      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
74    }
75    public ValueLookupParameter<IOperator> AnalyzerParameter {
76      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
77    }
78    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
79      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
80    }
81    public LookupParameter<DoubleValue> ComparisonFactorParameter {
82      get { return (LookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
83    }
84    public ValueLookupParameter<DoubleValue> ComparisonFactorStartParameter {
85      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorStart"]; }
86    }
87    public ValueLookupParameter<IOperator> ComparisonFactorModifierParameter {
88      get { return (ValueLookupParameter<IOperator>)Parameters["ComparisonFactorModifier"]; }
89    }
90    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
91      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
92    }
93    public ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
94      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
95    }
96    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
97      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
98    }
99    public IValueLookupParameter<BoolValue> FillPopulationWithParentsParameter {
100      get { return (IValueLookupParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
101    }
102    #endregion
103
104    [StorableConstructor]
105    private OffspringSelectionGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
106    private OffspringSelectionGeneticAlgorithmMainLoop(OffspringSelectionGeneticAlgorithmMainLoop original, Cloner cloner)
107      : base(original, cloner) {
108    }
109    public override IDeepCloneable Clone(Cloner cloner) {
110      return new OffspringSelectionGeneticAlgorithmMainLoop(this, cloner);
111    }
112    public OffspringSelectionGeneticAlgorithmMainLoop()
113      : base() {
114      Initialize();
115    }
116
117    [StorableHook(HookType.AfterDeserialization)]
118    private void AfterDeserialization() {
119      // BackwardsCompatibility3.3
120      #region Backwards compatible code, remove with 3.4
121      if (!Parameters.ContainsKey("ReevaluateElites")) {
122        Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
123      }
124      if (!Parameters.ContainsKey("FillPopulationWithParents"))
125        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."));
126      #endregion
127    }
128
129    private void Initialize() {
130      #region Create parameters
131      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
132      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
133      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
134      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
135      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
136      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
137      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
138      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
139      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."));
140      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
141      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
142      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
143      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
144      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
145      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved."));
146      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]."));
147      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorStart", "The initial value for the comparison factor."));
148      Parameters.Add(new ValueLookupParameter<IOperator>("ComparisonFactorModifier", "The operator used to modify the comparison factor."));
149      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm."));
150      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."));
151      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
152      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."));
153      #endregion
154
155      #region Create operators
156      VariableCreator variableCreator = new VariableCreator();
157      Assigner comparisonFactorInitializer = new Assigner();
158      Placeholder analyzer1 = new Placeholder();
159      ResultsCollector resultsCollector1 = new ResultsCollector();
160      OffspringSelectionGeneticAlgorithmMainOperator mainOperator = new OffspringSelectionGeneticAlgorithmMainOperator();
161      IntCounter generationsCounter = new IntCounter();
162      Placeholder comparisonFactorModifier = new Placeholder();
163      Placeholder analyzer2 = new Placeholder();
164      var termination = new TerminationOperator();
165
166      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class OffspringSelectionGeneticAlgorithm expects this to be called Generations
167      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
168      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentSuccessRatio", new DoubleValue(0)));
169
170      comparisonFactorInitializer.Name = "Initialize ComparisonFactor (placeholder)";
171      comparisonFactorInitializer.LeftSideParameter.ActualName = ComparisonFactorParameter.Name;
172      comparisonFactorInitializer.RightSideParameter.ActualName = ComparisonFactorStartParameter.Name;
173
174      analyzer1.Name = "Analyzer (placeholder)";
175      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
176
177      resultsCollector1.CopyValue = new BoolValue(false);
178      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
179      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Comparison Factor", null, ComparisonFactorParameter.Name));
180      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Selection Pressure", "Displays the rising selection pressure during a generation.", "SelectionPressure"));
181      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"));
182      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
183
184      mainOperator.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
185      mainOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
186      mainOperator.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
187      mainOperator.ElitesParameter.ActualName = ElitesParameter.Name;
188      mainOperator.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
189      mainOperator.EvaluatedSolutionsParameter.ActualName = EvaluatedSolutionsParameter.Name;
190      mainOperator.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
191      mainOperator.MaximizationParameter.ActualName = MaximizationParameter.Name;
192      mainOperator.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
193      mainOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
194      mainOperator.MutatorParameter.ActualName = MutatorParameter.Name;
195      mainOperator.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
196      mainOperator.QualityParameter.ActualName = QualityParameter.Name;
197      mainOperator.RandomParameter.ActualName = RandomParameter.Name;
198      mainOperator.SelectionPressureParameter.ActualName = "SelectionPressure";
199      mainOperator.SelectorParameter.ActualName = SelectorParameter.Name;
200      mainOperator.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
201      mainOperator.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
202
203      generationsCounter.Increment = new IntValue(1);
204      generationsCounter.ValueParameter.ActualName = "Generations";
205
206      comparisonFactorModifier.Name = "Update ComparisonFactor (placeholder)";
207      comparisonFactorModifier.OperatorParameter.ActualName = ComparisonFactorModifierParameter.Name;
208
209      analyzer2.Name = "Analyzer (placeholder)";
210      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
211      #endregion
212
213      #region Create operator graph
214      OperatorGraph.InitialOperator = variableCreator;
215      variableCreator.Successor = comparisonFactorInitializer;
216      comparisonFactorInitializer.Successor = analyzer1;
217      analyzer1.Successor = resultsCollector1;
218      resultsCollector1.Successor = mainOperator;
219      mainOperator.Successor = generationsCounter;
220      generationsCounter.Successor = comparisonFactorModifier;
221      comparisonFactorModifier.Successor = analyzer2;
222      analyzer2.Successor = termination;
223      termination.ContinueBranch = mainOperator;
224      #endregion
225    }
226  }
227}
Note: See TracBrowser for help on using the repository browser.