Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionSorter.cs @ 14820

Last change on this file since 14820 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization {
33  [Item("RunCollection Sorter", "Sorts a run collection according the specified key variable.")]
34  [StorableClass]
35  public class RunCollectionSorter : ParameterizedNamedItem, IRunCollectionModifier {
36
37    public override bool CanChangeName { get { return false; } }
38    public override bool CanChangeDescription { get { return false; } }
39
40    #region Parameters
41    public ValueParameter<StringValue> ValueParameter {
42      get { return (ValueParameter<StringValue>)Parameters["Value"]; }
43    }
44    #endregion
45
46    private string Value { get { return ValueParameter.Value.Value; } }
47
48    #region Construction & Cloning
49    [StorableConstructor]
50    protected RunCollectionSorter(bool deserializing) : base(deserializing) { }
51    protected RunCollectionSorter(RunCollectionSorter original, Cloner cloner)
52      : base(original, cloner) {
53      RegisterEvents();
54    }
55    public RunCollectionSorter() {
56      Parameters.Add(new ValueParameter<StringValue>("Value", "The variable name used as sorting key.", new StringValue("Value")));
57      RegisterEvents();
58      UpdateName();
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new RunCollectionSorter(this, cloner);
62    }
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() {
65      RegisterEvents();
66    }
67    #endregion
68
69    private void RegisterEvents() {
70      ValueParameter.ToStringChanged += Parameter_NameChanged;
71    }
72
73    private void Parameter_NameChanged(object sender, EventArgs e) {
74      UpdateName();
75    }
76
77    private void UpdateName() {
78      name = string.Format("Sort by {0}", Value);
79      OnNameChanged();
80    }
81
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.