[2908] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2908] | 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 |
|
---|
[3489] | 22 | using System.Collections.Generic;
|
---|
[3690] | 23 | using System.Linq;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[2908] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace 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.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[2908] | 37 | public class DataTableValuesCollector : ValuesCollector {
|
---|
| 38 | public ValueLookupParameter<DataTable> DataTableParameter {
|
---|
| 39 | get { return (ValueLookupParameter<DataTable>)Parameters["DataTable"]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[4722] | 42 | #region Storing & Cloning
|
---|
| 43 | [StorableConstructor]
|
---|
| 44 | protected DataTableValuesCollector(bool deserializing) : base(deserializing) { }
|
---|
| 45 | protected DataTableValuesCollector(DataTableValuesCollector original, Cloner cloner) : base(original, cloner) { }
|
---|
| 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new DataTableValuesCollector(this, cloner);
|
---|
| 48 | }
|
---|
| 49 | #endregion
|
---|
[2908] | 50 | public DataTableValuesCollector()
|
---|
| 51 | : base() {
|
---|
| 52 | Parameters.Add(new ValueLookupParameter<DataTable>("DataTable", "The table of data values where the collected values should be stored."));
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public override IOperation Apply() {
|
---|
| 56 | DataTable table = DataTableParameter.ActualValue;
|
---|
| 57 | if (table == null) {
|
---|
| 58 | table = new DataTable(DataTableParameter.ActualName);
|
---|
| 59 | DataTableParameter.ActualValue = table;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | foreach (IParameter param in CollectedValues) {
|
---|
[3687] | 63 | ILookupParameter lookupParam = param as ILookupParameter;
|
---|
| 64 | string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
|
---|
| 65 |
|
---|
[3479] | 66 | if (param.ActualValue is DoubleValue) {
|
---|
[3687] | 67 | AddValue(table, (param.ActualValue as DoubleValue).Value, name, param.Description);
|
---|
[3818] | 68 | } else if (param.ActualValue is IntValue) {
|
---|
| 69 | AddValue(table, (param.ActualValue as IntValue).Value, name, param.Description);
|
---|
[3489] | 70 | } else if (param.ActualValue is IEnumerable<DoubleValue>) {
|
---|
[3690] | 71 | IEnumerable<DoubleValue> values = (IEnumerable<DoubleValue>)param.ActualValue;
|
---|
| 72 | if (values.Count() <= 1) {
|
---|
| 73 | foreach (DoubleValue data in values)
|
---|
| 74 | AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
|
---|
| 75 | } else {
|
---|
| 76 | int counter = 1;
|
---|
| 77 | foreach (DoubleValue data in values) {
|
---|
| 78 | AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
|
---|
| 79 | counter++;
|
---|
| 80 | }
|
---|
[3479] | 81 | }
|
---|
[3818] | 82 | } else if (param.ActualValue is IEnumerable<IntValue>) {
|
---|
| 83 | IEnumerable<IntValue> values = (IEnumerable<IntValue>)param.ActualValue;
|
---|
| 84 | if (values.Count() <= 1) {
|
---|
| 85 | foreach (IntValue data in values)
|
---|
| 86 | AddValue(table, data != null ? data.Value : double.NaN, name, param.Description);
|
---|
| 87 | } else {
|
---|
| 88 | int counter = 1;
|
---|
| 89 | foreach (IntValue data in values) {
|
---|
| 90 | AddValue(table, data != null ? data.Value : double.NaN, name + " " + counter.ToString(), param.Description);
|
---|
| 91 | counter++;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[2908] | 94 | } else {
|
---|
[3687] | 95 | AddValue(table, double.NaN, name, param.Description);
|
---|
[2908] | 96 | }
|
---|
| 97 | }
|
---|
| 98 | return base.Apply();
|
---|
| 99 | }
|
---|
[3489] | 100 |
|
---|
| 101 | private void AddValue(DataTable table, double data, string name, string description) {
|
---|
| 102 | DataRow row;
|
---|
| 103 | table.Rows.TryGetValue(name, out row);
|
---|
| 104 | if (row == null) {
|
---|
| 105 | row = new DataRow(name, description);
|
---|
| 106 | row.Values.Add(data);
|
---|
| 107 | table.Rows.Add(row);
|
---|
| 108 | } else {
|
---|
| 109 | row.Values.Add(data);
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[2908] | 112 | }
|
---|
| 113 | }
|
---|