Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs @ 14293

Last change on this file since 14293 was 14293, checked in by bburlacu, 8 years ago

#2672: Implement online calculator cloning.

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineMeanErrorCalculator : IOnlineCalculator, IDeepCloneable {
28
29    private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
30    public double MeanError {
31      get { return meanAndVarianceCalculator.Mean; }
32    }
33
34    public OnlineMeanErrorCalculator() {
35      meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
36      Reset();
37    }
38
39    private OnlineMeanErrorCalculator(OnlineMeanAndVarianceCalculator meanAndVarianceCalculator) {
40      this.meanAndVarianceCalculator = meanAndVarianceCalculator;
41    }
42
43    #region IOnlineCalculator Members
44    public OnlineCalculatorError ErrorState {
45      get { return meanAndVarianceCalculator.MeanErrorState; }
46    }
47    public double Value {
48      get { return MeanError; }
49    }
50    public void Reset() {
51      meanAndVarianceCalculator.Reset();
52    }
53
54    public void Add(double original, double estimated) {
55      meanAndVarianceCalculator.Add(estimated - original);
56    }
57    #endregion
58
59    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
60      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
61      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
62      OnlineMeanErrorCalculator meCalculator = new OnlineMeanErrorCalculator();
63
64      // always move forward both enumerators (do not use short-circuit evaluation!)
65      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
66        double original = originalEnumerator.Current;
67        double estimated = estimatedEnumerator.Current;
68        meCalculator.Add(original, estimated);
69        if (meCalculator.ErrorState != OnlineCalculatorError.None) break;
70      }
71
72      // check if both enumerators are at the end to make sure both enumerations have the same length
73      if (meCalculator.ErrorState == OnlineCalculatorError.None &&
74         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
75        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
76      } else {
77        errorState = meCalculator.ErrorState;
78        return meCalculator.MeanError;
79      }
80    }
81
82    // IDeepCloneable members
83    public object Clone() {
84      var meanAndVarianceCalculatorClone = (OnlineMeanAndVarianceCalculator)meanAndVarianceCalculator.Clone();
85      return new OnlineMeanErrorCalculator(meanAndVarianceCalculatorClone);
86    }
87
88    public IDeepCloneable Clone(Cloner cloner) {
89      var clone = cloner.GetClone(this);
90      if (clone == null) {
91        var meanAndVarianceCalculatorClone = (OnlineMeanAndVarianceCalculator)meanAndVarianceCalculator.Clone(cloner);
92        clone = new OnlineMeanErrorCalculator(meanAndVarianceCalculatorClone);
93        cloner.RegisterClonedObject(this, clone);
94      }
95      return clone;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.