Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithmMainLoop.cs @ 7729

Last change on this file since 7729 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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