Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/LCSAdaptedGeneticAlgorithm.cs @ 9160

Last change on this file since 9160 was 9160, checked in by sforsten, 11 years ago

#1980:

  • added problem instance provider to import csv files for ConditionActionClassificationProblemData
  • adapted LCSAdaptedGeneticAlgorithm to use the crossover probability
  • fixed a bug in XCSModelView
File size: 15.0 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
22
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ConditionActionEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Selection;
32namespace HeuristicLab.Algorithms.LearningClassifierSystems {
33  /// <summary>
34  /// An operator which represents the main loop of a genetic algorithm.
35  /// </summary>
36  [Item("LCSAdaptedGeneticAlgorithm", "An operator which represents the main loop of a genetic algorithm, which has been adapdet for learning classifier systems.")]
37  [StorableClass]
38  public sealed class LCSAdaptedGeneticAlgorithm : AlgorithmOperator {
39    private const string TEMPID = "TempID";
40    private const string SUBSUMEDBY = "SubsumedBy";
41    private const string SUBSUMED = "Subsumed";
42
43    #region Parameter properties
44    public ValueLookupParameter<IRandom> RandomParameter {
45      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
46    }
47    public ValueLookupParameter<BoolValue> MaximizationParameter {
48      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
49    }
50    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    public ValueLookupParameter<IOperator> SelectorParameter {
54      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
55    }
56    public ValueLookupParameter<IOperator> CrossoverParameter {
57      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
58    }
59    public ValueLookupParameter<IOperator> AfterCrossoverParameter {
60      get { return (ValueLookupParameter<IOperator>)Parameters["AfterCrossover"]; }
61    }
62    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
63      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
64    }
65    public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
66      get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
67    }
68    public ValueLookupParameter<IOperator> MutatorParameter {
69      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
70    }
71    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
72      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
73    }
74    public ValueLookupParameter<VariableCollection> ResultsParameter {
75      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
76    }
77    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
78      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
79    }
80    public ValueLookupParameter<IntValue> PopulationSizeParameter {
81      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
82    }
83    public ValueLookupParameter<BoolValue> DoGASubsumptionParameter {
84      get { return (ValueLookupParameter<BoolValue>)Parameters["DoGASubsumption"]; }
85    }
86    private ScopeParameter CurrentScopeParameter {
87      get { return (ScopeParameter)Parameters["CurrentScope"]; }
88    }
89
90    public IScope CurrentScope {
91      get { return CurrentScopeParameter.ActualValue; }
92    }
93    #endregion
94
95    [StorableConstructor]
96    private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
97    private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
98      : base(original, cloner) {
99    }
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new LCSAdaptedGeneticAlgorithm(this, cloner);
102    }
103    public LCSAdaptedGeneticAlgorithm()
104      : base() {
105      Initialize();
106    }
107
108    private void Initialize() {
109      #region Create parameters
110      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
111      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
112      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
113      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
114      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
115      Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
116      Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
117      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that druing the mutation operator a mutation takes place."));
118      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
119      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
120      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
121      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
123      Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
124      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
125      #endregion
126
127      #region Create operators
128      VariableCreator variableCreator = new VariableCreator();
129      ResultsCollector resultsCollector1 = new ResultsCollector();
130      Placeholder selector = new Placeholder();
131      SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
132      ChildrenCreator childrenCreator = new ChildrenCreator();
133      UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
134      StochasticBranch crossoverStochasticBranch = new StochasticBranch();
135      RandomSelector randomSelector = new RandomSelector();
136      PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
137      Assigner numerosityAssigner = new Assigner();
138      Assigner experienceAssigner = new Assigner();
139      Placeholder crossover = new Placeholder();
140      Placeholder afterCrossover = new Placeholder();
141      Placeholder mutator = new Placeholder();
142      SubScopesRemover subScopesRemover = new SubScopesRemover();
143      SubScopesCounter subScopesCounter = new SubScopesCounter();
144      MergingReducer mergingReducer = new MergingReducer();
145      IntCounter intCounter = new IntCounter();
146      Comparator comparator = new Comparator();
147      ConditionalBranch conditionalBranch = new ConditionalBranch();
148
149      TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
150      ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
151      UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
152      Assigner setSubsumpByAssigner = new Assigner();
153      Assigner setSubsumptionFalseAssigner = new Assigner();
154      CheckGASubsumptionOperator checkGASubsumptionOperator = new CheckGASubsumptionOperator();
155      ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
156      ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
157      ConditionalSelector subsumptionSelector = new ConditionalSelector();
158      LeftReducer subsumptionLeftReducer = new LeftReducer();
159
160      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
161
162      //resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
163      resultsCollector1.ResultsParameter.ActualName = "Results";
164
165      tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
166
167      setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
168      setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
169
170      setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
171      setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
172
173      selector.Name = "Selector";
174      selector.OperatorParameter.ActualName = "Selector";
175
176      childrenCreator.ParentsPerChild = new IntValue(2);
177
178      crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.ActualName;
179      crossoverStochasticBranch.RandomParameter.ActualName = "Random";
180
181      randomSelector.CopySelected.Value = true;
182      randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
183
184      crossover.Name = "Crossover";
185      crossover.OperatorParameter.ActualName = "Crossover";
186
187      numerosityAssigner.LeftSideParameter.ActualName = "Numerosity";
188      numerosityAssigner.RightSideParameter.Value = new IntValue(1);
189
190      experienceAssigner.LeftSideParameter.ActualName = "Experience";
191      experienceAssigner.RightSideParameter.Value = new IntValue(0);
192
193      afterCrossover.Name = "AfterCrossover";
194      afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
195
196      mutator.Name = "Mutator";
197      mutator.OperatorParameter.ActualName = "Mutator";
198
199      doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
200
201      checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = "CombinedIntegerVector";
202      checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = "CombinedIntegerVector";
203      checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
204      checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience";
205      checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error";
206      checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID;
207      checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero";
208      checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption";
209      checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
210      checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED;
211
212      subScopesRemover.RemoveAllSubScopes = true;
213
214      doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
215
216      executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
217      executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID;
218      executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
219
220      subsumptionSelector.ConditionParameter.ActualName = SUBSUMED;
221      subsumptionSelector.CopySelected = new BoolValue(false);
222
223      subScopesCounter.Name = "Increment EvaluatedSolutions";
224      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
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      conditionalBranch.ConditionParameter.ActualName = "Terminate";
235      #endregion
236
237      #region Create operator graph
238      OperatorGraph.InitialOperator = variableCreator;
239      variableCreator.Successor = resultsCollector1;
240      resultsCollector1.Successor = tempIdAssigner;
241      tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor;
242      setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner;
243      setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner;
244      setSubsumptionFalseAssigner.Successor = null;
245      setSubsumptionFalseSubScopesProcessor.Successor = selector;
246      selector.Successor = subScopesProcessor1;
247      subScopesProcessor1.Operators.Add(new EmptyOperator());
248      subScopesProcessor1.Operators.Add(childrenCreator);
249      subScopesProcessor1.Successor = mergingReducer;
250      childrenCreator.Successor = uniformSubScopesProcessor1;
251      uniformSubScopesProcessor1.Operator = crossoverStochasticBranch;
252      uniformSubScopesProcessor1.Successor = subScopesCounter;
253      crossoverStochasticBranch.FirstBranch = crossover;
254      crossoverStochasticBranch.SecondBranch = randomSelector;
255      randomSelector.Successor = preservingRightReducer;
256      preservingRightReducer.Successor = numerosityAssigner;
257      numerosityAssigner.Successor = experienceAssigner;
258      experienceAssigner.Successor = null;
259      crossoverStochasticBranch.Successor = mutator;
260      crossover.Successor = afterCrossover;
261      mutator.Successor = doGASubsumptionBranch1;
262      doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator;
263      doGASubsumptionBranch1.FalseBranch = new EmptyOperator();
264      doGASubsumptionBranch1.Successor = subScopesRemover;
265      subScopesRemover.Successor = null;
266      subScopesCounter.Successor = null;
267      mergingReducer.Successor = doGASubsumptionBranch2;
268      doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator;
269      doGASubsumptionBranch2.FalseBranch = new EmptyOperator();
270      executeGAsubsumptionOperator.Successor = subsumptionSelector;
271      subsumptionSelector.Successor = subsumptionLeftReducer;
272      subsumptionLeftReducer.Successor = null;
273      doGASubsumptionBranch2.Successor = intCounter;
274      intCounter.Successor = comparator;
275      comparator.Successor = conditionalBranch;
276      conditionalBranch.FalseBranch = selector;
277      conditionalBranch.TrueBranch = null;
278      conditionalBranch.Successor = null;
279      #endregion
280    }
281
282    public override IOperation Apply() {
283      if (CrossoverParameter.ActualValue == null)
284        return null;
285      return base.Apply();
286    }
287  }
288}
Note: See TracBrowser for help on using the repository browser.