Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10138 was 9467, checked in by sforsten, 12 years ago

#1980:

  • added ProportionalTournamentSelector for XCS
  • fixed bug: if an initial population is created in XCS, the initial population also creates general classifier, not only specific ones
  • merged r9204:9466 HeuristicLab.Core from trunk to branch
File size: 4.5 KB
RevLine 
[9194]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", "")]
[9242]33  public class VariableVectorCondition : Item, ICondition {
[9194]34
[9242]35    [Storable]
36    private Dictionary<string, IVariable> variableDictionary;
37    public Dictionary<string, IVariable> VariableDictionary {
38      get { return variableDictionary; }
39      protected set { variableDictionary = value; }
40    }
[9204]41
[9242]42    public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
43
[9194]44    [StorableConstructor]
[9226]45    protected VariableVectorCondition(bool deserializing) { }
46    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner) {
[9242]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));
[9226]50      }
[9194]51    }
[9242]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    }
[9194]67
68    public bool Match(IInput target) {
69      var targetCast = target as VariableVectorInput;
70      if (targetCast == null) { return false; }
[9242]71      foreach (var key in VariableDictionary.Keys) {
72        if (!targetCast.InputDictionary.ContainsKey(key) || !VariableDictionary[key].MatchInput(targetCast.InputDictionary[key])) {
[9194]73          return false;
74        }
75      }
76      return true;
77    }
78
79    public void Add(IEnumerable<IVariable> condition) {
80      foreach (var variable in condition) {
[9242]81        VariableDictionary.Add(variable.VariableName, variable);
[9194]82      }
83    }
84
85    public override string ToString() {
86      StringBuilder sb = new StringBuilder();
87      bool first = true;
[9242]88      foreach (var variable in VariableDictionary.Values) {
[9226]89        if (first) {
[9194]90          first = false;
91        } else {
92          sb.Append(";");
93        }
94        sb.Append(variable.ToString());
95      }
96      return sb.ToString();
97    }
98
[9467]99    public void Randomize(IRandom random, double changeSymbolProbability, double spreadPercentage) {
[9242]100      foreach (var variable in VariableDictionary.Values) {
[9194]101        if (variable is DoubleVariable) {
[9467]102          ((DoubleVariable)variable).Randomize(random, changeSymbolProbability, spreadPercentage);
[9194]103        } else {
[9467]104          variable.Randomize(random, changeSymbolProbability);
[9194]105        }
106      }
107    }
108
109    public bool Identical(VariableVectorCondition target) {
[9392]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)) {
[9194]116          return false;
117        }
118      }
[9392]119
120      return !thisEnumerator.MoveNext() && !targetEnumerator.MoveNext();
[9194]121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.