Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Variable/IntVariable.cs @ 9651

Last change on this file since 9651 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.9 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("IntVariable", "")]
32  public class IntVariable : Variable<int> {
33
34    [Storable]
35    protected IList<int> possibleFeatures;
36    public IEnumerable<int> PossibleFeatures {
37      get { return possibleFeatures; }
38    }
39
40    [StorableConstructor]
41    protected IntVariable(bool deserializing) : base(deserializing) { }
42    protected IntVariable(IntVariable original, Cloner cloner)
43      : base(original, cloner) {
44      this.possibleFeatures = new List<int>(original.possibleFeatures);
45    }
46    public IntVariable() : base() { }
47    public IntVariable(string variableName, IList<int> 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 IntVariable(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 Match(int.Parse(input));
65    }
66
67    public bool Match(int input) {
68      return attributes[possibleFeatures.IndexOf(input)];
69    }
70
71    public override void SetToMatch(string variableValue) {
72      var value = int.Parse(variableValue);
73      if (!possibleFeatures.Contains(value)) throw new ArgumentException("variableValue " + variableValue + " is not a possible value of variable " + variableName);
74
75      attributes[possibleFeatures.IndexOf(value)] = true;
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.