Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17021 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: 3.3 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.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization.Operators.LCS;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.DecisionList {
31  [StorableClass]
32  [Item("Variable", "")]
33  public abstract class Variable<T> : Item, IVariable {
34    [Storable]
35    protected IList<bool> attributes;
36    public IEnumerable<bool> Attributes {
37      get { return attributes; }
38    }
39
40    public double Length {
41      get { return attributes.Count; }
42    }
43
44    [Storable]
45    protected string variableName;
46    public string VariableName {
47      get { return variableName; }
48    }
49
50    public Type VariableType { get { return typeof(T); } }
51
52    [StorableConstructor]
53    protected Variable(bool deserializing) : base(deserializing) { }
54    protected Variable(Variable<T> original, Cloner cloner)
55      : base(original, cloner) {
56      variableName = original.variableName;
57      attributes = new List<bool>(original.attributes);
58    }
59    public Variable()
60      : base() {
61      attributes = new List<bool>();
62    }
63    public Variable(string variableName)
64      : base() {
65      this.variableName = variableName;
66      attributes = new List<bool>();
67    }
68
69    public virtual void Randomize(IRandom random, double onePercentage, IEnumerable<IDiscretizer> descretizers) {
70      for (int i = 0; i < attributes.Count; i++) {
71        attributes[i] = random.NextDouble() < onePercentage;
72      }
73    }
74
75    public abstract double ComputeTheoryLength();
76    public abstract bool Match(string input);
77
78    public abstract void SetToMatch(string variableValue);
79
80    public virtual string ToFullString() { throw new NotImplementedException(); }
81
82    public override string ToString() {
83      StringBuilder sb = new StringBuilder();
84      sb.Append(variableName + ": ");
85      foreach (var attr in attributes) {
86        if (attr) {
87          sb.Append('1');
88        } else {
89          sb.Append('0');
90        }
91      }
92      return sb.ToString();
93    }
94
95    public void Mutate(IRandom random) {
96      int pos = random.Next(0, attributes.Count);
97      attributes[pos] = !attributes[pos];
98    }
99
100    public virtual void Merge(IRandom Random) { return; }
101    public virtual void Reinitialize(IRandom Random, double onePercentage, IEnumerable<IDiscretizer> descretizers) { return; }
102    public virtual void Split(IRandom Random) { return; }
103  }
104}
Note: See TracBrowser for help on using the repository browser.