Skip to content

Commit b8675b9

Browse files
author
Frankie Robertson
committed
Add RandomesqueStrategy
1 parent f3d9a6b commit b8675b9

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
2323
Mmap = "a63ad114-7e13-5084-954f-fe012c677804"
2424
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
2525
PsychometricsBazaarBase = "b0d9cada-d963-45e9-a4c6-4746243987f1"
26+
QuickHeaps = "30b38841-0f52-47f8-a5f8-18d5d4064379"
2627
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2728
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2829
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
@@ -60,6 +61,7 @@ Mmap = "^1.11"
6061
Optim = "1.7.3"
6162
PrecompileTools = "1.2.1"
6263
PsychometricsBazaarBase = "^0.8.1"
64+
QuickHeaps = "0.2.2"
6365
Random = "^1.11"
6466
Reexport = "1"
6567
Setfield = "^1"

src/next_item_rules/NextItemRules.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ include("./prelude/preallocate.jl")
6565

6666
# Selection strategies
6767
include("./strategies/random.jl")
68+
include("./strategies/randomesque.jl")
6869
include("./strategies/sequential.jl")
6970
include("./strategies/exhaustive.jl")
7071

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using QuickHeaps: BinaryHeap, FastMax, Node, get_val
2+
using StatsBase: sample
3+
4+
5+
function randomesque(
6+
rng::AbstractRNG,
7+
objective::ItemCriterion,
8+
responses::TrackedResponses,
9+
items::AbstractItemBank,
10+
k::Int
11+
)
12+
objective_state = init_thread(objective, responses)
13+
heap = BinaryHeap{Node{Int, Float64}}(o = FastMax)
14+
sizehint!(heap, k)
15+
for item_idx in eachindex(items)
16+
if (findfirst(idx -> idx == item_idx, responses.responses.indices) !== nothing)
17+
continue
18+
end
19+
20+
obj_val = compute_criterion(objective, objective_state, responses, item_idx)
21+
22+
if length(heap) < k
23+
push!(heap, Node(item_idx, obj_val))
24+
elseif obj_val < get_val(peek(heap))
25+
heap[1] = Node(item_idx, obj_val)
26+
end
27+
end
28+
if length(heap) >= 1
29+
Tuple(sample(rng, heap))
30+
else
31+
return (-1, Inf)
32+
end
33+
end
34+
35+
"""
36+
$(TYPEDEF)
37+
$(TYPEDFIELDS)
38+
39+
"""
40+
struct RandomesqueStrategy <: NextItemStrategy
41+
rng::AbstractRNG
42+
k::Int
43+
end
44+
45+
RandomesqueStrategy(k::Int) = RandomesqueStrategy(Xoshiro(), k)
46+
47+
function best_item(
48+
rule::ItemStrategyNextItemRule{RandomesqueStrategy, ItemCriterionT},
49+
responses::TrackedResponses,
50+
items
51+
) where {ItemCriterionT <: ItemCriterion}
52+
randomesque(rule.rng, rule.criterion, responses, items, rule.k)[1]
53+
end

0 commit comments

Comments
 (0)