metagraphoptimization.jl/test/known_graphs.jl

102 lines
2.6 KiB
Julia
Raw Normal View History

2023-06-22 17:24:35 +02:00
using MetagraphOptimization
2023-06-27 20:05:27 +02:00
using Random
2023-06-22 17:24:35 +02:00
using Test
2023-06-27 20:05:27 +02:00
Random.seed!(0)
2023-06-22 17:24:35 +02:00
function test_known_graphs()
g_ABAB = import_txt(joinpath(@__DIR__, "..", "examples", "AB->AB.txt"))
props = graph_properties(g_ABAB)
@test length(g_ABAB.nodes) == 34
@test props.compute_effort == 185
@test props.data == 102
2023-06-27 18:38:26 +02:00
@test length(generate_options(g_ABAB).nodeFusions) == 10
2023-06-26 14:24:14 +02:00
test_node_fusion(g_ABAB)
2023-06-27 20:05:27 +02:00
test_random_walk(g_ABAB, 100)
2023-06-26 14:24:14 +02:00
g_ABAB3 = import_txt(joinpath(@__DIR__, "..", "examples", "AB->ABBB.txt"))
props = graph_properties(g_ABAB3)
@test length(g_ABAB3.nodes) == 280
@test props.compute_effort == 2007
@test props.data == 828
test_node_fusion(g_ABAB3)
2023-06-27 20:05:27 +02:00
test_random_walk(g_ABAB3, 1000)
2023-06-26 14:24:14 +02:00
end
2023-06-27 20:05:27 +02:00
function test_node_fusion(g::DAG)
2023-06-26 14:24:14 +02:00
props = graph_properties(g)
options = generate_options(g)
nodes_number = length(g.nodes)
data = props.data
compute_effort = props.compute_effort
2023-06-27 18:38:26 +02:00
while !isempty(options.nodeFusions)
fusion = options.nodeFusions[1]
@test typeof(fusion) <: NodeFusion
push_operation!(g, fusion)
2023-06-26 14:24:14 +02:00
props = graph_properties(g)
@test props.data < data
@test props.compute_effort == compute_effort
nodes_number = length(g.nodes)
data = props.data
compute_effort = props.compute_effort
options = generate_options(g)
end
2023-06-27 20:05:27 +02:00
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)
2023-06-26 14:24:14 +02:00
2023-06-27 20:05:27 +02:00
@test properties == graph_properties(g)
2023-06-22 17:24:35 +02:00
end