1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the program and library */ 4 /* SCIP --- Solving Constraint Integer Programs */ 5 /* */ 6 /* Copyright (c) 2002-2023 Zuse Institute Berlin (ZIB) */ 7 /* */ 8 /* Licensed under the Apache License, Version 2.0 (the "License"); */ 9 /* you may not use this file except in compliance with the License. */ 10 /* You may obtain a copy of the License at */ 11 /* */ 12 /* http://www.apache.org/licenses/LICENSE-2.0 */ 13 /* */ 14 /* Unless required by applicable law or agreed to in writing, software */ 15 /* distributed under the License is distributed on an "AS IS" BASIS, */ 16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ 17 /* See the License for the specific language governing permissions and */ 18 /* limitations under the License. */ 19 /* */ 20 /* You should have received a copy of the Apache-2.0 license */ 21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */ 22 /* */ 23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 24 25 /**@file objbranchrule.cpp 26 * @brief C++ wrapper for branching rules 27 * @author Tobias Achterberg 28 */ 29 30 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/ 31 32 #include <cassert> 33 34 #include "objbranchrule.h" 35 36 37 38 39 /* 40 * Data structures 41 */ 42 43 /** branching rule data */ 44 struct SCIP_BranchruleData 45 { 46 scip::ObjBranchrule* objbranchrule; /**< branching rule object */ 47 SCIP_Bool deleteobject; /**< should the branching rule object be deleted when branching rule is freed? */ 48 }; 49 50 51 52 53 /* 54 * Callback methods of branching rule 55 */ 56 57 extern "C" 58 { 59 60 /** copy method for branchrule plugins (called when SCIP copies plugins) */ 61 static 62 SCIP_DECL_BRANCHCOPY(branchCopyObj) 63 { /*lint --e{715}*/ 64 SCIP_BRANCHRULEDATA* branchruledata; 65 66 assert(scip != NULL); 67 68 branchruledata = SCIPbranchruleGetData(branchrule); 69 assert(branchruledata != NULL); 70 assert(branchruledata->objbranchrule != NULL); 71 assert(branchruledata->objbranchrule->scip_ != scip); 72 73 if( branchruledata->objbranchrule->iscloneable() ) 74 { 75 scip::ObjBranchrule* newobjbranchrule; 76 newobjbranchrule = dynamic_cast<scip::ObjBranchrule*> (branchruledata->objbranchrule->clone(scip)); 77 78 /* call include method of branchrule object */ 79 SCIP_CALL( SCIPincludeObjBranchrule(scip, newobjbranchrule, TRUE) ); 80 } 81 82 return SCIP_OKAY; 83 } 84 85 /** destructor of branching rule to free user data (called when SCIP is exiting) */ 86 static 87 SCIP_DECL_BRANCHFREE(branchFreeObj) 88 { /*lint --e{715}*/ 89 SCIP_BRANCHRULEDATA* branchruledata; 90 91 branchruledata = SCIPbranchruleGetData(branchrule); 92 assert(branchruledata != NULL); 93 assert(branchruledata->objbranchrule != NULL); 94 assert(branchruledata->objbranchrule->scip_ == scip); 95 96 /* call virtual method of branchrule object */ 97 SCIP_CALL( branchruledata->objbranchrule->scip_free(scip, branchrule) ); 98 99 /* free branchrule object */ 100 if( branchruledata->deleteobject ) 101 delete branchruledata->objbranchrule; 102 103 /* free branchrule data */ 104 delete branchruledata; 105 SCIPbranchruleSetData(branchrule, NULL); /*lint !e64*/ 106 107 return SCIP_OKAY; 108 } 109 110 111 /** initialization method of branching rule (called after problem was transformed) */ 112 static 113 SCIP_DECL_BRANCHINIT(branchInitObj) 114 { /*lint --e{715}*/ 115 SCIP_BRANCHRULEDATA* branchruledata; 116 117 branchruledata = SCIPbranchruleGetData(branchrule); 118 assert(branchruledata != NULL); 119 assert(branchruledata->objbranchrule != NULL); 120 assert(branchruledata->objbranchrule->scip_ == scip); 121 122 /* call virtual method of branchrule object */ 123 SCIP_CALL( branchruledata->objbranchrule->scip_init(scip, branchrule) ); 124 125 return SCIP_OKAY; 126 } 127 128 129 /** deinitialization method of branching rule (called before transformed problem is freed) */ 130 static 131 SCIP_DECL_BRANCHEXIT(branchExitObj) 132 { /*lint --e{715}*/ 133 SCIP_BRANCHRULEDATA* branchruledata; 134 135 branchruledata = SCIPbranchruleGetData(branchrule); 136 assert(branchruledata != NULL); 137 assert(branchruledata->objbranchrule != NULL); 138 139 /* call virtual method of branchrule object */ 140 SCIP_CALL( branchruledata->objbranchrule->scip_exit(scip, branchrule) ); 141 142 return SCIP_OKAY; 143 } 144 145 146 /** solving process initialization method of branching rule (called when branch and bound process is about to begin) */ 147 static 148 SCIP_DECL_BRANCHINITSOL(branchInitsolObj) 149 { /*lint --e{715}*/ 150 SCIP_BRANCHRULEDATA* branchruledata; 151 152 branchruledata = SCIPbranchruleGetData(branchrule); 153 assert(branchruledata != NULL); 154 assert(branchruledata->objbranchrule != NULL); 155 156 /* call virtual method of branchrule object */ 157 SCIP_CALL( branchruledata->objbranchrule->scip_initsol(scip, branchrule) ); 158 159 return SCIP_OKAY; 160 } 161 162 163 /** solving process deinitialization method of branching rule (called before branch and bound process data is freed) */ 164 static 165 SCIP_DECL_BRANCHEXITSOL(branchExitsolObj) 166 { /*lint --e{715}*/ 167 SCIP_BRANCHRULEDATA* branchruledata; 168 169 branchruledata = SCIPbranchruleGetData(branchrule); 170 assert(branchruledata != NULL); 171 assert(branchruledata->objbranchrule != NULL); 172 173 /* call virtual method of branchrule object */ 174 SCIP_CALL( branchruledata->objbranchrule->scip_exitsol(scip, branchrule) ); 175 176 return SCIP_OKAY; 177 } 178 179 180 /** branching execution method for fractional LP solutions */ 181 static 182 SCIP_DECL_BRANCHEXECLP(branchExeclpObj) 183 { /*lint --e{715}*/ 184 SCIP_BRANCHRULEDATA* branchruledata; 185 186 branchruledata = SCIPbranchruleGetData(branchrule); 187 assert(branchruledata != NULL); 188 assert(branchruledata->objbranchrule != NULL); 189 190 /* call virtual method of branchrule object */ 191 SCIP_CALL( branchruledata->objbranchrule->scip_execlp(scip, branchrule, allowaddcons, result) ); 192 193 return SCIP_OKAY; 194 } 195 196 197 /** branching execution method for external candidates */ 198 static 199 SCIP_DECL_BRANCHEXECEXT(branchExecextObj) 200 { /*lint --e{715}*/ 201 SCIP_BRANCHRULEDATA* branchruledata; 202 203 branchruledata = SCIPbranchruleGetData(branchrule); 204 assert(branchruledata != NULL); 205 assert(branchruledata->objbranchrule != NULL); 206 207 /* call virtual method of branchrule object */ 208 SCIP_CALL( branchruledata->objbranchrule->scip_execext(scip, branchrule, allowaddcons, result) ); 209 210 return SCIP_OKAY; 211 } 212 213 214 /** branching execution method for not completely fixed pseudo solutions */ 215 static 216 SCIP_DECL_BRANCHEXECPS(branchExecpsObj) 217 { /*lint --e{715}*/ 218 SCIP_BRANCHRULEDATA* branchruledata; 219 220 branchruledata = SCIPbranchruleGetData(branchrule); 221 assert(branchruledata != NULL); 222 assert(branchruledata->objbranchrule != NULL); 223 224 /* call virtual method of branchrule object */ 225 SCIP_CALL( branchruledata->objbranchrule->scip_execps(scip, branchrule, allowaddcons, result) ); 226 227 return SCIP_OKAY; 228 } 229 } 230 231 232 233 /* 234 * branching rule specific interface methods 235 */ 236 237 /** creates the branching rule for the given branching rule object and includes it in SCIP */ 238 SCIP_RETCODE SCIPincludeObjBranchrule( 239 SCIP* scip, /**< SCIP data structure */ 240 scip::ObjBranchrule* objbranchrule, /**< branching rule object */ 241 SCIP_Bool deleteobject /**< should the branching rule object be deleted when branching rule is freed? */ 242 ) 243 { 244 SCIP_BRANCHRULEDATA* branchruledata; 245 246 assert(scip != NULL); 247 assert(objbranchrule != NULL); 248 249 /* create branching rule data */ 250 branchruledata = new SCIP_BRANCHRULEDATA; 251 branchruledata->objbranchrule = objbranchrule; 252 branchruledata->deleteobject = deleteobject; 253 254 /* include branching rule */ 255 SCIP_CALL( SCIPincludeBranchrule(scip, objbranchrule->scip_name_, objbranchrule->scip_desc_, 256 objbranchrule->scip_priority_, objbranchrule->scip_maxdepth_, objbranchrule->scip_maxbounddist_, 257 branchCopyObj, 258 branchFreeObj, branchInitObj, branchExitObj, branchInitsolObj, branchExitsolObj, 259 branchExeclpObj, branchExecextObj, branchExecpsObj, 260 branchruledata) ); /*lint !e429*/ 261 262 return SCIP_OKAY; /*lint !e429*/ 263 } 264 265 266 /** returns the branchrule object of the given name, or 0 if not existing */ 267 scip::ObjBranchrule* SCIPfindObjBranchrule( 268 SCIP* scip, /**< SCIP data structure */ 269 const char* name /**< name of branching rule */ 270 ) 271 { 272 SCIP_BRANCHRULE* branchrule; 273 SCIP_BRANCHRULEDATA* branchruledata; 274 275 branchrule = SCIPfindBranchrule(scip, name); 276 if( branchrule == NULL ) 277 return 0; 278 279 branchruledata = SCIPbranchruleGetData(branchrule); 280 assert(branchruledata != NULL); 281 282 return branchruledata->objbranchrule; 283 } 284 285 /** returns the branchrule object for the given branching rule */ 286 scip::ObjBranchrule* SCIPgetObjBranchrule( 287 SCIP* scip, /**< SCIP data structure */ 288 SCIP_BRANCHRULE* branchrule /**< branching rule */ 289 ) 290 { 291 SCIP_BRANCHRULEDATA* branchruledata; 292 293 assert(scip != NULL); 294 branchruledata = SCIPbranchruleGetData(branchrule); 295 assert(branchruledata != NULL); 296 297 return branchruledata->objbranchrule; 298 } 299