Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/VariableVectorCondition.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: 4.7 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("VariableVectorCondition", "")]
35  public class VariableVectorCondition : Dictionary<string, IVariable>, ICondition {
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 VariableVectorCondition(bool deserializing) { }
43    protected VariableVectorCondition(VariableVectorCondition original, Cloner cloner) {
44      foreach (var item in original) {
45        Add((string)item.Key.Clone(), (IVariable)item.Value.Clone());
46      }
47    }
48    public VariableVectorCondition() : base() { }
49    public VariableVectorCondition(int capacity) : base(capacity) { }
50    public VariableVectorCondition(IDictionary<string, IVariable> dictionary) : base(dictionary) { }
51
52    public bool Match(IInput target) {
53      var targetCast = target as VariableVectorInput;
54      if (targetCast == null) { return false; }
55      if (!this.Keys.All(x => targetCast.Keys.Contains(x))) { return false; }
56      foreach (var key in this.Keys) {
57        if (!this[key].MatchInput(targetCast[key])) {
58          return false;
59        }
60      }
61      return true;
62    }
63
64    public void Add(IEnumerable<IVariable> condition) {
65      foreach (var variable in condition) {
66        this.Add(variable.VariableName, variable);
67      }
68    }
69
70    public override string ToString() {
71      StringBuilder sb = new StringBuilder();
72      bool first = true;
73      foreach (var variable in this.Values) {
74        if (first) {
75          first = false;
76        } else {
77          sb.Append(";");
78        }
79        sb.Append(variable.ToString());
80      }
81      return sb.ToString();
82    }
83
84    public void Randomize(IRandom random, double spreadPercentage) {
85      foreach (var variable in this.Values) {
86        if (variable is DoubleVariable) {
87          ((DoubleVariable)variable).Randomize(random, spreadPercentage);
88        } else {
89          variable.Randomize(random);
90        }
91      }
92    }
93
94    public bool Identical(VariableVectorCondition target) {
95      if (!this.Order.SequenceEqual(target.Order)) { return false; }
96      foreach (var keyValuePair in target) {
97        if (!this[keyValuePair.Key].Identical(keyValuePair.Value)) {
98          return false;
99        }
100      }
101      return true;
102    }
103
104    public virtual string ItemName {
105      get { return ItemAttribute.GetName(this.GetType()); }
106    }
107    public virtual string ItemDescription {
108      get { return ItemAttribute.GetDescription(this.GetType()); }
109    }
110    public Version ItemVersion {
111      get { return ItemAttribute.GetVersion(this.GetType()); }
112    }
113    public static Image StaticItemImage {
114      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
115    }
116    public virtual Image ItemImage {
117      get { return ItemAttribute.GetImage(this.GetType()); }
118    }
119
120    public virtual IDeepCloneable Clone(Cloner cloner) {
121      return new VariableVectorCondition(this, cloner);
122    }
123
124    public object Clone() {
125      return Clone(new Cloner());
126    }
127
128    public event EventHandler ItemImageChanged;
129    protected virtual void OnItemImageChanged() {
130      EventHandler handler = ItemImageChanged;
131      if (handler != null) handler(this, EventArgs.Empty);
132    }
133    public event EventHandler ToStringChanged;
134    protected virtual void OnToStringChanged() {
135      EventHandler handler = ToStringChanged;
136      if (handler != null) handler(this, EventArgs.Empty);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.