Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4047 was 3750, checked in by abeham, 15 years ago

#893

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