Skip to content

Commit e8ac50f

Browse files
authored
Merge pull request #123 from SCIP-Interfaces/tk/print-statistics
Add user-friendly interface for statistics printing functionality.
2 parents 58bd0c3 + b229cd2 commit e8ac50f

File tree

7 files changed

+94
-6
lines changed

7 files changed

+94
-6
lines changed

Project.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
77
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
88
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
99

10+
[compat]
11+
CEnum = "^0.1.0"
12+
MathOptInterface = "^0.8.4"
13+
julia = "^1.0.0"
14+
1015
[extras]
1116
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1217

1318
[targets]
1419
test = ["Test"]
15-
16-
[compat]
17-
CEnum = "^0.1.0"
18-
MathOptInterface = "^0.8.4"
19-
julia = "^1.0.0"

src/SCIP.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module SCIP
22

3+
# assorted utility functions
4+
include("util.jl")
5+
36
# load deps, version check
47
include("init.jl")
58

src/managed_scip.jl

+23
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,26 @@ function add_indicator_constraint(mscip::ManagedSCIP, y, x, a, rhs)
276276
@SC SCIPaddCons(mscip, cons__[])
277277
return store_cons!(mscip, cons__)
278278
end
279+
280+
# Transform SCIP C function name as follows:
281+
# 1. Remove leading "SCIP" part (drop the first four characters).
282+
# 2. Convert camel case to snake case.
283+
# For example, `SCIPprintStatusStatistics` becomes `print_status_statistics`.
284+
const STATISTICS_FUNCS = map(x -> Symbol(camel_case_to_snake_case(string(x)[5 : end])), SCIP_STATISTICS_FUNCS)
285+
286+
for (scip_statistics_func, statistics_func) in zip(SCIP_STATISTICS_FUNCS, STATISTICS_FUNCS)
287+
@eval begin
288+
"""
289+
$($statistics_func)(mscip::ManagedSCIP)
290+
291+
Print statistics (calls `$($scip_statistics_func)`) to standard output.
292+
"""
293+
function $statistics_func end
294+
295+
function $statistics_func(mscip::ManagedSCIP)
296+
ret = $scip_statistics_func(mscip, C_NULL)
297+
ret !== nothing && @assert ret == SCIP_OKAY
298+
return nothing
299+
end
300+
end
301+
end

src/util.jl

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function camel_case_to_snake_case(x::AbstractString)
2+
# from https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
3+
s1 = replace(x, r"(.)([A-Z][a-z]+)" => s"\1_\2")
4+
return lowercase(replace(s1, r"([a-z0-9])([A-Z])" => s"\1_\2"))
5+
end

src/wrapper.jl

+28
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,31 @@ include(wrap("expr_manual"))
138138
macro SC(ex)
139139
return :(@assert $(esc(ex)) == SCIP_OKAY)
140140
end
141+
142+
const SCIP_STATISTICS_FUNCS = [
143+
:SCIPprintStatusStatistics,
144+
:SCIPprintTimingStatistics,
145+
:SCIPprintOrigProblemStatistics,
146+
:SCIPprintTransProblemStatistics,
147+
:SCIPprintPresolverStatistics,
148+
:SCIPprintConstraintStatistics,
149+
:SCIPprintConstraintTimingStatistics,
150+
:SCIPprintPropagatorStatistics,
151+
:SCIPprintConflictStatistics,
152+
:SCIPprintSeparatorStatistics,
153+
:SCIPprintPricerStatistics,
154+
:SCIPprintBranchruleStatistics,
155+
:SCIPprintHeuristicStatistics,
156+
:SCIPprintCompressionStatistics,
157+
:SCIPprintLPStatistics,
158+
:SCIPprintNLPStatistics,
159+
:SCIPprintRelaxatorStatistics,
160+
:SCIPprintTreeStatistics,
161+
:SCIPprintRootStatistics,
162+
:SCIPprintSolutionStatistics,
163+
:SCIPprintConcsolverStatistics,
164+
:SCIPprintBendersStatistics,
165+
:SCIPprintStatistics,
166+
:SCIPprintReoptStatistics,
167+
:SCIPprintBranchingStatistics
168+
]

src/wrapper/manual_commons.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const SCIP_EVENTTYPE_SYNC = UInt64(0x100000000)
3737

3838
const PRIx64 = "llx"
3939

40-
const FILE = Cvoid
40+
using Base.Libc: FILE
4141

4242
const BMS_BLKMEM = Cvoid
4343
const BMS_BUFMEM = Cvoid

test/managed_scip.jl

+29
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,32 @@ end
9292
@test mscip.scip[] == C_NULL
9393
end
9494
end
95+
96+
@testset "print statistics" begin
97+
mscip = SCIP.ManagedSCIP()
98+
SCIP.set_parameter(mscip, "display/verblevel", 0)
99+
100+
x = SCIP.add_variable(mscip)
101+
y = SCIP.add_variable(mscip)
102+
z = SCIP.add_variable(mscip)
103+
c = SCIP.add_linear_constraint(mscip, [x, y], [2.0, 3.0], 1.0, 9.0)
104+
SCIP.@SC SCIP.SCIPsolve(mscip)
105+
106+
@testset "$statistics_func" for statistics_func in map(x -> eval(:(SCIP.$x)), SCIP.STATISTICS_FUNCS)
107+
mktempdir() do dir
108+
filename = joinpath(dir, "statistics.txt")
109+
@test !isfile(filename)
110+
open(filename, write=true) do io
111+
redirect_stdout(io) do
112+
statistics_func(mscip)
113+
end
114+
end
115+
@test isfile(filename)
116+
if statistics_func == SCIP.print_statistics
117+
# Ensure that at least `print_statistics` produces output
118+
# (Not all statistics functions do for this simple model.)
119+
@test filesize(filename) > 0
120+
end
121+
end
122+
end
123+
end

0 commit comments

Comments
 (0)