-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLshape.cpp
More file actions
334 lines (291 loc) · 9.29 KB
/
Copy pathLshape.cpp
File metadata and controls
334 lines (291 loc) · 9.29 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "Lshape.h"
#include "modelSetting.h"
void LShape
(IloEnv env, IloExpr& objFirst, Var1D z, Var1D y, Var2D w, Var1D theta, Var1D v, Var3D p,
IloNum& obj_opt, IloNumArray z_opt, IloNumArray y_opt, IloNumArray2D w_opt, IloNumArray theta_opt,
IloExpr& objSecond, IloRangeArray constr9b, IloRangeArray constr9c, Constr2D constr9d) {
IloModel modelFirst(env);
IloCplex cplexFirst(modelFirst);
cplexFirst.setOut(env.getNullStream());
cplexFirst.setParam(IloCplex::TiLim, 1.0);
cplexFirst.setParam(IloCplex::EpAGap, 1.0e-6);
modelFirst.add(IloMinimize(env, objFirst));
//set up the basic constraints of the first-stage model
setFirstStageConstr(modelFirst, w, y, z);
/*L-shape*/
IloNum incumbent = IloInfinity;
bool intOptFirst = false;
vector<branchNode> branchTree;
bool optimum = false;
IloRange cut;
int iter = 0;
while (!optimum) {
//solve the first stage
intOptFirst = false;
incumbent = IloInfinity;
while (!intOptFirst) {
cplexFirst.solve();
if (cplexFirst.getStatus() == IloAlgorithm::Optimal) {
branchNode branchLeaf;
if (cplexFirst.getObjValue() < incumbent) {
if (checkBranch(cplexFirst, z, y, w, branchLeaf)) {
//branching
branchTree.push_back(branchLeaf);
changeBounds(branchLeaf, z, y, w, 1);
}
else {
//New incumbent, and then we move backward on the tree(fathom)
incumbent = cplexFirst.getObjValue();
cplexFirst.getValues(z, z_opt);
cplexFirst.getValues(y, y_opt);
for (int i = 0; i < I_mobile; i++) {
cplexFirst.getValues(w[i], w_opt[i]);
}
cplexFirst.getValues(theta, theta_opt);
fathom(branchTree, z, y, w, intOptFirst);
}
}
else {//Larger than incumbent so fathom
fathom(branchTree, z, y, w, intOptFirst);
}
}
else if (cplexFirst.getStatus() == IloAlgorithm::Infeasible) {
fathom(branchTree, z, y, w, intOptFirst);
}
else {
cout << "Unbounded status or other unknown CPLEX status appears.\n";
exit(1);
}
}
// release the memory space
branchTree.shrink_to_fit();
/*Optimality cut*/
//change the right-hand side of constraints in second stage
for (int j = 0; j < J_customer; j++) {
constr9c[j].setBounds(1.0 - y_opt[j], 1.0 - y_opt[j]);
}
for (int i = 0; i < I_mobile; i++) {
constr9d[i].setUbs(w_opt[i]);
}
//Optimality cut
optimum = true;
for (int s = 0; s < sampleSize; s++) {
if (optCut(env, objSecond, constr9b, constr9c, constr9d, theta[s], y, w, v, p, theta_opt[s], sample[s], cut)) {
modelFirst.add(cut);
optimum = false;
}
}
if (!(iter % 10)) {
cout << "Objective value in iteration " << iter << " : " << incumbent << endl;
}
iter++;
}
obj_opt = incumbent;
cplexFirst.end();
modelFirst.end();
}
//Check integer constraints satisfication and apply branch and bound
bool checkBranch(IloCplex cplex, Var1D z, Var1D y, Var2D w, branchNode& branchLeaf) {
IloEnv env = cplex.getEnv();
bool whetherBranch = false;
//consider z
IloNumArray z_result = IloNumArray(env, I_mobile);
cplex.getValues(z_result, z);
for (int i = 0; i < I_mobile; i++) {
if (z_result[i] < 1.0 && z_result[i] > 0.0) {
whetherBranch = true;
branchLeaf.varTitle = 'z';
branchLeaf.index = i;
branchLeaf.value = 0.0;
break;
}
}
if (!whetherBranch) {
//consider y
IloNumArray y_result = IloNumArray(env, J_customer);
cplex.getValues(y_result, y);
for (int j = 0; j < J_customer; j++) {
if (y_result[j] < 1.0 && y_result[j] > 0.0) {
whetherBranch = true;
branchLeaf.varTitle = 'y';
branchLeaf.index = j;
branchLeaf.value = 1.0;
break;
}
}
}
if (!whetherBranch) {
//consider w
for (int i = 0; i < I_mobile; i++) {
IloNumArray w_result = IloNumArray(env, J_customer);
cplex.getValues(w_result, w[i]);
for (int j = 0; j < J_customer; j++) {
if (w_result[j] < 1.0 && w_result[j] > 0.0) {
whetherBranch = true;
branchLeaf.varTitle = 'w';
branchLeaf.index = i * J_customer + j;
branchLeaf.value = 0.0;
break;
}
}
}
}
return whetherBranch;
}
void fathom(vector<branchNode>& branchTree, Var1D z, Var1D y, Var2D w, bool& intOpt) {
if (branchTree.empty()) { // If the first solution is already an integer-feasible solution.
intOpt = true;
}
else {
branchNode preBranchNode = branchTree.back();
branchTree.pop_back();
while (!branchTree.empty() && preBranchNode.secondTraverse) {
changeBounds(preBranchNode, z, y, w, 3); // after second traverse
preBranchNode = branchTree.back();
branchTree.pop_back();
}
if (!preBranchNode.secondTraverse) { // second traverse
branchTree.size();
preBranchNode.secondTraverse = true;
branchTree.push_back(preBranchNode);
changeBounds(preBranchNode, z, y, w, 2);
}
else {
//We clear the stack, so we have found the optimum
changeBounds(preBranchNode, z, y, w, 3);
intOpt = true;
}
}
}
void changeBounds(branchNode node, Var1D z, Var1D y, Var2D w, int option) {
switch(option){
case (1): { //new branch
if (node.varTitle == 'z') {
z[node.index].setBounds(node.value, node.value);
}
else if (node.varTitle == 'y') {
y[node.index].setBounds(node.value, node.value);
}
else { //w
w[node.index / J_customer][node.index % J_customer].setBounds(node.value, node.value);
}
break;
}
case (2): { //previously branched (second traverse)
if (node.varTitle == 'z') {
z[node.index].setBounds(1.0 - node.value, 1.0 - node.value);
}
else if (node.varTitle == 'y') {
y[node.index].setBounds(1.0 - node.value, 1.0 - node.value);
}
else { //w
w[node.index / J_customer][node.index % J_customer].setBounds(1.0 - node.value, 1.0 - node.value);
}
break;
}
case (3): { //reset the bounds to (0.0, 1.0) after second traverse
if (node.varTitle == 'z') {
z[node.index].setBounds(0.0, 1.0);
}
else if (node.varTitle == 'y') {
y[node.index].setBounds(0.0, 1.0);
}
else { //w
w[node.index / J_customer][node.index % J_customer].setBounds(0.0, 1.0);
}
break;
}
}
}
bool optCut(IloEnv env, IloExpr objSecond, IloRangeArray constr9b, IloRangeArray constr9c, Constr2D constr9d, IloNumVar theta, Var1D y, Var2D w, Var1D v, Var3D p, IloNum theta_val, scenario xi, IloRange& cut) {
//change the right-hand sides of constraints 9b in second stage
constr9b.setUbs(xi);
// Second stage model of a scenario
IloModel modelSecond(env);
IloCplex cplexSecond(modelSecond);
cplexSecond.setOut(env.getNullStream());
cplexSecond.setParam(IloCplex::TiLim, 1.0);
cplexSecond.setParam(IloCplex::EpAGap, 1.0e-6);
//objective
modelSecond.add(IloMinimize(env, objSecond));
//constraints 9b
modelSecond.add(constr9b);
//constraint 9c
modelSecond.add(constr9c);
//constraint 9d
for (int i = 0; i < I_mobile; i++) {
modelSecond.add(constr9d[i]);
}
cplexSecond.solve();
//Check the totally unimodular property
/*IloNumArray v_val = IloNumArray(env, J_customer);
cplexSecond.getValues(v, v_val);
for (int j = 0; j < J_customer; j++) {
if (v_val[j] > 0.0 && v_val[j] < 1.0) {
cout << "Not totally unimodular!!!!!\n";
cout << "v_" << j << ":" << v_val[j];
exit(1);
}
}
IloNumArray p_val = IloNumArray(env, K_shipper);
for (int i = 0; i < I_mobile; i++) {
for (int j = 0; j < J_customer; j++) {
cplexSecond.getValues(p[i][j], p_val);
for (int k = 0; k < K_shipper; k++) {
if (p_val[k] > 0.0 && p_val[k] < 1.0) {
cout << "Not totally unimodular!!!!!\n";
cout << "p_" << i << "_" << j << "_" << k << ":" << p_val[k];
exit(1);
}
}
}
}*/
//compare objective and theta
if (theta_val >= cplexSecond.getObjValue() - 1.0e-4) {
cplexSecond.end();
modelSecond.end();
return false;
}
/*Dual*/
IloNumArray dual9b(env, K_shipper), dual9c(env, J_customer);
cplexSecond.getDuals(dual9b, constr9b);
cplexSecond.getDuals(dual9c, constr9c);
IloArray<IloNumArray> dual9d(env, I_mobile);
for (int i = 0; i < I_mobile; i++) {
dual9d[i] = IloNumArray(env, J_customer);
cplexSecond.getDuals(dual9d[i], constr9d[i]);
}
/*cut*/
IloExpr cutExpr = - theta + IloScalProd(dual9b, xi) + IloSum(dual9c) - IloScalProd(dual9c, y);
for (int i = 0; i < I_mobile; i++) {
cutExpr += IloScalProd(dual9d[i], w[i]);
}
cut = IloRange(cutExpr <= 0);
cplexSecond.end();
modelSecond.end();
return true;
}
IloNum secondStageObj(IloEnv env, IloExpr objSecond, IloRangeArray constr9b, IloRangeArray constr9c, Constr2D constr9d, Var1D v, Var3D p, scenario xi) {
//change the right-hand side of constraints in second stage
constr9b.setUbs(xi);
// Second stage model of a scenario
IloModel modelSecond(env);
IloCplex cplexSecond(modelSecond);
cplexSecond.setOut(env.getNullStream());
cplexSecond.setParam(IloCplex::EpAGap, 1.0e-4);
//objective
modelSecond.add(IloMinimize(env, objSecond));
//constraints 9b
modelSecond.add(constr9b);
//constraint 9c
modelSecond.add(constr9c);
//constraint 9d
for (int i = 0; i < I_mobile; i++) {
modelSecond.add(constr9d[i]);
}
cplexSecond.solve();
IloNum obj_opt = cplexSecond.getObjValue();
cplexSecond.end();
modelSecond.end();
return obj_opt;
}