Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • included an adapted version of GA correctly
  • added action set subsumption
  • added deletion after GA and before covering
File size: 7.2 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<IntValue> ThetaDeletionParameter {
54      get { return (ILookupParameter<IntValue>)Parameters["ThetaDeletion"]; }
55    }
56    public ILookupParameter<PercentValue> DeltaParameter {
57      get { return (ILookupParameter<PercentValue>)Parameters["Delta"]; }
58    }
59    public ILookupParameter<IRandom> RandomParameter {
60      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
61    }
62    public ILookupParameter<IntValue> CurrentPopulationSizeParameter {
63      get { return (ILookupParameter<IntValue>)Parameters["CurrentPopulationSize"]; }
64    }
65    protected ScopeParameter CurrentScopeParameter {
66      get { return (ScopeParameter)Parameters["CurrentScope"]; }
67    }
68    public IScope CurrentScope {
69      get { return CurrentScopeParameter.ActualValue; }
70    }
71    public string HasToBeDeletedVariableName { get { return "HasToBeDeleted"; } }
72    #endregion
73
74    #region Parameter Properties
75    private double Delta { get { return DeltaParameter.ActualValue.Value; } }
76    private int ThetaDeletion { get { return ThetaDeletionParameter.ActualValue.Value; } }
77    #endregion
78
79    [StorableConstructor]
80    protected XCSDeletionOperator(bool deserializing) : base(deserializing) { }
81    protected XCSDeletionOperator(XCSDeletionOperator original, Cloner cloner)
82      : base(original, cloner) {
83    }
84    public XCSDeletionOperator()
85      : base() {
86      Parameters.Add(new LookupParameter<IntValue>("NumberToDelete"));
87      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosities"));
88      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("AverageActionSetSizes"));
89      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Fitnesses"));
90      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Experiences"));
91      Parameters.Add(new LookupParameter<IntValue>("ThetaDeletion"));
92      Parameters.Add(new LookupParameter<PercentValue>("Delta"));
93      Parameters.Add(new LookupParameter<IRandom>("Random"));
94      Parameters.Add(new ScopeParameter("CurrentScope"));
95      Parameters.Add(new LookupParameter<IntValue>("CurrentPopulationSize"));
96    }
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new XCSDeletionOperator(this, cloner);
99    }
100
101    public override IOperation Apply() {
102      var fitnesses = FitnessesParameter.ActualValue;
103      var numerosities = NumerositiesParameter.ActualValue;
104      var experiences = ExperiencesParameter.ActualValue;
105      var averageActionSetSizes = AverageActionSetSizesParameter.ActualValue;
106      var subscopes = CurrentScope.SubScopes;
107      double fitnessSum = fitnesses.Select(x => x.Value).Sum();
108      double numerositySum = numerosities.Select(x => x.Value).Sum();
109
110      if (fitnesses.Length != numerosities.Length && fitnesses.Length != experiences.Length && fitnesses.Length != subscopes.Count) {
111        throw new ArgumentException("Different number of fitness, numerosity and experience values.");
112      }
113
114      for (int cl = 0; cl < fitnesses.Length; cl++) {
115        if (subscopes[cl].Variables.ContainsKey(HasToBeDeletedVariableName)) {
116          ((BoolValue)subscopes[cl].Variables[HasToBeDeletedVariableName].Value).Value = false;
117        } else {
118          subscopes[cl].Variables.Add(new Variable(HasToBeDeletedVariableName, new BoolValue(false)));
119        }
120      }
121
122      for (int i = 0; i < NumberToDeleteParameter.ActualValue.Value; i++) {
123        double voteSum = 0;
124        double averageFitnessInPopulation = fitnessSum / numerositySum;
125        for (int cl = 0; cl < fitnesses.Length; cl++) {
126          if (numerosities[cl].Value > 0) {
127            voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value);
128          }
129        }
130        double choicePoint = RandomParameter.ActualValue.NextDouble() * voteSum;
131        voteSum = 0;
132        for (int cl = 0; cl < fitnesses.Length; cl++) {
133          if (numerosities[cl].Value > 0) {
134            voteSum += DeletionVote(averageFitnessInPopulation, averageActionSetSizes[cl].Value, numerosities[cl].Value, fitnesses[cl].Value, experiences[cl].Value);
135            if (voteSum > choicePoint) {
136              if (numerosities[cl].Value <= 1) {
137                ((BoolValue)subscopes[cl].Variables[HasToBeDeletedVariableName].Value).Value = true;
138                fitnessSum -= fitnesses[cl].Value;
139              }
140              numerosities[cl].Value--;
141              numerositySum--;
142              break;
143            }
144          }
145        }
146      }
147
148      CurrentPopulationSizeParameter.ActualValue.Value -= NumberToDeleteParameter.ActualValue.Value;
149
150      return base.Apply();
151    }
152
153    private double DeletionVote(double averageFitnessInPopulation, double ass, int num, double f, int exp) {
154      double vote = ass * num;
155      if (exp > ThetaDeletion && (f / num) < Delta * averageFitnessInPopulation) {
156        vote *= averageFitnessInPopulation / (f / num);
157      }
158      return vote;
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.