Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.VariableVector/3.3/Variable/IntVariable.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: 6.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.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 override void Randomize(IRandom random) {
125      int index = random.Next(possibleFeatures.Count());
126      currentValue = possibleFeatures.ElementAt(index);
127    }
128
129    public override bool Identical(IVariable target) {
130      var targetCast = target as IntVariable;
131      if (targetCast == null) { return false; }
132      if (variableName != targetCast.variableName || Wildcard != targetCast.Wildcard
133        || currentValue != targetCast.currentValue) { return false; }
134
135      if (possibleFeatures.Except(targetCast.possibleFeatures).Count() != 0
136        || targetCast.possibleFeatures.Except(possibleFeatures).Count() != 0) { return false; }
137
138      return true;
139    }
140
141    public override double GetGenerality() {
142      return Wildcard ? 1 : 0;
143    }
144
145    public override bool IsGreaterThanOrEquallyGeneral(IVariable target) {
146      var targetCast = target as IntVariable;
147      if (targetCast == null) { return false; }
148      return Wildcard || currentValue == targetCast.currentValue;
149    }
150
151    public new IntVariable CrossParentsAtPosition(IVariable parent2, int pos) {
152      var parent2Cast = parent2 as IntVariable;
153      if (parent2Cast == null) { throw new ArgumentException("Argument is not of the correct type."); }
154      if (pos != 0) { throw new ArgumentOutOfRangeException(); }
155      return (IntVariable)this.GetSetCopy();
156    }
157
158    public override void Manipulate(IRandom random, string stringValue, int pos) {
159      if (pos != 0) { throw new ArgumentOutOfRangeException(); }
160      Wildcard = !Wildcard;
161      if (!Wildcard) {
162        currentValue = int.Parse(stringValue);
163      }
164    }
165
166    public override void Cover(IRandom random, string stringValue, double changeSymbolProbability) {
167      Wildcard = random.NextDouble() < changeSymbolProbability;
168      if (!Wildcard) {
169        currentValue = int.Parse(stringValue);
170      }
171    }
172
173    public override int GetHashCode() {
174      int result = 1;
175      int wildcardInt = Wildcard ? 1 : 0;
176      result = 37 * result + wildcardInt;
177      result = 37 * result + currentValue;
178      foreach (var feature in possibleFeatures) {
179        result = 37 * result + feature;
180      }
181      return result;
182    }
183
184    #region IActionVariable Members
185    public void SetTo(string value) {
186      currentValue = int.Parse(value);
187    }
188
189    public IEnumerable<string> GetAllPossibleActions() {
190      return possibleFeatures.Select(x => x.ToString());
191    }
192    #endregion
193  }
194}
Note: See TracBrowser for help on using the repository browser.