-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCore.jl
275 lines (234 loc) · 8.73 KB
/
Core.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"""
setoptions!
This takes `options` arguments for setting DSP options.
# Arguments
- `options`: possible keys are `:param` (path to parameter file),
`:is_stochastic` (`true` if stochastic model; otherwise, `false`),
and `:solve_type` (algorithm type; see `Methods`).
"""
function setoptions!(options)
check_dsp()
for (optname, optval) in options
if optname == :param
readParamFile(dspenv, optval)
elseif optname == :is_stochastic
dspenv.is_stochastic = optval
elseif optname == :solve_type
if optval in instances(Methods)
dspenv.solve_type = optval
else
@warn("solve_type $optval is not available.")
end
else
@warn("Options $optname is not available.")
end
end
end
"""
load_problem!
Load problem from StructJuMP
# Arguments
- `m`: StructJuMP model
"""
function load_problem!(m::SJ.StructuredModel)
# set number of blocks (scenarios)
dspenv.nblocks = SJ.num_scenarios(m)
if dspenv.is_stochastic
loadStochasticProblem!(m)
else
loadStructuredProblem!(m)
end
setBlocks()
end
"""
loadStochasticProblem!
Load stochastic programming problem from StructJuMP
# Arguments
- `model`: StructJuMP model
"""
function loadStochasticProblem!(model::SJ.StructuredModel)
nscen = dspenv.nblocks
ncols1 = length(model.variables)
nrows1 = length(dspenv.linConstrs[0])
ncols2 = 0
nrows2 = 0
for (id, subm) in SJ.getchildren(model)
ncols2 = length(subm.variables)
nrows2 = length(dspenv.linConstrs[id])
break
end
# set scenario indices for each MPI processor
if dspenv.comm_size > 1
ncols2 = MPI.Allreduce([ncols2], MPI.MAX, dspenv.comm)[1]
nrows2 = MPI.Allreduce([nrows2], MPI.MAX, dspenv.comm)[1]
end
# set DSPProblem data
dspenv.numCols[0] = ncols1
dspenv.numRows[0] = nrows1
dspenv.colVal[0] = Vector{Float64}(undef, ncols1)
for (id, blk) in SJ.getchildren(model)
dspenv.numCols[id] = ncols2
dspenv.numRows[id] = nrows2
dspenv.colVal[id] = Vector{Float64}(undef, ncols2)
end
setNumberOfScenarios(dspenv, nscen)
setDimensions(dspenv, ncols1, nrows1, ncols2, nrows2)
qc_supported = true
# set problem data
start, index, value, rlbd, rubd, obj, clbd, cubd, ctype, cname, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval = get_model_data(model)
if nqrows == 0
loadFirstStage(dspenv, start, index, value, clbd, cubd, ctype, obj, rlbd, rubd)
else
if getVersionMajor(dspenv) >= 2
loadQCQPFirstStage(dspenv, start, index, value, clbd, cubd, ctype, obj, C_NULL, C_NULL, C_NULL, 0, rlbd, rubd, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval)
else
loadFirstStage(dspenv, start, index, value, clbd, cubd, ctype, obj, rlbd, rubd)
qc_supported = false
end
end
for (id, blk) in SJ.getchildren(model)
probability = SJ.getprobability(model)[id]
start, index, value, rlbd, rubd, obj, clbd, cubd, ctype, cname, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval = get_model_data(blk, id)
if nqrows == 0
loadSecondStage(dspenv, id-1, probability, start, index, value, clbd, cubd, ctype, obj, rlbd, rubd)
else
if getVersionMajor(dspenv) >= 2
loadQCQPSecondStage(dspenv, id-1, probability, start, index, value, clbd, cubd, ctype, obj, C_NULL, C_NULL, C_NULL, 0, rlbd, rubd, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval)
else
loadSecondStage(dspenv, id-1, probability, start, index, value, clbd, cubd, ctype, obj, rlbd, rubd)
qc_supported = false
end
end
end
if qc_supported == false
@warn "QCQP is not supported with DSP version $(getVersion(dspenv)). The unsupported objective/constraints are ignored."
end
# Set DRO data
loadDroData(dspenv, dspenv.dro)
end
"""
loadStructuredProblem!
Load generic block-structured problem from StructJuMP
# Arguments
- `model`: StructJuMP model
"""
function loadStructuredProblem!(model::SJ.StructuredModel)
ncols1 = length(model.variables)
nrows1 = length(model.constraints)
# set DSPProblem data
dspenv.numCols[0] = ncols1
dspenv.numRows[0] = nrows1
dspenv.colVal[0] = Vector{Float64}(undef, ncols1)
for (id, blk) in SJ.getchildren(model)
ncols2 = length(blk.variables)
nrows2 = length(dspenv.linConstrs[id])
dspenv.numCols[id] = ncols2
dspenv.numRows[id] = nrows2
dspenv.colVal[id] = Vector{Float64}(undef, ncols2)
end
# load master
start, index, value, rlbd, rubd, obj1, clbd1, cubd1, ctype1, cname, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval = get_model_data(model)
loadBlockProblem(dspenv, 0, ncols1, nrows1, start[end],
start, index, value, clbd1, cubd1, ctype1, obj1, rlbd, rubd)
# Check if the master block has quadratic constraints.
has_qc = nqrows == 0 ? false : true
# going over blocks
for (id, blk) in SJ.getchildren(model)
ncols2 = length(blk.variables)
nrows2 = length(dspenv.linConstrs[id])
start, index, value, rlbd, rubd, obj, clbd, cubd, ctype, cname, nqrows, linnzcnt, quadnzcnt, rhs, sense, linstart, linind, linval, quadstart, quadrow, quadcol, quadval = get_model_data(blk, id)
loadBlockProblem(dspenv, id, ncols1 + ncols2, nrows2, start[end],
start, index, value, [clbd1; clbd], [cubd1; cubd], [ctype1; ctype], [obj1; obj], rlbd, rubd)
# Check if the sub-block has quadratic constraints.
if !has_qc && nqrows > 0
has_qc = true
end
end
if has_qc
@warn "Quadratic constraints are not supported for generic structured programs and thus will be ignored."
end
# Finalize loading blocks
updateBlocks(dspenv)
end
function solve!()
if dspenv.comm_size == 1
if dspenv.solve_type == Dual
if dspenv.is_stochastic
solveDd(dspenv);
else
@warn("Dual decomposition is available for stochastic programming only.")
return
end
elseif dspenv.solve_type == Benders
if dspenv.is_stochastic
solveBd(dspenv);
else
@warn("Benders decomposition is available for stochastic programming only.")
return
end
elseif dspenv.solve_type == ExtensiveForm
solveDe(dspenv);
elseif dspenv.solve_type == DW
solveDw(dspenv);
else
@error("Unexpected error")
return
end
elseif dspenv.comm_size > 1
if dspenv.solve_type == Dual
if dspenv.is_stochastic
solveDdMpi(dspenv);
else
@warn("Dual decomposition is available for stochastic programming only.")
return
end
elseif dspenv.solve_type == Benders
if dspenv.is_stochastic
solveBdMpi(dspenv);
else
@warn("Benders decomposition is available for stochastic programming only.")
return
end
elseif dspenv.solve_type == DW
solveDwMpi(dspenv);
elseif dspenv.solve_type == ExtensiveForm
solveDe(dspenv);
else
@error("Unexpected error")
return
end
end
# solution status
dspenv.status = getStatus(dspenv)
end
function post_solve!()
if dspenv.status == 3998
return
end
# get solution time
dspenv.solve_time = getWallTime(dspenv)
# primal and dual objective values
dspenv.primVal = getPrimalBound(dspenv) * dspenv.objective_sense
dspenv.dualVal = getDualBound(dspenv) * dspenv.objective_sense
if dspenv.solve_type == Dual
dspenv.rowVal = getDualSolution(dspenv)
end
primVal = dspenv.primVal
if mysize() > 1
primVal = MPI.bcast(dspenv.primVal, 0, dspenv.comm)
end
if abs(primVal) < 1.0e+20
primsol = getSolution(dspenv)
# parse solution to each block
n_start = 1
n_end = dspenv.numCols[0]
dspenv.colVal[0] = primsol[n_start:n_end]
n_start += dspenv.numCols[0]
numBlockCols = getNumBlockCols()
for i in 1:dspenv.nblocks
n_end += numBlockCols[i]
dspenv.colVal[i] = primsol[n_start:n_end]
n_start += numBlockCols[i]
end
end
end