SCIP-SDP  3.2.0
branch_sdpmostfrac.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of SCIPSDP - a solving framework for mixed-integer */
4 /* semidefinite programs based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2020 Discrete Optimization, TU Darmstadt */
9 /* */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
24 /* */
25 /* */
26 /* Based on SCIP - Solving Constraint Integer Programs */
27 /* Copyright (C) 2002-2020 Zuse Institute Berlin */
28 /* SCIP is distributed under the terms of the SCIP Academic Licence, */
29 /* see file COPYING in the SCIP distribution. */
30 /* */
31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32 
42 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
43 
44 /* #define SCIP_DEBUG */
45 
46 #include <assert.h>
47 #include <string.h>
48 
49 #include "branch_sdpmostfrac.h"
50 
51 /* turn off lint warnings for whole file: */
52 /*lint --e{788,818}*/
53 
54 #define BRANCHRULE_NAME "sdpmostfrac"
55 #define BRANCHRULE_DESC "branch on the most fractional variable of the SDP"
56 #define BRANCHRULE_PRIORITY 500000
57 #define BRANCHRULE_MAXDEPTH -1
58 #define BRANCHRULE_MAXBOUNDDIST 1.0
59 
60 
61 /*
62  * Data structures
63  */
64 
65 /*
66  * Local methods
67  */
68 
69 /* put your local methods here, and declare them static */
70 
71 
72 /*
73  * Callback methods of branching rule
74  */
75 
77 static
78 SCIP_DECL_BRANCHCOPY(branchCopySdpmostfrac)
79 { /*lint --e{715}*/
80  assert(scip != NULL);
81  assert(branchrule != NULL);
82  assert(strcmp(SCIPbranchruleGetName(branchrule), BRANCHRULE_NAME) == 0);
83 
84  /* call inclusion method of branchrule */
86 
87  return SCIP_OKAY;
88 }
89 
91 static
92 SCIP_DECL_BRANCHEXECEXT(branchExecextSdpmostfrac)
93 {/*lint --e{715}*/
94  int i;
95  int ncands;
96  SCIP_VAR** cands = NULL;
97  SCIP_Real* candssol; /* solution values of all candidates */
98  SCIP_Real* candsscore; /* scores of all candidates */
99  SCIP_Real mostfracfrac; /* fractionality of the current most fractional variable */
100  SCIP_Real mostfracscore; /* score of the current most fractional variable */
101  SCIP_Real mostfracobj; /* objective of the current most fractional variable */
102  SCIP_Real mostfracval; /* value of the current most fractional variable */
103  SCIP_VAR* mostfracvar = NULL; /* variable with the highest current fractionality */
104 
105  assert( scip != NULL );
106  assert( result != NULL );
107 
108  SCIPdebugMsg(scip, "Executing External Branching method of SDP-mostfrac!\n");
109 
110  /* get the external candidates, as we use the score only as a tiebreaker, we aren't interested in the number of
111  * variables of different types with maximal score, so these return values are set to NULL */
112  SCIP_CALL( SCIPgetExternBranchCands(scip, &cands, &candssol, &candsscore, &ncands, NULL, NULL, NULL, NULL) );
113 
114  assert( ncands > 0 ); /* branchExecext should only be called if the list of external branching candidates is non-empty */
115 
116 #ifdef SCIP_DEBUG
117  SCIPdebugMsg(scip, "branching candidates for SDP-mostfrac:\n");
118  for (i = 0; i < ncands; i++)
119  SCIPdebugMsg(scip, "%s, value = %f, score = %f\n", SCIPvarGetName(cands[i]), candssol[i], candsscore[i]);
120 #endif
121 
122  mostfracfrac = -1.0;
123  mostfracscore = 0.0;
124  mostfracval = 0.0;
125  mostfracobj = -1.0;
126 
127  /* iterate over all solution candidates to find the one with the highest fractionality */
128  for (i = 0; i < ncands; i++)
129  {
130  /* we skip all continuous variables, since we first want to branch on integral variables */
131  if ( SCIPvarGetType(cands[i]) == SCIP_VARTYPE_CONTINUOUS )
132  {
133  SCIPdebugMsg(scip, "skipping continuous variable %s\n", SCIPvarGetName(cands[i]));
134  continue;
135  }
136 
137  /* a candidate is better than the current one if:
138  * - the fractionality is (feastol-)bigger than before or
139  * - the fractionality is (feastol-)equal and the score is (epsilon-)bigger or
140  * - the fractionality and score are (feastol-/epsilon-)equal and the objective is (epsilon) bigger or
141  * - all three are (feastol-/epsilon-)bigger and the index is smaller */
142  if ( SCIPisFeasGT(scip, SCIPfeasFrac(scip, candssol[i]), mostfracfrac) ||
143  (SCIPisFeasEQ(scip, SCIPfeasFrac(scip, candssol[i]), mostfracfrac) && SCIPisGT(scip, candsscore[i], mostfracscore)) ||
144  (SCIPisFeasEQ(scip, SCIPfeasFrac(scip, candssol[i]), mostfracfrac) && SCIPisEQ(scip, candsscore[i], mostfracscore)
145  && SCIPisGT(scip, SCIPvarGetObj(cands[i]), mostfracobj)) ||
146  (SCIPisFeasEQ(scip, SCIPfeasFrac(scip, candssol[i]), mostfracfrac) && SCIPisEQ(scip, candsscore[i], mostfracscore)
147  && SCIPisEQ(scip, SCIPvarGetObj(cands[i]), mostfracobj) && SCIPvarGetIndex(cands[i]) < SCIPvarGetIndex(mostfracvar)) )
148  {
149  /* update the current best candidate */
150  mostfracfrac = SCIPfeasFrac(scip, candssol[i]);
151  mostfracscore = candsscore[i];
152  mostfracobj = REALABS(SCIPvarGetObj(cands[i]));
153  mostfracval = candssol[i];
154  mostfracvar = cands[i];
155  }
156  }
157 
158  /* if all variables were continuous, we return DIDNOTRUN and let one of the SCIP branching rules decide */
159  if ( mostfracfrac == -1.0 )
160  {
161  SCIPdebugMsg(scip, "Skipping SDP-mostfrac branching rule since all branching variables are continuous\n");
162  *result = SCIP_DIDNOTFIND;
163  return SCIP_OKAY;
164  }
165 
166  assert( mostfracvar != NULL );
167  assert( SCIPisFeasGT(scip, mostfracfrac, 0.0) );
168 
169  /* branch */
170  SCIPdebugMsg(scip, "branching on variable %s with value %f and score %f\n", SCIPvarGetName(mostfracvar), mostfracval, mostfracscore);
171  SCIP_CALL( SCIPbranchVarVal(scip, mostfracvar, mostfracval, NULL, NULL, NULL) );
172 
173  *result = SCIP_BRANCHED;
174 
175  return SCIP_OKAY;
176 }
177 
178 /*
179  * branching rule specific interface methods
180  */
181 
184  SCIP* scip
185  )
186 {
187  SCIP_BRANCHRULEDATA* branchruledata;
188  SCIP_BRANCHRULE* branchrule;
189 
190  /* create empty branching rule data */
191  branchruledata = NULL;
192 
193  branchrule = NULL;
194 
195  /* include branching rule */
196  SCIP_CALL( SCIPincludeBranchruleBasic(scip, &branchrule, BRANCHRULE_NAME, BRANCHRULE_DESC, BRANCHRULE_PRIORITY,
197  BRANCHRULE_MAXDEPTH, BRANCHRULE_MAXBOUNDDIST, branchruledata) );
198 
199  assert(branchrule != NULL);
200 
201  /* set non fundamental callbacks via setter functions */
202  SCIP_CALL( SCIPsetBranchruleCopy(scip, branchrule, branchCopySdpmostfrac) );
203  SCIP_CALL( SCIPsetBranchruleExecExt(scip, branchrule, branchExecextSdpmostfrac) );
204 
205  return SCIP_OKAY;
206 }
#define BRANCHRULE_MAXBOUNDDIST
#define BRANCHRULE_DESC
most fractional branching rule for SCIP-SDP
SCIP_RETCODE SCIPincludeBranchruleSdpmostfrac(SCIP *scip)
#define BRANCHRULE_PRIORITY
#define BRANCHRULE_NAME
#define BRANCHRULE_MAXDEPTH
static SCIP_DECL_BRANCHCOPY(branchCopySdpmostfrac)
static SCIP_DECL_BRANCHEXECEXT(branchExecextSdpmostfrac)