Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.GAssist/3.3/GAssistMainLoop.cs @ 10080

Last change on this file since 10080 was 9392, checked in by sforsten, 12 years ago

#1980:

  • several small bug fixes
  • added windowing technique ILAS to GAssist
  • GAssist and XCS work now with real-valued features
  • severely improved the performance of XCS
File size: 14.8 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.Optimization.Operators.LCS;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Algorithms.GAssist {
33  /// <summary>
34  /// An operator which represents the main loop of a genetic algorithm.
35  /// </summary>
36  [Item("GAssistMainLoop", "An operator which represents the main loop of GAssist.")]
37  [StorableClass]
38  public sealed class GAssistMainLoop : 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    }
46    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public ValueLookupParameter<IOperator> SelectorParameter {
50      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
51    }
52    public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
53      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
54    }
55    public ValueLookupParameter<IOperator> CrossoverParameter {
56      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
57    }
58    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
59      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
60    }
61    public ValueLookupParameter<IOperator> MutatorParameter {
62      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
63    }
64    public ValueLookupParameter<IOperator> MDLIterationParameter {
65      get { return (ValueLookupParameter<IOperator>)Parameters["MDLIteration"]; }
66    }
67    public ValueLookupParameter<IOperator> DefaultRuleParameter {
68      get { return (ValueLookupParameter<IOperator>)Parameters["DefaultRule"]; }
69    }
70    public ValueLookupParameter<IOperator> SpecialStagesParameter {
71      get { return (ValueLookupParameter<IOperator>)Parameters["SpecialStages"]; }
72    }
73    public ValueLookupParameter<IOperator> EvaluatorParameter {
74      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
75    }
76    public ValueLookupParameter<IntValue> ElitesParameter {
77      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
78    }
79    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
80      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
81    }
82    public ValueLookupParameter<VariableCollection> ResultsParameter {
83      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
84    }
85    public ValueLookupParameter<IOperator> AnalyzerParameter {
86      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
87    }
88    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
89      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
90    }
91    public ValueLookupParameter<IntValue> PopulationSizeParameter {
92      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
93    }
94    public ValueLookupParameter<IOperator> ReinitializationProbabilityOperatorParameter {
95      get { return (ValueLookupParameter<IOperator>)Parameters["ReinitializationProbabilityOperator"]; }
96    }
97    private ScopeParameter CurrentScopeParameter {
98      get { return (ScopeParameter)Parameters["CurrentScope"]; }
99    }
100
101    public IScope CurrentScope {
102      get { return CurrentScopeParameter.ActualValue; }
103    }
104    #endregion
105
106    [StorableConstructor]
107    private GAssistMainLoop(bool deserializing) : base(deserializing) { }
108    private GAssistMainLoop(GAssistMainLoop original, Cloner cloner)
109      : base(original, cloner) {
110    }
111    public override IDeepCloneable Clone(Cloner cloner) {
112      return new GAssistMainLoop(this, cloner);
113    }
114    public GAssistMainLoop()
115      : base() {
116      Initialize();
117    }
118
119    private void Initialize() {
120      #region Create parameters
121      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
122      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
123      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
124      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
125      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
126      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
127      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
128      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
129      Parameters.Add(new ValueLookupParameter<IOperator>("SpecialStages", "The operator used for the special stages of GAssist."));
130      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."));
131      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
132      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
133      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
134      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
135      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
136      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
137      Parameters.Add(new ValueLookupParameter<IOperator>("ReinitializationProbabilityOperator", ""));
138      Parameters.Add(new ValueLookupParameter<IOperator>("DefaultRule", ""));
139      Parameters.Add(new ValueLookupParameter<IOperator>("MDLIteration", ""));
140      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
141      #endregion
142
143      #region Create operators
144      ResultsCollector resultsCollector1 = new ResultsCollector();
145      Placeholder analyzer1 = new Placeholder();
146      Placeholder mdlIterationOperator = new Placeholder();
147      Placeholder defaultRuleOperator = new Placeholder();
148      Placeholder reinitializationProbabilityOperator = new Placeholder();
149      Placeholder selector = new Placeholder();
150      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
151      ChildrenCreator childrenCreator = new ChildrenCreator();
152      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
153      StochasticBranch stochasticBranchCrossover = new StochasticBranch();
154      Placeholder crossover = new Placeholder();
155      RandomSelector randomSelector = new RandomSelector();
156      PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
157      StochasticBranch stochasticBranchMutator = new StochasticBranch();
158      Placeholder mutator = new Placeholder();
159      Placeholder specialStages = new Placeholder();
160      SubScopesRemover subScopesRemover = new SubScopesRemover();
161      UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
162      Placeholder evaluator = new Placeholder();
163      SubScopesCounter subScopesCounter = new SubScopesCounter();
164      SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
165      BestSelector bestSelector = new BestSelector();
166      RightReducer rightReducer = new RightReducer();
167      MergingReducer mergingReducer = new MergingReducer();
168      IntCounter intCounter = new IntCounter();
169      Comparator comparator = new Comparator();
170      Placeholder analyzer2 = new Placeholder();
171      ConditionalBranch conditionalBranch = new ConditionalBranch();
172
173      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
174      resultsCollector1.ResultsParameter.ActualName = "Results";
175
176      analyzer1.Name = "Analyzer";
177      analyzer1.OperatorParameter.ActualName = "Analyzer";
178
179      mdlIterationOperator.Name = "MDL Iteration Operator";
180      mdlIterationOperator.OperatorParameter.ActualName = MDLIterationParameter.Name;
181
182      defaultRuleOperator.Name = "Default Rule Operator";
183      defaultRuleOperator.OperatorParameter.ActualName = DefaultRuleParameter.Name;
184
185      reinitializationProbabilityOperator.Name = "Reinitialization Probability operator (placeholder)";
186      reinitializationProbabilityOperator.OperatorParameter.ActualName = ReinitializationProbabilityOperatorParameter.Name;
187
188      selector.Name = "Selector";
189      selector.OperatorParameter.ActualName = "Selector";
190
191      childrenCreator.ParentsPerChild = new IntValue(2);
192
193      stochasticBranchCrossover.ProbabilityParameter.ActualName = "CrossoverProbability";
194      stochasticBranchCrossover.RandomParameter.ActualName = "Random";
195
196      crossover.Name = "Crossover";
197      crossover.OperatorParameter.ActualName = "Crossover";
198
199      randomSelector.CopySelected.Value = true;
200      randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
201
202      stochasticBranchMutator.ProbabilityParameter.ActualName = "MutationProbability";
203      stochasticBranchMutator.RandomParameter.ActualName = "Random";
204
205      mutator.Name = "Mutator";
206      mutator.OperatorParameter.ActualName = "Mutator";
207
208      specialStages.Name = "SpecialStages";
209      specialStages.OperatorParameter.ActualName = "SpecialStages";
210
211      subScopesRemover.RemoveAllSubScopes = true;
212
213      uniformSubScopesProcessor2.Parallel.Value = true;
214
215      evaluator.Name = "Evaluator";
216      evaluator.OperatorParameter.ActualName = "Evaluator";
217
218      subScopesCounter.Name = "Increment EvaluatedSolutions";
219      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
220
221      bestSelector.CopySelected = new BoolValue(false);
222      bestSelector.MaximizationParameter.ActualName = "Maximization";
223      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
224      bestSelector.QualityParameter.ActualName = "Quality";
225
226      intCounter.Increment = new IntValue(1);
227      intCounter.ValueParameter.ActualName = "Generations";
228
229      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
230      comparator.LeftSideParameter.ActualName = "Generations";
231      comparator.ResultParameter.ActualName = "Terminate";
232      comparator.RightSideParameter.ActualName = "MaximumGenerations";
233
234      analyzer2.Name = "Analyzer";
235      analyzer2.OperatorParameter.ActualName = "Analyzer";
236
237      conditionalBranch.ConditionParameter.ActualName = "Terminate";
238      #endregion
239
240      #region Create operator graph
241      OperatorGraph.InitialOperator = resultsCollector1;
242      resultsCollector1.Successor = analyzer1;
243      analyzer1.Successor = mdlIterationOperator;
244      mdlIterationOperator.Successor = defaultRuleOperator;
245      defaultRuleOperator.Successor = reinitializationProbabilityOperator;
246      reinitializationProbabilityOperator.Successor = selector;
247      selector.Successor = subScopesProcessor1;
248      subScopesProcessor1.Operators.Add(new EmptyOperator());
249      subScopesProcessor1.Operators.Add(childrenCreator);
250      subScopesProcessor1.Successor = subScopesProcessor2;
251      childrenCreator.Successor = uniformSubScopesProcessor1;
252      uniformSubScopesProcessor1.Operator = stochasticBranchCrossover;
253      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
254      stochasticBranchCrossover.FirstBranch = crossover;
255      stochasticBranchCrossover.SecondBranch = randomSelector;
256      randomSelector.Successor = preservingRightReducer;
257      stochasticBranchCrossover.Successor = stochasticBranchMutator;
258      crossover.Successor = null;
259      preservingRightReducer.Successor = null;
260      stochasticBranchMutator.FirstBranch = mutator;
261      stochasticBranchMutator.SecondBranch = null;
262      stochasticBranchMutator.Successor = specialStages;
263      mutator.Successor = null;
264      specialStages.Successor = subScopesRemover;
265      subScopesRemover.Successor = null;
266      uniformSubScopesProcessor2.Operator = evaluator;
267      uniformSubScopesProcessor2.Successor = subScopesCounter;
268      evaluator.Successor = null;
269      subScopesCounter.Successor = null;
270      subScopesProcessor2.Operators.Add(bestSelector);
271      subScopesProcessor2.Operators.Add(new EmptyOperator());
272      subScopesProcessor2.Successor = mergingReducer;
273      bestSelector.Successor = rightReducer;
274      rightReducer.Successor = null;
275      mergingReducer.Successor = intCounter;
276      intCounter.Successor = comparator;
277      comparator.Successor = analyzer2;
278      analyzer2.Successor = conditionalBranch;
279      conditionalBranch.FalseBranch = mdlIterationOperator;
280      conditionalBranch.TrueBranch = null;
281      conditionalBranch.Successor = null;
282      #endregion
283    }
284
285    public override IOperation Apply() {
286      if (CrossoverParameter.ActualValue == null)
287        return null;
288      return base.Apply();
289    }
290  }
291}
Note: See TracBrowser for help on using the repository browser.