Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9370 was 9352, checked in by sforsten, 12 years ago

#1980:

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