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

Last change on this file since 9475 was 9475, checked in by sforsten, 8 years ago

#1980: several small bug fixes

File size: 4.8 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        if (tl.IsAlmost(0.0)) {
85          //as done in the original implementation
86          tl = 0.00000001;
87        }
88        theoryWeight = (initialTheoryLengthRatio / (1.0 - initialTheoryLengthRatio)) * (error / tl);
89        iterationsSinceBest = 0;
90      }
91
92      if (activated && !fixedWeight &&
93        GetLastIterationsAccuracyAverage(weightAdaptionIterations).IsAlmost(1.0)) {
94        fixedWeight = true;
95      }
96
97      if (activated && !fixedWeight) {
98        if (bestSolution.TrainingAccuracy.IsAlmost(1.0)) {
99          if (iterationsSinceBest == weightAdaptionIterations) {
100            theoryWeight *= weightRelaxFactor;
101            iterationsSinceBest = 0;
102          }
103        }
104      }
105
106      UpdateStatistic(bestSolution.TrainingAccuracy);
107    }
108
109    private void UpdateStatistic(double accuracy) {
110      if (iterationsSinceBest == 0) {
111        bestFitness = accuracy;
112        iterationsSinceBest++;
113      } else if (accuracy > bestFitness) {
114        bestFitness = accuracy;
115        iterationsSinceBest = 1;
116      } else {
117        iterationsSinceBest++;
118      }
119    }
120
121    private double GetLastIterationsAccuracyAverage(int iterations) {
122      int startAt = accuracyStatistic.Count - iterations;
123      startAt = startAt > 0 ? startAt : 0;
124      return accuracyStatistic.Skip(startAt).Sum() / (accuracyStatistic.Count - startAt);
125    }
126
127    public double CalculateFitness(IGAssistSolution dls) {
128      double fitness = 0;
129      if (activated) {
130        fitness = dls.TrainingTheoryLength * theoryWeight;
131      }
132      fitness += 105.0 - dls.TrainingAccuracy * 100.0;
133      return fitness;
134    }
135
136    public double CalculateFitness(double theoryLength, double accuracy) {
137      double fitness = 0;
138      if (activated) {
139        fitness = theoryLength * theoryWeight;
140      }
141      fitness += 105.0 - accuracy * 100.0;
142      return fitness;
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.