Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs @ 14351

Last change on this file since 14351 was 14330, checked in by gkronber, 8 years ago

#2650 Merged r14282:14322 from trunk to branch (fixing conflicts)

File size: 3.7 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    protected OnlineMeanErrorCalculator(OnlineMeanErrorCalculator other, Cloner cloner) {
40      meanAndVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.meanAndVarianceCalculator.Clone(cloner);
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 cloner = new Cloner();
85      return new OnlineMeanErrorCalculator(this, cloner);
86    }
87
88    public IDeepCloneable Clone(Cloner cloner) {
89      var clone = cloner.GetClone(this);
90      if (clone == null) {
91        clone = new OnlineMeanErrorCalculator(this, cloner);
92        cloner.RegisterClonedObject(this, clone);
93      }
94      return clone;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.