Skip to content

Commit d99937c

Browse files
committed
Create utility functions for benchmarks
1 parent 5a44af0 commit d99937c

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

benchmark/benchmark_utils.jl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import DynamicExpressions:
2+
Node, copy_node, set_node!, count_nodes, has_constants, has_operators
3+
4+
# This code is copied from SymbolicRegression.jl and modified
5+
6+
# Return a random node from the tree
7+
function random_node(tree::Node{T})::Node{T} where {T}
8+
if tree.degree == 0
9+
return tree
10+
end
11+
b = 0
12+
c = 0
13+
if tree.degree >= 1
14+
b = count_nodes(tree.l)
15+
end
16+
if tree.degree == 2
17+
c = count_nodes(tree.r)
18+
end
19+
20+
i = rand(1:(1 + b + c))
21+
if i <= b
22+
return random_node(tree.l)
23+
elseif i == b + 1
24+
return tree
25+
end
26+
27+
return random_node(tree.r)
28+
end
29+
30+
function make_random_leaf(nfeatures::Int, ::Type{T})::Node{T} where {T}
31+
if rand() > 0.5
32+
return Node(; val=randn(T))
33+
else
34+
return Node(T; feature=rand(1:nfeatures))
35+
end
36+
end
37+
38+
# Add a random unary/binary operation to the end of a tree
39+
function append_random_op(
40+
tree::Node{T}, operators, nfeatures::Int; makeNewBinOp::Union{Bool,Nothing}=nothing
41+
)::Node{T} where {T}
42+
nuna = length(operators.unaops)
43+
nbin = length(operators.binops)
44+
45+
node = random_node(tree)
46+
while node.degree != 0
47+
node = random_node(tree)
48+
end
49+
50+
if makeNewBinOp === nothing
51+
choice = rand()
52+
makeNewBinOp = choice < nbin / (nuna + nbin)
53+
end
54+
55+
if makeNewBinOp
56+
newnode = Node(
57+
rand(1:nbin), make_random_leaf(nfeatures, T), make_random_leaf(nfeatures, T)
58+
)
59+
else
60+
newnode = Node(rand(1:nuna), make_random_leaf(nfeatures, T))
61+
end
62+
63+
set_node!(node, newnode)
64+
65+
return tree
66+
end
67+
68+
function gen_random_tree_fixed_size(
69+
node_count::Int, operators, nfeatures::Int, ::Type{T}
70+
)::Node{T} where {T}
71+
tree = make_random_leaf(nfeatures, T)
72+
cur_size = count_nodes(tree)
73+
while cur_size < node_count
74+
if cur_size == node_count - 1 # only unary operator allowed.
75+
length(operators.unaops) == 0 && break # We will go over the requested amount, so we must break.
76+
tree = append_random_op(tree, operators, nfeatures; makeNewBinOp=false)
77+
else
78+
tree = append_random_op(tree, operators, nfeatures)
79+
end
80+
cur_size = count_nodes(tree)
81+
end
82+
return tree
83+
end

benchmark/benchmarks.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DynamicExpressions, BenchmarkTools, Random
22
using DynamicExpressions: copy_node
3-
using SymbolicRegression: gen_random_tree_fixed_size, Options
3+
4+
include("benchmark_utils.jl")
45

56
const v_PACKAGE_VERSION = try
67
VersionNumber(DynamicExpressions.PACKAGE_VERSION)
@@ -82,7 +83,7 @@ end
8283

8384
function benchmark_utilities()
8485
suite = BenchmarkGroup()
85-
options = Options(; binary_operators=[+, -, /, *], unary_operators=[cos, exp])
86+
operators = OperatorEnum(; binary_operators=[+, -, /, *], unary_operators=[cos, exp])
8687
for func_k in ("copy", "convert", "simplify_tree", "combine_operators")
8788
suite[func_k] = let s = BenchmarkGroup()
8889
for k in ("break_sharing", "preserve_sharing")
@@ -99,20 +100,19 @@ function benchmark_utilities()
99100
preserve_sharing=(k == "preserve_sharing"),
100101
)
101102
elseif func_k == "simplify_tree"
102-
tree -> simplify_tree(tree, options.operators)
103+
tree -> simplify_tree(tree, operators)
103104
elseif func_k == "combine_operators"
104-
tree -> combine_operators(tree, options.operators)
105+
tree -> combine_operators(tree, operators)
105106
end
106107

107108
#! format: off
108109
s[k] = @benchmarkable(
109-
$(f)(tree),
110-
evals=300,
111-
samples=300,
110+
[$(f)(tree) for tree in trees],
112111
seconds=10.0,
113112
setup=(
113+
ntrees=100;
114114
n=20;
115-
tree=gen_random_tree_fixed_size(n, $options, 5, Float32)
115+
trees=[gen_random_tree_fixed_size(n, $operators, 5, Float32) for _ in 1:ntrees]
116116
)
117117
)
118118
#! format: on

0 commit comments

Comments
 (0)