#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.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Optimization.Operators.LCS { [StorableClass] [Item("UniformWidthDiscretizer", "")] public class UniformWidthDiscretizer : Item, IDiscretizer { [Storable] private int bins; [Storable] private IDictionary> variableMicroItervals; [StorableConstructor] protected UniformWidthDiscretizer(bool deserializing) : base(deserializing) { } protected UniformWidthDiscretizer(UniformWidthDiscretizer original, Cloner cloner) : base(original, cloner) { } public UniformWidthDiscretizer() : base() { bins = 5; } public UniformWidthDiscretizer(int bins) : base() { variableMicroItervals = new Dictionary>(); this.bins = bins; } public override IDeepCloneable Clone(Cloner cloner) { return new UniformWidthDiscretizer(this, cloner); } public int NumberOfMicroIntervals(string attribute) { return bins; } public IEnumerable discretizeValues(string attribute, IEnumerable values) { if (variableMicroItervals.ContainsKey(attribute)) { throw new ArgumentException("Values of attribute " + attribute + " are already set."); } double min = values.Min(); double max = values.Max(); double intervalWidth = (max - min) / bins; List cutPoints = new List(bins - 1); double val = min; for (int i = 0; i < bins - 1; i++) { val += intervalWidth; cutPoints.Add(val); } variableMicroItervals[attribute] = cutPoints; return cutPoints; } public IEnumerable GetCutPoints(string attribute) { return variableMicroItervals[attribute]; } } }