Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Variable/StringVariable.cs

Last change on this file was 9605, checked in by sforsten, 12 years ago

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 2.8 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.DecisionList {
30  [StorableClass]
31  [Item("StringVariable", "")]
32  public class StringVariable : Variable<string> {
33
34    [Storable]
35    protected IList<string> possibleFeatures;
36    public IEnumerable<string> PossibleFeatures {
37      get { return possibleFeatures; }
38    }
39
40    [StorableConstructor]
41    protected StringVariable(bool deserializing) : base(deserializing) { }
42    protected StringVariable(StringVariable original, Cloner cloner)
43      : base(original, cloner) {
44      possibleFeatures = new List<string>(original.possibleFeatures);
45    }
46    public StringVariable() : base() { }
47    public StringVariable(string variableName, IList<string> variableValues)
48      : base(variableName) {
49      if (variableValues.Count() > 0 && variableValues.Count() != variableValues.Distinct().Count()) {
50        throw new ArgumentException("variableValues have to be distinct and there has to be at least one value.");
51      }
52      possibleFeatures = variableValues;
53      attributes = Enumerable.Repeat<bool>(true, possibleFeatures.Count).ToList();
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new StringVariable(this, cloner);
57    }
58
59    public override double ComputeTheoryLength() {
60      return attributes.Count + attributes.Count(x => x == false);
61    }
62
63    public override bool Match(string input) {
64      return attributes[possibleFeatures.IndexOf(input)];
65    }
66
67    public override void SetToMatch(string variableValue) {
68      var valueIndex = possibleFeatures.IndexOf(variableValue);
69      if (valueIndex == -1) throw new ArgumentException("variableValue " + variableValue + " is not a possible value of variable " + variableName);
70
71      attributes[valueIndex] = true;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.