Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/ParallelIslandGeneticAlgorithmMainLoop.cs @ 7325

Last change on this file since 7325 was 7325, checked in by ascheibe, 13 years ago

#1745 added result storing operations for the reducer

File size: 21.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Algorithms.GeneticAlgorithm {
33  /// <summary>
34  /// An island genetic algorithm main loop operator.
35  /// </summary>
36  [Item("ParallelIslandGeneticAlgorithmMainLoop", "An island genetic algorithm main loop operator.")]
37  [StorableClass]
38  public sealed class ParallelIslandGeneticAlgorithmMainLoop : AlgorithmOperator {
39    #region Parameter Properties
40    public ValueLookupParameter<IRandom> RandomParameter {
41      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    public ValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
50      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
51    }
52    public ValueLookupParameter<IntValue> NumberOfIslandsParameter {
53      get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfIslands"]; }
54    }
55    public ValueLookupParameter<IntValue> MigrationIntervalParameter {
56      get { return (ValueLookupParameter<IntValue>)Parameters["MigrationInterval"]; }
57    }
58    public ValueLookupParameter<PercentValue> MigrationRateParameter {
59      get { return (ValueLookupParameter<PercentValue>)Parameters["MigrationRate"]; }
60    }
61    public ValueLookupParameter<IOperator> MigratorParameter {
62      get { return (ValueLookupParameter<IOperator>)Parameters["Migrator"]; }
63    }
64    public ValueLookupParameter<IOperator> EmigrantsSelectorParameter {
65      get { return (ValueLookupParameter<IOperator>)Parameters["EmigrantsSelector"]; }
66    }
67    public ValueLookupParameter<IOperator> ImmigrationReplacerParameter {
68      get { return (ValueLookupParameter<IOperator>)Parameters["ImmigrationReplacer"]; }
69    }
70    public ValueLookupParameter<IntValue> PopulationSizeParameter {
71      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
72    }
73    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
74      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
75    }
76    public ValueLookupParameter<IOperator> SelectorParameter {
77      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
78    }
79    public ValueLookupParameter<IOperator> CrossoverParameter {
80      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
81    }
82    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
83      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
84    }
85    public ValueLookupParameter<IOperator> MutatorParameter {
86      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
87    }
88    public ValueLookupParameter<IOperator> EvaluatorParameter {
89      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
90    }
91    public ValueLookupParameter<IntValue> ElitesParameter {
92      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
93    }
94    public ValueLookupParameter<ResultCollection> ResultsParameter {
95      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
96    }
97    public ValueLookupParameter<IOperator> AnalyzerParameter {
98      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
99    }
100    public ValueLookupParameter<IOperator> IslandAnalyzerParameter {
101      get { return (ValueLookupParameter<IOperator>)Parameters["IslandAnalyzer"]; }
102    }
103    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
104      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
105    }
106    public LookupParameter<IntValue> IslandGenerations {
107      get { return (LookupParameter<IntValue>)Parameters["IslandGenerations"]; }
108    }
109    public LookupParameter<IntValue> IslandEvaluatedSolutions {
110      get { return (LookupParameter<IntValue>)Parameters["IslandEvaluatedSolutions"]; }
111    }
112    public ValueLookupParameter<BoolValue> Migrate {
113      get { return (ValueLookupParameter<BoolValue>)Parameters["Migrate"]; }
114    }
115    #endregion
116
117    [StorableConstructor]
118    private ParallelIslandGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
119    private ParallelIslandGeneticAlgorithmMainLoop(ParallelIslandGeneticAlgorithmMainLoop original, Cloner cloner)
120      : base(original, cloner) {
121    }
122    public override IDeepCloneable Clone(Cloner cloner) {
123      return new ParallelIslandGeneticAlgorithmMainLoop(this, cloner);
124    }
125    public ParallelIslandGeneticAlgorithmMainLoop()
126      : base() {
127      #region Create parameters
128      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
129      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
130      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
131      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfIslands", "The number of islands."));
133      Parameters.Add(new ValueLookupParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases."));
134      Parameters.Add(new ValueLookupParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands."));
135      Parameters.Add(new ValueLookupParameter<IOperator>("Migrator", "The migration strategy."));
136      Parameters.Add(new ValueLookupParameter<IOperator>("EmigrantsSelector", "Selects the individuals that will be migrated."));
137      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationReplacer", "Replaces some of the original population with the immigrants."));
138      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
139      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
140      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
141      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
142      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
143      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
144      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."));
145      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
146      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection to store the results."));
147      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to the analyze the islands."));
148      Parameters.Add(new ValueLookupParameter<IOperator>("IslandAnalyzer", "The operator used to analyze each island."));
149      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times a solution has been evaluated."));
150      Parameters.Add(new LookupParameter<IntValue>("IslandGenerations", "The number of generations calculated on one island."));
151      Parameters.Add(new LookupParameter<IntValue>("IslandEvaluatedSolutions", "The number of times a solution has been evaluated on one island."));
152      Parameters.Add(new ValueLookupParameter<BoolValue>("Migrate", "Migrate the island?"));
153      #endregion
154
155      #region Create operators
156      VariableCreator variableCreator = new VariableCreator();
157      UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor();
158      VariableCreator islandVariableCreator = new VariableCreator();
159      Placeholder islandAnalyzer1 = new Placeholder();
160      Placeholder analyzer1 = new Placeholder();
161      ResultsCollector resultsCollector1 = new ResultsCollector();
162      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
163      Assigner generationsAssigner = new Assigner();
164      Assigner evaluatedSolutionsAssigner = new Assigner();
165      Placeholder selector = new Placeholder();
166      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
167      ChildrenCreator childrenCreator = new ChildrenCreator();
168      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
169      Placeholder crossover = new Placeholder();
170      StochasticBranch stochasticBranch = new StochasticBranch();
171      Placeholder mutator = new Placeholder();
172      SubScopesRemover subScopesRemover = new SubScopesRemover();
173      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
174      Placeholder evaluator = new Placeholder();
175      SubScopesCounter subScopesCounter = new SubScopesCounter();
176      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
177      BestSelector bestSelector = new BestSelector();
178      RightReducer rightReducer = new RightReducer();
179      MergingReducer mergingReducer = new MergingReducer();
180      IntCounter islandGenerationsCounter = new IntCounter();
181      Comparator checkIslandGenerationsReachedMaximum = new Comparator();
182      ConditionalBranch checkContinueEvolution = new ConditionalBranch();
183      IntDataReducer generationsReducer = new IntDataReducer();
184      IntDataReducer evaluatedSolutionsReducer = new IntDataReducer();
185      Placeholder islandAnalyzer2 = new Placeholder();
186      UniformSubScopesProcessor uniformSubScopesProcessor5 = new UniformSubScopesProcessor();
187      Placeholder emigrantsSelector = new Placeholder();
188      IntCounter migrationsCounter = new IntCounter();
189      Placeholder migrator = new Placeholder();
190      UniformSubScopesProcessor uniformSubScopesProcessor6 = new UniformSubScopesProcessor();
191      Placeholder immigrationReplacer = new Placeholder();
192      Comparator generationsComparator = new Comparator();
193      Placeholder analyzer2 = new Placeholder();
194      ConditionalBranch generationsTerminationCondition = new ConditionalBranch();
195
196
197      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations", new IntValue(0)));
198      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastMigration", new IntValue(0)));
199      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class IslandGeneticAlgorithm expects this to be called Generations
200
201      islandVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
202      islandVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("IslandGenerations", new IntValue(0)));
203      islandVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("IslandEvaluatedSolutions", new IntValue(0)));
204
205      islandAnalyzer1.Name = "Island Analyzer (placeholder)";
206      islandAnalyzer1.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
207
208      analyzer1.Name = "Analyzer (placeholder)";
209      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
210
211      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
212      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
213      resultsCollector1.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IslandResults", "Result set for each island", "Results"));
214      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
215
216      uniformSubScopesProcessor1.Parallel.Value = true;
217
218      generationsAssigner.Name = "Initialize Island Generations";
219      generationsAssigner.LeftSideParameter.ActualName = IslandGenerations.Name;
220      generationsAssigner.RightSideParameter.Value = new IntValue(0);
221
222      evaluatedSolutionsAssigner.Name = "Initialize Island evaluated solutions";
223      evaluatedSolutionsAssigner.LeftSideParameter.ActualName = IslandEvaluatedSolutions.Name;
224      evaluatedSolutionsAssigner.RightSideParameter.Value = new IntValue(0);
225
226      selector.Name = "Selector (placeholder)";
227      selector.OperatorParameter.ActualName = SelectorParameter.Name;
228
229      childrenCreator.ParentsPerChild = new IntValue(2);
230
231      crossover.Name = "Crossover (placeholder)";
232      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
233
234      stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
235      stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
236
237      mutator.Name = "Mutator (placeholder)";
238      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
239
240      subScopesRemover.RemoveAllSubScopes = true;
241
242      evaluator.Name = "Evaluator (placeholder)";
243      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
244
245      subScopesCounter.Name = "Increment EvaluatedSolutions";
246      subScopesCounter.ValueParameter.ActualName = IslandEvaluatedSolutions.Name;
247
248      bestSelector.CopySelected = new BoolValue(false);
249      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
250      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
251      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
252
253      islandGenerationsCounter.Name = "Increment island generatrions";
254      islandGenerationsCounter.ValueParameter.ActualName = IslandGenerations.Name;
255      islandGenerationsCounter.Increment = new IntValue(1);
256
257      checkIslandGenerationsReachedMaximum.LeftSideParameter.ActualName = IslandGenerations.Name;
258      checkIslandGenerationsReachedMaximum.RightSideParameter.ActualName = MigrationIntervalParameter.Name;
259      checkIslandGenerationsReachedMaximum.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
260      checkIslandGenerationsReachedMaximum.ResultParameter.ActualName = Migrate.Name;
261
262      checkContinueEvolution.Name = "Migrate?";
263      checkContinueEvolution.ConditionParameter.ActualName = Migrate.Name;
264      checkContinueEvolution.FalseBranch = selector;
265
266      islandAnalyzer2.Name = "Island Analyzer (placeholder)";
267      islandAnalyzer2.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
268
269      generationsReducer.Name = "Increment Generations";
270      generationsReducer.ParameterToReduce.ActualName = islandGenerationsCounter.ValueParameter.ActualName;
271      generationsReducer.TargetParameterName.Value = new StringValue("Generations");
272      generationsReducer.ReductionOperation.Value = new ReductionType(ReductionTypes.Min);
273      generationsReducer.TargetOperation.Value = new TargetOperationType(TargetOperationTypes.Sum);
274
275      evaluatedSolutionsReducer.Name = "Increment Evaluated Solutions";
276      evaluatedSolutionsReducer.ParameterToReduce.ActualName = IslandEvaluatedSolutions.Name;
277      evaluatedSolutionsReducer.TargetParameterName.Value = new StringValue(EvaluatedSolutionsParameter.Name);
278      evaluatedSolutionsReducer.ReductionOperation.Value = new ReductionType(ReductionTypes.Sum);
279      evaluatedSolutionsReducer.TargetOperation.Value = new TargetOperationType(TargetOperationTypes.Sum);
280
281      emigrantsSelector.Name = "Emigrants Selector (placeholder)";
282      emigrantsSelector.OperatorParameter.ActualName = EmigrantsSelectorParameter.Name;
283
284      migrationsCounter.Name = "Increment number of Migrations";
285      migrationsCounter.ValueParameter.ActualName = "Migrations";
286      migrationsCounter.Increment = new IntValue(1);
287
288      migrator.Name = "Migrator (placeholder)";
289      migrator.OperatorParameter.ActualName = MigratorParameter.Name;
290
291      immigrationReplacer.Name = "Immigration Replacer (placeholder)";
292      immigrationReplacer.OperatorParameter.ActualName = ImmigrationReplacerParameter.Name;
293
294      generationsComparator.Name = "Generations >= MaximumGenerations ?";
295      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
296      generationsComparator.LeftSideParameter.ActualName = "Generations";
297      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
298      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
299
300      analyzer2.Name = "Analyzer (placeholder)";
301      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
302
303      generationsTerminationCondition.Name = "Terminate?";
304      generationsTerminationCondition.ConditionParameter.ActualName = "TerminateGenerations";
305      #endregion
306
307      #region Create operator graph
308      OperatorGraph.InitialOperator = variableCreator;
309      variableCreator.Successor = uniformSubScopesProcessor0;
310      uniformSubScopesProcessor0.Operator = islandVariableCreator;
311      uniformSubScopesProcessor0.Successor = analyzer1;
312      islandVariableCreator.Successor = islandAnalyzer1;
313      islandAnalyzer1.Successor = null;
314      analyzer1.Successor = resultsCollector1;
315      resultsCollector1.Successor = uniformSubScopesProcessor1;
316      uniformSubScopesProcessor1.Operator = generationsAssigner;
317      uniformSubScopesProcessor1.Successor = generationsReducer;
318      generationsReducer.Successor = evaluatedSolutionsReducer;
319      evaluatedSolutionsReducer.Successor = migrationsCounter;
320      migrationsCounter.Successor = uniformSubScopesProcessor5;
321      generationsAssigner.Successor = evaluatedSolutionsAssigner;
322      evaluatedSolutionsAssigner.Successor = selector;
323      selector.Successor = subScopesProcessor1;
324      subScopesProcessor1.Operators.Add(new EmptyOperator());
325      subScopesProcessor1.Operators.Add(childrenCreator);
326      subScopesProcessor1.Successor = subScopesProcessor2;
327      childrenCreator.Successor = uniformSubScopesProcessor2;
328      uniformSubScopesProcessor2.Operator = crossover;
329      uniformSubScopesProcessor2.Successor = uniformSubScopesProcessor3;
330      crossover.Successor = stochasticBranch;
331      stochasticBranch.FirstBranch = mutator;
332      stochasticBranch.SecondBranch = null;
333      stochasticBranch.Successor = subScopesRemover;
334      mutator.Successor = null;
335      subScopesRemover.Successor = null;
336      uniformSubScopesProcessor3.Operator = evaluator;
337      uniformSubScopesProcessor3.Successor = subScopesCounter;
338      evaluator.Successor = null;
339      subScopesCounter.Successor = null;
340      subScopesCounter.Successor = null;
341      subScopesProcessor2.Operators.Add(bestSelector);
342      subScopesProcessor2.Operators.Add(new EmptyOperator());
343      subScopesProcessor2.Successor = mergingReducer;
344      mergingReducer.Successor = islandAnalyzer2;
345      bestSelector.Successor = rightReducer;
346      rightReducer.Successor = null;
347      islandAnalyzer2.Successor = islandGenerationsCounter;
348      islandGenerationsCounter.Successor = checkIslandGenerationsReachedMaximum;
349      checkIslandGenerationsReachedMaximum.Successor = checkContinueEvolution;
350      uniformSubScopesProcessor5.Operator = emigrantsSelector;
351      emigrantsSelector.Successor = null;
352      uniformSubScopesProcessor5.Successor = migrator;
353      migrator.Successor = uniformSubScopesProcessor6;
354      uniformSubScopesProcessor6.Operator = immigrationReplacer;
355      uniformSubScopesProcessor6.Successor = generationsComparator;
356      generationsComparator.Successor = analyzer2;
357      analyzer2.Successor = generationsTerminationCondition;
358      generationsTerminationCondition.TrueBranch = null;
359      generationsTerminationCondition.FalseBranch = uniformSubScopesProcessor1;
360      generationsTerminationCondition.Successor = null;
361      #endregion
362    }
363
364    public override IOperation Apply() {
365      if (CrossoverParameter.ActualValue == null)
366        return null;
367      return base.Apply();
368    }
369  }
370}
Note: See TracBrowser for help on using the repository browser.