Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithmMainLoop.cs @ 5740

Last change on this file since 5740 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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