Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithmMainLoop.cs @ 5304

Last change on this file since 5304 was 5208, checked in by swagner, 14 years ago

Adapted EAs to enable parallel solution evaluation (#1333)

File size: 19.2 KB
RevLine 
[3356]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
[4722]22using HeuristicLab.Common;
[3356]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("IslandGeneticAlgorithmMainLoop", "An island genetic algorithm main loop operator.")]
37  [StorableClass]
38  public sealed class IslandGeneticAlgorithmMainLoop : 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    }
[3659]46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3356]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    }
[3601]67    public ValueLookupParameter<IOperator> ImmigrationReplacerParameter {
68      get { return (ValueLookupParameter<IOperator>)Parameters["ImmigrationReplacer"]; }
[3356]69    }
70    public ValueLookupParameter<IntValue> PopulationSizeParameter {
71      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
72    }
[3609]73    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
74      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3356]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    }
[3616]97    public ValueLookupParameter<IOperator> AnalyzerParameter {
98      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3356]99    }
[3650]100    public ValueLookupParameter<IOperator> IslandAnalyzerParameter {
101      get { return (ValueLookupParameter<IOperator>)Parameters["IslandAnalyzer"]; }
102    }
[3356]103    #endregion
104
105    [StorableConstructor]
[4722]106    private IslandGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
107    private IslandGeneticAlgorithmMainLoop(IslandGeneticAlgorithmMainLoop original, Cloner cloner)
108      : base(original, cloner) {
109    }
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new IslandGeneticAlgorithmMainLoop(this, cloner);
112    }
[3356]113    public IslandGeneticAlgorithmMainLoop()
114      : base() {
115      #region Create parameters
116      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
117      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
[3659]118      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3356]119      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
120      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfIslands", "The number of islands."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases."));
122      Parameters.Add(new ValueLookupParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands."));
123      Parameters.Add(new ValueLookupParameter<IOperator>("Migrator", "The migration strategy."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("EmigrantsSelector", "Selects the individuals that will be migrated."));
[3601]125      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationReplacer", "Replaces some of the original population with the immigrants."));
[3356]126      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
[3609]127      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
[3356]128      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
129      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
130      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
131      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
[5208]132      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."));
[3356]133      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
134      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection to store the results."));
[3650]135      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to the analyze the islands."));
136      Parameters.Add(new ValueLookupParameter<IOperator>("IslandAnalyzer", "The operator used to analyze each island."));
[3356]137      #endregion
138
139      #region Create operators
140      VariableCreator variableCreator = new VariableCreator();
[3609]141      UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor();
142      VariableCreator islandVariableCreator = new VariableCreator();
[3616]143      Placeholder islandAnalyzer1 = new Placeholder();
[3650]144      Placeholder analyzer1 = new Placeholder();
145      ResultsCollector resultsCollector1 = new ResultsCollector();
146      ResultsCollector resultsCollector2 = new ResultsCollector();
[3609]147      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
148      Placeholder selector = new Placeholder();
149      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
150      ChildrenCreator childrenCreator = new ChildrenCreator();
151      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
152      Placeholder crossover = new Placeholder();
153      StochasticBranch stochasticBranch = new StochasticBranch();
154      Placeholder mutator = new Placeholder();
[5208]155      SubScopesRemover subScopesRemover = new SubScopesRemover();
156      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
[3609]157      Placeholder evaluator = new Placeholder();
158      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
159      BestSelector bestSelector = new BestSelector();
160      RightReducer rightReducer = new RightReducer();
161      MergingReducer mergingReducer = new MergingReducer();
[3616]162      Placeholder islandAnalyzer2 = new Placeholder();
[3611]163      IntCounter generationsCounter = new IntCounter();
[3609]164      IntCounter generationsSinceLastMigrationCounter = new IntCounter();
165      Comparator migrationComparator = new Comparator();
166      ConditionalBranch migrationBranch = new ConditionalBranch();
167      Assigner resetGenerationsSinceLastMigrationAssigner = new Assigner();
168      IntCounter migrationsCounter = new IntCounter();
[5208]169      UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
[3356]170      Placeholder emigrantsSelector = new Placeholder();
171      Placeholder migrator = new Placeholder();
[5208]172      UniformSubScopesProcessor uniformSubScopesProcessor5 = new UniformSubScopesProcessor();
[3601]173      Placeholder immigrationReplacer = new Placeholder();
[3609]174      Comparator generationsComparator = new Comparator();
[3650]175      Placeholder analyzer2 = new Placeholder();
176      ResultsCollector resultsCollector3 = new ResultsCollector();
[3609]177      ConditionalBranch generationsTerminationCondition = new ConditionalBranch();
[3356]178
179      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations", new IntValue(0)));
[3609]180      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastMigration", new IntValue(0)));
[3750]181      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class IslandGeneticAlgorithm expects this to be called Generations
[3356]182
[3650]183      islandVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
[3356]184
[3650]185      islandAnalyzer1.Name = "Island Analyzer (placeholder)";
186      islandAnalyzer1.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
[3609]187
[3650]188      analyzer1.Name = "Analyzer (placeholder)";
189      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3609]190
[3650]191      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
192      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
193      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3609]194
[3650]195      resultsCollector2.Name = "Reference Island Results";
196      resultsCollector2.CopyValue = new BoolValue(false);
[3659]197      resultsCollector2.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IslandResults", "Result set for each island", "Results"));
[3650]198      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
[3609]199
200      selector.Name = "Selector (placeholder)";
201      selector.OperatorParameter.ActualName = SelectorParameter.Name;
[3356]202
[3609]203      childrenCreator.ParentsPerChild = new IntValue(2);
204
205      crossover.Name = "Crossover (placeholder)";
206      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
207
208      stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
209      stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
210
211      mutator.Name = "Mutator (placeholder)";
212      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
213
[5208]214      subScopesRemover.RemoveAllSubScopes = true;
215
216      uniformSubScopesProcessor3.Parallel.Value = true;
217
[3609]218      evaluator.Name = "Evaluator (placeholder)";
219      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
220
221      bestSelector.CopySelected = new BoolValue(false);
222      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
223      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
224      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
225
[3650]226      islandAnalyzer2.Name = "Island Analyzer (placeholder)";
227      islandAnalyzer2.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
[3609]228
[3611]229      generationsCounter.Name = "Generations + 1";
230      generationsCounter.Increment = new IntValue(1);
231      generationsCounter.ValueParameter.ActualName = "Generations";
232
233      generationsSinceLastMigrationCounter.Name = "GenerationsSinceLastMigration + 1";
[3609]234      generationsSinceLastMigrationCounter.ValueParameter.ActualName = "GenerationsSinceLastMigration";
235      generationsSinceLastMigrationCounter.Increment = new IntValue(1);
236
[3611]237      migrationComparator.Name = "GenerationsSinceLastMigration = MigrationInterval ?";
[3609]238      migrationComparator.LeftSideParameter.ActualName = "GenerationsSinceLastMigration";
239      migrationComparator.Comparison = new Comparison(ComparisonType.Equal);
240      migrationComparator.RightSideParameter.ActualName = MigrationIntervalParameter.Name;
241      migrationComparator.ResultParameter.ActualName = "Migrate";
242
[3611]243      migrationBranch.Name = "Migrate?";
[3609]244      migrationBranch.ConditionParameter.ActualName = "Migrate";
245
[3611]246      resetGenerationsSinceLastMigrationAssigner.Name = "Reset GenerationsSinceLastMigration";
[3609]247      resetGenerationsSinceLastMigrationAssigner.LeftSideParameter.ActualName = "GenerationsSinceLastMigration";
248      resetGenerationsSinceLastMigrationAssigner.RightSideParameter.Value = new IntValue(0);
249
250      migrationsCounter.Name = "Migrations + 1";
251      migrationsCounter.IncrementParameter.Value = new IntValue(1);
252      migrationsCounter.ValueParameter.ActualName = "Migrations";
253
[3356]254      emigrantsSelector.Name = "Emigrants Selector (placeholder)";
255      emigrantsSelector.OperatorParameter.ActualName = EmigrantsSelectorParameter.Name;
256
257      migrator.Name = "Migrator (placeholder)";
258      migrator.OperatorParameter.ActualName = MigratorParameter.Name;
259
[3601]260      immigrationReplacer.Name = "Immigration Replacer (placeholder)";
261      immigrationReplacer.OperatorParameter.ActualName = ImmigrationReplacerParameter.Name;
[3356]262
[3611]263      generationsComparator.Name = "Generations >= MaximumGenerations ?";
[3609]264      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
265      generationsComparator.LeftSideParameter.ActualName = "Generations";
266      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
267      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]268
[3650]269      analyzer2.Name = "Analyzer (placeholder)";
270      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3356]271
[3650]272      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
273      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
274      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
[3356]275
[3611]276      generationsTerminationCondition.Name = "Terminate?";
[3609]277      generationsTerminationCondition.ConditionParameter.ActualName = "TerminateGenerations";
[3356]278      #endregion
279
280      #region Create operator graph
281      OperatorGraph.InitialOperator = variableCreator;
[3609]282      variableCreator.Successor = uniformSubScopesProcessor0;
283      uniformSubScopesProcessor0.Operator = islandVariableCreator;
[3650]284      uniformSubScopesProcessor0.Successor = analyzer1;
285      islandVariableCreator.Successor = islandAnalyzer1;
286      islandAnalyzer1.Successor = null;
287      analyzer1.Successor = resultsCollector1;
288      resultsCollector1.Successor = resultsCollector2;
289      resultsCollector2.Successor = uniformSubScopesProcessor1;
[3609]290      uniformSubScopesProcessor1.Operator = selector;
[3611]291      uniformSubScopesProcessor1.Successor = generationsCounter;
[3609]292      selector.Successor = subScopesProcessor1;
293      subScopesProcessor1.Operators.Add(new EmptyOperator());
294      subScopesProcessor1.Operators.Add(childrenCreator);
295      subScopesProcessor1.Successor = subScopesProcessor2;
296      childrenCreator.Successor = uniformSubScopesProcessor2;
297      uniformSubScopesProcessor2.Operator = crossover;
[5208]298      uniformSubScopesProcessor2.Successor = uniformSubScopesProcessor3;
[3609]299      crossover.Successor = stochasticBranch;
300      stochasticBranch.FirstBranch = mutator;
301      stochasticBranch.SecondBranch = null;
[5208]302      stochasticBranch.Successor = subScopesRemover;
[3609]303      mutator.Successor = null;
304      subScopesRemover.Successor = null;
[5208]305      uniformSubScopesProcessor3.Operator = evaluator;
306      uniformSubScopesProcessor3.Successor = null;
307      evaluator.Successor = null;
[3609]308      subScopesProcessor2.Operators.Add(bestSelector);
309      subScopesProcessor2.Operators.Add(new EmptyOperator());
310      subScopesProcessor2.Successor = mergingReducer;
311      bestSelector.Successor = rightReducer;
312      rightReducer.Successor = null;
[3650]313      mergingReducer.Successor = islandAnalyzer2;
[3616]314      islandAnalyzer2.Successor = null;
[3611]315      generationsCounter.Successor = generationsSinceLastMigrationCounter;
[3609]316      generationsSinceLastMigrationCounter.Successor = migrationComparator;
317      migrationComparator.Successor = migrationBranch;
318      migrationBranch.TrueBranch = resetGenerationsSinceLastMigrationAssigner;
319      migrationBranch.FalseBranch = null;
[3611]320      migrationBranch.Successor = generationsComparator;
[3609]321      resetGenerationsSinceLastMigrationAssigner.Successor = migrationsCounter;
[5208]322      migrationsCounter.Successor = uniformSubScopesProcessor4;
323      uniformSubScopesProcessor4.Operator = emigrantsSelector;
324      uniformSubScopesProcessor4.Successor = migrator;
325      migrator.Successor = uniformSubScopesProcessor5;
326      uniformSubScopesProcessor5.Operator = immigrationReplacer;
327      uniformSubScopesProcessor5.Successor = null;
[3650]328      generationsComparator.Successor = analyzer2;
329      analyzer2.Successor = resultsCollector3;
330      resultsCollector3.Successor = generationsTerminationCondition;
[3609]331      generationsTerminationCondition.TrueBranch = null;
332      generationsTerminationCondition.FalseBranch = uniformSubScopesProcessor1;
333      generationsTerminationCondition.Successor = null;
[3356]334      #endregion
335    }
[3715]336
337    public override IOperation Apply() {
338      if (CrossoverParameter.ActualValue == null)
339        return null;
340      return base.Apply();
341    }
[3356]342  }
343}
Note: See TracBrowser for help on using the repository browser.