Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18047 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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