Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Evaluator/MDLCalculator.cs @ 9334

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

#1980:

  • added Algorithms.GAssist
  • adapted Problems.DecisionListClassification and Encodings.DecisionList
File size: 4.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.DecisionList {
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    [StorableConstructor]
57    protected MDLCalculator(bool deserializing) : base(deserializing) { }
58    protected MDLCalculator(MDLCalculator original, Cloner cloner)
59      : base(original, cloner) {
60    }
61    public MDLCalculator(int activationIteration, double initialTheoryLengthRatio, double weightRelaxFactor)
62      : base() {
63      this.activationIteration = activationIteration;
64      this.initialTheoryLengthRatio = initialTheoryLengthRatio;
65      this.weightRelaxFactor = weightRelaxFactor;
66      activated = false;
67      fixedWeight = false;
68      accuracyStatistic = new List<double>();
69      iterationsSinceBest = 0;
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new MDLCalculator(this, cloner);
73    }
74
75    public void StartNewIteration(DecisionListSolution bestSolution, int currentIteration) {
76      if (currentIteration == activationIteration) {
77        activated = true;
78        double error = bestSolution.TrainingExceptionsLength;
79        double tl = (bestSolution.TrainingTheoryLength * bestSolution.ProblemData.Classes) / bestSolution.TrainingNumberOfAliveRules;
80        theoryWeight = (initialTheoryLengthRatio / (1.0 - initialTheoryLengthRatio)) * (error / tl);
81        iterationsSinceBest = 0;
82      }
83
84      if (activated && !fixedWeight &&
85        GetLastIterationsAccuracyAverage(10) == 1.0) {
86        fixedWeight = true;
87      }
88
89      if (activated && !fixedWeight) {
90        if (bestSolution.TrainingAccuracy != 1.0) {
91          if (iterationsSinceBest == 10) {
92            theoryWeight *= weightRelaxFactor;
93            iterationsSinceBest = 0;
94          }
95        }
96      }
97
98      if (currentIteration != 0) {
99        UpdateStatistic(bestSolution.TrainingAccuracy);
100      }
101    }
102
103    private void UpdateStatistic(double accuracy) {
104      if (iterationsSinceBest == 0) {
105        bestFitness = accuracy;
106        iterationsSinceBest++;
107      } else if (accuracy > bestFitness) {
108        bestFitness = accuracy;
109        iterationsSinceBest = 1;
110      } else {
111        iterationsSinceBest++;
112      }
113    }
114
115    private double GetLastIterationsAccuracyAverage(int iterations) {
116      int startAt = accuracyStatistic.Count - iterations;
117      startAt = startAt > 0 ? startAt : 0;
118      return accuracyStatistic.Skip(startAt).Sum() / (accuracyStatistic.Count - startAt);
119    }
120
121    public double CalculateFitness(DecisionListSolution dls) {
122      double fitness = 0;
123      if (activated) {
124        fitness = dls.TrainingTheoryLength * theoryWeight;
125      }
126      fitness += 105.0 - dls.TrainingAccuracy * 100.0;
127      return fitness;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.