Skip to content

Commit 638e417

Browse files
batch coloring test (#392)
* batch coloring test * first series of tests
1 parent b5ec148 commit 638e417

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

test/test_coloring.jl

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using CTDirect: CTDirect, solve, direct_transcription, set_initial_guess, build_OCP_solution
2+
using CTModels: CTModels, objective, state, control, variable, costate, time_grid, iterations
3+
using CTParser: CTParser, @def, set_prefix
4+
set_prefix(:CTModels) # tell CTParser def macro to use CTModels instead of OptimalControl
5+
6+
using NLPModelsIpopt
7+
using ADNLPModels
8+
using SparseMatrixColorings
9+
using Printf
10+
11+
# load examples library
12+
problem_path = pwd() * "/test/problems"
13+
for problem_file in filter(contains(r".jl$"), readdir(problem_path; join = true))
14+
include(problem_file)
15+
end
16+
17+
# coloring test function
18+
function coloring_test(ocp; order = NaturalOrder(), grid_size = CTDirect.__grid_size(), disc_method = CTDirect.__disc_method())
19+
20+
# build DOCP
21+
time_grid = CTDirect.__time_grid()
22+
docp = CTDirect.DOCP(ocp; grid_size=grid_size, time_grid=time_grid, disc_method=disc_method)
23+
24+
# build sparsity pattern
25+
J = CTDirect.DOCP_Jacobian_pattern(docp)
26+
H = CTDirect.DOCP_Hessian_pattern(docp)
27+
28+
## Coloring for Jacobians
29+
problem_J = ColoringProblem(; structure=:nonsymmetric, partition=:column)
30+
order_J = order
31+
algo_J = GreedyColoringAlgorithm(order_J; decompression=:direct)
32+
result_J = coloring(J, problem_J, algo_J)
33+
num_colors_J = ncolors(result_J)
34+
35+
## Coloring for Hessians
36+
problem_H = ColoringProblem(; structure=:symmetric, partition=:column)
37+
order_H = order
38+
algo_H = GreedyColoringAlgorithm(order_H; decompression=:substitution, postprocessing=true)
39+
result_H = coloring(H, problem_H, algo_H)
40+
num_colors_H = ncolors(result_H)
41+
42+
return num_colors_J, num_colors_H
43+
end
44+
45+
# batch testing
46+
function batch_coloring_test(; order = NaturalOrder(), target_list = :default, verbose = 1, grid_size = CTDirect.__grid_size(), disc_method = CTDirect.__disc_method())
47+
48+
if target_list == :default
49+
target_list = ["beam", "double_integrator_mintf", "double_integrator_minenergy", "fuller", "goddard", "goddard_all", "jackson", "simple_integrator", "vanderpol"]
50+
end
51+
52+
verbose > 1 && println("\nProblem list: ", target_list)
53+
problem_list = []
54+
for problem_name in target_list
55+
ocp_data = getfield(Main, Symbol(problem_name))()
56+
push!(problem_list, ocp_data)
57+
end
58+
59+
num_J_list = []
60+
num_H_list = []
61+
for problem in problem_list
62+
(num_J, num_H) = coloring_test(problem.ocp; order=order, grid_size=grid_size, disc_method=disc_method)
63+
push!(num_J_list, num_J)
64+
push!(num_H_list, num_H)
65+
verbose > 1 && @printf("%-30s J colors %2d H colors %2d\n", problem.name, num_J, num_H)
66+
end
67+
return sum(num_J_list), sum(num_H_list)
68+
end

0 commit comments

Comments
 (0)