Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 19.2 KB
RevLine 
[3356]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3356]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
[4722]22using HeuristicLab.Common;
[3356]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    }
[3659]46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3356]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    }
[3601]67    public ValueLookupParameter<IOperator> ImmigrationReplacerParameter {
68      get { return (ValueLookupParameter<IOperator>)Parameters["ImmigrationReplacer"]; }
[3356]69    }
70    public ValueLookupParameter<IntValue> PopulationSizeParameter {
71      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
72    }
[3609]73    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
74      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
[3356]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    }
[3616]97    public ValueLookupParameter<IOperator> AnalyzerParameter {
98      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3356]99    }
[3650]100    public ValueLookupParameter<IOperator> IslandAnalyzerParameter {
101      get { return (ValueLookupParameter<IOperator>)Parameters["IslandAnalyzer"]; }
102    }
[5351]103    public LookupParameter<IntValue> EvaluatedSolutionsParameter {
104      get { return (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
105    }
[3356]106    #endregion
107
108    [StorableConstructor]
[4722]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    }
[3356]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."));
[3659]121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[3356]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."));
[3601]128      Parameters.Add(new ValueLookupParameter<IOperator>("ImmigrationReplacer", "Replaces some of the original population with the immigrants."));
[3356]129      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions."));
[3609]130      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations that the algorithm should process."));
[3356]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."));
[5208]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."));
[3356]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."));
[3650]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."));
[5351]140      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times a solution has been evaluated."));
[3356]141      #endregion
142
143      #region Create operators
144      VariableCreator variableCreator = new VariableCreator();
[3609]145      UniformSubScopesProcessor uniformSubScopesProcessor0 = new UniformSubScopesProcessor();
146      VariableCreator islandVariableCreator = new VariableCreator();
[3616]147      Placeholder islandAnalyzer1 = new Placeholder();
[3650]148      Placeholder analyzer1 = new Placeholder();
149      ResultsCollector resultsCollector1 = new ResultsCollector();
[3609]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();
[5208]158      SubScopesRemover subScopesRemover = new SubScopesRemover();
159      UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor();
[3609]160      Placeholder evaluator = new Placeholder();
[5356]161      SubScopesCounter subScopesCounter = new SubScopesCounter();
[3609]162      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
163      BestSelector bestSelector = new BestSelector();
164      RightReducer rightReducer = new RightReducer();
165      MergingReducer mergingReducer = new MergingReducer();
[5351]166      IntCounter generationsCounter = new IntCounter();
167      UniformSubScopesProcessor uniformSubScopesProcessor4 = new UniformSubScopesProcessor();
[3616]168      Placeholder islandAnalyzer2 = new Placeholder();
[3609]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();
[5351]174      UniformSubScopesProcessor uniformSubScopesProcessor5 = new UniformSubScopesProcessor();
[3356]175      Placeholder emigrantsSelector = new Placeholder();
176      Placeholder migrator = new Placeholder();
[5351]177      UniformSubScopesProcessor uniformSubScopesProcessor6 = new UniformSubScopesProcessor();
[3601]178      Placeholder immigrationReplacer = new Placeholder();
[3609]179      Comparator generationsComparator = new Comparator();
[3650]180      Placeholder analyzer2 = new Placeholder();
[3609]181      ConditionalBranch generationsTerminationCondition = new ConditionalBranch();
[3356]182
183      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Migrations", new IntValue(0)));
[3609]184      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("GenerationsSinceLastMigration", new IntValue(0)));
[3750]185      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class IslandGeneticAlgorithm expects this to be called Generations
[3356]186
[3650]187      islandVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
[3356]188
[3650]189      islandAnalyzer1.Name = "Island Analyzer (placeholder)";
190      islandAnalyzer1.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
[3609]191
[3650]192      analyzer1.Name = "Analyzer (placeholder)";
193      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3609]194
[3650]195      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Migrations"));
196      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
[5351]197      resultsCollector1.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IslandResults", "Result set for each island", "Results"));
[3650]198      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
[3609]199
200      selector.Name = "Selector (placeholder)";
201      selector.OperatorParameter.ActualName = SelectorParameter.Name;
[3356]202
[3609]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
[5208]214      subScopesRemover.RemoveAllSubScopes = true;
215
216      uniformSubScopesProcessor3.Parallel.Value = true;
217
[3609]218      evaluator.Name = "Evaluator (placeholder)";
219      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
220
[5356]221      subScopesCounter.Name = "Increment EvaluatedSolutions";
222      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
223
[3609]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
[3650]229      islandAnalyzer2.Name = "Island Analyzer (placeholder)";
230      islandAnalyzer2.OperatorParameter.ActualName = IslandAnalyzerParameter.Name;
[3609]231
[3611]232      generationsCounter.Name = "Generations + 1";
233      generationsCounter.Increment = new IntValue(1);
234      generationsCounter.ValueParameter.ActualName = "Generations";
235
236      generationsSinceLastMigrationCounter.Name = "GenerationsSinceLastMigration + 1";
[3609]237      generationsSinceLastMigrationCounter.ValueParameter.ActualName = "GenerationsSinceLastMigration";
238      generationsSinceLastMigrationCounter.Increment = new IntValue(1);
239
[3611]240      migrationComparator.Name = "GenerationsSinceLastMigration = MigrationInterval ?";
[3609]241      migrationComparator.LeftSideParameter.ActualName = "GenerationsSinceLastMigration";
242      migrationComparator.Comparison = new Comparison(ComparisonType.Equal);
243      migrationComparator.RightSideParameter.ActualName = MigrationIntervalParameter.Name;
244      migrationComparator.ResultParameter.ActualName = "Migrate";
245
[3611]246      migrationBranch.Name = "Migrate?";
[3609]247      migrationBranch.ConditionParameter.ActualName = "Migrate";
248
[3611]249      resetGenerationsSinceLastMigrationAssigner.Name = "Reset GenerationsSinceLastMigration";
[3609]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
[3356]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
[3601]263      immigrationReplacer.Name = "Immigration Replacer (placeholder)";
264      immigrationReplacer.OperatorParameter.ActualName = ImmigrationReplacerParameter.Name;
[3356]265
[3611]266      generationsComparator.Name = "Generations >= MaximumGenerations ?";
[3609]267      generationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
268      generationsComparator.LeftSideParameter.ActualName = "Generations";
269      generationsComparator.ResultParameter.ActualName = "TerminateGenerations";
270      generationsComparator.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
[3356]271
[3650]272      analyzer2.Name = "Analyzer (placeholder)";
273      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
[3356]274
[3611]275      generationsTerminationCondition.Name = "Terminate?";
[3609]276      generationsTerminationCondition.ConditionParameter.ActualName = "TerminateGenerations";
[3356]277      #endregion
278
279      #region Create operator graph
280      OperatorGraph.InitialOperator = variableCreator;
[3609]281      variableCreator.Successor = uniformSubScopesProcessor0;
282      uniformSubScopesProcessor0.Operator = islandVariableCreator;
[3650]283      uniformSubScopesProcessor0.Successor = analyzer1;
284      islandVariableCreator.Successor = islandAnalyzer1;
285      islandAnalyzer1.Successor = null;
286      analyzer1.Successor = resultsCollector1;
[5351]287      resultsCollector1.Successor = uniformSubScopesProcessor1;
[3609]288      uniformSubScopesProcessor1.Operator = selector;
[3611]289      uniformSubScopesProcessor1.Successor = generationsCounter;
[3609]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;
[5208]296      uniformSubScopesProcessor2.Successor = uniformSubScopesProcessor3;
[3609]297      crossover.Successor = stochasticBranch;
298      stochasticBranch.FirstBranch = mutator;
299      stochasticBranch.SecondBranch = null;
[5208]300      stochasticBranch.Successor = subScopesRemover;
[3609]301      mutator.Successor = null;
302      subScopesRemover.Successor = null;
[5208]303      uniformSubScopesProcessor3.Operator = evaluator;
[5351]304      uniformSubScopesProcessor3.Successor = subScopesCounter;
[5208]305      evaluator.Successor = null;
[5351]306      subScopesCounter.Successor = null;
[3609]307      subScopesProcessor2.Operators.Add(bestSelector);
308      subScopesProcessor2.Operators.Add(new EmptyOperator());
309      subScopesProcessor2.Successor = mergingReducer;
310      bestSelector.Successor = rightReducer;
311      rightReducer.Successor = null;
[5351]312      mergingReducer.Successor = null;
313      generationsCounter.Successor = uniformSubScopesProcessor4;
314      uniformSubScopesProcessor4.Operator = islandAnalyzer2;
315      uniformSubScopesProcessor4.Successor = generationsSinceLastMigrationCounter;
[3616]316      islandAnalyzer2.Successor = null;
[3609]317      generationsSinceLastMigrationCounter.Successor = migrationComparator;
318      migrationComparator.Successor = migrationBranch;
319      migrationBranch.TrueBranch = resetGenerationsSinceLastMigrationAssigner;
320      migrationBranch.FalseBranch = null;
[3611]321      migrationBranch.Successor = generationsComparator;
[3609]322      resetGenerationsSinceLastMigrationAssigner.Successor = migrationsCounter;
[5351]323      migrationsCounter.Successor = uniformSubScopesProcessor5;
324      uniformSubScopesProcessor5.Operator = emigrantsSelector;
325      uniformSubScopesProcessor5.Successor = migrator;
326      migrator.Successor = uniformSubScopesProcessor6;
327      uniformSubScopesProcessor6.Operator = immigrationReplacer;
328      uniformSubScopesProcessor6.Successor = null;
[3650]329      generationsComparator.Successor = analyzer2;
[5356]330      analyzer2.Successor = generationsTerminationCondition;
[3609]331      generationsTerminationCondition.TrueBranch = null;
332      generationsTerminationCondition.FalseBranch = uniformSubScopesProcessor1;
333      generationsTerminationCondition.Successor = null;
[3356]334      #endregion
335    }
[3715]336
337    public override IOperation Apply() {
338      if (CrossoverParameter.ActualValue == null)
339        return null;
340      return base.Apply();
341    }
[3356]342  }
343}
Note: See TracBrowser for help on using the repository browser.