Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithmMainLoop.cs @ 4669

Last change on this file since 4669 was 4669, checked in by mkommend, 13 years ago

Refactored Algorithms.* and fixed BoolValue (ticket #922).

File size: 18.8 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.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("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    }
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    #endregion
104
105    [StorableConstructor]
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    }
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."));
118      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
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."));
125      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationReplacer", "Replaces some of the original population with the immigrants."));
126      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
127      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
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."));
132      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
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."));
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."));
137      #endregion
138
139      #region Create operators
140      VariableCreator variableCreator = new VariableCreator();
141      UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor();
142      VariableCreator islandVariableCreator = new VariableCreator();
143      Placeholder islandAnalyzer1 = new Placeholder();
144      Placeholder analyzer1 = new Placeholder();
145      ResultsCollector resultsCollector1 = new ResultsCollector();
146      ResultsCollector resultsCollector2 = new ResultsCollector();
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();
155      Placeholder evaluator = new Placeholder();
156      SubScopesRemover subScopesRemover = new SubScopesRemover();
157      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
158      BestSelector bestSelector = new BestSelector();
159      RightReducer rightReducer = new RightReducer();
160      MergingReducer mergingReducer = new MergingReducer();
161      Placeholder islandAnalyzer2 = new Placeholder();
162      IntCounter generationsCounter = new IntCounter();
163      IntCounter generationsSinceLastMigrationCounter = new IntCounter();
164      Comparator migrationComparator = new Comparator();
165      ConditionalBranch migrationBranch = new ConditionalBranch();
166      Assigner resetGenerationsSinceLastMigrationAssigner = new Assigner();
167      IntCounter migrationsCounter = new IntCounter();
168      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
169      Placeholder emigrantsSelector = new Placeholder();
170      Placeholder migrator = new Placeholder();
171      UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
172      Placeholder immigrationReplacer = new Placeholder();
173      Comparator generationsComparator = new Comparator();
174      Placeholder analyzer2 = new Placeholder();
175      ResultsCollector resultsCollector3 = new ResultsCollector();
176      ConditionalBranch generationsTerminationCondition = new ConditionalBranch();
177
178      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations", new IntValue(0)));
179      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastMigration", new IntValue(0)));
180      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class IslandGeneticAlgorithm expects this to be called Generations
181
182      islandVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
183
184      islandAnalyzer1.Name = "Island Analyzer (placeholder)";
185      islandAnalyzer1.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
186
187      analyzer1.Name = "Analyzer (placeholder)";
188      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
189
190      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
191      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
192      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
193
194      resultsCollector2.Name = "Reference Island Results";
195      resultsCollector2.CopyValue = new BoolValue(false);
196      resultsCollector2.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IslandResults", "Result set for each island", "Results"));
197      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
198
199      selector.Name = "Selector (placeholder)";
200      selector.OperatorParameter.ActualName = SelectorParameter.Name;
201
202      childrenCreator.ParentsPerChild = new IntValue(2);
203
204      crossover.Name = "Crossover (placeholder)";
205      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
206
207      stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
208      stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
209
210      mutator.Name = "Mutator (placeholder)";
211      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
212
213      evaluator.Name = "Evaluator (placeholder)";
214      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
215
216      subScopesRemover.RemoveAllSubScopes = true;
217
218      bestSelector.CopySelected = new BoolValue(false);
219      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
220      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
221      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
222
223      islandAnalyzer2.Name = "Island Analyzer (placeholder)";
224      islandAnalyzer2.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
225
226      generationsCounter.Name = "Generations + 1";
227      generationsCounter.Increment = new IntValue(1);
228      generationsCounter.ValueParameter.ActualName = "Generations";
229
230      generationsSinceLastMigrationCounter.Name = "GenerationsSinceLastMigration + 1";
231      generationsSinceLastMigrationCounter.ValueParameter.ActualName = "GenerationsSinceLastMigration";
232      generationsSinceLastMigrationCounter.Increment = new IntValue(1);
233
234      migrationComparator.Name = "GenerationsSinceLastMigration = MigrationInterval ?";
235      migrationComparator.LeftSideParameter.ActualName = "GenerationsSinceLastMigration";
236      migrationComparator.Comparison = new Comparison(ComparisonType.Equal);
237      migrationComparator.RightSideParameter.ActualName = MigrationIntervalParameter.Name;
238      migrationComparator.ResultParameter.ActualName = "Migrate";
239
240      migrationBranch.Name = "Migrate?";
241      migrationBranch.ConditionParameter.ActualName = "Migrate";
242
243      resetGenerationsSinceLastMigrationAssigner.Name = "Reset GenerationsSinceLastMigration";
244      resetGenerationsSinceLastMigrationAssigner.LeftSideParameter.ActualName = "GenerationsSinceLastMigration";
245      resetGenerationsSinceLastMigrationAssigner.RightSideParameter.Value = new IntValue(0);
246
247      migrationsCounter.Name = "Migrations + 1";
248      migrationsCounter.IncrementParameter.Value = new IntValue(1);
249      migrationsCounter.ValueParameter.ActualName = "Migrations";
250
251      emigrantsSelector.Name = "Emigrants Selector (placeholder)";
252      emigrantsSelector.OperatorParameter.ActualName = EmigrantsSelectorParameter.Name;
253
254      migrator.Name = "Migrator (placeholder)";
255      migrator.OperatorParameter.ActualName = MigratorParameter.Name;
256
257      immigrationReplacer.Name = "Immigration Replacer (placeholder)";
258      immigrationReplacer.OperatorParameter.ActualName = ImmigrationReplacerParameter.Name;
259
260      generationsComparator.Name = "Generations >= MaximumGenerations ?";
261      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
262      generationsComparator.LeftSideParameter.ActualName = "Generations";
263      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
264      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
265
266      analyzer2.Name = "Analyzer (placeholder)";
267      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
268
269      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
270      resultsCollector3.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
271      resultsCollector3.ResultsParameter.ActualName = ResultsParameter.Name;
272
273      generationsTerminationCondition.Name = "Terminate?";
274      generationsTerminationCondition.ConditionParameter.ActualName = "TerminateGenerations";
275      #endregion
276
277      #region Create operator graph
278      OperatorGraph.InitialOperator = variableCreator;
279      variableCreator.Successor = uniformSubScopesProcessor0;
280      uniformSubScopesProcessor0.Operator = islandVariableCreator;
281      uniformSubScopesProcessor0.Successor = analyzer1;
282      islandVariableCreator.Successor = islandAnalyzer1;
283      islandAnalyzer1.Successor = null;
284      analyzer1.Successor = resultsCollector1;
285      resultsCollector1.Successor = resultsCollector2;
286      resultsCollector2.Successor = uniformSubScopesProcessor1;
287      uniformSubScopesProcessor1.Operator = selector;
288      uniformSubScopesProcessor1.Successor = generationsCounter;
289      selector.Successor = subScopesProcessor1;
290      subScopesProcessor1.Operators.Add(new EmptyOperator());
291      subScopesProcessor1.Operators.Add(childrenCreator);
292      subScopesProcessor1.Successor = subScopesProcessor2;
293      childrenCreator.Successor = uniformSubScopesProcessor2;
294      uniformSubScopesProcessor2.Operator = crossover;
295      uniformSubScopesProcessor2.Successor = null;
296      crossover.Successor = stochasticBranch;
297      stochasticBranch.FirstBranch = mutator;
298      stochasticBranch.SecondBranch = null;
299      stochasticBranch.Successor = evaluator;
300      mutator.Successor = null;
301      evaluator.Successor = subScopesRemover;
302      subScopesRemover.Successor = null;
303      subScopesProcessor2.Operators.Add(bestSelector);
304      subScopesProcessor2.Operators.Add(new EmptyOperator());
305      subScopesProcessor2.Successor = mergingReducer;
306      bestSelector.Successor = rightReducer;
307      rightReducer.Successor = null;
308      mergingReducer.Successor = islandAnalyzer2;
309      islandAnalyzer2.Successor = null;
310      generationsCounter.Successor = generationsSinceLastMigrationCounter;
311      generationsSinceLastMigrationCounter.Successor = migrationComparator;
312      migrationComparator.Successor = migrationBranch;
313      migrationBranch.TrueBranch = resetGenerationsSinceLastMigrationAssigner;
314      migrationBranch.FalseBranch = null;
315      migrationBranch.Successor = generationsComparator;
316      resetGenerationsSinceLastMigrationAssigner.Successor = migrationsCounter;
317      migrationsCounter.Successor = uniformSubScopesProcessor3;
318      uniformSubScopesProcessor3.Operator = emigrantsSelector;
319      uniformSubScopesProcessor3.Successor = migrator;
320      migrator.Successor = uniformSubScopesProcessor4;
321      uniformSubScopesProcessor4.Operator = immigrationReplacer;
322      uniformSubScopesProcessor4.Successor = null;
323      generationsComparator.Successor = analyzer2;
324      analyzer2.Successor = resultsCollector3;
325      resultsCollector3.Successor = generationsTerminationCondition;
326      generationsTerminationCondition.TrueBranch = null;
327      generationsTerminationCondition.FalseBranch = uniformSubScopesProcessor1;
328      generationsTerminationCondition.Successor = null;
329      #endregion
330    }
331
332    public override IOperation Apply() {
333      if (CrossoverParameter.ActualValue == null)
334        return null;
335      return base.Apply();
336    }
337  }
338}
Note: See TracBrowser for help on using the repository browser.