Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Optimization/3.3/RunCollectionModification/RunCollectionSorter.cs @ 16462

Last change on this file since 16462 was 16462, checked in by jkarder, 5 years ago

#2520: worked on reintegration of new persistence

  • added nuget references to HEAL.Fossil
  • added StorableType attributes to many classes
  • changed signature of StorableConstructors
  • removed some classes in old persistence
  • removed some unnecessary usings
File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Fossil;
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    private class ValueComparer : IComparer<IComparable> {
82
83      #region IComparer<IComparable> Members
84
85      public int Compare(IComparable x, IComparable y) {
86        if (x == null && y == null) return 0;
87        if (x == null) return -1;
88        if (y == null) return 1;
89        return x.CompareTo(y);
90      }
91
92      #endregion
93    }
94
95    private static readonly ValueComparer Comparer = new ValueComparer();
96
97    #region IRunCollectionModifier Members
98
99    public void Modify(List<IRun> runs) {
100      var sortedRuns = runs
101        .Select(r => new {Run = r, Key = GetValue(r)})
102        .OrderBy(r => r.Key, Comparer)
103        .Select(r => r.Run).ToList();
104      runs.Clear();
105      runs.AddRange(sortedRuns);
106    }
107
108    private IComparable GetValue(IRun run) {
109      return GetValue(run.Results) ?? GetValue(run.Parameters);
110    }
111
112    private IComparable GetValue(IDictionary<string, IItem> variables) {
113      IItem value;
114      variables.TryGetValue(Value, out value);
115      var intValue = value as IntValue;
116      if (intValue != null)
117        return intValue;
118      var doubleValue = value as DoubleValue;
119      if (doubleValue != null)
120        return doubleValue;
121      if (value != null)
122        return value.ToString();
123      return null;
124    }
125
126    #endregion
127  }
128}
Note: See TracBrowser for help on using the repository browser.