Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/GAssist/DefaultRule/AutoDefaultRule.cs @ 11303

Last change on this file since 11303 was 9605, checked in by sforsten, 12 years ago

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 6.1 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization.Operators.LCS.DefaultRule {
32  [Item("AutoDefaultRule", "Description missing")]
33  [StorableClass]
34  public class AutoDefaultRule : DefaultRuleOperator, IStochasticOperator {
35
36    #region Parameter Properties
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
39    }
40    public IValueLookupParameter<ItemDictionary<IGAssistNiche, ItemList<DoubleValue>>> AverageAccuraciesParameter {
41      get { return (IValueLookupParameter<ItemDictionary<IGAssistNiche, ItemList<DoubleValue>>>)Parameters["AverageAccuracies"]; }
42    }
43    public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
44      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Quality"]; }
45    }
46    public IValueLookupParameter<IntValue> AccuraciesCountParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters["AccuraciesCount"]; }
48    }
49    public IValueLookupParameter<IGAssistNicheEqualityComparer> NicheComparerParameter {
50      get { return (IValueLookupParameter<IGAssistNicheEqualityComparer>)Parameters["NicheComparer"]; }
51    }
52    public IValueLookupParameter<DoubleValue> NicheStandardDeviationParameter {
53      get { return (IValueLookupParameter<DoubleValue>)Parameters["NicheStandardDeviation"]; }
54    }
55    #endregion
56
57    public ItemDictionary<IGAssistNiche, ItemList<DoubleValue>> AverageAccuracies { get { return AverageAccuraciesParameter.ActualValue; } }
58    public int AccuraciesCount { get { return AccuraciesCountParameter.ActualValue.Value; } }
59
60    [StorableConstructor]
61    protected AutoDefaultRule(bool deserializing) : base(deserializing) { }
62    protected AutoDefaultRule(AutoDefaultRule original, Cloner cloner)
63      : base(original, cloner) {
64    }
65    public AutoDefaultRule()
66      : base() {
67      Parameters.Add(new ValueLookupParameter<ItemDictionary<IGAssistNiche, ItemList<DoubleValue>>>("AverageAccuracies"));
68      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
69      Parameters.Add(new LookupParameter<IRandom>("Random"));
70      Parameters.Add(new ValueLookupParameter<IntValue>("AccuraciesCount", new IntValue(15)));
71      Parameters.Add(new ValueLookupParameter<IGAssistNicheEqualityComparer>("NicheComparer"));
72      Parameters.Add(new ValueLookupParameter<DoubleValue>("NicheStandardDeviation", new DoubleValue(0.005)));
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new AutoDefaultRule(this, cloner);
76    }
77
78    public override IOperation Apply() {
79      if (NichingParameter.ActualValue == null) {
80        IList<IGAssistNiche> niches = GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().ToList();
81        NichingParameter.ActualValue = new BoolValue(true);
82        NichesParameter.ActualValue = new IntValue(niches.Count);
83        AverageAccuraciesParameter.ActualValue = new ItemDictionary<IGAssistNiche, ItemList<DoubleValue>>(NicheComparerParameter.ActualValue as IEqualityComparer<IGAssistNiche>);
84        foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
85          AverageAccuracies.Add(niche, new ItemList<DoubleValue>(AccuraciesCount));
86        }
87      } else {
88        var nicheFitness = new Dictionary<IGAssistNiche, double>(NicheComparerParameter.ActualValue);
89        foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
90          nicheFitness[niche] = 0.0;
91          while (AverageAccuracies[niche].Count >= AccuraciesCount) {
92            AverageAccuracies[niche].RemoveAt(AverageAccuracies.Count - 1);
93          }
94        }
95
96        var qualityEnumerator = QualityParameter.ActualValue.GetEnumerator();
97        var individualEnumerator = IndividualParameter.ActualValue.GetEnumerator();
98        while (qualityEnumerator.MoveNext() && individualEnumerator.MoveNext()) {
99          if (nicheFitness[individualEnumerator.Current.Niche] < qualityEnumerator.Current.Value) {
100            nicheFitness[individualEnumerator.Current.Niche] = qualityEnumerator.Current.Value;
101          }
102        }
103
104        foreach (var niche in GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches()) {
105          AverageAccuracies[niche].Insert(0, new DoubleValue(nicheFitness[niche]));
106        }
107
108        var averages = new List<double>(GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().Count());
109        if (AverageAccuracies.Values.First().Count >= AccuraciesCount) {
110          foreach (var averageAccuracy in AverageAccuracies.Values) {
111            averages.Add(averageAccuracy.Select(x => x.Value).Average());
112          }
113          double avg = averages.Average();
114          double sum = averages.Sum(d => Math.Pow(d - avg, 2));
115          double std = Math.Sqrt(sum / averages.Count);
116
117          if (std < NicheStandardDeviationParameter.ActualValue.Value) {
118            NichingParameter.ActualValue.Value = false;
119          }
120        }
121      }
122      return base.Apply();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.