source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/GAssist/MDL/MDLCalculator.cs @ 9392

Last change on this file since 9392 was 9392, checked in by sforsten, 8 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: 4.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization.Operators.LCS {
29  [StorableClass]
30  [Item("MDLCalculator", "")]
31  public class MDLCalculator : Item {
32
33    [Storable]
34    private int activationIteration;
35
36    [Storable]
37    private bool activated;
38    [Storable]
39    private bool fixedWeight;
40
41    [Storable]
42    private double theoryWeight;
43
44    [Storable]
45    private double initialTheoryLengthRatio;
46    [Storable]
47    private double weightRelaxFactor;
48
49    [Storable]
50    private IList<double> accuracyStatistic;
51    [Storable]
52    private int iterationsSinceBest;
53    [Storable]
54    private double bestFitness;
55
56    [Storable]
57    private int weightAdaptionIterations;
58
59    [StorableConstructor]
60    protected MDLCalculator(bool deserializing) : base(deserializing) { }
61    protected MDLCalculator(MDLCalculator original, Cloner cloner)
62      : base(original, cloner) {
63    }
64    public MDLCalculator(int activationIteration, double initialTheoryLengthRatio, double weightRelaxFactor, int weightAdaptionIterations)
65      : base() {
66      this.activationIteration = activationIteration;
67      this.initialTheoryLengthRatio = initialTheoryLengthRatio;
68      this.weightRelaxFactor = weightRelaxFactor;
69      this.weightAdaptionIterations = weightAdaptionIterations;
70      activated = false;
71      fixedWeight = false;
72      accuracyStatistic = new List<double>();
73      iterationsSinceBest = 0;
74    }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new MDLCalculator(this, cloner);
77    }
78
79    public void StartNewIteration(IGAssistSolution bestSolution, int currentIteration) {
80      if (currentIteration == activationIteration) {
81        activated = true;
82        double error = bestSolution.TrainingExceptionsLength;
83        double tl = (bestSolution.TrainingTheoryLength * bestSolution.Classes) / bestSolution.TrainingNumberOfAliveRules;
84        theoryWeight = (initialTheoryLengthRatio / (1.0 - initialTheoryLengthRatio)) * (error / tl);
85        iterationsSinceBest = 0;
86      }
87
88      if (activated && !fixedWeight &&
89        GetLastIterationsAccuracyAverage(weightAdaptionIterations) == 1.0) {
90        fixedWeight = true;
91      }
92
93      if (activated && !fixedWeight) {
94        if (bestSolution.TrainingAccuracy != 1.0) {
95          if (iterationsSinceBest == weightAdaptionIterations) {
96            theoryWeight *= weightRelaxFactor;
97            iterationsSinceBest = 0;
98          }
99        }
100      }
101
102      //if (currentIteration != 0) {
103      UpdateStatistic(bestSolution.TrainingAccuracy);
104      //}
105    }
106
107    private void UpdateStatistic(double accuracy) {
108      if (iterationsSinceBest == 0) {
109        bestFitness = accuracy;
110        iterationsSinceBest++;
111      } else if (accuracy > bestFitness) {
112        bestFitness = accuracy;
113        iterationsSinceBest = 1;
114      } else {
115        iterationsSinceBest++;
116      }
117    }
118
119    private double GetLastIterationsAccuracyAverage(int iterations) {
120      int startAt = accuracyStatistic.Count - iterations;
121      startAt = startAt > 0 ? startAt : 0;
122      return accuracyStatistic.Skip(startAt).Sum() / (accuracyStatistic.Count - startAt);
123    }
124
125    public double CalculateFitness(IGAssistSolution dls) {
126      double fitness = 0;
127      if (activated) {
128        fitness = dls.TrainingTheoryLength * theoryWeight;
129      }
130      fitness += 105.0 - dls.TrainingAccuracy * 100.0;
131      return fitness;
132    }
133
134    public double CalculateFitness(double theoryLength, double accuracy) {
135      double fitness = 0;
136      if (activated) {
137        fitness = theoryLength * theoryWeight;
138      }
139      fitness += 105.0 - accuracy * 100.0;
140      return fitness;
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.