Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/Subsumption/ActionSetSubsumptionoperator.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: 5.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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ConditionActionEncoding {
31  [Item("ActionSetSubsumptionoperator", "Description missing")]
32  [StorableClass]
33  public class ActionSetSubsumptionoperator : SingleSuccessorOperator, IActionSetSubsumption {
34
35    #region Parameter Properties
36    public ILookupParameter<ItemArray<IClassifier>> ClassifiersParameter {
37      get { return (ILookupParameter<ItemArray<IClassifier>>)Parameters["Classifiers"]; }
38    }
39    public ILookupParameter<ItemArray<IntValue>> NumerositiesParameter {
40      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Numerosities"]; }
41    }
42    public ILookupParameter<ItemArray<IntValue>> ExperiencesParameter {
43      get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Experiences"]; }
44    }
45    public ILookupParameter<ItemArray<DoubleValue>> ErrorsParameter {
46      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["Errors"]; }
47    }
48    public ILookupParameter<DoubleValue> ErrorZeroParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["ErrorZero"]; }
50    }
51    public ILookupParameter<IntValue> ThetaSubsumptionParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["ThetaSubsumption"]; }
53    }
54    protected ScopeParameter CurrentScopeParameter {
55      get { return (ScopeParameter)Parameters["CurrentScope"]; }
56    }
57    public IScope CurrentScope {
58      get { return CurrentScopeParameter.ActualValue; }
59    }
60
61    public string HasBeenSubsumedParameterName {
62      get { return "HasBeenSubsumed"; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected ActionSetSubsumptionoperator(bool deserializing) : base(deserializing) { }
68    protected ActionSetSubsumptionoperator(ActionSetSubsumptionoperator original, Cloner cloner)
69      : base(original, cloner) {
70    }
71    public ActionSetSubsumptionoperator()
72      : base() {
73      Parameters.Add(new ScopeTreeLookupParameter<IClassifier>("Classifiers"));
74      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosities"));
75      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Experiences"));
76      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Errors"));
77      Parameters.Add(new LookupParameter<DoubleValue>("ErrorZero"));
78      Parameters.Add(new LookupParameter<IntValue>("ThetaSubsumption"));
79      Parameters.Add(new ScopeParameter("CurrentScope"));
80    }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new ActionSetSubsumptionoperator(this, cloner);
83    }
84
85    public override IOperation Apply() {
86      var subscopes = CurrentScope.SubScopes;
87      var classifiers = ClassifiersParameter.ActualValue;
88      var numerosities = NumerositiesParameter.ActualValue;
89      var experiences = ExperiencesParameter.ActualValue;
90      var errors = ErrorsParameter.ActualValue;
91      if (subscopes.Count != classifiers.Length || subscopes.Count != numerosities.Length
92        || subscopes.Count != experiences.Length || subscopes.Count != errors.Length) {
93        throw new ArgumentException("The number of subscopes is not equal to the number of classifiers.");
94      }
95
96      int subsumptionClassifier = -1;
97
98      for (int i = 0; i < classifiers.Length; i++) {
99        if (CouldSubsume(experiences[i], errors[i])) {
100          if (subsumptionClassifier == -1 || classifiers[i].IsMoreGeneral(classifiers[subsumptionClassifier])) {
101            subsumptionClassifier = i;
102          }
103        }
104      }
105
106      if (subsumptionClassifier != -1) {
107        for (int i = 0; i < classifiers.Length; i++) {
108          if (classifiers[subsumptionClassifier].IsMoreGeneral(classifiers[i])) {
109            numerosities[subsumptionClassifier].Value += numerosities[i].Value;
110            AddVariableToScope(subscopes[i], new BoolValue(true));
111          } else {
112            AddVariableToScope(subscopes[i], new BoolValue(false));
113          }
114        }
115      } else {
116        for (int i = 0; i < classifiers.Length; i++) {
117          AddVariableToScope(subscopes[i], new BoolValue(false));
118        }
119      }
120
121      return base.Apply();
122    }
123
124    private void AddVariableToScope(IScope scope, BoolValue boolValue) {
125      if (scope.Variables.ContainsKey(HasBeenSubsumedParameterName)) {
126        scope.Variables[HasBeenSubsumedParameterName].Value = boolValue;
127      } else {
128        scope.Variables.Add(new Variable(HasBeenSubsumedParameterName, boolValue));
129      }
130    }
131
132    private bool CouldSubsume(IntValue exp, DoubleValue error) {
133      return exp.Value > ThetaSubsumptionParameter.ActualValue.Value && error.Value < ErrorZeroParameter.ActualValue.Value;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.