-
Notifications
You must be signed in to change notification settings - Fork 3
/
branch_mostinf.c
374 lines (315 loc) · 13.6 KB
/
branch_mostinf.c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the program and library */
/* SCIP --- Solving Constraint Integer Programs */
/* */
/* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
/* fuer Informationstechnik Berlin */
/* */
/* SCIP is distributed under the terms of the ZIB Academic License. */
/* */
/* You should have received a copy of the ZIB Academic License */
/* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file branch_mostinf.c
* @brief most infeasible LP branching rule
* @author Tobias Achterberg
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include <assert.h>
#include <string.h>
#include "scip/branch_mostinf.h"
#define BRANCHRULE_NAME "mostinf"
#define BRANCHRULE_DESC "most infeasible branching"
#define BRANCHRULE_PRIORITY 100
#define BRANCHRULE_MAXDEPTH -1
#define BRANCHRULE_MAXBOUNDDIST 1.0
/*
* Local methods
*/
/** compares the so far best branching candidate with a new candidate and updates best candidate, if new candidate is better */
static
void updateBestCandidate(
SCIP* scip, /**< SCIP data structure */
SCIP_VAR** bestvar, /**< best branching candidate */
SCIP_Real* bestscore, /**< score of best branching candidate */
SCIP_Real* bestobj, /**< absolute objective value of best branching candidate */
SCIP_Real* bestsol, /**< proposed branching point of best branching candidate */
SCIP_VAR* cand, /**< branching candidate to consider */
SCIP_Real candscore, /**< scoring of branching candidate */
SCIP_Real candsol /**< proposed branching point of branching candidate */
)
{
SCIP_Real obj;
assert(scip != NULL);
assert(bestvar != NULL);
assert(bestscore != NULL);
assert(bestobj != NULL);
assert(*bestobj >= 0.0);
assert(cand != NULL);
/* a branching variable candidate should either be an active problem variable or a multi-aggregated variable */
assert(SCIPvarIsActive(SCIPvarGetProbvar(cand)) ||
SCIPvarGetStatus(SCIPvarGetProbvar(cand)) == SCIP_VARSTATUS_MULTAGGR);
if( SCIPvarGetStatus(SCIPvarGetProbvar(cand)) == SCIP_VARSTATUS_MULTAGGR )
{
/* for a multi-aggregated variable, we call updateBestCandidate function recursively with all variables in the multi-aggregation */
SCIP_VAR** multvars;
int nmultvars;
int i;
SCIP_Bool success;
SCIP_Real multvarlb;
SCIP_Real multvarub;
cand = SCIPvarGetProbvar(cand);
multvars = SCIPvarGetMultaggrVars(cand);
nmultvars = SCIPvarGetMultaggrNVars(cand);
/* if we have a candidate branching point, then first register only aggregation variables
* for which we can compute a corresponding branching point too (see also comments below)
* if this fails, then register all (unfixed) aggregation variables, thereby forgetting about candsol
*/
success = FALSE;
if( candsol != SCIP_INVALID ) /*lint !e777*/
{
SCIP_Real* multscalars;
SCIP_Real minact;
SCIP_Real maxact;
SCIP_Real aggrvarsol;
SCIP_Real aggrvarsol1;
SCIP_Real aggrvarsol2;
multscalars = SCIPvarGetMultaggrScalars(cand);
/* for computing the branching point, we need the current bounds of the multi-aggregated variable */
minact = SCIPcomputeVarLbLocal(scip, cand);
maxact = SCIPcomputeVarUbLocal(scip, cand);
for( i = 0; i < nmultvars; ++i )
{
/* skip fixed variables */
multvarlb = SCIPcomputeVarLbLocal(scip, multvars[i]);
multvarub = SCIPcomputeVarUbLocal(scip, multvars[i]);
if( SCIPisEQ(scip, multvarlb, multvarub) )
continue;
assert(multscalars != NULL);
assert(multscalars[i] != 0.0);
/* we cannot ensure that both the upper bound in the left node and the lower bound in the right node
* will be candsol by a clever choice for the branching point of multvars[i],
* but we can try to ensure that at least one of them will be at candsol
*/
if( multscalars[i] > 0.0 )
{
/* cand >= candsol
* if multvars[i] >= (candsol - (maxact - multscalars[i] * ub(multvars[i]))) / multscalars[i]
* = (candsol - maxact) / multscalars[i] + ub(multvars[i])
*/
aggrvarsol1 = (candsol - maxact) / multscalars[i] + multvarub;
/* cand <= candsol
* if multvars[i] <= (candsol - (minact - multscalar[i] * lb(multvars[i]))) / multscalars[i]
* = (candsol - minact) / multscalars[i] + lb(multvars[i])
*/
aggrvarsol2 = (candsol - minact) / multscalars[i] + multvarlb;
}
else
{
/* cand >= candsol
* if multvars[i] <= (candsol - (maxact - multscalars[i] * lb(multvars[i]))) / multscalars[i]
* = (candsol - maxact) / multscalars[i] + lb(multvars[i])
*/
aggrvarsol2 = (candsol - maxact) / multscalars[i] + multvarlb;
/* cand <= candsol
* if multvars[i] >= (candsol - (minact - multscalar[i] * ub(multvars[i]))) / multscalars[i]
* = (candsol - minact) / multscalars[i] + ub(multvars[i])
*/
aggrvarsol1 = (candsol - minact) / multscalars[i] + multvarub;
}
/* by the above choice, aggrvarsol1 <= ub(multvars[i]) and aggrvarsol2 >= lb(multvars[i])
* if aggrvarsol1 <= lb(multvars[i]) or aggrvarsol2 >= ub(multvars[i]), then choose the other one
* if both are out of bounds, then give up
* if both are inside bounds, then choose the one closer to 0.0 (someone has better idea???)
*/
if( SCIPisFeasLE(scip, aggrvarsol1, multvarlb) )
{
if( SCIPisFeasGE(scip, aggrvarsol2, multvarub) )
continue;
else
aggrvarsol = aggrvarsol2;
}
else
{
if( SCIPisFeasGE(scip, aggrvarsol2, multvarub) )
aggrvarsol = aggrvarsol1;
else
aggrvarsol = REALABS(aggrvarsol1) < REALABS(aggrvarsol2) ? aggrvarsol1 : aggrvarsol2;
}
success = TRUE;
updateBestCandidate(scip, bestvar, bestscore, bestobj, bestsol,
multvars[i], candscore, aggrvarsol);
}
}
if( !success )
for( i = 0; i < nmultvars; ++i )
{
/* skip fixed variables */
multvarlb = SCIPcomputeVarLbLocal(scip, multvars[i]);
multvarub = SCIPcomputeVarUbLocal(scip, multvars[i]);
if( SCIPisEQ(scip, multvarlb, multvarub) )
continue;
updateBestCandidate(scip, bestvar, bestscore, bestobj, bestsol,
multvars[i], candscore, SCIP_INVALID);
}
assert(*bestvar != NULL); /* if all variables were fixed, something is strange */
return;
}
candscore *= SCIPvarGetBranchFactor(cand);
obj = SCIPvarGetObj(cand);
obj = REALABS(obj);
if( SCIPisInfinity(scip, candscore)
|| (!SCIPisInfinity(scip, *bestscore) &&
(SCIPisGT(scip, candscore, *bestscore) || (SCIPisGE(scip, candscore, *bestscore) && obj > *bestobj))) )
{
*bestvar = cand;
*bestscore = candscore;
*bestobj = obj;
*bestsol = candsol;
}
}
/*
* Callback methods
*/
/** copy method for branchrule plugins (called when SCIP copies plugins) */
static
SCIP_DECL_BRANCHCOPY(branchCopyMostinf)
{ /*lint --e{715}*/
assert(scip != NULL);
assert(branchrule != NULL);
assert(strcmp(SCIPbranchruleGetName(branchrule), BRANCHRULE_NAME) == 0);
/* call inclusion method of branchrule */
SCIP_CALL( SCIPincludeBranchruleMostinf(scip) );
return SCIP_OKAY;
}
/** branching execution method for fractional LP solutions */
static
SCIP_DECL_BRANCHEXECLP(branchExeclpMostinf)
{ /*lint --e{715}*/
SCIP_VAR** lpcands;
SCIP_Real* lpcandsfrac;
int nlpcands;
SCIP_Real infeasibility;
SCIP_Real score;
SCIP_Real obj;
SCIP_Real bestscore;
SCIP_Real bestobj;
int bestcand;
int i;
assert(branchrule != NULL);
assert(strcmp(SCIPbranchruleGetName(branchrule), BRANCHRULE_NAME) == 0);
assert(scip != NULL);
assert(result != NULL);
SCIPdebugMessage("Execlp method of mostinf branching\n");
/* get branching candidates */
SCIP_CALL( SCIPgetLPBranchCands(scip, &lpcands, NULL, &lpcandsfrac, NULL, &nlpcands, NULL) );
assert(nlpcands > 0);
/* search the most infeasible candidate */
bestscore = SCIP_REAL_MIN;
bestobj = 0.0;
bestcand = -1;
for( i = 0; i < nlpcands; ++i )
{
assert(lpcands[i] != NULL);
infeasibility = lpcandsfrac[i];
infeasibility = MIN(infeasibility, 1.0-infeasibility);
score = infeasibility;
score *= SCIPvarGetBranchFactor(lpcands[i]);
obj = SCIPvarGetObj(lpcands[i]);
obj = REALABS(obj);
if( SCIPisGT(scip, score, bestscore)
|| (SCIPisGE(scip, score, bestscore) && obj > bestobj) )
{
bestscore = score;
bestobj = obj;
bestcand = i;
}
}
assert(bestcand >= 0);
SCIPdebugMessage(" -> %d candidates, selected candidate %d: variable <%s> (frac=%g, obj=%g, factor=%g, score=%g)\n",
nlpcands, bestcand, SCIPvarGetName(lpcands[bestcand]), lpcandsfrac[bestcand], bestobj,
SCIPvarGetBranchFactor(lpcands[bestcand]), bestscore);
/* perform the branching */
SCIP_CALL( SCIPbranchVar(scip, lpcands[bestcand], NULL, NULL, NULL) );
*result = SCIP_BRANCHED;
return SCIP_OKAY;
}
/** branching execution method for external candidates */
static
SCIP_DECL_BRANCHEXECEXT(branchExecextMostinf)
{ /*lint --e{715}*/
SCIP_VAR** externcands;
SCIP_Real* externcandssol;
SCIP_Real* externcandsscore;
int nexterncands;
SCIP_VAR* bestcand;
SCIP_Real bestscore;
SCIP_Real bestobj;
SCIP_Real bestsol;
SCIP_Real brpoint;
int i;
SCIP_NODE* downchild;
SCIP_NODE* eqchild;
SCIP_NODE* upchild;
assert(branchrule != NULL);
assert(strcmp(SCIPbranchruleGetName(branchrule), BRANCHRULE_NAME) == 0);
assert(scip != NULL);
assert(result != NULL);
SCIPdebugMessage("Execext method of mostinf branching\n");
/* get branching candidates */
SCIP_CALL( SCIPgetExternBranchCands(scip, &externcands, &externcandssol, &externcandsscore, NULL, &nexterncands, NULL, NULL, NULL) );
assert(nexterncands > 0);
/* search the most infeasible candidate */
bestscore = SCIP_REAL_MIN;
bestobj = 0.0;
bestcand = NULL;
bestsol = SCIP_INVALID;
for( i = 0; i < nexterncands; ++i )
{
updateBestCandidate(scip, &bestcand, &bestscore, &bestobj, &bestsol, externcands[i], externcandsscore[i], externcandssol[i]);
}
if( bestcand == NULL )
{
SCIPerrorMessage("branchExecextMostinf failed to select a branching variable from %d candidates\n", nexterncands);
*result = SCIP_DIDNOTRUN;
return SCIP_OKAY;
}
brpoint = SCIPgetBranchingPoint(scip, bestcand, bestsol);
SCIPdebugMessage(" -> %d candidates, selected variable <%s> (infeas=%g, obj=%g, factor=%g, score=%g), branching point=%g\n",
nexterncands, SCIPvarGetName(bestcand), bestsol, bestobj,
SCIPvarGetBranchFactor(bestcand), bestscore, brpoint);
/* perform the branching */
SCIP_CALL( SCIPbranchVarVal(scip, bestcand, brpoint, &downchild, &eqchild, &upchild) );
if( downchild != NULL || eqchild != NULL || upchild != NULL )
{
*result = SCIP_BRANCHED;
}
else
{
/* if there are no children, then variable should have been fixed by SCIPbranchVarVal */
assert(SCIPisEQ(scip, SCIPvarGetLbLocal(bestcand), SCIPvarGetUbLocal(bestcand)));
*result = SCIP_REDUCEDDOM;
}
return SCIP_OKAY;
}
/*
* branching specific interface methods
*/
/** creates the most infeasible LP branching rule and includes it in SCIP */
SCIP_RETCODE SCIPincludeBranchruleMostinf(
SCIP* scip /**< SCIP data structure */
)
{
SCIP_BRANCHRULE* branchrule;
/* include branching rule */
SCIP_CALL( SCIPincludeBranchruleBasic(scip, &branchrule, BRANCHRULE_NAME, BRANCHRULE_DESC, BRANCHRULE_PRIORITY,
BRANCHRULE_MAXDEPTH, BRANCHRULE_MAXBOUNDDIST, NULL) );
assert(branchrule != NULL);
/* set non-fundamental callbacks via specific setter functions*/
SCIP_CALL( SCIPsetBranchruleCopy(scip, branchrule, branchCopyMostinf) );
SCIP_CALL( SCIPsetBranchruleExecLp(scip, branchrule, branchExeclpMostinf) );
SCIP_CALL( SCIPsetBranchruleExecExt(scip, branchrule, branchExecextMostinf) );
return SCIP_OKAY;
}