Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3721 was 3715, checked in by abeham, 15 years ago

#893

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