Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/Discretizer/UniformWidthDiscretizer.cs @ 9385

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

#1980:

  • added be project Optimization.Operators.LCS
  • added default rule strategies for GAssist
File size: 2.8 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Operators.LCS {
30  [StorableClass]
31  [Item("UniformWidthDiscretizer", "")]
32  public class UniformWidthDiscretizer : Item, IDiscretizer {
33
34    [Storable]
35    private int bins;
36
37    [Storable]
38    private IDictionary<string, IList<double>> variableMicroItervals;
39
40    [StorableConstructor]
41    protected UniformWidthDiscretizer(bool deserializing) : base(deserializing) { }
42    protected UniformWidthDiscretizer(UniformWidthDiscretizer original, Cloner cloner)
43      : base(original, cloner) {
44    }
45    public UniformWidthDiscretizer()
46      : base() {
47      bins = 5;
48    }
49    public UniformWidthDiscretizer(int bins)
50      : base() {
51      variableMicroItervals = new Dictionary<string, IList<double>>();
52      this.bins = bins;
53    }
54    public override IDeepCloneable Clone(Cloner cloner) {
55      return new UniformWidthDiscretizer(this, cloner);
56    }
57
58    public int NumberOfMicroIntervals(string attribute) {
59      return bins;
60    }
61
62    public IEnumerable<double> discretizeValues(string attribute, IEnumerable<double> values) {
63      if (variableMicroItervals.ContainsKey(attribute)) {
64        throw new ArgumentException("Values of attribute " + attribute + " are already set.");
65      }
66
67      double min = values.Min();
68      double max = values.Max();
69
70      double intervalWidth = (max - min) / bins;
71      List<double> cutPoints = new List<double>(bins - 1);
72      double val = min;
73      for (int i = 0; i < bins - 1; i++) {
74        val += intervalWidth;
75        cutPoints.Add(val);
76      }
77
78      variableMicroItervals[attribute] = cutPoints;
79
80      return cutPoints;
81    }
82
83    public IEnumerable<double> GetCutPoints(string attribute) {
84      return variableMicroItervals[attribute];
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.