Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9342 was 9342, checked in by sforsten, 11 years ago

#1980:

  • added be project Optimization.Operators.LCS
  • added default rule strategies for GAssist
File size: 3.2 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    [Storable]
41    protected string variableName;
42    public string VariableName {
43      get { return variableName; }
44    }
45
46    public Type VariableType { get { return typeof(T); } }
47
48    [StorableConstructor]
49    protected Variable(bool deserializing) : base(deserializing) { }
50    protected Variable(Variable<T> original, Cloner cloner)
51      : base(original, cloner) {
52      variableName = original.variableName;
53      attributes = new List<bool>(original.attributes);
54    }
55    public Variable()
56      : base() {
57      attributes = new List<bool>();
58    }
59    public Variable(string variableName)
60      : base() {
61      this.variableName = variableName;
62      attributes = new List<bool>();
63    }
64
65    public virtual void Randomize(IRandom random, double onePercentage, IEnumerable<IDiscretizer> descretizers) {
66      for (int i = 0; i < attributes.Count; i++) {
67        attributes[i] = random.NextDouble() < onePercentage;
68      }
69    }
70
71    public abstract double ComputeTheoryLength();
72    public abstract bool Match(string input);
73
74    public virtual string ToFullString() { throw new NotImplementedException(); }
75
76    public override string ToString() {
77      StringBuilder sb = new StringBuilder();
78      foreach (var attr in attributes) {
79        if (attr) {
80          sb.Append('1');
81        } else {
82          sb.Append('0');
83        }
84      }
85      return sb.ToString();
86    }
87
88    public void Mutate(IRandom random) {
89      int pos = random.Next(0, attributes.Count);
90      attributes[pos] = !attributes[pos];
91    }
92
93    public virtual void Merge(IRandom Random) { return; }
94    public virtual void Reinitialize(IRandom Random, double onePercentage, IEnumerable<IDiscretizer> descretizers) { return; }
95    public virtual void Split(IRandom Random) { return; }
96  }
97}
Note: See TracBrowser for help on using the repository browser.