Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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