source: branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs @ 15883

Last change on this file since 15883 was 15883, checked in by lkammere, 3 years ago

#2886: Priorize phrases whose (fully expanded) terms result in high R².

File size: 3.4 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4
5namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration {
6
7  public class StoredSymbolString {
8    public readonly int Hash;
9    public readonly SymbolString SymbolString;
10
11    public StoredSymbolString(int hash, SymbolString symbolString) {
12      Hash = hash;
13      SymbolString = symbolString;
14    }
15  }
16
17  public enum StorageType {
18    PriorityQueue, Stack, Queue, RandomList
19  }
20
21  class SearchDataStore : IEnumerable<SymbolString> {
22
23    private Dictionary<int, SymbolString> storedValues; // Store hash-references and associated, actual values
24    private Action<int, double> storeInternal; // Store hash-references
25    private Func<int> fetchInternal; // Fetch hash-reference
26
27    public SearchDataStore(StorageType storageType) {
28      storedValues = new Dictionary<int, SymbolString>();
29
30      switch (storageType) {
31        case StorageType.PriorityQueue:
32          InitPriorityQueue();
33          break;
34        case StorageType.Stack:
35          InitStack();
36          break;
37        case StorageType.Queue:
38          InitQueue();
39          break;
40        case StorageType.RandomList:
41          InitRandomList();
42          break;
43      }
44
45    }
46
47    #region SearchStrategies
48
49    private void InitPriorityQueue() {
50      PriorityQueue<double, int> queue = new PriorityQueue<double, int>(double.MaxValue, double.MinValue, (int)Math.Pow(2.0, 20.0) / 4);
51      storeInternal = (hash, prio) => queue.Insert(prio, hash);
52      fetchInternal = () => {
53        int ret = queue.PeekMinValue();
54        queue.RemoveMin();
55        return ret;
56      };
57    }
58
59    private void InitStack() {
60      Stack<int> stack = new Stack<int>();
61
62      storeInternal = (hash, prio) => stack.Push(hash);
63      fetchInternal = () => stack.Pop();
64    }
65
66    private void InitQueue() {
67      Queue<int> queue = new Queue<int>();
68
69      storeInternal = (hash, prio) => queue.Enqueue(hash);
70      fetchInternal = () => queue.Dequeue();
71    }
72
73    private void InitRandomList() {
74      List<int> list = new List<int>();
75      System.Random rand = new System.Random(999);
76
77      storeInternal = (hash, prio) => list.Add(hash);
78      fetchInternal = () => {
79        int indexOfHash = rand.Next(list.Count);
80        int result = list[indexOfHash];
81        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.
82        return result;
83      };
84    }
85
86    #endregion
87
88    #region Interface
89
90    public StoredSymbolString GetNext() {
91      int hash = fetchInternal.Invoke();
92      SymbolString result = storedValues[hash];
93      storedValues.Remove(hash);
94      return new StoredSymbolString(hash, result);
95    }
96
97    public void Store(int hash, double priority, SymbolString s) {
98      storeInternal(hash, priority);
99      storedValues[hash] = s;
100    }
101
102    public bool Contains(int hash) {
103      return storedValues.ContainsKey(hash);
104    }
105
106    #endregion
107
108    #region Collection-Interface
109
110    public int Count {
111      get { return storedValues.Count; }
112    }
113
114    public IEnumerator<SymbolString> GetEnumerator() {
115      return storedValues.Values.GetEnumerator();
116    }
117
118    IEnumerator IEnumerable.GetEnumerator() {
119      return GetEnumerator();
120    }
121
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.