Add Random Walk test

This commit is contained in:
2023-06-27 20:05:27 +02:00
parent 80a3912e3e
commit d24d354fa0
7 changed files with 72 additions and 14 deletions

View File

@ -1,6 +1,9 @@
using MetagraphOptimization
using Random
using Test
Random.seed!(0)
function test_known_graphs()
g_ABAB = import_txt(joinpath(@__DIR__, "..", "examples", "AB->AB.txt"))
props = graph_properties(g_ABAB)
@ -12,6 +15,7 @@ function test_known_graphs()
@test length(generate_options(g_ABAB).nodeFusions) == 10
test_node_fusion(g_ABAB)
test_random_walk(g_ABAB, 100)
g_ABAB3 = import_txt(joinpath(@__DIR__, "..", "examples", "AB->ABBB.txt"))
@ -22,9 +26,10 @@ function test_known_graphs()
@test props.data == 828
test_node_fusion(g_ABAB3)
test_random_walk(g_ABAB3, 1000)
end
function test_node_fusion(g)
function test_node_fusion(g::DAG)
props = graph_properties(g)
options = generate_options(g)
@ -50,6 +55,47 @@ function test_node_fusion(g)
options = generate_options(g)
end
print(g)
end
function test_random_walk(g::DAG, n::Int64)
# the purpose here is to do "random" operations and reverse them again and validate that the graph stays the same and doesn't diverge
reset_graph!(g)
properties = graph_properties(g)
println("Random Walking... ")
for i = 1:n
print("\r", i)
# choose push or pop
if rand(Bool)
# push
opt = generate_options(g)
# choose one of fuse/split/reduce
option = rand(1:3)
if option == 1 && !isempty(opt.nodeFusions)
push_operation!(g, opt.nodeFusions[rand(1 : length(opt.nodeFusions))])
elseif option == 2 && !isempty(opt.nodeReductions)
push_operation!(g, opt.nodeReductions[rand(1 : length(opt.nodeReductions))])
elseif option == 3 && !isempty(opt.nodeSplits)
push_operation!(g, opt.nodeSplits[rand(1 : length(opt.nodeSplits))])
else
i = i-1
end
else
# pop
if (can_pop(g))
pop_operation!(g)
else
i = i-1
end
end
end
println("\rDone.")
reset_graph!(g)
@test properties == graph_properties(g)
end