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
/
bendersserial.jl
166 lines (135 loc) · 4.63 KB
/
bendersserial.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require("extensive")
const clpsubproblem = ClpModel()
set_log_level(clpsubproblem,0)
function setGlobalProbData(d::SMPSData)
global probdata = d
end
function solveSubproblems(rowlbs, rowubs)
return [solveSubproblem(rowlbs[i],rowubs[i]) for i in 1:length(rowlbs)]
end
function solveSubproblem(rowlb, rowub)
options = ClpSolve()
set_presolve_type(options,1)
Tmat = probdata.Tmat
ncol1 = probdata.firstStageData.ncol
nrow2 = probdata.secondStageTemplate.nrow
chg_row_lower(clpsubproblem,rowlb)
chg_row_upper(clpsubproblem,rowub)
initial_solve(clpsubproblem,options)
# don't handle infeasible subproblems yet
@assert is_proven_optimal(clpsubproblem)
optval = get_obj_value(clpsubproblem)
duals = dual_row_solution(clpsubproblem)
subgrad = zeros(ncol1)
for i in 1:nrow2
status = get_row_status(clpsubproblem,i)
if (status == 1) # basic
continue
end
for k in 1:ncol1
subgrad[k] += -duals[i]*Tmat[i,k]
end
end
return optval, subgrad
end
function addCut(master::ClpModel, optval::Float64, subgrad::Vector{Float64}, stage1sol::Vector{Float64}, scen)
#print("adding cut: [")
# add (0-based) cut to master
cutvec = Float64[]
cutcolidx = Int32[]
for k in 1:length(subgrad)
# print("$(subgrad[k]),")
if abs(subgrad[k]) > 1e-10
push!(cutvec,-subgrad[k])
push!(cutcolidx,k-1)
end
end
#println("]")
push!(cutvec,1.)
push!(cutcolidx,length(subgrad)+scen-1)
cutnnz = length(cutvec)
cutlb = optval-dot(subgrad,stage1sol)
add_rows(master, 1, [cutlb], [1e25], Int32[0,cutnnz], cutcolidx, cutvec)
end
function addCuts(master::ClpModel, tasks, results, candidates::Vector{Vector{Float64}}, nvar1, nscen)
@assert number_cols(master) == nvar1 + nscen
@assert length(tasks) == length(results)
row = zeros(nvar1+nscen)
cutlb = 0.0
for k in 1:length(tasks)
cand,s = tasks[k]
optval, subgrad = results[k]
row[1:nvar1] -= subgrad
cutlb += optval
cutlb -= dot(subgrad,candidates[cand])
row[nvar1 + s] += 1
end
# add (0-based) cut to master
cutvec = Float64[]
cutcolidx = Int32[]
for k in 1:length(row)
if abs(row[k]) > 1e-10
push!(cutvec,row[k])
push!(cutcolidx,k-1)
end
end
cutnnz = length(cutvec)
add_rows(master, 1, [cutlb], [1e25], Int32[0,cutnnz], cutcolidx, cutvec)
end
function solveBendersSerial(d::SMPSData, nscen::Integer)
scenarioData = monteCarloSample(d,1:nscen)
stage1sol = solveExtensive(d,1)
clpmaster = ClpModel()
setGlobalProbData(d)
ncol1 = d.firstStageData.ncol
nrow1 = d.firstStageData.nrow
nrow2 = d.secondStageTemplate.nrow
# add \theta variables for cuts
thetaidx = [(ncol1+1):(ncol1+nscen)]
load_problem(clpmaster, d.Amat, d.firstStageData.collb,
d.firstStageData.colub, d.firstStageData.obj, d.firstStageData.rowlb,
d.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)
load_problem(clpsubproblem, d.Wmat, d.secondStageTemplate.collb,
d.secondStageTemplate.colub, d.secondStageTemplate.obj,
d.secondStageTemplate.rowlb, d.secondStageTemplate.rowub)
thetasol = -1e8*ones(nscen)
converged = false
niter = 0
mastertime = 0.
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("]")
for s in 1:nscen
optval, subgrad = solveSubproblem(scenarioData[s][1]-Tx,scenarioData[s][2]-Tx)
#println("For scen $s, optval is $optval and model value is $(thetasol[s])")
if (optval > thetasol[s] + 1e-7)
nviolated += 1
addCut(clpmaster, optval, subgrad, stage1sol, s)
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