Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • made classes in Problems.ConditionActionClassification abstract
  • added Problems.VariableVectorClassification and Problems.CombinedIntegerVectorClassification
  • LCS works now with arbitrary problems, which implement ConditionActionClassificationProblem
File size: 5.5 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.Drawing;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.ConditionActionEncoding;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.VariableVector {
33  [StorableClass]
34  [Item("VariableVectorAction", "")]
35  public class VariableVectorAction : Dictionary<string, IActionVariable>, IAction {
36
37    public IOrderedEnumerable<string> Order { get { return this.Keys.OrderBy(x => x); } }
38
39    public int VirtualLength { get { return this.Values.Sum(x => x.VirtualLength); } }
40
41    [StorableConstructor]
42    protected VariableVectorAction(bool deserializing) { }
43    protected VariableVectorAction(VariableVectorAction original, Cloner cloner) {
44      foreach (var item in original) {
45        Add((string)item.Key.Clone(), (IActionVariable)item.Value.Clone());
46      }
47    }
48    public VariableVectorAction() : base() { }
49    public VariableVectorAction(int capacity) : base(capacity) { }
50    public VariableVectorAction(IDictionary<string, IActionVariable> dictionary) : base(dictionary) { }
51
52    //protected override void OnItemsAdded(IEnumerable<KeyValuePair<string, IActionVariable>> items) {
53    //  foreach (var item in items) {
54    //    if (!(item.Value is StringVariable || item.Value is IntVariable)) {
55    //      throw new ArgumentException("Value has to be of type 'StringVariable' or 'IntVariable'");
56    //    }
57    //  }
58    //}
59
60    public VariableVectorAction GetEmptyCopy() {
61      VariableVectorAction emptyCopy = new VariableVectorAction(this.Count);
62      foreach (var keyValuePair in this) {
63        emptyCopy.Add(keyValuePair.Key, keyValuePair.Value.GetEmptyCopy());
64      }
65      return emptyCopy;
66    }
67
68    public VariableVectorAction GetSetCopy() {
69      VariableVectorAction setCopy = new VariableVectorAction(this.Count);
70      foreach (var keyValuePair in this) {
71        setCopy.Add(keyValuePair.Key, keyValuePair.Value.GetSetCopy());
72      }
73      return setCopy;
74    }
75
76    public bool Match(IAction target) {
77      var targetCast = target as VariableVectorAction;
78      if (targetCast == null) { return false; }
79      if (!this.Order.SequenceEqual(targetCast.Order)) { return false; }
80      foreach (var keyValuePair in targetCast) {
81        if (!this[keyValuePair.Key].MatchVariable(keyValuePair.Value)) {
82          return false;
83        }
84      }
85      return true;
86    }
87
88    public void Add(IEnumerable<IActionVariable> action) {
89      foreach (var variable in action) {
90        this.Add(variable.VariableName, variable);
91      }
92    }
93
94    public override string ToString() {
95      StringBuilder sb = new StringBuilder();
96      bool first = true;
97      foreach (var variable in this.Values) {
98        if (first) {
99          first = false;
100        } else {
101          sb.Append(";");
102        }
103        sb.Append(variable.ToString());
104      }
105      return sb.ToString();
106    }
107
108    public void Randomize(IRandom random) {
109      foreach (var variable in this.Values) {
110        variable.Randomize(random);
111      }
112    }
113
114    public bool Identical(VariableVectorAction target) {
115      if (this.Order.SequenceEqual(target.Order)) { return false; }
116      foreach (var keyValuePair in target) {
117        if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
118          return false;
119        }
120      }
121      return true;
122    }
123
124    public virtual string ItemName {
125      get { return ItemAttribute.GetName(this.GetType()); }
126    }
127    public virtual string ItemDescription {
128      get { return ItemAttribute.GetDescription(this.GetType()); }
129    }
130    public Version ItemVersion {
131      get { return ItemAttribute.GetVersion(this.GetType()); }
132    }
133    public static Image StaticItemImage {
134      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
135    }
136    public virtual Image ItemImage {
137      get { return ItemAttribute.GetImage(this.GetType()); }
138    }
139
140    public virtual IDeepCloneable Clone(Cloner cloner) {
141      return new VariableVectorAction(this, cloner);
142    }
143
144    public object Clone() {
145      return Clone(new Cloner());
146    }
147
148    public event EventHandler ItemImageChanged;
149    protected virtual void OnItemImageChanged() {
150      EventHandler handler = ItemImageChanged;
151      if (handler != null) handler(this, EventArgs.Empty);
152    }
153    public event EventHandler ToStringChanged;
154    protected virtual void OnToStringChanged() {
155      EventHandler handler = ToStringChanged;
156      if (handler != null) handler(this, EventArgs.Empty);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.