#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Optimization.Operators.LCS;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.DecisionList {
[StorableClass]
[Item("Variable", "")]
public abstract class Variable : Item, IVariable {
[Storable]
protected IList attributes;
public IEnumerable Attributes {
get { return attributes; }
}
public double Length {
get { return attributes.Count; }
}
[Storable]
protected string variableName;
public string VariableName {
get { return variableName; }
}
public Type VariableType { get { return typeof(T); } }
[StorableConstructor]
protected Variable(bool deserializing) : base(deserializing) { }
protected Variable(Variable original, Cloner cloner)
: base(original, cloner) {
variableName = original.variableName;
attributes = new List(original.attributes);
}
public Variable()
: base() {
attributes = new List();
}
public Variable(string variableName)
: base() {
this.variableName = variableName;
attributes = new List();
}
public virtual void Randomize(IRandom random, double onePercentage, IEnumerable descretizers) {
for (int i = 0; i < attributes.Count; i++) {
attributes[i] = random.NextDouble() < onePercentage;
}
}
public abstract double ComputeTheoryLength();
public abstract bool Match(string input);
public abstract void SetToMatch(string variableValue);
public virtual string ToFullString() { throw new NotImplementedException(); }
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append(variableName + ": ");
foreach (var attr in attributes) {
if (attr) {
sb.Append('1');
} else {
sb.Append('0');
}
}
return sb.ToString();
}
public void Mutate(IRandom random) {
int pos = random.Next(0, attributes.Count);
attributes[pos] = !attributes[pos];
}
public virtual void Merge(IRandom Random) { return; }
public virtual void Reinitialize(IRandom Random, double onePercentage, IEnumerable descretizers) { return; }
public virtual void Split(IRandom Random) { return; }
}
}