1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Xml;
|
---|
28 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.CEDMA.Core {
|
---|
32 | public class Histogram {
|
---|
33 | private int buckets;
|
---|
34 | public int Buckets {
|
---|
35 | get { return buckets; }
|
---|
36 | set {
|
---|
37 | buckets = value;
|
---|
38 | ResetBucketBounds();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | private double[] limit;
|
---|
43 |
|
---|
44 | private List<double> values;
|
---|
45 |
|
---|
46 | public Histogram(int buckets) {
|
---|
47 | this.buckets = buckets;
|
---|
48 | values = new List<double>();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public double LowerValue(int bucketIndex) {
|
---|
52 | return limit[bucketIndex];
|
---|
53 | }
|
---|
54 |
|
---|
55 | public double UpperValue(int bucketIndex) {
|
---|
56 | return limit[bucketIndex+1];
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int Frequency(int bucketIndex) {
|
---|
60 | return values.Count(x => x >= LowerValue(bucketIndex) && x < UpperValue(bucketIndex));
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void AddValues(IEnumerable<double> xs) {
|
---|
64 | values.AddRange(xs.Where(x=>
|
---|
65 | !double.IsInfinity(x) && !double.IsNaN(x) && double.MaxValue!=x && double.MinValue !=x));
|
---|
66 | ResetBucketBounds();
|
---|
67 | }
|
---|
68 |
|
---|
69 | private void ResetBucketBounds() {
|
---|
70 | limit = new double[buckets+1];
|
---|
71 | values.Sort();
|
---|
72 | double min = values[10];
|
---|
73 | double max = values[values.Count-10];
|
---|
74 |
|
---|
75 | double step = (max - min) / buckets;
|
---|
76 | double cur = min;
|
---|
77 | for(int i = 0; i < buckets+1; i++) {
|
---|
78 | limit[i] = cur;
|
---|
79 | cur += step;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|