1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Optimization {
|
---|
32 |
|
---|
33 | [Item("RunCollection Group Creater", "Regroups existing runs according to equal values in GroupBy and prefixes them according to their value in Prefix.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class RunCollectionGroupCreater : ParameterizedNamedItem, IRunCollectionModifier {
|
---|
36 |
|
---|
37 | public ValueParameter<ItemCollection<StringValue>> GroupByParameter {
|
---|
38 | get { return (ValueParameter<ItemCollection<StringValue>>)Parameters["GroupBy"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ValueParameter<StringValue> PrefixParameter {
|
---|
42 | get { return (ValueParameter<StringValue>)Parameters["Prefix"]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private IEnumerable<string> GroupBy { get { return GroupByParameter.Value.Select(v => v.Value); } }
|
---|
46 | private string Prefix { get { return PrefixParameter.Value.Value; } }
|
---|
47 |
|
---|
48 | #region Construction & Cloning
|
---|
49 | [StorableConstructor]
|
---|
50 | protected RunCollectionGroupCreater(bool deserializing) : base(deserializing) { }
|
---|
51 | protected RunCollectionGroupCreater(RunCollectionGroupCreater original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public RunCollectionGroupCreater() {
|
---|
53 | Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("GroupBy", "The variable that has to be the same for all members of a group.",
|
---|
54 | new ItemCollection<StringValue>(new[] { new StringValue("Problem Name") })));
|
---|
55 | Parameters.Add(new ValueParameter<StringValue>("Prefix", "The distinguishing prefix values for the individual runs.",
|
---|
56 | new StringValue("Algorithm Name")));
|
---|
57 | }
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new RunCollectionGroupCreater(this, cloner);
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | private static string GetStringValue(string name, IRun r) {
|
---|
64 | IItem item;
|
---|
65 | r.Results.TryGetValue(name, out item);
|
---|
66 | if (item != null)
|
---|
67 | return item.ToString();
|
---|
68 | r.Parameters.TryGetValue(name, out item);
|
---|
69 | return item != null ? item.ToString() : "<none>";
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static string GetValues(IEnumerable<string> names, IRun r) {
|
---|
73 | return string.Join("/", names.Select(n => GetStringValue(n, r)));
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void Modify(List<IRun> runs) {
|
---|
77 | var groups = runs.GroupBy(r => GetValues(GroupBy, r).ToString()).ToList();
|
---|
78 | runs.Clear();
|
---|
79 | foreach (var group in groups) {
|
---|
80 | var run = new Run { Name = string.Format(group.Key) };
|
---|
81 | foreach (var r in group) {
|
---|
82 | var prefix = GetStringValue(Prefix, r);
|
---|
83 | foreach (var result in r.Results) {
|
---|
84 | InsertNew(run.Results, prefix, result.Key, result.Value);
|
---|
85 | }
|
---|
86 | foreach (var parameter in r.Parameters) {
|
---|
87 | InsertNew(run.Parameters, prefix, parameter.Key, parameter.Value);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | runs.Add(run);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private static void InsertNew(IDictionary<string, IItem> dict, string prefix, string key, IItem value) {
|
---|
95 | if (prefix == null)
|
---|
96 | prefix = "<null>";
|
---|
97 | var n = 0;
|
---|
98 | var name = string.Format("{0}.{1}", prefix, key);
|
---|
99 | while (dict.ContainsKey(name)) {
|
---|
100 | name = string.Format("{0}_{1}.{2}", prefix, ++n, key);
|
---|
101 | }
|
---|
102 | dict.Add(name, value);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|