Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Analysis/3.3/DataTableValuesCollector.cs @ 3687

Last change on this file since 3687 was 3687, checked in by swagner, 14 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • fixed naming issues in DataTableValuesCollector, ResultsCollector and VariableCreator
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Analysis {
32  /// <summary>
33  /// An operator which collects the actual values of parameters and adds them to a table of data values.
34  /// </summary>
35  [Item("DataTableValuesCollector", "An operator which collects the actual values of parameters and adds them to a table of data values.")]
36  [StorableClass]
37  public class DataTableValuesCollector : ValuesCollector {
38    public ValueLookupParameter<DataTable> DataTableParameter {
39      get { return (ValueLookupParameter<DataTable>)Parameters["DataTable"]; }
40    }
41
42    public DataTableValuesCollector()
43      : base() {
44      Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
45    }
46
47    public override IOperation Apply() {
48      DataTable table = DataTableParameter.ActualValue;
49      if (table == null) {
50        table = new DataTable(DataTableParameter.ActualName);
51        DataTableParameter.ActualValue = table;
52      }
53
54      foreach (IParameter param in CollectedValues) {
55        ILookupParameter lookupParam = param as ILookupParameter;
56        string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
57
58        if (param.ActualValue is DoubleValue) {
59          AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
60        } else if (param.ActualValue is IEnumerable<DoubleValue>) {
61          int counter = 0;
62          foreach (DoubleValue data in (param.ActualValue as IEnumerable<DoubleValue>)) {
63            AddValue(table, data.Value, name + " " + counter.ToString(), param.Description);
64            counter++;
65          }
66        } else {
67          AddValue(table, double.NaN, name, param.Description);
68        }
69      }
70      return base.Apply();
71    }
72
73    private void AddValue(DataTable table, double data, string name, string description) {
74      DataRow row;
75      table.Rows.TryGetValue(name, out row);
76      if (row == null) {
77        row = new DataRow(name, description);
78        row.Values.Add(data);
79        table.Rows.Add(row);
80      } else {
81        row.Values.Add(data);
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.