Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3027-NormalDistribution/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionSorter.cs @ 17481

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

#2875: Removed years in copyrights

File size: 4.1 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
30
31namespace HeuristicLab.Optimization {
32  [Item("RunCollection Sorter", "Sorts a run collection according the specified key variable.")]
33  [StorableType("0F64D1D2-8934-4697-B997-A8072ADB4CF3")]
34  public class RunCollectionSorter : ParameterizedNamedItem, IRunCollectionModifier {
35
36    public override bool CanChangeName { get { return false; } }
37    public override bool CanChangeDescription { get { return false; } }
38
39    #region Parameters
40    public ValueParameter<StringValue> ValueParameter {
41      get { return (ValueParameter<StringValue>)Parameters["Value"]; }
42    }
43    #endregion
44
45    private string Value { get { return ValueParameter.Value.Value; } }
46
47    #region Construction & Cloning
48    [StorableConstructor]
49    protected RunCollectionSorter(StorableConstructorFlag _) : base(_) { }
50    protected RunCollectionSorter(RunCollectionSorter original, Cloner cloner)
51      : base(original, cloner) {
52      RegisterEvents();
53    }
54    public RunCollectionSorter() {
55      Parameters.Add(new ValueParameter<StringValue>("Value", "The variable name used as sorting key.", new StringValue("Value")));
56      RegisterEvents();
57      UpdateName();
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new RunCollectionSorter(this, cloner);
61    }
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      RegisterEvents();
65    }
66    #endregion
67
68    private void RegisterEvents() {
69      ValueParameter.ToStringChanged += Parameter_NameChanged;
70    }
71
72    private void Parameter_NameChanged(object sender, EventArgs e) {
73      UpdateName();
74    }
75
76    private void UpdateName() {
77      name = string.Format("Sort by {0}", Value);
78      OnNameChanged();
79    }
80
81    [StorableType("824092f4-863b-4ba8-ac0a-98f871876a6e")]
82    private class ValueComparer : IComparer<IComparable> {
83
84      #region IComparer<IComparable> Members
85
86      public int Compare(IComparable x, IComparable y) {
87        if (x == null && y == null) return 0;
88        if (x == null) return -1;
89        if (y == null) return 1;
90        return x.CompareTo(y);
91      }
92
93      #endregion
94    }
95
96    private static readonly ValueComparer Comparer = new ValueComparer();
97
98    #region IRunCollectionModifier Members
99
100    public void Modify(List<IRun> runs) {
101      var sortedRuns = runs
102        .Select(r => new {Run = r, Key = GetValue(r)})
103        .OrderBy(r => r.Key, Comparer)
104        .Select(r => r.Run).ToList();
105      runs.Clear();
106      runs.AddRange(sortedRuns);
107    }
108
109    private IComparable GetValue(IRun run) {
110      return GetValue(run.Results) ?? GetValue(run.Parameters);
111    }
112
113    private IComparable GetValue(IDictionary<string, IItem> variables) {
114      IItem value;
115      variables.TryGetValue(Value, out value);
116      var intValue = value as IntValue;
117      if (intValue != null)
118        return intValue;
119      var doubleValue = value as DoubleValue;
120      if (doubleValue != null)
121        return doubleValue;
122      if (value != null)
123        return value.ToString();
124      return null;
125    }
126
127    #endregion
128  }
129}
Note: See TracBrowser for help on using the repository browser.