Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 20.4 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 SubScopesLookupParameter<DoubleValue> QualityParameter {
52      get { return (SubScopesLookupParameter<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> ImmigrationSelectorParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["ImmigrationSelector"]; }
74    }
75    public ValueLookupParameter<IntValue> PopulationSizeParameter {
76      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
77    }
78    public ValueLookupParameter<IntValue> MaximumMigrationsParameter {
79      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumMigrations"]; }
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> VisualizerParameter {
103      get { return (ValueLookupParameter<IOperator>)Parameters["Visualizer"]; }
104    }
105    public LookupParameter<IItem> VisualizationParameter {
106      get { return (LookupParameter<IItem>)Parameters["Visualization"]; }
107    }
108    #endregion
109
110    /*#region Properties
111    private GeneticAlgorithmMainLoop GeneticAlgorithmMainLoop {
112      get { return (GeneticAlgorithmMainLoop)((UniformSubScopesProcessor)OperatorGraph.InitialOperator).Operator; }
113    }
114    #endregion*/
115
116    [StorableConstructor]
117    private IslandGeneticAlgorithmMainLoop(bool deserializing) : base() { }
118    public IslandGeneticAlgorithmMainLoop()
119      : base() {
120      #region Create parameters
121      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
122      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
123      Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
124      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
125      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfIslands", "The number of islands."));
126      Parameters.Add(new ValueLookupParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases."));
127      Parameters.Add(new ValueLookupParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands."));
128      Parameters.Add(new ValueLookupParameter<IOperator>("Migrator", "The migration strategy."));
129      Parameters.Add(new ValueLookupParameter<IOperator>("EmigrantsSelector", "Selects the individuals that will be migrated."));
130      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationSelector", "Selects the population from the unification of the original population and the immigrants."));
131      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumMigrations", "The maximum number of migrations after which the operator should terminate."));
133      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
134      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
135      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
136      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
137      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
138      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
139      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The results collection to store the results."));
140      Parameters.Add(new ValueLookupParameter<IOperator>("Visualizer", "The operator used to visualize solutions."));
141      Parameters.Add(new LookupParameter<IItem>("Visualization", "The item which represents the visualization of solutions."));
142      #endregion
143
144      #region Create operators
145      VariableCreator variableCreator = new VariableCreator();
146      UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
147      VariableCreator resultCollectionCreator = new VariableCreator();
148      BestQualityMemorizer bestQualityMemorizer1 = new BestQualityMemorizer();
149      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator1 = new BestAverageWorstQualityCalculator();
150      BestQualityMemorizer bestQualityMemorizer2 = new BestQualityMemorizer();
151      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator2 = new BestAverageWorstQualityCalculator();
152      DataTableValuesCollector dataTableValuesCollector1 = new DataTableValuesCollector();
153      QualityDifferenceCalculator qualityDifferenceCalculator1 = new QualityDifferenceCalculator();
154      ResultsCollector resultsCollector = new ResultsCollector();
155      UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
156      GeneticAlgorithmMainLoop geneticAlgorithmMainLoop = new GeneticAlgorithmMainLoop();
157      Placeholder emigrantsSelector = new Placeholder();
158      Placeholder migrator = new Placeholder();
159      UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
160      MergingReducer mergingReducer = new MergingReducer();
161      Placeholder immigrationSelector = new Placeholder();
162      RightReducer rightReducer = new RightReducer();
163      IntCounter migrationsCounter = new IntCounter();
164      Comparator maxMigrationsComparator = new Comparator();
165      BestQualityMemorizer bestQualityMemorizer3 = new BestQualityMemorizer();
166      BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator3 = new BestAverageWorstQualityCalculator();
167      DataTableValuesCollector dataTableValuesCollector2 = new DataTableValuesCollector();
168      QualityDifferenceCalculator qualityDifferenceCalculator2 = new QualityDifferenceCalculator();
169      ConditionalBranch migrationTerminationCondition = new ConditionalBranch();
170
171      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations", new IntValue(0)));
172
173      resultCollectionCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("IslandResults", new ResultCollection()));
174
175      bestQualityMemorizer1.BestQualityParameter.ActualName = "BestQuality";
176      bestQualityMemorizer1.MaximizationParameter.ActualName = MaximizationParameter.Name;
177      bestQualityMemorizer1.QualityParameter.ActualName = QualityParameter.Name;
178
179      bestAverageWorstQualityCalculator1.AverageQualityParameter.ActualName = "CurrentAverageQuality";
180      bestAverageWorstQualityCalculator1.BestQualityParameter.ActualName = "CurrentBestQuality";
181      bestAverageWorstQualityCalculator1.MaximizationParameter.ActualName = MaximizationParameter.Name;
182      bestAverageWorstQualityCalculator1.QualityParameter.ActualName = QualityParameter.Name;
183      bestAverageWorstQualityCalculator1.WorstQualityParameter.ActualName = "CurrentWorstQuality";
184
185      bestQualityMemorizer2.BestQualityParameter.ActualName = "BestQuality";
186      bestQualityMemorizer2.MaximizationParameter.ActualName = MaximizationParameter.Name;
187      bestQualityMemorizer2.QualityParameter.ActualName = "BestQuality";
188
189      bestAverageWorstQualityCalculator2.AverageQualityParameter.ActualName = "CurrentAverageBestQuality";
190      bestAverageWorstQualityCalculator2.BestQualityParameter.ActualName = "CurrentBestBestQuality";
191      bestAverageWorstQualityCalculator2.MaximizationParameter.ActualName = MaximizationParameter.Name;
192      bestAverageWorstQualityCalculator2.QualityParameter.ActualName = "CurrentBestQuality";
193      bestAverageWorstQualityCalculator2.WorstQualityParameter.ActualName = "CurrentWorstBestQuality";
194
195      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best BestQuality", null, "CurrentBestBestQuality"));
196      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average BestQuality", null, "CurrentAverageBestQuality"));
197      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst BestQuality", null, "CurrentWorstBestQuality"));
198      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
199      dataTableValuesCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
200      dataTableValuesCollector1.DataTableParameter.ActualName = "BestQualities";
201
202      qualityDifferenceCalculator1.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
203      qualityDifferenceCalculator1.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
204      qualityDifferenceCalculator1.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
205      qualityDifferenceCalculator1.SecondQualityParameter.ActualName = "BestQuality";
206
207      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
208      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best BestQuality", null, "CurrentBestBestQuality"));
209      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average BestQuality", null, "CurrentAverageBestQuality"));
210      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst BestQuality", null, "CurrentWorstBestQuality"));
211      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
212      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
213      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Absolute Difference of Best Known Quality to Best Quality", null, "AbsoluteDifferenceBestKnownToBest"));
214      resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>("Relative Difference of Best Known Quality to Best Quality", null, "RelativeDifferenceBestKnownToBest"));
215      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("BestQualities"));
216      resultsCollector.CollectedValues.Add(new SubScopesLookupParameter<ResultCollection>("IslandResults", "Result set for each island"));
217      resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;
218
219      geneticAlgorithmMainLoop.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
220      geneticAlgorithmMainLoop.MaximizationParameter.ActualName = MaximizationParameter.Name;
221      geneticAlgorithmMainLoop.QualityParameter.ActualName = QualityParameter.Name;
222      geneticAlgorithmMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
223      geneticAlgorithmMainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
224      geneticAlgorithmMainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
225      geneticAlgorithmMainLoop.MaximumGenerationsParameter.ActualName = MigrationIntervalParameter.Name;
226      geneticAlgorithmMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
227      geneticAlgorithmMainLoop.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
228      geneticAlgorithmMainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
229      geneticAlgorithmMainLoop.RandomParameter.ActualName = RandomParameter.Name;
230      geneticAlgorithmMainLoop.ResultsParameter.ActualName = "IslandResults";
231      geneticAlgorithmMainLoop.VisualizerParameter.ActualName = VisualizerParameter.Name;
232      geneticAlgorithmMainLoop.VisualizationParameter.ActualName = VisualizationParameter.Name;
233
234      emigrantsSelector.Name = "Emigrants Selector (placeholder)";
235      emigrantsSelector.OperatorParameter.ActualName = EmigrantsSelectorParameter.Name;
236
237      migrator.Name = "Migrator (placeholder)";
238      migrator.OperatorParameter.ActualName = MigratorParameter.Name;
239
240      immigrationSelector.Name = "Immigration Selector (placeholder)";
241      immigrationSelector.OperatorParameter.ActualName = ImmigrationSelectorParameter.Name;
242
243      migrationsCounter.Name = "Migrations + 1";
244      migrationsCounter.IncrementParameter.Value = new IntValue(1);
245      migrationsCounter.ValueParameter.ActualName = "Migrations";
246
247      maxMigrationsComparator.Name = "Migrations >= MaximumMigration ?";
248      maxMigrationsComparator.LeftSideParameter.ActualName = "Migrations";
249      maxMigrationsComparator.RightSideParameter.ActualName = MaximumMigrationsParameter.Name;
250      maxMigrationsComparator.Comparison.Value = ComparisonType.GreaterOrEqual;
251      maxMigrationsComparator.ResultParameter.ActualName = "MigrationTerminate";
252
253      bestQualityMemorizer3.BestQualityParameter.ActualName = "BestQuality";
254      bestQualityMemorizer3.MaximizationParameter.ActualName = MaximizationParameter.Name;
255      bestQualityMemorizer3.QualityParameter.ActualName = "BestQuality";
256
257      bestAverageWorstQualityCalculator3.AverageQualityParameter.ActualName = "CurrentAverageBestQuality";
258      bestAverageWorstQualityCalculator3.BestQualityParameter.ActualName = "CurrentBestBestQuality";
259      bestAverageWorstQualityCalculator3.MaximizationParameter.ActualName = MaximizationParameter.Name;
260      bestAverageWorstQualityCalculator3.QualityParameter.ActualName = "CurrentBestQuality";
261      bestAverageWorstQualityCalculator3.WorstQualityParameter.ActualName = "CurrentWorstBestQuality";
262
263      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Best BestQuality", null, "CurrentBestBestQuality"));
264      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Average BestQuality", null, "CurrentAverageBestQuality"));
265      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Current Worst BestQuality", null, "CurrentWorstBestQuality"));
266      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
267      dataTableValuesCollector2.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Known Quality", null, BestKnownQualityParameter.Name));
268      dataTableValuesCollector2.DataTableParameter.ActualName = "BestQualities";
269
270      qualityDifferenceCalculator2.AbsoluteDifferenceParameter.ActualName = "AbsoluteDifferenceBestKnownToBest";
271      qualityDifferenceCalculator2.FirstQualityParameter.ActualName = BestKnownQualityParameter.Name;
272      qualityDifferenceCalculator2.RelativeDifferenceParameter.ActualName = "RelativeDifferenceBestKnownToBest";
273      qualityDifferenceCalculator2.SecondQualityParameter.ActualName = "BestQuality";
274
275      migrationTerminationCondition.ConditionParameter.ActualName = "MigrationTerminate";
276      #endregion
277
278      #region Create operator graph
279      OperatorGraph.InitialOperator = variableCreator;
280      variableCreator.Successor = ussp0;
281      ussp0.Operator = resultCollectionCreator;
282      ussp0.Successor = bestQualityMemorizer2;
283      resultCollectionCreator.Successor = bestQualityMemorizer1;
284      bestQualityMemorizer1.Successor = bestAverageWorstQualityCalculator1;
285      bestQualityMemorizer2.Successor = bestAverageWorstQualityCalculator2;
286      bestAverageWorstQualityCalculator2.Successor = dataTableValuesCollector1;
287      dataTableValuesCollector1.Successor = qualityDifferenceCalculator1;
288      qualityDifferenceCalculator1.Successor = resultsCollector;
289      resultsCollector.Successor = ussp1;
290      ussp1.Operator = geneticAlgorithmMainLoop;
291      ussp1.Successor = migrator;
292      geneticAlgorithmMainLoop.Successor = emigrantsSelector;
293      emigrantsSelector.Successor = null;
294      migrator.Successor = ussp2;
295      ussp2.Operator = mergingReducer;
296      ussp2.Successor = migrationsCounter;
297      mergingReducer.Successor = immigrationSelector;
298      immigrationSelector.Successor = rightReducer;
299      rightReducer.Successor = null;
300      migrationsCounter.Successor = maxMigrationsComparator;
301      maxMigrationsComparator.Successor = bestQualityMemorizer3;
302      bestQualityMemorizer3.Successor = bestAverageWorstQualityCalculator3;
303      bestAverageWorstQualityCalculator3.Successor = dataTableValuesCollector2;
304      dataTableValuesCollector2.Successor = qualityDifferenceCalculator2;
305      qualityDifferenceCalculator2.Successor = migrationTerminationCondition;
306      migrationTerminationCondition.FalseBranch = ussp1;
307      migrationTerminationCondition.TrueBranch = null;
308      migrationTerminationCondition.Successor = null;
309      #endregion
310    }
311  }
312}
Note: See TracBrowser for help on using the repository browser.