Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/XCSModel.cs @ 9228

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

#1980:

  • added necessary interface ICondition, IAction, IInput
  • removed not used class MatchSelector and interface IMatchSelector
  • added constructors to ItemDictionary
  • added new encoding
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.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ConditionActionEncoding {
30  [Item("XCSModel", "Represents a model of XCS")]
31  [StorableClass]
32  public class XCSModel : ReadOnlyItemCollection<XCSClassifier>, IXCSModel {
33
34    public int ClassifierCount { get { return Count; } }
35
36    public IClassifierComparer ClassifierComparer { get; set; }
37
38    [StorableConstructor]
39    protected XCSModel(bool deserializing) : base(deserializing) { }
40    protected XCSModel(XCSModel original, Cloner cloner)
41      : base(original, cloner) {
42      name = (string)original.name.Clone();
43      description = (string)original.description.Clone();
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new XCSModel(this, cloner);
47    }
48    public XCSModel() : base(new ItemCollection<XCSClassifier>()) { }
49    public XCSModel(IItemCollection<XCSClassifier> collection)
50      : base(collection) {
51      this.name = ItemName;
52      this.description = ItemDescription;
53    }
54
55    public IEnumerable<IAction> GetAction(IEnumerable<IInput> classifiers) {
56      foreach (var classifier in classifiers) {
57        yield return GetAction(classifier);
58      }
59    }
60
61    public IAction GetAction(IInput classifier) {
62      var matchedClassifiers = new ItemCollection<XCSClassifier>();
63      foreach (var xcsClassifier in this) {
64        if (xcsClassifier.Classifier.MatchInput(classifier)) {
65          matchedClassifiers.Add(xcsClassifier);
66        }
67      }
68
69      if (matchedClassifiers.Count == 0) { return null; }
70
71      IDictionary<IAction, double> predictionArray = CreatePredictionArray(matchedClassifiers);
72      return predictionArray.OrderByDescending(x => x.Value).First().Key;
73    }
74
75    private IDictionary<IAction, double> CreatePredictionArray(ItemCollection<XCSClassifier> matchedClassifiers) {
76      var predictionArray = new Dictionary<IAction, double>(ClassifierComparer);
77      var fitnessSumPerAction = new Dictionary<IAction, double>(ClassifierComparer);
78
79      foreach (var xcsClassifier in matchedClassifiers) {
80        var action = xcsClassifier.Classifier.Action;
81        if (predictionArray.ContainsKey(action)) {
82          predictionArray[action] += xcsClassifier.Prediction.Value * xcsClassifier.Fitness.Value;
83          fitnessSumPerAction[action] += xcsClassifier.Fitness.Value;
84        } else {
85          predictionArray[action] = xcsClassifier.Prediction.Value * xcsClassifier.Fitness.Value;
86          fitnessSumPerAction[action] = xcsClassifier.Fitness.Value;
87        }
88      }
89
90      var actions = new List<IAction>(predictionArray.Keys);
91      foreach (var action in actions) {
92        if (fitnessSumPerAction[action] != 0) {
93          predictionArray[action] = predictionArray[action] / fitnessSumPerAction[action];
94        }
95      }
96      return predictionArray;
97    }
98
99    public IXCSSolution CreateConditionActionSolution(IConditionActionProblemData problemData) {
100      return new XCSSolution(this, problemData);
101    }
102
103    IConditionActionSolution IConditionActionModel.CreateConditionActionSolution(IConditionActionProblemData problemData) {
104      return CreateConditionActionSolution(problemData);
105    }
106
107    #region INamedItem Members
108    [Storable]
109    protected string name;
110    public string Name {
111      get { return name; }
112      set {
113        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
114        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
115          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
116          OnNameChanging(e);
117          if (!e.Cancel) {
118            name = value == null ? string.Empty : value;
119            OnNameChanged();
120          }
121        }
122      }
123    }
124    public virtual bool CanChangeName {
125      get { return true; }
126    }
127    [Storable]
128    protected string description;
129    public string Description {
130      get { return description; }
131      set {
132        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
133        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
134          description = value == null ? string.Empty : value;
135          OnDescriptionChanged();
136        }
137      }
138    }
139    public virtual bool CanChangeDescription {
140      get { return true; }
141    }
142
143    public override string ToString() {
144      return Name;
145    }
146
147    public event EventHandler<CancelEventArgs<string>> NameChanging;
148    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
149      var handler = NameChanging;
150      if (handler != null) handler(this, e);
151    }
152
153    public event EventHandler NameChanged;
154    protected virtual void OnNameChanged() {
155      var handler = NameChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157      OnToStringChanged();
158    }
159
160    public event EventHandler DescriptionChanged;
161    protected virtual void OnDescriptionChanged() {
162      var handler = DescriptionChanged;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
165    #endregion
166  }
167}
Note: See TracBrowser for help on using the repository browser.