Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs @ 8313

Last change on this file since 8313 was 8313, checked in by jkarder, 12 years ago

#1247: initial commit

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