Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/GeneticAlgorithmMainLoop.cs @ 5119

Last change on this file since 5119 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 10.8 KB
RevLine 
[2830]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
[4722]22using HeuristicLab.Common;
[2830]23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
[3021]26using HeuristicLab.Optimization.Operators;
[2830]27using HeuristicLab.Parameters;
[3000]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2830]29using HeuristicLab.Selection;
30
[3196]31namespace HeuristicLab.Algorithms.GeneticAlgorithm {
[2830]32  /// <summary>
[3198]33  /// An operator which represents the main loop of a genetic algorithm.
[2830]34  /// </summary>
[3198]35  [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
[3017]36  [StorableClass]
[3198]37  public sealed class GeneticAlgorithmMainLoop : AlgorithmOperator {
[2830]38    #region Parameter properties
39    public ValueLookupParameter<IRandom> RandomParameter {
40      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
41    }
[3048]42    public ValueLookupParameter<BoolValue> MaximizationParameter {
43      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
[2830]44    }
[3659]45    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
46      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[2830]47    }
[2882]48    public ValueLookupParameter<IOperator> SelectorParameter {
49      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
[2830]50    }
[2882]51    public ValueLookupParameter<IOperator> CrossoverParameter {
52      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
53    }
[3095]54    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
55      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
[2830]56    }
[2882]57    public ValueLookupParameter<IOperator> MutatorParameter {
58      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
[2830]59    }
[2882]60    public ValueLookupParameter<IOperator> EvaluatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
[2830]62    }
[3048]63    public ValueLookupParameter<IntValue> ElitesParameter {
64      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
[2830]65    }
[3048]66    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
67      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
[2830]68    }
[2882]69    public ValueLookupParameter<VariableCollection> ResultsParameter {
70      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
71    }
[3616]72    public ValueLookupParameter<IOperator> AnalyzerParameter {
73      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
[3107]74    }
[2830]75    private ScopeParameter CurrentScopeParameter {
76      get { return (ScopeParameter)Parameters["CurrentScope"]; }
77    }
78
79    public IScope CurrentScope {
80      get { return CurrentScopeParameter.ActualValue; }
81    }
82    #endregion
83
[3080]84    [StorableConstructor]
[4722]85    private GeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
86    private GeneticAlgorithmMainLoop(GeneticAlgorithmMainLoop original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new GeneticAlgorithmMainLoop(this, cloner);
91    }
[3198]92    public GeneticAlgorithmMainLoop()
[2830]93      : base() {
[3080]94      Initialize();
95    }
96
97    private void Initialize() {
[2830]98      #region Create parameters
99      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
[3048]100      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
[3659]101      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
[2882]102      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
103      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
[3095]104      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
[2882]105      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
106      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
[3048]107      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
108      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
[2882]109      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
[3616]110      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
[3198]111      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
[2830]112      #endregion
113
[3080]114      #region Create operators
[2908]115      VariableCreator variableCreator = new VariableCreator();
[3616]116      ResultsCollector resultsCollector1 = new ResultsCollector();
117      Placeholder analyzer1 = new Placeholder();
[2882]118      Placeholder selector = new Placeholder();
[3193]119      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
[2830]120      ChildrenCreator childrenCreator = new ChildrenCreator();
[3193]121      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
[2830]122      Placeholder crossover = new Placeholder();
123      StochasticBranch stochasticBranch = new StochasticBranch();
124      Placeholder mutator = new Placeholder();
125      Placeholder evaluator = new Placeholder();
126      SubScopesRemover subScopesRemover = new SubScopesRemover();
[3193]127      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
[3096]128      BestSelector bestSelector = new BestSelector();
[2830]129      RightReducer rightReducer = new RightReducer();
130      MergingReducer mergingReducer = new MergingReducer();
131      IntCounter intCounter = new IntCounter();
132      Comparator comparator = new Comparator();
[3616]133      ResultsCollector resultsCollector2 = new ResultsCollector();
134      Placeholder analyzer2 = new Placeholder();
[2830]135      ConditionalBranch conditionalBranch = new ConditionalBranch();
136
[3750]137      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
[2908]138
[3616]139      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
140      resultsCollector1.ResultsParameter.ActualName = "Results";
[3080]141
[3616]142      analyzer1.Name = "Analyzer";
143      analyzer1.OperatorParameter.ActualName = "Analyzer";
[3095]144
[2882]145      selector.Name = "Selector";
146      selector.OperatorParameter.ActualName = "Selector";
[2830]147
[3048]148      childrenCreator.ParentsPerChild = new IntValue(2);
[2830]149
[2882]150      crossover.Name = "Crossover";
151      crossover.OperatorParameter.ActualName = "Crossover";
[2830]152
153      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
154      stochasticBranch.RandomParameter.ActualName = "Random";
155
[2882]156      mutator.Name = "Mutator";
157      mutator.OperatorParameter.ActualName = "Mutator";
[2830]158
[2882]159      evaluator.Name = "Evaluator";
160      evaluator.OperatorParameter.ActualName = "Evaluator";
[2830]161
162      subScopesRemover.RemoveAllSubScopes = true;
163
[3096]164      bestSelector.CopySelected = new BoolValue(false);
165      bestSelector.MaximizationParameter.ActualName = "Maximization";
166      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
167      bestSelector.QualityParameter.ActualName = "Quality";
[2830]168
[3048]169      intCounter.Increment = new IntValue(1);
[2830]170      intCounter.ValueParameter.ActualName = "Generations";
171
[3048]172      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
[2830]173      comparator.LeftSideParameter.ActualName = "Generations";
174      comparator.ResultParameter.ActualName = "Terminate";
175      comparator.RightSideParameter.ActualName = "MaximumGenerations";
176
[3616]177      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
178      resultsCollector2.ResultsParameter.ActualName = "Results";
[2891]179
[3616]180      analyzer2.Name = "Analyzer";
181      analyzer2.OperatorParameter.ActualName = "Analyzer";
[3095]182
[2830]183      conditionalBranch.ConditionParameter.ActualName = "Terminate";
[3080]184      #endregion
185
186      #region Create operator graph
187      OperatorGraph.InitialOperator = variableCreator;
[3616]188      variableCreator.Successor = resultsCollector1;
189      resultsCollector1.Successor = analyzer1;
190      analyzer1.Successor = selector;
[3193]191      selector.Successor = subScopesProcessor1;
192      subScopesProcessor1.Operators.Add(new EmptyOperator());
193      subScopesProcessor1.Operators.Add(childrenCreator);
194      subScopesProcessor1.Successor = subScopesProcessor2;
195      childrenCreator.Successor = uniformSubScopesProcessor;
196      uniformSubScopesProcessor.Operator = crossover;
197      uniformSubScopesProcessor.Successor = null;
[3080]198      crossover.Successor = stochasticBranch;
199      stochasticBranch.FirstBranch = mutator;
200      stochasticBranch.SecondBranch = null;
201      stochasticBranch.Successor = evaluator;
202      mutator.Successor = null;
203      evaluator.Successor = subScopesRemover;
204      subScopesRemover.Successor = null;
[3193]205      subScopesProcessor2.Operators.Add(bestSelector);
206      subScopesProcessor2.Operators.Add(new EmptyOperator());
207      subScopesProcessor2.Successor = mergingReducer;
[3096]208      bestSelector.Successor = rightReducer;
[3080]209      rightReducer.Successor = null;
210      mergingReducer.Successor = intCounter;
211      intCounter.Successor = comparator;
[3616]212      comparator.Successor = resultsCollector2;
213      resultsCollector2.Successor = analyzer2;
214      analyzer2.Successor = conditionalBranch;
[3096]215      conditionalBranch.FalseBranch = selector;
[2830]216      conditionalBranch.TrueBranch = null;
217      conditionalBranch.Successor = null;
218      #endregion
219    }
[3715]220
221    public override IOperation Apply() {
222      if (CrossoverParameter.ActualValue == null)
223        return null;
224      return base.Apply();
225    }
[2830]226  }
227}
Note: See TracBrowser for help on using the repository browser.