Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionGroupCreater.cs @ 8124

Last change on this file since 8124 was 7228, checked in by epitzer, 13 years ago

#1622: Move run collection modifiers into a separate folder

File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Optimization {
11
12  [Item("RunCollection Group Creater", "Regroups existing runs according to equal values in GroupBy and prefixes them according to their value in Prefix.")]
13  [StorableClass]
14  public class RunCollectionGroupCreater : ParameterizedNamedItem, IRunCollectionModifier {
15   
16    public ValueParameter<ItemCollection<StringValue>> GroupByParameter {
17      get { return (ValueParameter<ItemCollection<StringValue>>)Parameters["GroupBy"]; }
18    }
19
20    public ValueParameter<StringValue> PrefixParameter {
21      get { return (ValueParameter<StringValue>)Parameters["Prefix"]; }
22    }
23
24    private IEnumerable<string> GroupBy { get { return GroupByParameter.Value.Select(v => v.Value); } }
25    private string Prefix { get { return PrefixParameter.Value.Value; } }
26
27    #region Construction & Cloning
28    [StorableConstructor]
29    protected RunCollectionGroupCreater(bool deserializing) : base(deserializing) { }
30    protected RunCollectionGroupCreater(RunCollectionGroupCreater original, Cloner cloner) : base(original, cloner) { }
31    public RunCollectionGroupCreater() {
32      Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("GroupBy", "The variable that has to be the same for all members of a group.",
33        new ItemCollection<StringValue>(new[] { new StringValue("Problem Name") })));
34      Parameters.Add(new ValueParameter<StringValue>("Prefix", "The distinguishing prefix values for the individual runs.",
35        new StringValue("Algorithm Name")));     
36    }
37    public override IDeepCloneable Clone(Cloner cloner) {
38      return new RunCollectionGroupCreater(this, cloner);
39    }
40    #endregion   
41
42    private static string GetStringValue(string name, IRun r) {
43      IItem item;
44      r.Results.TryGetValue(name, out item);
45      if (item != null)
46        return item.ToString();
47      r.Parameters.TryGetValue(name, out item);
48      return item != null ? item.ToString() : "<none>";
49    }
50
51    private static string GetValues(IEnumerable<string> names, IRun r) {
52      return string.Join("/", names.Select(n => GetStringValue(n, r)));
53    }
54
55    public void Modify(List<IRun> runs) {
56      var groups = runs.GroupBy(r => GetValues(GroupBy, r).ToString()).ToList();
57      runs.Clear();
58      foreach (var group in groups) {
59        var run = new Run { Name = string.Format(group.Key) };
60        foreach (var r in group) {
61          var prefix = GetStringValue(Prefix, r);
62          foreach (var result in r.Results) {
63            InsertNew(run.Results, prefix, result.Key, result.Value);
64          }
65          foreach (var parameter in r.Parameters) {
66            InsertNew(run.Parameters, prefix, parameter.Key, parameter.Value);
67          }
68        }
69        runs.Add(run);
70      }
71    }
72
73    private static void InsertNew(IDictionary<string, IItem> dict, string prefix, string key, IItem value) {
74      if (prefix == null)
75        prefix = "<null>";
76      var n = 0;
77      var name = string.Format("{0}.{1}", prefix, key);
78      while (dict.ContainsKey(name)) {
79        name = string.Format("{0}_{1}.{2}", prefix, ++n, key);
80      }
81      dict.Add(name, value);     
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.