[7272] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7272] | 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;
|
---|
[14293] | 24 | using HeuristicLab.Common;
|
---|
[7272] | 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[14376] | 27 | public class OnlineMeanErrorCalculator : DeepCloneable, IOnlineCalculator {
|
---|
[7272] | 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 |
|
---|
[14465] | 39 | protected OnlineMeanErrorCalculator(OnlineMeanErrorCalculator original, Cloner cloner)
|
---|
| 40 | : base(original, cloner) {
|
---|
| 41 | meanAndVarianceCalculator = cloner.Clone(original.meanAndVarianceCalculator);
|
---|
[14293] | 42 | }
|
---|
[14465] | 43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 44 | return new OnlineMeanErrorCalculator(this, cloner);
|
---|
| 45 | }
|
---|
[14293] | 46 |
|
---|
[7272] | 47 | #region IOnlineCalculator Members
|
---|
| 48 | public OnlineCalculatorError ErrorState {
|
---|
| 49 | get { return meanAndVarianceCalculator.MeanErrorState; }
|
---|
| 50 | }
|
---|
| 51 | public double Value {
|
---|
| 52 | get { return MeanError; }
|
---|
| 53 | }
|
---|
| 54 | public void Reset() {
|
---|
| 55 | meanAndVarianceCalculator.Reset();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public void Add(double original, double estimated) {
|
---|
| 59 | meanAndVarianceCalculator.Add(estimated - original);
|
---|
| 60 | }
|
---|
| 61 | #endregion
|
---|
| 62 |
|
---|
| 63 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
| 64 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 65 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
| 66 | OnlineMeanErrorCalculator meCalculator = new OnlineMeanErrorCalculator();
|
---|
| 67 |
|
---|
| 68 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
| 69 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 70 | double original = originalEnumerator.Current;
|
---|
| 71 | double estimated = estimatedEnumerator.Current;
|
---|
| 72 | meCalculator.Add(original, estimated);
|
---|
| 73 | if (meCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 77 | if (meCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 78 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
| 79 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
| 80 | } else {
|
---|
| 81 | errorState = meCalculator.ErrorState;
|
---|
| 82 | return meCalculator.MeanError;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|