Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17915 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.6 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("VariableVectorAction", "")]
33  public class VariableVectorAction : Item, IAction {
34
35    [Storable]
36    private Dictionary<string, IActionVariable> variableDictionary;
37    public Dictionary<string, IActionVariable> VariableDictionary {
38      get { return variableDictionary; }
39      protected set { variableDictionary = value; }
40    }
41
42    public IOrderedEnumerable<string> Order { get { return VariableDictionary.Keys.OrderBy(x => x); } }
43
44    public int VirtualLength { get { return VariableDictionary.Values.Sum(x => x.VirtualLength); } }
45
46    [StorableConstructor]
47    protected VariableVectorAction(bool deserializing) { }
48    protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
49      VariableDictionary = new Dictionary<string, IActionVariable>(original.VariableDictionary.Count);
50      foreach (var item in original.VariableDictionary) {
51        VariableDictionary.Add(item.Key, cloner.Clone(item.Value));
52      }
53    }
54    public VariableVectorAction()
55      : base() {
56      VariableDictionary = new Dictionary<string, IActionVariable>();
57    }
58    public VariableVectorAction(int capacity)
59      : base() {
60      VariableDictionary = new Dictionary<string, IActionVariable>(capacity);
61    }
62    public VariableVectorAction(IDictionary<string, IActionVariable> dictionary)
63      : base() {
64      VariableDictionary = new Dictionary<string, IActionVariable>(dictionary);
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new VariableVectorAction(this, cloner);
68    }
69
70    public VariableVectorAction GetEmptyCopy() {
71      VariableVectorAction emptyCopy = new VariableVectorAction(VariableDictionary.Count);
72      emptyCopy.Add(VariableDictionary.Values.Select(x => x.GetEmptyCopy()));
73      return emptyCopy;
74    }
75
76    public VariableVectorAction GetSetCopy() {
77      VariableVectorAction setCopy = new VariableVectorAction(VariableDictionary.Count);
78      setCopy.Add(VariableDictionary.Values.Select(x => x.GetSetCopy()));
79      return setCopy;
80    }
81
82    public bool Match(IAction target) {
83      var targetCast = target as VariableVectorAction;
84      if (targetCast == null) { return false; }
85      foreach (var key in targetCast.VariableDictionary.Keys) {
86        if (!VariableDictionary.ContainsKey(key) || !VariableDictionary[key].MatchVariable(targetCast.VariableDictionary[key])) {
87          return false;
88        }
89      }
90      return true;
91    }
92
93    public void Add(IEnumerable<IActionVariable> action) {
94      foreach (var variable in action) {
95        VariableDictionary.Add(variable.VariableName, variable);
96      }
97    }
98
99    public override string ToString() {
100      StringBuilder sb = new StringBuilder();
101      bool first = true;
102      foreach (var variable in VariableDictionary.Values) {
103        if (first) {
104          first = false;
105        } else {
106          sb.Append(";");
107        }
108        sb.Append(variable.ToString());
109      }
110      return sb.ToString();
111    }
112
113    public void Randomize(IRandom random) {
114      foreach (var variable in VariableDictionary.Values) {
115        variable.RandomizeAction(random);
116      }
117    }
118
119    public bool Identical(VariableVectorAction target) {
120      if (!this.Order.SequenceEqual(target.Order)) { return false; }
121      foreach (var keyValuePair in target.VariableDictionary) {
122        if (!VariableDictionary[keyValuePair.Key].Identical(keyValuePair.Value)) {
123          return false;
124        }
125      }
126      return true;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.