1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
27 | public class OnlineDirectionalSymmetryCalculator : IOnlineCalculator {
|
---|
28 | private double prevEstimated;
|
---|
29 | private double prevOriginal;
|
---|
30 | private int n;
|
---|
31 | private int nCorrect;
|
---|
32 |
|
---|
33 | public double DirectionalSymmetry {
|
---|
34 | get {
|
---|
35 | if (n <= 1) return 0.0;
|
---|
36 | return (double)nCorrect / (n - 1) * 100.0;
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public OnlineDirectionalSymmetryCalculator() {
|
---|
41 | Reset();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public double Value {
|
---|
45 | get { return DirectionalSymmetry; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private OnlineCalculatorError errorState;
|
---|
49 | public OnlineCalculatorError ErrorState {
|
---|
50 | get { return errorState; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void Add(double original, double estimated) {
|
---|
54 | if (double.IsNaN(estimated) || double.IsInfinity(estimated) || double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
55 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
56 | } else if (n == 0) {
|
---|
57 | prevOriginal = original;
|
---|
58 | prevEstimated = estimated;
|
---|
59 | n++;
|
---|
60 | } else {
|
---|
61 | if ((original - prevOriginal) * (estimated - prevEstimated) >= 0.0) {
|
---|
62 | nCorrect++;
|
---|
63 | }
|
---|
64 | n++;
|
---|
65 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
66 | prevOriginal = original;
|
---|
67 | prevEstimated = estimated;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void Reset() {
|
---|
72 | n = 0;
|
---|
73 | nCorrect = 0;
|
---|
74 | prevOriginal = double.NaN;
|
---|
75 | prevEstimated = double.NaN;
|
---|
76 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | public static double Calculate(IEnumerable<double> first, IEnumerable<double> second, out OnlineCalculatorError errorState) {
|
---|
81 | IEnumerator<double> firstEnumerator = first.GetEnumerator();
|
---|
82 | IEnumerator<double> secondEnumerator = second.GetEnumerator();
|
---|
83 | OnlineDirectionalSymmetryCalculator dsCalculator = new OnlineDirectionalSymmetryCalculator();
|
---|
84 |
|
---|
85 | // add first element of time series as a reference point
|
---|
86 | firstEnumerator.MoveNext();
|
---|
87 | secondEnumerator.MoveNext();
|
---|
88 | dsCalculator.Add(firstEnumerator.Current, secondEnumerator.Current);
|
---|
89 |
|
---|
90 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
91 | while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
|
---|
92 | double estimated = secondEnumerator.Current;
|
---|
93 | double original = firstEnumerator.Current;
|
---|
94 | dsCalculator.Add(original, estimated);
|
---|
95 | if (dsCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
99 | if (dsCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
100 | (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) {
|
---|
101 | throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
|
---|
102 | } else {
|
---|
103 | errorState = dsCalculator.ErrorState;
|
---|
104 | return dsCalculator.DirectionalSymmetry;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|