Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9467 was 9467, checked in by sforsten, 11 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: 7.0 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;
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;
[9226]65      this.currentValue = original.currentValue;
[9194]66    }
67    public IntVariable()
68      : base() {
69      Wildcard = false;
70    }
71    public IntVariable(string variableName, IEnumerable<int> variableValues)
72      : base(variableName) {
[9226]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.");
[9194]75      }
76      possibleFeatures = variableValues;
77      Wildcard = false;
[9226]78      currentValue = possibleFeatures.First();
[9194]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() {
[9204]90      return Wildcard ? "#" : currentValue.ToString();
[9194]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
[9226]106    public override IVariable GetEmptyCopy() {
[9194]107      return new IntVariable(variableName, possibleFeatures);
108    }
[9226]109
110    public override IVariable GetSetCopy() {
111      IntVariable copy = (IntVariable)GetEmptyCopy();
[9194]112      copy.Wildcard = this.Wildcard;
113      copy.CurrentValue = this.CurrentValue;
114      return copy;
115    }
116
[9226]117    IActionVariable IActionVariable.GetEmptyCopy() {
118      return (IntVariable)GetEmptyCopy();
119    }
120    IActionVariable IActionVariable.GetSetCopy() {
121      return (IntVariable)GetSetCopy();
122    }
123
[9467]124    public void RandomizeAction(IRandom random) {
[9194]125      int index = random.Next(possibleFeatures.Count());
126      currentValue = possibleFeatures.ElementAt(index);
127    }
128
[9467]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
[9194]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
[9392]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      }
[9194]147
[9392]148      return !thisEnumerator.MoveNext() && !castEnumerator.MoveNext();
[9194]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
[9242]161    public override IVariable CrossParentsAtPosition(IVariable parent2, int pos) {
[9194]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(); }
[9242]165      return (IntVariable)parent2.GetSetCopy();
[9194]166    }
[9204]167
168    public override void Manipulate(IRandom random, string stringValue, int pos) {
169      if (pos != 0) { throw new ArgumentOutOfRangeException(); }
170      Wildcard = !Wildcard;
171      if (!Wildcard) {
172        currentValue = int.Parse(stringValue);
173      }
174    }
175
176    public override void Cover(IRandom random, string stringValue, double changeSymbolProbability) {
177      Wildcard = random.NextDouble() < changeSymbolProbability;
178      if (!Wildcard) {
179        currentValue = int.Parse(stringValue);
180      }
181    }
[9226]182
183    public override int GetHashCode() {
184      int result = 1;
185      int wildcardInt = Wildcard ? 1 : 0;
186      result = 37 * result + wildcardInt;
187      result = 37 * result + currentValue;
188      foreach (var feature in possibleFeatures) {
189        result = 37 * result + feature;
190      }
191      return result;
192    }
193
194    #region IActionVariable Members
195    public void SetTo(string value) {
196      currentValue = int.Parse(value);
197    }
198
199    public IEnumerable<string> GetAllPossibleActions() {
200      return possibleFeatures.Select(x => x.ToString());
201    }
202    #endregion
[9194]203  }
204}
Note: See TracBrowser for help on using the repository browser.