forked from viliwonka/WeightedRandomSelector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomSelectorTests.cs
323 lines (222 loc) · 10.8 KB
/
RandomSelectorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using Stopwatch = System.Diagnostics.Stopwatch;
/// <summary>
/// By Vili Volcini
/// Generic structure for high performance random selection of items on very big arrays
/// Is precompiled/precomputed, and uses binary search to find item based on random
/// O(log2(N)) per random pick for bigger arrays
/// O(n) per random pick for smaller arrays
/// O(n) construction
/// </summary>
namespace DataStructures.RandomSelector.Test {
using DataStructures.RandomSelector.Math;
public class RandomSelectorTests : MonoBehaviour {
// Run series of tests
void Start() {
var result = TestEqualityOfLinearVsBinarySearch();
Debug.Log("Do both searches (linear and binary) produce identical results? " + (result?"Yes":"No"));
int optimalBreakpointArray = FindOptimalBreakpointArray();
Debug.Log("Optimal breakpoint for arrays is at size of " + optimalBreakpointArray);
int optimalBreakpointList = FindOptimalBreakpointList();
Debug.Log("Optimal breakpoint for lists is at size of " + optimalBreakpointList);
TestStaticSelector();
TestDynamicSelector();
}
/// <summary>
/// Test and compare linear and binary searches, they should return identical results
/// </summary>
/// <returns></returns>
bool TestEqualityOfLinearVsBinarySearch() {
var random = new System.Random();
for (int i = 0; i < 1000000; i++) {
float u = i / 999999f;
float r = (float) random.NextDouble();
float[] randomWeights = RandomMath.RandomWeightsArray(random, 33);
RandomMath.BuildCumulativeDistribution(randomWeights);
if (randomWeights.SelectIndexLinearSearch(1f) != randomWeights.SelectIndexBinarySearch(1f))
return false;
if (randomWeights.SelectIndexLinearSearch(u) != randomWeights.SelectIndexBinarySearch(u)) {
Debug.Log("Not matching u");
Debug.Log(u);
Debug.Log(randomWeights.SelectIndexLinearSearch(u));
Debug.Log(randomWeights.SelectIndexBinarySearch(u));
return false;
}
if (randomWeights.SelectIndexLinearSearch(r) != randomWeights.SelectIndexBinarySearch(r)) {
Debug.Log("Not matching r");
Debug.Log(r);
Debug.Log(randomWeights.SelectIndexLinearSearch(r));
Debug.Log(randomWeights.SelectIndexBinarySearch(r));
return false;
}
}
return true;
}
// time both searches (linear and binary (log)), and find optimal breakpoint - where to use which for maximal performance
int FindOptimalBreakpointArray() {
int optimalBreakpoint = 2;
var random = new System.Random();
Stopwatch stopwatchLinear = new Stopwatch();
Stopwatch stopwatchBinary = new Stopwatch();
float lin = 0f;
float log = 1f;
// continue increasing "optimalBreakpoint" until linear becomes slower than log
// result is around 15-16, varies a bit due to random nature of test
while (lin <= log) {
int numOfDiffArrays = 100;
int numOfTestPerArr = 10000;
// u = uniform grid, r = uniform random
float u, r;
///Linear Search
stopwatchLinear.Stop();
stopwatchLinear.Reset();
float[] items = RandomMath.IdentityArray(optimalBreakpoint);
float selectedItem; //here just to simulate selecting from array
float[] arr = new float[optimalBreakpoint];
for (int k = 0; k < numOfDiffArrays; k++) {
RandomMath.RandomWeightsArray(ref arr, random);
RandomMath.BuildCumulativeDistribution(arr);
stopwatchLinear.Start();
for (int i = 0; i < numOfTestPerArr; i++) {
u = i / (numOfTestPerArr - 1f);
selectedItem = items[arr.SelectIndexLinearSearch(u)];
r = (float) random.NextDouble();
selectedItem = items[arr.SelectIndexLinearSearch(r)];
}
stopwatchLinear.Stop();
}
lin = stopwatchLinear.ElapsedMilliseconds;
/// Binary Search
stopwatchBinary.Stop();
stopwatchBinary.Reset();
for (int k = 0; k < numOfDiffArrays; k++) {
RandomMath.RandomWeightsArray(ref arr, random);
RandomMath.BuildCumulativeDistribution(arr);
stopwatchBinary.Start();
for (int i = 0; i < numOfTestPerArr; i++) {
u = i / (numOfTestPerArr - 1f);
selectedItem = items[arr.SelectIndexBinarySearch(u)];
r = (float) random.NextDouble();
selectedItem = items[arr.SelectIndexBinarySearch(r)];
}
stopwatchBinary.Stop();
}
log = stopwatchBinary.ElapsedMilliseconds;
optimalBreakpoint++;
}
return optimalBreakpoint;
}
//same as before, but for lists instead of arrays
int FindOptimalBreakpointList() {
int optimalBreakpoint = 2;
var random = new System.Random();
Stopwatch stopwatchLinear = new Stopwatch();
Stopwatch stopwatchBinary = new Stopwatch();
float lin = 0f;
float log = 1f;
// continue increasing "optimalBreakpoint" until linear becomes slower than log
// result is around 15-16, varies a bit due to random nature of test
while (lin <= log) {
int numOfDiffArrays = 100;
int numOfTestPerArr = 10000;
// u = uniform grid, r = uniform random
float u, r;
///Linear Search
stopwatchLinear.Stop();
stopwatchLinear.Reset();
List<float> items = RandomMath.IdentityList(optimalBreakpoint);
float selectedItem; //simulate selecting from array
List<float> list = new List<float>(optimalBreakpoint);
for(int i = 0; i < optimalBreakpoint; i++)
list.Add(0f);
for (int k = 0; k < numOfDiffArrays; k++) {
RandomMath.RandomWeightsList(ref list, random);
RandomMath.BuildCumulativeDistribution(list);
stopwatchLinear.Start();
for (int i = 0; i < numOfTestPerArr; i++) {
u = i / (numOfTestPerArr - 1f);
selectedItem = items[list.SelectIndexLinearSearch(u)];
r = (float) random.NextDouble();
selectedItem = items[list.SelectIndexLinearSearch(r)];
}
stopwatchLinear.Stop();
}
lin = stopwatchLinear.ElapsedMilliseconds;
/// Binary Search
stopwatchBinary.Stop();
stopwatchBinary.Reset();
for (int k = 0; k < numOfDiffArrays; k++) {
RandomMath.RandomWeightsList(ref list, random);
RandomMath.BuildCumulativeDistribution(list);
stopwatchBinary.Start();
for (int i = 0; i < numOfTestPerArr; i++) {
u = i / (numOfTestPerArr - 1f);
selectedItem = items[list.SelectIndexBinarySearch(u)];
r = (float) random.NextDouble();
selectedItem = items[list.SelectIndexBinarySearch(r)];
}
stopwatchBinary.Stop();
}
log = stopwatchBinary.ElapsedMilliseconds;
optimalBreakpoint++;
}
return optimalBreakpoint;
}
void TestStaticSelector() {
System.Random r = new System.Random();
RandomSelectorBuilder<float> builder = new RandomSelectorBuilder<float>();
// add items
// pair (item, unnormalized probability)
for(int i = 0; i < 32; i++)
builder.Add(i, Mathf.Sqrt(i+1));
//build with seed 42
IRandomSelector<float> selector = builder.Build(42);
string print = "";
for(int i = 0; i < 100; i++) {
print += i.ToString() + ". " + selector.SelectRandomItem() + "\n";
}
Debug.Log(print);
/// LONG version, to test binary search
// add items
// pair (item, unnormalized probability)
for (int i = 0; i < 1024; i++)
builder.Add(i, Mathf.Sqrt(i + 1));
//build with seed 42
IRandomSelector<float> longSelector = builder.Build(42);
// just run 10000 tests, should be enough
for (int i = 0; i < 10000; i++)
longSelector.SelectRandomItem();
// wont print long version, would spam console too much
}
void TestDynamicSelector() {
//seed = 42, expected number of item = 32
DynamicRandomSelector<float> selector = new DynamicRandomSelector<float>(42, 32);
// add items
for (int i = 0; i < 32; i++)
selector.Add(i, Mathf.Sqrt(i + 1));
// Build internals
// pair (item, unnormalized probability)
selector.Build();
string print = "";
for (int i = 0; i < 100; i++) {
print += i.ToString() + " " + selector.SelectRandomItem() + "\n";
}
Debug.Log(print);
/// LONG version, to test binary search
//we can just keep adding new members
for(int i = 0; i < 1024; i++) {
selector.Add(i, Mathf.Sqrt(i));
}
//do not forget to (re)build
selector.Build();
//just test it this way
for(int i = 0; i < 10000; i++) {
selector.SelectRandomItem();
}
// wont print long version, would spam console too much
}
}
}