Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/Variable/IntVariable.cs @ 17578

Last change on this file since 17578 was 9475, checked in by sforsten, 12 years ago

#1980: several small bug fixes

File size: 7.2 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.VariableVector {
30  [StorableClass]
31  [Item(Name = "IntVariable", Description = "")]
32  public class IntVariable : Variable<int>, IActionVariable {
33
34    public override int VirtualLength {
35      get { return 1; }
36    }
37
38    [Storable]
39    public bool Wildcard { get; set; }
40
41    [Storable]
42    protected IEnumerable<int> possibleFeatures;
43    public IEnumerable<int> PossibleFeatures {
44      get { return possibleFeatures; }
45    }
46
47    [Storable]
48    protected int currentValue;
49    public int CurrentValue {
50      get { return currentValue; }
51      set {
52        if (!possibleFeatures.Contains(value)) {
53          throw new ArgumentException("Value is not a possible feature of this variable.");
54        }
55        currentValue = value;
56      }
57    }
58
59    [StorableConstructor]
60    protected IntVariable(bool deserializing) : base(deserializing) { }
61    protected IntVariable(IntVariable original, Cloner cloner)
62      : base(original, cloner) {
63      this.possibleFeatures = new List<int>(original.possibleFeatures);
64      this.Wildcard = original.Wildcard;
65      this.currentValue = original.currentValue;
66    }
67    public IntVariable()
68      : base() {
69      Wildcard = false;
70    }
71    public IntVariable(string variableName, IEnumerable<int> variableValues)
72      : base(variableName) {
73      if (variableValues.Count() > 0 && variableValues.Count() != variableValues.Distinct().Count()) {
74        throw new ArgumentException("variableValues have to be distinct and there has to be at least one value.");
75      }
76      possibleFeatures = variableValues;
77      Wildcard = false;
78      currentValue = possibleFeatures.First();
79    }
80    public IntVariable(string variableName, IEnumerable<int> variableValues, int currentValue)
81      : this(variableName, variableValues) {
82      CurrentValue = currentValue;
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new IntVariable(this, cloner);
87    }
88
89    public override string ToString() {
90      return Wildcard ? "#" : currentValue.ToString();
91    }
92
93    public override bool MatchInput(string target) {
94      return Match(int.Parse(target));
95    }
96
97    public override bool Match(int target) {
98      return Wildcard || CurrentValue == target;
99    }
100
101    public override bool MatchVariable(IVariable target) {
102      var targetCast = target as IntVariable;
103      return targetCast != null && this.variableName.Equals(targetCast.VariableName) && Match(targetCast.CurrentValue);
104    }
105
106    public override IVariable GetEmptyCopy() {
107      return new IntVariable(variableName, possibleFeatures);
108    }
109
110    public override IVariable GetSetCopy() {
111      IntVariable copy = (IntVariable)GetEmptyCopy();
112      copy.Wildcard = this.Wildcard;
113      copy.CurrentValue = this.CurrentValue;
114      return copy;
115    }
116
117    IActionVariable IActionVariable.GetEmptyCopy() {
118      return (IntVariable)GetEmptyCopy();
119    }
120    IActionVariable IActionVariable.GetSetCopy() {
121      return (IntVariable)GetSetCopy();
122    }
123
124    public void RandomizeAction(IRandom random) {
125      int index = random.Next(possibleFeatures.Count());
126      currentValue = possibleFeatures.ElementAt(index);
127    }
128
129    public override void Randomize(IRandom random, double changeSymbolProbability) {
130      Wildcard = random.NextDouble() < changeSymbolProbability;
131      int index = random.Next(possibleFeatures.Count());
132      currentValue = possibleFeatures.ElementAt(index);
133    }
134
135    public override bool Identical(IVariable target) {
136      var targetCast = target as IntVariable;
137      if (targetCast == null) { return false; }
138      if (variableName != targetCast.variableName || Wildcard != targetCast.Wildcard
139        || currentValue != targetCast.currentValue) { return false; }
140
141      var thisEnumerator = possibleFeatures.GetEnumerator();
142      var castEnumerator = targetCast.possibleFeatures.GetEnumerator();
143      while (thisEnumerator.MoveNext() && castEnumerator.MoveNext()) {
144        if (thisEnumerator.Current != castEnumerator.Current)
145          return false;
146      }
147
148      return !thisEnumerator.MoveNext() && !castEnumerator.MoveNext();
149    }
150
151    public override double GetGenerality() {
152      return Wildcard ? 1 : 0;
153    }
154
155    public override bool IsGreaterThanOrEquallyGeneral(IVariable target) {
156      var targetCast = target as IntVariable;
157      if (targetCast == null) { return false; }
158      return Wildcard || currentValue == targetCast.currentValue;
159    }
160
161    public override IVariable CrossParentsAtPosition(IVariable parent2, int pos) {
162      var parent2Cast = parent2 as IntVariable;
163      if (parent2Cast == null) { throw new ArgumentException("Argument is not of the correct type."); }
164      if (pos != 0) { throw new ArgumentOutOfRangeException(); }
165      return (IntVariable)parent2.GetSetCopy();
166    }
167
168    public override void Manipulate(IRandom random, string stringValue, int pos,
169                                   double spreadPercentage) {
170      Manipulate(random, stringValue, pos);
171    }
172
173    public override void Manipulate(IRandom random, string stringValue, int pos) {
174      if (pos != 0) { throw new ArgumentOutOfRangeException(); }
175      Wildcard = !Wildcard;
176      if (!Wildcard) {
177        currentValue = int.Parse(stringValue);
178      }
179    }
180
181    public override void Cover(IRandom random, string stringValue, double changeSymbolProbability) {
182      Wildcard = random.NextDouble() < changeSymbolProbability;
183      if (!Wildcard) {
184        currentValue = int.Parse(stringValue);
185      }
186    }
187
188    public override int GetHashCode() {
189      int result = 1;
190      int wildcardInt = Wildcard ? 1 : 0;
191      result = 37 * result + wildcardInt;
192      result = 37 * result + currentValue;
193      foreach (var feature in possibleFeatures) {
194        result = 37 * result + feature;
195      }
196      return result;
197    }
198
199    #region IActionVariable Members
200    public void SetTo(string value) {
201      currentValue = int.Parse(value);
202    }
203
204    public IEnumerable<string> GetAllPossibleActions() {
205      return possibleFeatures.Select(x => x.ToString());
206    }
207    #endregion
208  }
209}
Note: See TracBrowser for help on using the repository browser.