Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval/IntervalCollection.cs @ 17825

Last change on this file since 17825 was 17564, checked in by mkommend, 5 years ago

#2971: Minor changes in interval collection and interval interpreter.

File size: 3.7 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 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    private IDictionary<string, Interval> intervals { get; } = new Dictionary<string, Interval>();
37
38    [Storable(Name = "StorableIntervalInformation")]
39    private KeyValuePair<string, double[]>[] StorableIntervalInformation {
40      get {
41        var l = new List<KeyValuePair<string, double[]>>();
42        foreach (var varInt in intervals)
43
44          l.Add(new KeyValuePair<string, double[]>(varInt.Key,
45            new double[] { varInt.Value.LowerBound, varInt.Value.UpperBound }));
46        return l.ToArray();
47      }
48
49      set {
50        foreach (var varInt in value)
51          intervals.Add(varInt.Key, new Interval(varInt.Value[0], varInt.Value[1]));
52      }
53    }
54
55    public int Count => intervals.Count;
56
57    public IntervalCollection() : base() { }
58    [StorableConstructor]
59    protected IntervalCollection(StorableConstructorFlag _) : base(_) { }
60
61    protected IntervalCollection(IntervalCollection original, Cloner cloner) : base(original, cloner) {
62      foreach (var keyValuePair in original.intervals) {
63        intervals.Add(keyValuePair.Key, new Interval(keyValuePair.Value.LowerBound, keyValuePair.Value.UpperBound));
64      }
65    }
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new IntervalCollection(this, cloner);
68    }
69
70    public IntervalCollection(IDictionary<string, Interval> intervals) {
71      this.intervals = intervals;
72    }
73
74    public Interval GetInterval(string identifier) {
75      if (!intervals.ContainsKey(identifier)) throw new ArgumentException($"The given identifier:{ identifier } is not present!");
76
77      return intervals[identifier];
78    }
79
80    public void SetInterval(string identifier, Interval interval) {
81      intervals[identifier] = interval;
82    }
83
84    public void AddInterval(string identifier, Interval interval) {
85      intervals.Add(identifier, interval);
86    }
87
88    public void DeleteInterval(string identifier) {
89      intervals.Remove(identifier);
90    }
91
92    public IReadOnlyDictionary<string, Interval> GetReadonlyDictionary() {
93      return intervals.ToDictionary(pair => pair.Key, pair => pair.Value);
94    }
95
96    public IEnumerable<Tuple<string, Interval>> GetVariableIntervals() {
97      foreach (var variableInterval in intervals)
98        yield return Tuple.Create(variableInterval.Key, variableInterval.Value);
99    }
100  }
101}
102
Note: See TracBrowser for help on using the repository browser.