This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
runbendersparallel.jl
96 lines (83 loc) · 3.25 KB
/
runbendersparallel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require("bendersserial")
# simple synchronous parallel benders
function solveBendersParallel(nscen::Integer)
scenarioData = monteCarloSample(probdata,1:nscen)
stage1sol = solveExtensive(probdata,1)
clpmaster = ClpModel()
np = nprocs()
ncol1 = probdata.firstStageData.ncol
nrow1 = probdata.firstStageData.nrow
nrow2 = probdata.secondStageTemplate.nrow
# add \theta variables for cuts
thetaidx = [(ncol1+1):(ncol1+nscen)]
load_problem(clpmaster, probdata.Amat, probdata.firstStageData.collb,
probdata.firstStageData.colub, probdata.firstStageData.obj,
probdata.firstStageData.rowlb, probdata.firstStageData.rowub)
zeromat = SparseMatrixCSC(int32(nrow1),int32(nscen),ones(Int32,nscen+1),Int32[],Float64[])
add_columns(clpmaster, -1e8*ones(nscen), Inf*ones(nscen),
(1/nscen)*ones(nscen), zeromat)
@everywhere load_problem(clpsubproblem, probdata.Wmat, probdata.secondStageTemplate.collb,
probdata.secondStageTemplate.colub, probdata.secondStageTemplate.obj,
probdata.secondStageTemplate.rowlb, probdata.secondStageTemplate.rowub)
thetasol = -1e8*ones(nscen)
converged = false
niter = 0
mastertime = 0.
const blocksize = 2
while true
Tx = d.Tmat*stage1sol
# solve benders subproblems
nviolated = 0
#print("current solution is [")
#for i in 1:ncol1
# print("$(stage1sol[i]),")
#end
#println("]")
scen_count = 1
next_block() = (idx=scen_count; scen_count+=blocksize; idx:min(idx+blocksize-1,nscen))
new_violated() = (nviolated += 1)
@sync for p in 1:np
if p != myid() || np == 1
@async while true
scenblock = next_block()
if length(scenblock) == 0
break
end
results = remotecall_fetch(p,solveSubproblems,
[scenarioData[s][1]-Tx for s in scenblock],
[scenarioData[s][2]-Tx for s in scenblock])
for (s,result) in zip(scenblock,results)
optval, subgrad = result
#println("For scen $s, optval is $optval and model value is $(thetasol[s])")
if (optval > thetasol[s] + 1e-7)
new_violated()
addCut(clpmaster, optval, subgrad, stage1sol, s)
end
end
end
end
end
if nviolated == 0
break
end
println("Generated $nviolated violated cuts")
# resolve master
t = time()
initial_solve(clpmaster)
mastertime += time() - t
@assert is_proven_optimal(clpmaster)
sol = get_col_solution(clpmaster)
stage1sol = sol[1:ncol1]
thetasol = sol[(ncol1+1):end]
niter += 1
end
println("Optimal objective is: $(get_obj_value(clpmaster)), $niter iterations")
println("Time in master: $mastertime sec")
end
s = ARGS[1]
nscen = int(ARGS[2])
d = SMPSData(string(s,".cor"),string(s,".tim"),string(s,".sto"))
for p in 1:nprocs()
remotecall_fetch(p,setGlobalProbData,d)
end
@time solveBendersParallel(nscen)