Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • several small bug fixes
  • added windowing technique ILAS to GAssist
  • GAssist and XCS work now with real-valued features
  • severely improved the performance of XCS
File size: 4.4 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.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.ConditionActionEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.VariableVector {
31  [StorableClass]
32  [Item("VariableVectorCondition", "")]
33  public class VariableVectorCondition : Item, ICondition {
34
35    [Storable]
36    private Dictionary<string, IVariable> variableDictionary;
37    public Dictionary<string, IVariable> VariableDictionary {
38      get { return variableDictionary; }
39      protected set { variableDictionary = value; }
40    }
41
42    public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
43
44    [StorableConstructor]
45    protected VariableVectorCondition(bool deserializing) { }
46    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner) {
47      VariableDictionary = new Dictionary<string, IVariable>(original.VariableDictionary.Count);
48      foreach (var item in original.VariableDictionary) {
49        VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
50      }
51    }
52    public VariableVectorCondition()
53      : base() {
54      VariableDictionary = new Dictionary<string, IVariable>();
55    }
56    public VariableVectorCondition(int capacity)
57      : base() {
58      VariableDictionary = new Dictionary<string, IVariable>(capacity);
59    }
60    public VariableVectorCondition(IDictionary<string, IVariable> dictionary)
61      : base() {
62      VariableDictionary = new Dictionary<string, IVariable>(dictionary);
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new VariableVectorCondition(this, cloner);
66    }
67
68    public bool Match(IInput target) {
69      var targetCast = target as VariableVectorInput;
70      if (targetCast == null) { return false; }
71      foreach (var key in VariableDictionary.Keys) {
72        if (!targetCast.InputDictionary.ContainsKey(key) || !VariableDictionary[key].MatchInput(targetCast.InputDictionary[key])) {
73          return false;
74        }
75      }
76      return true;
77    }
78
79    public void Add(IEnumerable<IVariable> condition) {
80      foreach (var variable in condition) {
81        VariableDictionary.Add(variable.VariableName, variable);
82      }
83    }
84
85    public override string ToString() {
86      StringBuilder sb = new StringBuilder();
87      bool first = true;
88      foreach (var variable in VariableDictionary.Values) {
89        if (first) {
90          first = false;
91        } else {
92          sb.Append(";");
93        }
94        sb.Append(variable.ToString());
95      }
96      return sb.ToString();
97    }
98
99    public void Randomize(IRandom random, double spreadPercentage) {
100      foreach (var variable in VariableDictionary.Values) {
101        if (variable is DoubleVariable) {
102          ((DoubleVariable)variable).Randomize(random, spreadPercentage);
103        } else {
104          variable.Randomize(random);
105        }
106      }
107    }
108
109    public bool Identical(VariableVectorCondition target) {
110      var thisEnumerator = VariableDictionary.GetEnumerator();
111      var targetEnumerator = target.VariableDictionary.GetEnumerator();
112
113      while (thisEnumerator.MoveNext() && targetEnumerator.MoveNext()) {
114        if (thisEnumerator.Current.Key != targetEnumerator.Current.Key ||
115          !thisEnumerator.Current.Value.Identical(targetEnumerator.Current.Value)) {
116          return false;
117        }
118      }
119
120      return !thisEnumerator.MoveNext() && !targetEnumerator.MoveNext();
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.