Skip to content

Commit eb69fb7

Browse files
authored
Remove branches that depend on knitro_version (#347)
1 parent e858d3e commit eb69fb7

File tree

7 files changed

+59
-110
lines changed

7 files changed

+59
-110
lines changed

examples/nlp2noderivs.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,4 @@ function example_nlp2noderivs(; verbose=true)
140140
return KNITRO.KN_free(kc)
141141
end
142142

143-
if KNITRO.knitro_version() >= v"12.4"
144-
example_nlp2noderivs(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)
145-
else
146-
println("Example `nlp2noderivs.jl` is only available with Knitro >= 12.4")
147-
end
143+
example_nlp2noderivs(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)

examples/nlp2resolve.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,4 @@ function example_nlp2resolve(; verbose=true)
304304
return KNITRO.KN_free(kc)
305305
end
306306

307-
if KNITRO.knitro_version() >= v"12.4"
308-
example_nlp2resolve(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)
309-
else
310-
println("Example `nlp2resolve.jl` is only available with Knitro >= 12.4")
311-
end
307+
example_nlp2resolve(; verbose=isdefined(Main, :KN_VERBOSE) ? KN_VERBOSE : true)

examples/tuner.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ function example_tuner(; verbose=true)
189189
println(
190190
"Force Knitro multistart to run in parallel with 1 threads (instead of $nThreads).",
191191
)
192-
if KNITRO.knitro_version() >= v"13.0"
193-
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_NUMTHREADS, 1)
194-
else
195-
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_PAR_NUMTHREADS, 1)
196-
end
192+
KNITRO.KN_set_int_param(kc, KNITRO.KN_PARAM_NUMTHREADS, 1)
197193
end
198194

199195
# Solve the problem.

ext/KNITROMathOptInterfaceExt.jl

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,7 @@ function MOI.set(model::Optimizer, ::MOI.TimeLimitSec, value)
247247
model.time_limit_sec = value
248248
# By default, maxtime is set to 1e8 in Knitro.
249249
limit = something(value, 1e8)
250-
if KNITRO.knitro_version() >= v"15.0"
251-
KNITRO.@_checked KNITRO.KN_set_double_param(model, KNITRO.KN_PARAM_MAXTIME, limit)
252-
else
253-
# KNITRO does not have a single option to control the global time limit,
254-
# so we set various options.
255-
# MAXTIME_REAL is the base option, which applies if the problem is a NLP.
256-
# MIP_MAXTIME_REAL applies if the problem is a MINLP
257-
for p in (KNITRO.KN_PARAM_MAXTIMEREAL, KNITRO.KN_PARAM_MIP_MAXTIMEREAL)
258-
KNITRO.@_checked KNITRO.KN_set_double_param(model, p, limit)
259-
end
260-
end
250+
KNITRO.@_checked KNITRO.KN_set_double_param(model, KNITRO.KN_PARAM_MAXTIME, limit)
261251
return
262252
end
263253

@@ -1605,9 +1595,7 @@ end
16051595

16061596
function MOI.get(model::Optimizer, ::MOI.SolveTimeSec)
16071597
p = Ref{Cdouble}(NaN)
1608-
if KNITRO.knitro_version() >= v"12.0"
1609-
KNITRO.@_checked KNITRO.KN_get_solve_time_cpu(model, p)
1610-
end
1598+
KNITRO.@_checked KNITRO.KN_get_solve_time_cpu(model, p)
16111599
return p[]
16121600
end
16131601

src/C_wrapper.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,9 @@ function KN_solve(model::Model)
157157
# as we have trouble calling Julia code from multithreaded C
158158
# code. See issue #93 on https://github.com/jump-dev/KNITRO.jl.
159159
if !isempty(model.callbacks)
160-
if knitro_version() >= v"13.0"
161-
@_checked KN_set_int_param(model, KN_PARAM_MS_NUMTHREADS, 1)
162-
@_checked KN_set_int_param(model, KN_PARAM_NUMTHREADS, 1)
163-
@_checked KN_set_int_param(model, KN_PARAM_MIP_NUMTHREADS, 1)
164-
else
165-
@_checked KN_set_int_param_by_name(model, "par_numthreads", 1)
166-
@_checked KN_set_int_param_by_name(model, "par_msnumthreads", 1)
167-
end
160+
@_checked KN_set_int_param(model, KN_PARAM_MS_NUMTHREADS, 1)
161+
@_checked KN_set_int_param(model, KN_PARAM_NUMTHREADS, 1)
162+
@_checked KN_set_int_param(model, KN_PARAM_MIP_NUMTHREADS, 1)
168163
end
169164
# For KN_solve, we do not return an error if ret is different of 0.
170165
return KN_solve(model.env)

test/C_wrapper.jl

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,33 @@ function callback(name)
9595
return callbackFn
9696
end
9797

98-
if KNITRO.knitro_version() >= v"12.1"
99-
@testset "MPS reader/writer" begin
100-
mps_name = joinpath(dirname(@__FILE__), "lp.mps")
101-
mps_name_out = joinpath(dirname(@__FILE__), "lp2.mps")
102-
open(mps_name, "w") do io
103-
return write(io, MPS_PROBLEM)
104-
end
105-
kc = KN_new()
106-
KN_load_mps_file(kc, mps_name)
107-
KN_set_int_param_by_name(kc, "outlev", 0)
108-
KN_write_mps_file(kc, mps_name_out)
109-
status = KN_solve(kc)
110-
obj = Ref{Cdouble}(0.0)
111-
KN_get_solution(kc, Ref{Cint}(), obj, C_NULL, C_NULL)
112-
KN_free(kc)
113-
@test status == 0
114-
@test isapprox(obj[], 250.0 / 3.0, rtol=1e-6)
115-
116-
# Resolve with dumped MPS file
117-
kc = KN_new()
118-
KN_load_mps_file(kc, mps_name_out)
119-
KN_set_int_param_by_name(kc, "outlev", 0)
120-
status = KN_solve(kc)
121-
obj = Ref{Cdouble}(0.0)
122-
KN_get_solution(kc, Ref{Cint}(), obj, C_NULL, C_NULL)
123-
KN_free(kc)
124-
@test status == 0
125-
@test isapprox(obj[], 250.0 / 3.0, rtol=1e-6)
98+
@testset "MPS reader/writer" begin
99+
mps_name = joinpath(dirname(@__FILE__), "lp.mps")
100+
mps_name_out = joinpath(dirname(@__FILE__), "lp2.mps")
101+
open(mps_name, "w") do io
102+
return write(io, MPS_PROBLEM)
126103
end
104+
kc = KN_new()
105+
KN_load_mps_file(kc, mps_name)
106+
KN_set_int_param_by_name(kc, "outlev", 0)
107+
KN_write_mps_file(kc, mps_name_out)
108+
status = KN_solve(kc)
109+
obj = Ref{Cdouble}(0.0)
110+
KN_get_solution(kc, Ref{Cint}(), obj, C_NULL, C_NULL)
111+
KN_free(kc)
112+
@test status == 0
113+
@test isapprox(obj[], 250.0 / 3.0, rtol=1e-6)
114+
115+
# Resolve with dumped MPS file
116+
kc = KN_new()
117+
KN_load_mps_file(kc, mps_name_out)
118+
KN_set_int_param_by_name(kc, "outlev", 0)
119+
status = KN_solve(kc)
120+
obj = Ref{Cdouble}(0.0)
121+
KN_get_solution(kc, Ref{Cint}(), obj, C_NULL, C_NULL)
122+
KN_free(kc)
123+
@test status == 0
124+
@test isapprox(obj[], 250.0 / 3.0, rtol=1e-6)
127125
end
128126

129127
@testset "First problem" begin
@@ -137,11 +135,7 @@ end
137135
options = joinpath(dirname(@__FILE__), "..", "examples", "test_knitro.opt")
138136
tuner1 = joinpath(dirname(@__FILE__), "..", "examples", "tuner-fixed.opt")
139137
tuner2 = joinpath(dirname(@__FILE__), "..", "examples", "tuner-explore.opt")
140-
param_alg_name = if KNITRO.knitro_version() >= v"15"
141-
"nlp_algorithm"
142-
else
143-
"algorithm"
144-
end
138+
param_alg_name = "nlp_algorithm"
145139
KN_set_int_param_by_name(kc, param_alg_name, 0)
146140
KN_set_char_param_by_name(kc, "cplexlibname", ".")
147141
KN_set_double_param_by_name(kc, "xtol", 1e-15)
@@ -164,9 +158,7 @@ end
164158
_to_string(x) = GC.@preserve(x, unsafe_string(pointer(x)))
165159
@test _to_string(tmp) == "xtol"
166160
KN_get_param_doc(kc, KN_PARAM_XTOL, tmp, 1024)
167-
header = KNITRO.knitro_version() >= v"15" ? "" : "# "
168-
@test _to_string(tmp) ==
169-
"$(header)Step size tolerance used for terminating the optimization.\n"
161+
@test _to_string(tmp) == "Step size tolerance used for terminating the optimization.\n"
170162
KN_get_param_type(kc, KN_PARAM_XTOL, pCint)
171163
@test pCint[] == KN_PARAMTYPE_FLOAT
172164
KN_get_num_param_values(kc, KN_PARAM_XTOL, pCint)
@@ -212,21 +204,19 @@ end
212204
KN_set_con_upbnds_all(kc, [2 * 2 * 0.99])
213205

214206
# Test getters.
215-
if KNITRO.knitro_version() >= v"12.0"
216-
xindex = Cint[0, 1, 2]
217-
ret = zeros(Cdouble, 3)
218-
KN_get_var_lobnds(kc, 3, xindex, ret)
219-
@test ret == [0, 0.1, 0]
220-
KN_get_var_upbnds(kc, 3, xindex, ret)
221-
@test ret == [0.0, 2, 2]
222-
223-
cindex = Cint[0]
224-
ret = zeros(Cdouble, 1)
225-
KN_get_con_lobnds(kc, 1, cindex, ret)
226-
@test ret == [0.1]
227-
KN_get_con_upbnds(kc, 1, cindex, ret)
228-
@test ret == [2 * 2 * 0.99]
229-
end
207+
xindex = Cint[0, 1, 2]
208+
ret = zeros(Cdouble, 3)
209+
KN_get_var_lobnds(kc, 3, xindex, ret)
210+
@test ret == [0, 0.1, 0]
211+
KN_get_var_upbnds(kc, 3, xindex, ret)
212+
@test ret == [0.0, 2, 2]
213+
214+
cindex = Cint[0]
215+
ret = zeros(Cdouble, 1)
216+
KN_get_con_lobnds(kc, 1, cindex, ret)
217+
@test ret == [0.1]
218+
KN_get_con_upbnds(kc, 1, cindex, ret)
219+
@test ret == [2 * 2 * 0.99]
230220

231221
# Load quadratic structure x1*x2 for the constraint.
232222
KN_add_con_quadratic_struct(kc, 1, Cint[0], Cint[1], Cint[2], [1.0])
@@ -298,17 +288,15 @@ end
298288
@test objSol 31.363 atol = 1e-3
299289

300290
# Test getters for primal and dual variables
301-
if KNITRO.knitro_version() >= v"12.0"
302-
xopt = zeros(Cdouble, 3)
303-
KN_get_var_primal_values(kc, 3, Cint[0, 1, 2], xopt)
304-
@test xopt == x
305-
rc = zeros(Cdouble, 3)
306-
KN_get_var_dual_values(kc, 3, Cint[0, 1, 2], rc)
307-
@test rc == lambda_[2:4]
308-
dual = zeros(Cdouble, 1)
309-
KN_get_con_dual_values(kc, 1, Cint[0], dual)
310-
@test dual == [lambda_[1]]
311-
end
291+
xopt = zeros(Cdouble, 3)
292+
KN_get_var_primal_values(kc, 3, Cint[0, 1, 2], xopt)
293+
@test xopt == x
294+
rc = zeros(Cdouble, 3)
295+
KN_get_var_dual_values(kc, 3, Cint[0, 1, 2], rc)
296+
@test rc == lambda_[2:4]
297+
dual = zeros(Cdouble, 1)
298+
KN_get_con_dual_values(kc, 1, Cint[0], dual)
299+
@test dual == [lambda_[1]]
312300

313301
KN_free(kc)
314302
end
@@ -692,9 +680,6 @@ end
692680
end
693681

694682
@testset "Knitro violation information" begin
695-
if KNITRO.knitro_version() < v"12.4"
696-
return 0
697-
end
698683
#*------------------------------------------------------------------*
699684
#* FUNCTION callbackEvalFC *
700685
#*------------------------------------------------------------------*
@@ -790,9 +775,6 @@ end
790775
end
791776

792777
@testset "Knitro structural manipulation" begin
793-
if KNITRO.knitro_version() < v"12.4"
794-
return 0
795-
end
796778
#*------------------------------------------------------------------*
797779
#* FUNCTION callbackEvalFC *
798780
#*------------------------------------------------------------------*

test/MOI_wrapper.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,7 @@ function test_RawOptimizerParameter_tuner_file()
296296
@test MOI.supports(model, MOI.RawOptimizerAttribute("tuner_file"))
297297
dir = mktempdir()
298298
filename = joinpath(dir, "tuner_file")
299-
if KNITRO.knitro_version() >= v"15"
300-
write(filename, "nlp_algorithm")
301-
else
302-
write(filename, "algorithm")
303-
end
299+
write(filename, "nlp_algorithm")
304300
MOI.set(model, MOI.RawOptimizerAttribute("tuner_file"), filename)
305301
return
306302
end

0 commit comments

Comments
 (0)