Skip to content

Commit

Permalink
Improving code coverage on solve_mod=ILP()
Browse files Browse the repository at this point in the history
  • Loading branch information
jkhamphousone committed Aug 1, 2024
1 parent bd9c7c0 commit a01ad48
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 68 deletions.
Binary file not shown.
Binary file not shown.
138 changes: 74 additions & 64 deletions src/instance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,35 +143,9 @@ function createinstance_rrsp(filename, α, pars)
if α == 0
error("Please define α")
end
x_coors = data[1:n, 2]
y_coors = data[1:n, 3]
@assert pars.r_ij isa Euclidian
c = Dict(
(e[1], e[2]) => ceil_labbe(
distance([x_coors[e[1]], y_coors[e[1]]], [x_coors[e[2]], y_coors[e[2]]]),
) for e in E
)


d = Dict(
(a[1], a[2]) =>
a[1] == a[2] ? 0 : round(1 / rand(Uniform(n / 2, 3n / 2)), digits = 5)
for a in A
)
if pars.s_ij isa Euclidian
d = Dict(
(a[1], a[2]) => ceil_labbe(
distance(
[x_coors[a[1]], y_coors[a[1]]],
[x_coors[a[2]], y_coors[a[2]]],
),
) for a in A
)
end
for i 2:n
c[i, n+1] = c[1, i]
end
c[1, n+1] = 0
xs = data[1:n, 2]
ys = data[1:n, 3]
c, d = createweights(xs, ys, A, pars.r_ij, pars.s_ij)

o = zeros(Float64, n)
if pars.o_i == RandomInterval
Expand All @@ -183,7 +157,7 @@ function createinstance_rrsp(filename, α, pars)
open(random_filepath, "w") do f
println(f, "$n 0.0 ")
for i 1:n
println(f, "$i $(x_coors[i]) $(y_coors[i])")
println(f, "$i $(xs[i]) $(ys[i])")
end
println(f, "opening costs")
for i 1:n
Expand All @@ -206,7 +180,7 @@ function createinstance_rrsp(filename, α, pars)
for kv in d
d′[kv[1]] = d[kv[1]] * pars.backup_factor
end
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, x_coors, y_coors)
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, xs, ys)

elseif pars.nrand == 0

Expand Down Expand Up @@ -270,13 +244,13 @@ function createinstance_rrsp(filename, α, pars)
shift_n = 414 - 7
end

x_coors, y_coors = readcoordinates(data)
xs, ys = readcoordinates(data)

c = Dict(
(e[1], e[2]) => ceil_labbe(
distance(
[x_coors[e[1]], y_coors[e[1]]],
[x_coors[e[2]], y_coors[e[2]]],
[xs[e[1]], ys[e[1]]],
[xs[e[2]], ys[e[2]]],
) * (α),
) for e in E
)
Expand All @@ -291,8 +265,8 @@ function createinstance_rrsp(filename, α, pars)
d = Dict(
(a[1], a[2]) => ceil_labbe(
distance(
[x_coors[a[1]], y_coors[a[1]]],
[x_coors[a[2]], y_coors[a[2]]],
[xs[a[1]], ys[a[1]]],
[xs[a[2]], ys[a[2]]],
) * (10 - α),
) for a in A
)
Expand All @@ -305,7 +279,7 @@ function createinstance_rrsp(filename, α, pars)
for kv in d
d′[kv[1]] = d[kv[1]] * pars.backup_factor
end
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, x_coors, y_coors)
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, xs, ys)
end

else
Expand All @@ -316,19 +290,11 @@ function createinstance_rrsp(filename, α, pars)
E = [(i, j) for i in V, j in V if i < j]
A = [(i, j) for i in V, j in V]
Ac = [(i, j) for i in V, j in V if i != j]
x_coors, y_coors = readcoordinates(data)
xs, ys = readcoordinates(data)
o = data[n+3, 1:n]
@assert pars.r_ij isa Euclidian
c = Dict(
(e[1], e[2]) => ceil_labbe(
distance([x_coors[e[1]], y_coors[e[1]]], [x_coors[e[2]], y_coors[e[2]]]),
) for e in E
)

for i 2:n
c[i, n+1] = c[1, i]
end
c[1, n+1] = 0
c, d = createweights(xs, ys, A, pars.r_ij, pars.s_ij)

d_data = data[n+5:2n+4, 1:n]
d = Dict((a[1], a[2]) => d_data[a[1], a[2]] for a in A)
c′ = Dict{Tuple{Int,Int},Float64}()
Expand All @@ -340,7 +306,7 @@ function createinstance_rrsp(filename, α, pars)
d′[kv[1]] = d[kv[1]] * pars.backup_factor
end

return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, x_coors, y_coors)
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, xs, ys)
end
end

Expand All @@ -353,7 +319,7 @@ end
function createinstance_rrsp(instdataname::Tuple{Vector{Tuple{Int, Int}},Int}, α, pars)
n = length(instdataname[2])

x_coors, y_coors = first.(instdataname[1]), last.(instdataname[1])
xs, ys = first.(instdataname[1]), last.(instdataname[1])

V = 1:n
tildeV = 2:Int(ceil(n * pars.tildeV / 100))
Expand All @@ -371,8 +337,8 @@ function createinstance_rrsp(instdataname::Tuple{Vector{Tuple{Int, Int}},Int},
c = Dict(
(e[1], e[2]) => ceil_labbe(
distance(
[x_coors[e[1]], y_coors[e[1]]],
[x_coors[e[2]], y_coors[e[2]]],
[xs[e[1]], ys[e[1]]],
[xs[e[2]], ys[e[2]]],
) * (α),
) for e in E
)
Expand All @@ -387,8 +353,8 @@ function createinstance_rrsp(instdataname::Tuple{Vector{Tuple{Int, Int}},Int},
d = Dict(
(a[1], a[2]) => ceil_labbe(
distance(
[x_coors[a[1]], y_coors[a[1]]],
[x_coors[a[2]], y_coors[a[2]]],
[xs[a[1]], ys[a[1]]],
[xs[a[2]], ys[a[2]]],
) * (10 - α),
) for a in A
)
Expand All @@ -401,7 +367,7 @@ function createinstance_rrsp(instdataname::Tuple{Vector{Tuple{Int, Int}},Int},
for kv in d
d′[kv[1]] = d[kv[1]] * pars.backup_factor
end
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, x_coors, y_coors)
return RRSPInstance(n, V, tildeV, pars.F, o, α, c, c′, d, d′, xs, ys)
end


Expand All @@ -418,30 +384,74 @@ end
function readcoordinates(data)

n_start = 2
x_coors = Float64[]
y_coors = Float64[]
while length(x_coors) == 0
xs = Float64[]
ys = Float64[]
while length(xs) == 0
try
if data[end, 2] == ""
x_coors = Float64.(data[n_start:end-1, 2])
xs = Float64.(data[n_start:end-1, 2])
else
x_coors = Float64.(data[n_start:end, 2])
xs = Float64.(data[n_start:end, 2])
end
catch
n_start += 1
end
end
n_start = 2
while length(y_coors) == 0
while length(ys) == 0
try
if data[end, 2] == ""
y_coors = Float64.(data[n_start:end-1, 3])
ys = Float64.(data[n_start:end-1, 3])
else
y_coors = Float64.(data[n_start:end, 3])
ys = Float64.(data[n_start:end, 3])
end
catch
n_start += 1
end
end
return x_coors, y_coors
return xs, ys
end


function createweights(xs, ys, A, r_ij, s_ij)
if r_ij isa RandomInterval
c = Dict(
(a[1], a[2]) =>
a[1] == a[2] ? 0 : rand(pars.r_ij[1]:pars.r_ij[2])
for a in A
)
end
if r_ij isa Euclidian
c = Dict(
(e[1], e[2]) => ceil_labbe(
distance([xs[e[1]], ys[e[1]]], [xs[e[2]], ys[e[2]]]),
) for e in E
)
end

if s_ij isa RandomInterval
d = Dict(
(a[1], a[2]) =>
a[1] == a[2] ? 0 : rand(pars.r_ij[1]:pars.r_ij[2])
for a in A
)
end
if s_ij isa Euclidian
d = Dict(
(a[1], a[2]) => ceil_labbe(
distance(
[xs[a[1]], ys[a[1]]],
[xs[a[2]], ys[a[2]]],
),
) for a in A
)
end

for i 2:n
c[i, n+1] = c[1, i]
end
c[1, n+1] = 0


return c, d
end
14 changes: 11 additions & 3 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,20 @@ function main(pars::SolverParameters, instdataname, optimizer, solutionchecker =
ilp_table = read_ilp_table(input_filepath, pars.plot_id)
end

if pars.plotting && pars.writeresults != ""
if pars.plotting && pars.writeresults
if pars.solve_mod == ILP()
perform_plot(pars, inst, instdataname[1], ilp_table, true)
if ilp_table.sol.n != 0
perform_plot(pars, inst, instdataname[1], ilp_table, true)
else
@info "No feasible solution has been found by ILP within the solving time, can not perform plotting"
end
end
if pars.solve_mod == BranchBendersCut()
perform_plot(pars, inst, instdataname[1], benders_table, false)
if benders_table.sol.n != 0
perform_plot(pars, inst, instdataname[1], benders_table, false)
else
@info "No feasible solution has been found by B&BC within the solving time, can not perform plotting"
end
end
end
if pars.writeresults != ""
Expand Down
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ include("aqua.jl")


pars = SolverParameters(
solve_mod = BranchBendersCut(), # ILP, or BranchBendersCut
solve_mod = BranchBendersCut(), # ILP(), or BranchBendersCut()
F = 7, # total failing time F in days per year, see [`PhD manuscript`](https://theses.hal.science/tel-04319443)
sp_solve = Poly(),
o_i = 1, # opening costs
Expand Down Expand Up @@ -112,4 +112,12 @@ include("aqua.jl")
optimizer_with_attributes(GLPK.Optimizer, "msg_lev" => 2, "tm_lim" => 1_000),
true,
) == 0

pars.solve_mod = ILP()
@test rspoptimize(
pars,
:TinyInstance_10_3,
optimizer_with_attributes(GLPK.Optimizer, "msg_lev" => 2, "tm_lim" => 1_000),
true,
) == 0
end

0 comments on commit a01ad48

Please sign in to comment.