Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/Deletion/XCSDeletionOperator.cs @ 10355

Last change on this file since 10355 was 9154, checked in by sforsten, 12 years ago

#1980:

  • added XCSSolution, XCSModel, XCSClassifier to represent the xcs classifier
  • XCSSolution also shows the current accuracy (training and test partition has to be added)
  • added XCSSolutionAnalyzer to create a XCSSolution during the run of the algorithm
  • added XCSModelView to show the xcs model
  • fixed a bug in XCSDeletionOperator (sometimes it deleted less classifiers than it should)
  • moved some parameter from ConditionActionClassificationProblem to ConditionActionClassificationProblemData
File size: 6.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 System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.ConditionActionEncoding {
33  [Item("XCSDeletionOperator", "Description missing")]
34  [StorableClass]
35  public class XCSDeletionOperator : SingleSuccessorOperator, IStochasticOperator {
36
37    #region Parameter Properties
38    public ILookupParameter<IntValue> NumberToDeleteParameter {
39      get { return (ILookupParameter<IntValue>)Parameters["NumberToDelete"]; }
40    }
41    public ILookupParameter<ItemArray<IntValue>> NumerositiesParameter {
42      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Numerosities"]; }
43    }
44    public ILookupParameter<ItemArray<DoubleValue>> AverageActionSetSizesParameter {
45      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["AverageActionSetSizes"]; }
46    }
47    public ILookupParameter<ItemArray<DoubleValue>> FitnessesParameter {
48      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Fitnesses"]; }
49    }
50    public ILookupParameter<ItemArray<IntValue>> ExperiencesParameter {
51      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Experiences"]; }
52    }
53    public ILookupParameter<ItemArray<BoolValue>> HasToBeDeletedParameter {
54      get { return (ILookupParameter<ItemArray<BoolValue>>)Parameters["HasToBeDeleted"]; }
55    }
56    public ILookupParameter<IntValue> ThetaDeletionParameter {
57      get { return (ILookupParameter<IntValue>)Parameters["ThetaDeletion"]; }
58    }
59    public ILookupParameter<PercentValue> DeltaParameter {
60      get { return (ILookupParameter<PercentValue>)Parameters["Delta"]; }
61    }
62    public ILookupParameter<IRandom> RandomParameter {
63      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
64    }
65    public ILookupParameter<IntValue> CurrentPopulationSizeParameter {
66      get { return (ILookupParameter<IntValue>)Parameters["CurrentPopulationSize"]; }
67    }
68    #endregion
69
70    #region Parameter Properties
71    private double Delta { get { return DeltaParameter.ActualValue.Value; } }
72    private int ThetaDeletion { get { return ThetaDeletionParameter.ActualValue.Value; } }
73    #endregion
74
75    [StorableConstructor]
76    protected XCSDeletionOperator(bool deserializing) : base(deserializing) { }
77    protected XCSDeletionOperator(XCSDeletionOperator original, Cloner cloner)
78      : base(original, cloner) {
79    }
80    public XCSDeletionOperator()
81      : base() {
82      Parameters.Add(new LookupParameter<IntValue>("NumberToDelete"));
83      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosities"));
84      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("AverageActionSetSizes"));
85      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Fitnesses"));
86      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Experiences"));
87      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("HasToBeDeleted"));
88      Parameters.Add(new LookupParameter<IntValue>("ThetaDeletion"));
89      Parameters.Add(new LookupParameter<PercentValue>("Delta"));
90      Parameters.Add(new LookupParameter<IRandom>("Random"));
91      Parameters.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new XCSDeletionOperator(this, cloner);
95    }
96
97    public override IOperation Apply() {
98      var fitnesses = FitnessesParameter.ActualValue;
99      var numerosities = NumerositiesParameter.ActualValue;
100      var experiences = ExperiencesParameter.ActualValue;
101      var averageActionSetSizes = AverageActionSetSizesParameter.ActualValue;
102      var hasToBeDeleted = HasToBeDeletedParameter.ActualValue;
103      double fitnessSum = fitnesses.Select(x => x.Value).Sum();
104      double numerositySum = numerosities.Select(x => x.Value).Sum();
105
106      if (fitnesses.Length != numerosities.Length && fitnesses.Length != experiences.Length) {
107        throw new ArgumentException("Different number of fitness, numerosity and experience values.");
108      }
109
110      for (int i = 0; i < NumberToDeleteParameter.ActualValue.Value; i++) {
111        double voteSum = 0;
112        double averageFitnessInPopulation = fitnessSum / numerositySum;
113        for (int cl = 0; cl < fitnesses.Length; cl++) {
114          if (numerosities[cl].Value > 0 && !hasToBeDeleted[cl].Value) {
115            voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value);
116          }
117        }
118        double choicePoint = RandomParameter.ActualValue.NextDouble() * voteSum;
119        voteSum = 0;
120        for (int cl = 0; cl < fitnesses.Length; cl++) {
121          if (numerosities[cl].Value > 0 && !hasToBeDeleted[cl].Value) {
122            voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value);
123            if (voteSum > choicePoint) {
124              if (numerosities[cl].Value <= 1) {
125                hasToBeDeleted[cl].Value = true;
126                fitnessSum -= fitnesses[cl].Value;
127              }
128              numerosities[cl].Value--;
129              numerositySum--;
130              break;
131            }
132          }
133        }
134      }
135
136      CurrentPopulationSizeParameter.ActualValue.Value -= NumberToDeleteParameter.ActualValue.Value;
137
138      return base.Apply();
139    }
140
141    private double DeletionVote(double averageFitnessInPopulation, double ass, int num, double f, int exp) {
142      double vote = ass * num;
143      if (exp > ThetaDeletion && (f / num) < Delta * averageFitnessInPopulation) {
144        vote *= averageFitnessInPopulation / (f / num);
145      }
146      return vote;
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.