Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3116_GAM_Interactions/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs

Last change on this file was 17906, checked in by gkronber, 4 years ago

#3073 bugfixing

File size: 4.4 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 HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [Item("IntervalCollection", "Represents variables with their interval ranges.")]
31  [StorableType("230B4E4B-41E5-4D33-9BC3-E2DAADDCA5AE")]
32  public class IntervalCollection : Item {
33    public static new System.Drawing.Image StaticItemImage {
34      get => HeuristicLab.Common.Resources.VSImageLibrary.Object;
35    }
36
37    private IDictionary<string, Interval> intervals { get; set; } = new Dictionary<string, Interval>();
38
39    [Storable(OldName = "StorableIntervalInformation")]
40    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
41      set {
42        foreach (var varInt in value)
43          intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
44      }
45    }
46
47    [Storable]
48    private object[] StorableIntervals {
49      get {
50        var names = intervals.Keys.ToArray();
51        var lowerBounds = intervals.Values.Select(i => i.LowerBound).ToArray();
52        var upperBounds = intervals.Values.Select(i => i.UpperBound).ToArray();
53
54        return new object[] { names, lowerBounds, upperBounds };
55      }
56
57      set {
58        var names = (string[])value[0];
59        var lowerBounds = (double[])value[1];
60        var upperBounds = (double[])value[2];
61
62        for (int i = 0; i < names.Length; i++) {
63          intervals.Add(names[i], new Interval(lowerBounds[i], upperBounds[i]));
64        }
65      }
66    }
67
68    public int Count => intervals.Count;
69
70    public IntervalCollection() : base() { }
71    [StorableConstructor]
72    protected IntervalCollection(StorableConstructorFlag _) : base(_) { }
73
74    protected IntervalCollection(IntervalCollection original, Cloner cloner) : base(original, cloner) {
75      foreach (var keyValuePair in original.intervals) {
76        intervals.Add(keyValuePair.Key, new Interval(keyValuePair.Value.LowerBound, keyValuePair.Value.UpperBound));
77      }
78    }
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new IntervalCollection(this, cloner);
81    }
82
83    public IntervalCollection(IDictionary<string, Interval> intervals) {
84      this.intervals = intervals;
85    }
86
87    public Interval GetInterval(string identifier) {
88      if (!intervals.ContainsKey(identifier)) throw new ArgumentException($"The given identifier:{ identifier } is not present!");
89
90      return intervals[identifier];
91    }
92
93    public void SetInterval(string identifier, Interval interval) {
94      intervals[identifier] = interval;
95      RaiseChanged();
96    }
97
98    public void AddInterval(string identifier, Interval interval) {
99      intervals.Add(identifier, interval);
100      RaiseChanged();
101    }
102
103    public void DeleteInterval(string identifier) {
104      intervals.Remove(identifier);
105      RaiseChanged();
106    }
107
108    public IReadOnlyDictionary<string, Interval> GetReadonlyDictionary() {
109      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
110    }
111
112    public IDictionary<string, Interval> GetDictionary() {
113      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
114    }
115
116    public IEnumerable<Tuple<string, Interval>> GetVariableIntervals() {
117      foreach (var variableInterval in intervals)
118        yield return Tuple.Create(variableInterval.Key, variableInterval.Value);
119    }
120
121    public event EventHandler Changed;
122    private void RaiseChanged() {
123      var handler = Changed;
124      if (handler != null)
125        handler(this, EventArgs.Empty);
126    }
127
128  }
129}
130
Note: See TracBrowser for help on using the repository browser.