Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithmMainLoop.cs @ 10217

Last change on this file since 10217 was 7401, checked in by ascheibe, 13 years ago

#1744 updated branch from trunk

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