1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Evaluators {
|
---|
12 | public class OnlineMultiVariateEvaluator<T> : IMultiVariateOnlineEvaluator where T : IOnlineEvaluator, new() {
|
---|
13 | private List<T> evaluators;
|
---|
14 |
|
---|
15 | public OnlineMultiVariateEvaluator() {
|
---|
16 |
|
---|
17 | Reset();
|
---|
18 | }
|
---|
19 |
|
---|
20 | #region IMultiVariateOnlineEvaluator Members
|
---|
21 | public double Value {
|
---|
22 | get { return evaluators.Sum(e => e.Value); }
|
---|
23 | }
|
---|
24 |
|
---|
25 | public void Add(IEnumerable<double> original, IEnumerable<double> estimated) {
|
---|
26 | var originalEnumerator = original.GetEnumerator();
|
---|
27 | var estimatedEnumerator = estimated.GetEnumerator();
|
---|
28 | // first call of Add() => initialize evaluators list
|
---|
29 | if (evaluators == null) {
|
---|
30 | evaluators = new List<T>();
|
---|
31 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
32 | T evaluator = new T();
|
---|
33 | evaluator.Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
34 | evaluators.Add(evaluator);
|
---|
35 | }
|
---|
36 | } else {
|
---|
37 | // subsequent call of Add(): for each evaluator a value must be added
|
---|
38 | int i = 0;
|
---|
39 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext() && i < evaluators.Count) {
|
---|
40 | evaluators[i++].Add(originalEnumerator.Current, estimatedEnumerator.Current);
|
---|
41 | }
|
---|
42 | if (i < evaluators.Count) {
|
---|
43 | throw new ArgumentException("Number of elements in original and estimated does not match the number of elements in previously added vectors.");
|
---|
44 | }
|
---|
45 | }
|
---|
46 | if (originalEnumerator.MoveNext() | estimatedEnumerator.MoveNext()) {
|
---|
47 | throw new ArgumentException("Number of elements in original and estimated does not match.");
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void Reset() {
|
---|
52 | if (evaluators != null) {
|
---|
53 | foreach (var evaluator in evaluators)
|
---|
54 | evaluator.Reset();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | #endregion
|
---|
59 | }
|
---|
60 | }
|
---|