[9154] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace 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 |
|
---|
[9411] | 36 | [Storable]
|
---|
| 37 | private IClassifierComparer comparer;
|
---|
| 38 | public IClassifierComparer ClassifierComparer {
|
---|
| 39 | get { return comparer; }
|
---|
| 40 | set { comparer = value; }
|
---|
| 41 | }
|
---|
[9194] | 42 |
|
---|
[9154] | 43 | [StorableConstructor]
|
---|
| 44 | protected XCSModel(bool deserializing) : base(deserializing) { }
|
---|
| 45 | protected XCSModel(XCSModel original, Cloner cloner)
|
---|
| 46 | : base(original, cloner) {
|
---|
| 47 | name = (string)original.name.Clone();
|
---|
| 48 | description = (string)original.description.Clone();
|
---|
[9411] | 49 | if (original.comparer != null) {
|
---|
| 50 | comparer = (IClassifierComparer)original.comparer.Clone();
|
---|
| 51 | }
|
---|
[9154] | 52 | }
|
---|
| 53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 54 | return new XCSModel(this, cloner);
|
---|
| 55 | }
|
---|
| 56 | public XCSModel() : base(new ItemCollection<XCSClassifier>()) { }
|
---|
| 57 | public XCSModel(IItemCollection<XCSClassifier> collection)
|
---|
| 58 | : base(collection) {
|
---|
| 59 | this.name = ItemName;
|
---|
| 60 | this.description = ItemDescription;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[9194] | 63 | public IEnumerable<IAction> GetAction(IEnumerable<IInput> classifiers) {
|
---|
[9161] | 64 | foreach (var classifier in classifiers) {
|
---|
| 65 | yield return GetAction(classifier);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[9194] | 69 | public IAction GetAction(IInput classifier) {
|
---|
[9154] | 70 | var matchedClassifiers = new ItemCollection<XCSClassifier>();
|
---|
| 71 | foreach (var xcsClassifier in this) {
|
---|
[9194] | 72 | if (xcsClassifier.Classifier.MatchInput(classifier)) {
|
---|
[9154] | 73 | matchedClassifiers.Add(xcsClassifier);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[9411] | 77 | if (matchedClassifiers.Count == 0) {
|
---|
| 78 | return null;
|
---|
| 79 | }
|
---|
[9154] | 80 |
|
---|
[9194] | 81 | IDictionary<IAction, double> predictionArray = CreatePredictionArray(matchedClassifiers);
|
---|
[9154] | 82 | return predictionArray.OrderByDescending(x => x.Value).First().Key;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[9194] | 85 | private IDictionary<IAction, double> CreatePredictionArray(ItemCollection<XCSClassifier> matchedClassifiers) {
|
---|
[9411] | 86 | if (ClassifierComparer == null) {
|
---|
| 87 | throw new ArgumentException("No classifier comparer specified!");
|
---|
| 88 | }
|
---|
[9194] | 89 | var predictionArray = new Dictionary<IAction, double>(ClassifierComparer);
|
---|
| 90 | var fitnessSumPerAction = new Dictionary<IAction, double>(ClassifierComparer);
|
---|
[9154] | 91 |
|
---|
| 92 | foreach (var xcsClassifier in matchedClassifiers) {
|
---|
| 93 | var action = xcsClassifier.Classifier.Action;
|
---|
| 94 | if (predictionArray.ContainsKey(action)) {
|
---|
| 95 | predictionArray[action] += xcsClassifier.Prediction.Value * xcsClassifier.Fitness.Value;
|
---|
| 96 | fitnessSumPerAction[action] += xcsClassifier.Fitness.Value;
|
---|
| 97 | } else {
|
---|
| 98 | predictionArray[action] = xcsClassifier.Prediction.Value * xcsClassifier.Fitness.Value;
|
---|
| 99 | fitnessSumPerAction[action] = xcsClassifier.Fitness.Value;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[9194] | 103 | var actions = new List<IAction>(predictionArray.Keys);
|
---|
[9154] | 104 | foreach (var action in actions) {
|
---|
| 105 | if (fitnessSumPerAction[action] != 0) {
|
---|
| 106 | predictionArray[action] = predictionArray[action] / fitnessSumPerAction[action];
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | return predictionArray;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public IXCSSolution CreateConditionActionSolution(IConditionActionProblemData problemData) {
|
---|
| 113 | return new XCSSolution(this, problemData);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | IConditionActionSolution IConditionActionModel.CreateConditionActionSolution(IConditionActionProblemData problemData) {
|
---|
| 117 | return CreateConditionActionSolution(problemData);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | #region INamedItem Members
|
---|
| 121 | [Storable]
|
---|
| 122 | protected string name;
|
---|
| 123 | public string Name {
|
---|
| 124 | get { return name; }
|
---|
| 125 | set {
|
---|
| 126 | if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
|
---|
| 127 | if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
|
---|
| 128 | CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
|
---|
| 129 | OnNameChanging(e);
|
---|
| 130 | if (!e.Cancel) {
|
---|
| 131 | name = value == null ? string.Empty : value;
|
---|
| 132 | OnNameChanged();
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | public virtual bool CanChangeName {
|
---|
| 138 | get { return true; }
|
---|
| 139 | }
|
---|
| 140 | [Storable]
|
---|
| 141 | protected string description;
|
---|
| 142 | public string Description {
|
---|
| 143 | get { return description; }
|
---|
| 144 | set {
|
---|
| 145 | if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
|
---|
| 146 | if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
|
---|
| 147 | description = value == null ? string.Empty : value;
|
---|
| 148 | OnDescriptionChanged();
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | public virtual bool CanChangeDescription {
|
---|
| 153 | get { return true; }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | public override string ToString() {
|
---|
| 157 | return Name;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 161 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
| 162 | var handler = NameChanging;
|
---|
| 163 | if (handler != null) handler(this, e);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | public event EventHandler NameChanged;
|
---|
| 167 | protected virtual void OnNameChanged() {
|
---|
| 168 | var handler = NameChanged;
|
---|
| 169 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 170 | OnToStringChanged();
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | public event EventHandler DescriptionChanged;
|
---|
| 174 | protected virtual void OnDescriptionChanged() {
|
---|
| 175 | var handler = DescriptionChanged;
|
---|
| 176 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 177 | }
|
---|
| 178 | #endregion
|
---|
| 179 | }
|
---|
| 180 | }
|
---|