Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/VariableVector.cs @ 9204

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

#1980:

  • deleted not needed interface IMatching
  • finished VariableVector encoding
  • the algorithm LearningClassifierSystem is now independent of a specific encoding. It still needs the ConditionActionEncoding.
  • merged r9191:9203 HeuristicLab.Core from trunk to branch
File size: 5.9 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 System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.ConditionActionEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.VariableVector {
32  [StorableClass]
33  [Item("VariableVector", "")]
34  public class VariableVector : Item, IClassifier {
35
36    [Storable]
37    protected VariableVectorCondition condition;
38    public VariableVectorCondition Condition { get { return condition; } }
39
40    [Storable]
41    protected VariableVectorAction action;
42    public VariableVectorAction Action { get { return action; } }
43
44    public int Count {
45      get { return condition.Count + action.Count; }
46    }
47
48    public int VirtualLength {
49      get { return condition.Values.Sum(x => x.VirtualLength) + action.Values.Sum(x => x.VirtualLength); }
50    }
51
52    [StorableConstructor]
53    protected VariableVector(bool deserializing) : base(deserializing) { }
54    protected VariableVector(VariableVector original, Cloner cloner)
55      : base(original, cloner) {
56      condition = new VariableVectorCondition(original.condition);
57      action = new VariableVectorAction(original.action);
58    }
59    public VariableVector() {
60      condition = new VariableVectorCondition();
61      action = new VariableVectorAction();
62    }
63    public VariableVector(IEnumerable<IVariable> condition, IEnumerable<IVariable> action)
64      : this() {
65      if (condition == null || action == null) { throw new ArgumentNullException(); }
66      if (action.All(x => x is IActionVariable)) { throw new ArgumentException("Action can only use IActionVariable"); }
67      this.Condition.Add(condition);
68      this.Action.Add(action.Cast<IActionVariable>());
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new VariableVector(this, cloner);
72    }
73
74    public override string ToString() {
75      StringBuilder sb = new StringBuilder();
76      sb.Append("[");
77      sb.Append(condition.ToString());
78      sb.Append("||");
79      sb.Append(action.ToString());
80      sb.Append("]");
81      return sb.ToString();
82    }
83
84    public void Randomize(IRandom random, double spreadPercentage) {
85      condition.Randomize(random, spreadPercentage);
86      action.Randomize(random);
87    }
88
89    ICondition IClassifier.Condition {
90      get { return Condition; }
91    }
92
93    IAction IClassifier.Action {
94      get { return Action; }
95    }
96
97    public bool MatchInput(IInput target) {
98      return Condition.Match(target);
99    }
100
101    public bool MatchAction(IAction target) {
102      return Action.Match(target);
103    }
104
105    public bool IsMoreGeneral(IClassifier target) {
106      return IsMoreGeneral(target.Condition);
107    }
108
109    public bool IsMoreGeneral(ICondition target) {
110      var targetCast = target as VariableVector;
111      if (targetCast == null) { return false; }
112      if (Condition.Keys.Except(targetCast.Condition.Keys).Count() != 0
113        || targetCast.Condition.Keys.Except(Condition.Keys).Count() != 0) { return false; }
114
115      double thisGenerality = Condition.Values.Sum(x => x.GetGenerality());
116      double targetGenerality = Condition.Values.Sum(x => x.GetGenerality());
117      if (thisGenerality <= targetGenerality) { return false; }
118
119      foreach (var variables in targetCast.Condition) {
120        if (!Condition[variables.Key].IsGreaterThanOrEquallyGeneral(variables.Value)) {
121          return false;
122        }
123      }
124      return true;
125    }
126
127    public bool Identical(IClassifier target) {
128      var targetCast = target as VariableVector;
129      if (targetCast == null) { return false; }
130      if (!condition.Identical(targetCast.condition)) { return false; }
131      if (!action.Identical(targetCast.condition)) { return false; }
132      return true;
133    }
134
135    [StorableClass]
136    [Item("VariableVectorActionComparer", "")]
137    public class VariableVectorActionComparer : Item, IEqualityComparer<VariableVectorAction>, IClassifierComparer {
138
139      [StorableConstructor]
140      protected VariableVectorActionComparer(bool deserializing) : base(deserializing) { }
141      protected VariableVectorActionComparer(VariableVectorActionComparer original, Cloner cloner)
142        : base(original, cloner) {
143      }
144      public override IDeepCloneable Clone(Cloner cloner) {
145        return new VariableVectorActionComparer(this, cloner);
146      }
147      public VariableVectorActionComparer() : base() { }
148
149      public bool Equals(VariableVectorAction x, VariableVectorAction y) {
150        throw new NotImplementedException();
151      }
152
153      public int GetHashCode(VariableVectorAction obj) {
154        throw new NotImplementedException();
155      }
156
157      public bool Equals(IAction x, IAction y) {
158        var xCast = x as VariableVectorAction;
159        var yCast = y as VariableVectorAction;
160        return x != null && y != null && Equals(xCast, yCast);
161      }
162
163      public int GetHashCode(IAction obj) {
164        var objCast = obj as VariableVectorAction;
165        return objCast != null ? GetHashCode(objCast) : obj.GetHashCode();
166      }
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.