[15746] | 1 | using System;
|
---|
| 2 | using System.Collections;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[15960] | 4 | using System.Linq;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[15746] | 7 |
|
---|
| 8 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
|
---|
[15960] | 9 | [StorableClass]
|
---|
| 10 | public class SearchNode : DeepCloneable {
|
---|
| 11 | [Storable]
|
---|
[15800] | 12 | public readonly int Hash;
|
---|
[15974] | 13 |
|
---|
[15960] | 14 | [Storable]
|
---|
[15800] | 15 | public readonly SymbolString SymbolString;
|
---|
[15974] | 16 |
|
---|
[15960] | 17 | [Storable]
|
---|
[15915] | 18 | public readonly double Priority;
|
---|
[15974] | 19 |
|
---|
[15960] | 20 | [Storable]
|
---|
[15915] | 21 | public readonly double R2;
|
---|
[15746] | 22 |
|
---|
[15960] | 23 | private SearchNode() { }
|
---|
| 24 |
|
---|
[15915] | 25 | public SearchNode(int hash, double priority, double r2, SymbolString symbolString) {
|
---|
[15746] | 26 | Hash = hash;
|
---|
[15915] | 27 | Priority = priority;
|
---|
[15746] | 28 | SymbolString = symbolString;
|
---|
[15915] | 29 | R2 = r2;
|
---|
[15746] | 30 | }
|
---|
[15960] | 31 |
|
---|
| 32 | protected SearchNode(SearchNode original, Cloner cloner) : base(original, cloner) {
|
---|
| 33 | Hash = original.Hash;
|
---|
| 34 | Priority = original.Priority;
|
---|
| 35 | SymbolString = original.SymbolString;
|
---|
| 36 | R2 = original.R2;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | [StorableConstructor]
|
---|
| 40 | protected SearchNode(bool deserializing) { }
|
---|
| 41 |
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new SearchNode(this, cloner);
|
---|
| 44 | }
|
---|
[15746] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public enum StorageType {
|
---|
[15974] | 48 | PriorityQueue, Stack, Queue, RandomList, SortedSet
|
---|
[15746] | 49 | }
|
---|
| 50 |
|
---|
[15960] | 51 | [StorableClass]
|
---|
| 52 | class SearchDataStore : DeepCloneable, IEnumerable<SearchNode> {
|
---|
| 53 | [Storable]
|
---|
[15993] | 54 | private Dictionary<int, SearchNode> storedValues; // Store hash-references and associated, actual values
|
---|
[15974] | 55 |
|
---|
[15960] | 56 | [Storable]
|
---|
| 57 | private StorageType storageType;
|
---|
[15746] | 58 |
|
---|
[15960] | 59 | [Storable]
|
---|
| 60 | private Queue<int> queue;
|
---|
| 61 |
|
---|
| 62 | [Storable]
|
---|
| 63 | private Stack<int> stack;
|
---|
| 64 |
|
---|
| 65 | [Storable]
|
---|
| 66 | private List<int> list;
|
---|
| 67 |
|
---|
[15974] | 68 | [Storable]
|
---|
| 69 | private SortedSet<Tuple<double, int>> sortedSet;
|
---|
| 70 |
|
---|
| 71 | [Storable]
|
---|
| 72 | private int searchDataStructureSize; // storage size for search nodes
|
---|
| 73 |
|
---|
[15960] | 74 | [ExcludeFromObjectGraphTraversal]
|
---|
| 75 | PriorityQueue<double, int> priorityQueue; // does not support [Storable], we rebuild it from the storedValues
|
---|
| 76 |
|
---|
[15883] | 77 | private Action<int, double> storeInternal; // Store hash-references
|
---|
[15746] | 78 | private Func<int> fetchInternal; // Fetch hash-reference
|
---|
| 79 |
|
---|
[15977] | 80 | public SearchDataStore() : this(StorageType.SortedSet) { }
|
---|
[15960] | 81 |
|
---|
| 82 | [StorableConstructor]
|
---|
| 83 | protected SearchDataStore(bool deserializing) : this() { }
|
---|
| 84 |
|
---|
[15993] | 85 | public SearchDataStore(StorageType storageType, int searchDataStructureSize = (int)1e5) {
|
---|
[15960] | 86 | this.storageType = storageType;
|
---|
[15974] | 87 |
|
---|
| 88 | this.searchDataStructureSize = searchDataStructureSize;
|
---|
| 89 |
|
---|
[15993] | 90 | storedValues = new Dictionary<int, SearchNode>();
|
---|
[15977] | 91 | InitSearchDataStructure();
|
---|
[15960] | 92 | }
|
---|
[15746] | 93 |
|
---|
[15977] | 94 | private void InitSearchDataStructure() {
|
---|
[15746] | 95 | switch (storageType) {
|
---|
[15883] | 96 | case StorageType.PriorityQueue:
|
---|
| 97 | InitPriorityQueue();
|
---|
| 98 | break;
|
---|
[15746] | 99 | case StorageType.Stack:
|
---|
| 100 | InitStack();
|
---|
| 101 | break;
|
---|
| 102 | case StorageType.Queue:
|
---|
| 103 | InitQueue();
|
---|
| 104 | break;
|
---|
| 105 | case StorageType.RandomList:
|
---|
| 106 | InitRandomList();
|
---|
| 107 | break;
|
---|
[15974] | 108 | case StorageType.SortedSet:
|
---|
| 109 | InitSortedSet();
|
---|
| 110 | break;
|
---|
[15746] | 111 | }
|
---|
[15960] | 112 | }
|
---|
[15746] | 113 |
|
---|
[15977] | 114 | private void ClearSearchDataStructure() {
|
---|
| 115 | switch (storageType) {
|
---|
| 116 | case StorageType.PriorityQueue:
|
---|
| 117 | priorityQueue = null;
|
---|
| 118 | break;
|
---|
| 119 | case StorageType.Stack:
|
---|
| 120 | stack.Clear();
|
---|
| 121 | break;
|
---|
| 122 | case StorageType.Queue:
|
---|
| 123 | queue.Clear();
|
---|
| 124 | break;
|
---|
| 125 | case StorageType.RandomList:
|
---|
| 126 | list.Clear();
|
---|
| 127 | break;
|
---|
| 128 | case StorageType.SortedSet:
|
---|
| 129 | sortedSet.Clear();
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[15960] | 134 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 135 | return new SearchDataStore(this, cloner);
|
---|
[15746] | 136 | }
|
---|
| 137 |
|
---|
[15960] | 138 | protected SearchDataStore(SearchDataStore original, Cloner cloner) : base(original, cloner) {
|
---|
[15993] | 139 | storedValues = original.storedValues.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
|
---|
[15960] | 140 | storageType = original.storageType;
|
---|
[15974] | 141 | searchDataStructureSize = original.searchDataStructureSize;
|
---|
| 142 |
|
---|
[15977] | 143 | InitSearchDataStructure();
|
---|
[15960] | 144 | switch (storageType) {
|
---|
| 145 | case StorageType.PriorityQueue:
|
---|
| 146 | foreach (var t in storedValues)
|
---|
| 147 | storeInternal(t.Key, t.Value.Priority);
|
---|
| 148 | break;
|
---|
| 149 | case StorageType.Stack:
|
---|
| 150 | foreach (var v in original.stack.Reverse()) {
|
---|
| 151 | stack.Push(v);
|
---|
| 152 | }
|
---|
| 153 | break;
|
---|
| 154 | case StorageType.Queue:
|
---|
| 155 | foreach (var v in original.queue) {
|
---|
| 156 | queue.Enqueue(v);
|
---|
| 157 | }
|
---|
| 158 | break;
|
---|
| 159 | case StorageType.RandomList:
|
---|
| 160 | list.AddRange(original.list);
|
---|
| 161 | break;
|
---|
[15974] | 162 | case StorageType.SortedSet:
|
---|
| 163 | sortedSet = new SortedSet<Tuple<double, int>>(original.sortedSet);
|
---|
| 164 | break;
|
---|
[15960] | 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 169 | private void AfterDeserialization() {
|
---|
| 170 | // access lambdas need to be reinitialized
|
---|
| 171 | switch (storageType) {
|
---|
| 172 | case StorageType.PriorityQueue:
|
---|
| 173 | InitPriorityQueue();
|
---|
| 174 | foreach (var t in storedValues)
|
---|
| 175 | storeInternal(t.Key, t.Value.Priority);
|
---|
| 176 | break;
|
---|
| 177 | case StorageType.Stack:
|
---|
| 178 | storeInternal = (hash, prio) => stack.Push(hash);
|
---|
| 179 | fetchInternal = () => stack.Pop();
|
---|
| 180 | break;
|
---|
| 181 | case StorageType.Queue:
|
---|
| 182 | storeInternal = (hash, prio) => queue.Enqueue(hash);
|
---|
| 183 | fetchInternal = () => queue.Dequeue();
|
---|
| 184 | break;
|
---|
| 185 | case StorageType.RandomList:
|
---|
| 186 | System.Random rand = new System.Random(999);
|
---|
| 187 | storeInternal = (hash, prio) => list.Add(hash);
|
---|
| 188 | fetchInternal = () => {
|
---|
| 189 | int indexOfHash = rand.Next(list.Count);
|
---|
| 190 | int result = list[indexOfHash];
|
---|
| 191 | list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this. Maybe change to priority queue with random key.
|
---|
| 192 | return result;
|
---|
| 193 | };
|
---|
| 194 | break;
|
---|
[15974] | 195 | case StorageType.SortedSet:
|
---|
| 196 | storeInternal = (hash, prio) => {
|
---|
| 197 | if (sortedSet.Count >= searchDataStructureSize) {
|
---|
| 198 | var max = sortedSet.Max;
|
---|
| 199 | sortedSet.Remove(max);
|
---|
[15993] | 200 | storedValues.Remove(max.Item2); // should always be in sync with the sorted set
|
---|
[15974] | 201 | }
|
---|
| 202 | sortedSet.Add(Tuple.Create(prio, hash));
|
---|
| 203 | };
|
---|
| 204 | fetchInternal = () => {
|
---|
| 205 | var elem = sortedSet.FirstOrDefault();
|
---|
| 206 | if (elem == null)
|
---|
[15993] | 207 | return default(int);
|
---|
[15974] | 208 | sortedSet.Remove(elem);
|
---|
| 209 | return elem.Item2;
|
---|
| 210 | };
|
---|
| 211 | break;
|
---|
[15960] | 212 | }
|
---|
| 213 | }
|
---|
[15746] | 214 | #region SearchStrategies
|
---|
| 215 |
|
---|
[15883] | 216 | private void InitPriorityQueue() {
|
---|
[15974] | 217 | int capacity = searchDataStructureSize;
|
---|
[15960] | 218 | priorityQueue = new PriorityQueue<double, int>(double.MaxValue, double.MinValue, capacity);
|
---|
[15974] | 219 | storeInternal = (hash, prio) => {
|
---|
| 220 | // size is the 0-based index of the last used element
|
---|
| 221 | if (priorityQueue.Size == capacity - 1) {
|
---|
[15993] | 222 | return; // if the queue is at capacity we have to return
|
---|
[15974] | 223 | }
|
---|
| 224 | priorityQueue.Insert(prio, hash);
|
---|
| 225 | };
|
---|
[15883] | 226 | fetchInternal = () => {
|
---|
[15960] | 227 | int ret = priorityQueue.PeekMinValue();
|
---|
[15974] | 228 | if (priorityQueue.Size > 0) {
|
---|
| 229 | priorityQueue.RemoveMin();
|
---|
| 230 | }
|
---|
[15883] | 231 | return ret;
|
---|
| 232 | };
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[15746] | 235 | private void InitStack() {
|
---|
[15960] | 236 | stack = new Stack<int>();
|
---|
[15746] | 237 |
|
---|
[15883] | 238 | storeInternal = (hash, prio) => stack.Push(hash);
|
---|
[15746] | 239 | fetchInternal = () => stack.Pop();
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | private void InitQueue() {
|
---|
[15960] | 243 | queue = new Queue<int>();
|
---|
[15746] | 244 |
|
---|
[15883] | 245 | storeInternal = (hash, prio) => queue.Enqueue(hash);
|
---|
[15746] | 246 | fetchInternal = () => queue.Dequeue();
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | private void InitRandomList() {
|
---|
[15960] | 250 | list = new List<int>();
|
---|
[15746] | 251 | System.Random rand = new System.Random(999);
|
---|
| 252 |
|
---|
[15883] | 253 | storeInternal = (hash, prio) => list.Add(hash);
|
---|
[15746] | 254 | fetchInternal = () => {
|
---|
| 255 | int indexOfHash = rand.Next(list.Count);
|
---|
| 256 | int result = list[indexOfHash];
|
---|
[15883] | 257 | list.RemoveAt(indexOfHash); // TODO: beware this is O(n), at some point in time we should fix this. Maybe change to priority queue with random key.
|
---|
[15746] | 258 | return result;
|
---|
| 259 | };
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[15974] | 262 | private void InitSortedSet() {
|
---|
| 263 | sortedSet = new SortedSet<Tuple<double, int>>();
|
---|
| 264 | storeInternal = (hash, prio) => {
|
---|
| 265 | if (sortedSet.Count >= searchDataStructureSize) {
|
---|
| 266 | var max = sortedSet.Max;
|
---|
| 267 | sortedSet.Remove(max);
|
---|
| 268 | storedValues.Remove(max.Item2);
|
---|
| 269 | }
|
---|
| 270 | sortedSet.Add(Tuple.Create(prio, hash));
|
---|
| 271 | };
|
---|
| 272 | fetchInternal = () => {
|
---|
| 273 | var elem = sortedSet.FirstOrDefault();
|
---|
| 274 | if (elem == null)
|
---|
| 275 | return default(int);
|
---|
| 276 | sortedSet.Remove(elem);
|
---|
| 277 | return elem.Item2;
|
---|
| 278 | };
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[15746] | 281 | #endregion
|
---|
| 282 |
|
---|
| 283 | #region Interface
|
---|
| 284 |
|
---|
[15915] | 285 | public SearchNode GetNext() {
|
---|
[15746] | 286 | int hash = fetchInternal.Invoke();
|
---|
[15974] | 287 | SearchNode result = null;
|
---|
| 288 | if (storedValues.TryGetValue(hash, out result)) {
|
---|
| 289 | storedValues.Remove(hash);
|
---|
| 290 | }
|
---|
[15915] | 291 | return result;
|
---|
[15746] | 292 | }
|
---|
| 293 |
|
---|
[15915] | 294 | public void Store(SearchNode sn) {
|
---|
| 295 | storeInternal(sn.Hash, sn.Priority);
|
---|
| 296 | storedValues[sn.Hash] = sn;
|
---|
[15746] | 297 | }
|
---|
| 298 |
|
---|
| 299 | public bool Contains(int hash) {
|
---|
[15974] | 300 | SearchNode result;
|
---|
| 301 | return storedValues.TryGetValue(hash, out result);
|
---|
[15746] | 302 | }
|
---|
| 303 |
|
---|
[15977] | 304 | public void Clear() {
|
---|
| 305 | storedValues.Clear();
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[15746] | 308 | #endregion
|
---|
| 309 |
|
---|
| 310 | #region Collection-Interface
|
---|
| 311 |
|
---|
| 312 | public int Count {
|
---|
[15974] | 313 | get {
|
---|
| 314 | switch (storageType) {
|
---|
| 315 | case StorageType.PriorityQueue:
|
---|
| 316 | return priorityQueue.Size;
|
---|
| 317 | case StorageType.Queue:
|
---|
| 318 | return queue.Count;
|
---|
| 319 | case StorageType.RandomList:
|
---|
| 320 | return list.Count;
|
---|
| 321 | case StorageType.Stack:
|
---|
| 322 | return stack.Count;
|
---|
| 323 | case StorageType.SortedSet:
|
---|
| 324 | return sortedSet.Count;
|
---|
| 325 | default:
|
---|
| 326 | throw new InvalidOperationException("");
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
[15746] | 329 | }
|
---|
| 330 |
|
---|
[15915] | 331 | public IEnumerator<SearchNode> GetEnumerator() {
|
---|
[15974] | 332 | return storedValues.Select(x => x.Value).GetEnumerator();
|
---|
[15746] | 333 | }
|
---|
| 334 |
|
---|
| 335 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 336 | return GetEnumerator();
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | #endregion
|
---|
| 340 | }
|
---|
| 341 | }
|
---|