Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting_reintegration/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs @ 17896

Last change on this file since 17896 was 17896, checked in by gkronber, 3 years ago

#3073: refactoring to prepare for trunk integration

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