1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling {
|
---|
9 | public class LimitedPriorityQueue : IEnumerable<Tuple<double, RealVector>> {
|
---|
10 | public class AscendingPriorityComparer : IComparer<double> {
|
---|
11 | public int Compare(double x, double y) {
|
---|
12 | return x.CompareTo(y);
|
---|
13 | }
|
---|
14 | }
|
---|
15 |
|
---|
16 | public class DescendingPriorityComparer : IComparer<double> {
|
---|
17 | public int Compare(double x, double y) {
|
---|
18 | return y.CompareTo(x);
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | private class LimitedPriorityQueueComparer : IComparer<Tuple<double, RealVector>> {
|
---|
23 | private IComparer<double> comparer;
|
---|
24 | public LimitedPriorityQueueComparer(IComparer<double> priorityComparer) {
|
---|
25 | comparer = priorityComparer;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public int Compare(Tuple<double, RealVector> x, Tuple<double, RealVector> y) {
|
---|
29 | return comparer.Compare(x.Item1, y.Item1);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private readonly object locker = new object();
|
---|
34 | private readonly List<Tuple<double, RealVector>> list = new List<Tuple<double, RealVector>>();
|
---|
35 | private readonly HashSet<RealVector> hashes = new HashSet<RealVector>(new RealVectorEqualityComparer());
|
---|
36 |
|
---|
37 | private readonly LimitedPriorityQueueComparer comparer;
|
---|
38 | private readonly SemaphoreSlim semaphore;
|
---|
39 | private readonly int limit;
|
---|
40 | private bool addingCompleted;
|
---|
41 |
|
---|
42 | public int Count {
|
---|
43 | get { lock (locker) return list.Count; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public bool IsAddingCompleted {
|
---|
47 | get { lock (locker) return addingCompleted; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public LimitedPriorityQueue(int maxCount, IComparer<double> priorityComparer) {
|
---|
51 | limit = maxCount;
|
---|
52 | comparer = new LimitedPriorityQueueComparer(priorityComparer);
|
---|
53 | semaphore = new SemaphoreSlim(0, limit);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public bool Contains(RealVector value) {
|
---|
57 | lock (locker) return hashes.Contains(value);
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void Enqueue(double priority, RealVector value) {
|
---|
62 | lock (locker) {
|
---|
63 | var tuple = Tuple.Create(priority, value);
|
---|
64 | int index = list.BinarySearch(tuple, comparer);
|
---|
65 | if (index < 0) index = ~index;
|
---|
66 |
|
---|
67 | if (list.Count == limit) {
|
---|
68 | var lastIndex = limit - 1;
|
---|
69 | var last = list[lastIndex];
|
---|
70 | list.RemoveAt(lastIndex);
|
---|
71 | hashes.Remove(last.Item2);
|
---|
72 | }
|
---|
73 |
|
---|
74 | list.Insert(Math.Min(index, list.Count), tuple);
|
---|
75 | hashes.Add(value);
|
---|
76 |
|
---|
77 | if (semaphore.CurrentCount < limit)
|
---|
78 | semaphore.Release();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public bool TryDequeue(out RealVector item) {
|
---|
83 | if (semaphore.Wait(0)) {
|
---|
84 | lock (locker) {
|
---|
85 | if (list.Any()) {
|
---|
86 | item = list[0].Item2;
|
---|
87 | list.RemoveAt(0);
|
---|
88 | hashes.Remove(item);
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | item = default(RealVector);
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public Tuple<double, RealVector> LastOrDefault() {
|
---|
99 | lock (locker) return list.LastOrDefault();
|
---|
100 | }
|
---|
101 |
|
---|
102 | public void Clear() {
|
---|
103 | lock (locker) {
|
---|
104 | list.Clear();
|
---|
105 | hashes.Clear();
|
---|
106 |
|
---|
107 | for (int i = 0; i < semaphore.CurrentCount; i++)
|
---|
108 | semaphore.Wait();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void CompleteAdding() {
|
---|
113 | lock (locker) addingCompleted = true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public IEnumerator<Tuple<double, RealVector>> GetEnumerator() {
|
---|
117 | return list.GetEnumerator();
|
---|
118 | }
|
---|
119 |
|
---|
120 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
121 | return ((IEnumerable)list).GetEnumerator();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|