[9334] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[9342] | 27 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
[9334] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 |
|
---|
[9605] | 40 | public double Length {
|
---|
| 41 | get { return attributes.Count; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[9334] | 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 |
|
---|
[9605] | 78 | public abstract void SetToMatch(string variableValue);
|
---|
| 79 |
|
---|
[9334] | 80 | public virtual string ToFullString() { throw new NotImplementedException(); }
|
---|
| 81 |
|
---|
| 82 | public override string ToString() {
|
---|
| 83 | StringBuilder sb = new StringBuilder();
|
---|
[9352] | 84 | sb.Append(variableName + ": ");
|
---|
[9334] | 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 | }
|
---|