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 objrelax.cpp 26 * @brief C++ wrapper for relaxators 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 "objrelax.h" 35 36 37 38 39 /* 40 * Data structures 41 */ 42 43 /** relaxator data */ 44 struct SCIP_RelaxData 45 { 46 scip::ObjRelax* objrelax; /**< relaxator object */ 47 SCIP_Bool deleteobject; /**< should the relaxator object be deleted when relaxator is freed? */ 48 }; 49 50 51 52 53 /* 54 * Callback methods of relaxator 55 */ 56 57 extern "C" 58 { 59 60 /** copy method for relaxator plugins (called when SCIP copies plugins) */ 61 static 62 SCIP_DECL_RELAXCOPY(relaxCopyObj) 63 { /*lint --e{715}*/ 64 SCIP_RELAXDATA* relaxdata; 65 66 assert(scip != NULL); 67 68 relaxdata = SCIPrelaxGetData(relax); 69 assert(relaxdata != NULL); 70 assert(relaxdata->objrelax != NULL); 71 assert(relaxdata->objrelax->scip_ != scip); 72 73 if( relaxdata->objrelax->iscloneable() ) 74 { 75 scip::ObjRelax* newobjrelax; 76 newobjrelax = dynamic_cast<scip::ObjRelax*> (relaxdata->objrelax->clone(scip)); 77 78 /* call include method of relaxator object */ 79 SCIP_CALL( SCIPincludeObjRelax(scip, newobjrelax, TRUE) ); 80 } 81 82 return SCIP_OKAY; 83 } 84 85 /** destructor of relaxator to free user data (called when SCIP is exiting) */ 86 static 87 SCIP_DECL_RELAXFREE(relaxFreeObj) 88 { /*lint --e{715}*/ 89 SCIP_RELAXDATA* relaxdata; 90 91 relaxdata = SCIPrelaxGetData(relax); 92 assert(relaxdata != NULL); 93 assert(relaxdata->objrelax != NULL); 94 assert(relaxdata->objrelax->scip_ == scip); 95 96 /* call virtual method of relax object */ 97 SCIP_CALL( relaxdata->objrelax->scip_free(scip, relax) ); 98 99 /* free relax object */ 100 if( relaxdata->deleteobject ) 101 delete relaxdata->objrelax; 102 103 /* free relax data */ 104 delete relaxdata; 105 SCIPrelaxSetData(relax, NULL); /*lint !e64*/ 106 107 return SCIP_OKAY; 108 } 109 110 111 /** initialization method of relaxator (called after problem was transformed) */ 112 static 113 SCIP_DECL_RELAXINIT(relaxInitObj) 114 { /*lint --e{715}*/ 115 SCIP_RELAXDATA* relaxdata; 116 117 relaxdata = SCIPrelaxGetData(relax); 118 assert(relaxdata != NULL); 119 assert(relaxdata->objrelax != NULL); 120 assert(relaxdata->objrelax->scip_ == scip); 121 122 /* call virtual method of relax object */ 123 SCIP_CALL( relaxdata->objrelax->scip_init(scip, relax) ); 124 125 return SCIP_OKAY; 126 } 127 128 129 /** deinitialization method of relaxator (called before transformed problem is freed) */ 130 static 131 SCIP_DECL_RELAXEXIT(relaxExitObj) 132 { /*lint --e{715}*/ 133 SCIP_RELAXDATA* relaxdata; 134 135 relaxdata = SCIPrelaxGetData(relax); 136 assert(relaxdata != NULL); 137 assert(relaxdata->objrelax != NULL); 138 139 /* call virtual method of relax object */ 140 SCIP_CALL( relaxdata->objrelax->scip_exit(scip, relax) ); 141 142 return SCIP_OKAY; 143 } 144 145 146 /** solving process initialization method of relaxator (called when branch and bound process is about to begin) */ 147 static 148 SCIP_DECL_RELAXINITSOL(relaxInitsolObj) 149 { /*lint --e{715}*/ 150 SCIP_RELAXDATA* relaxdata; 151 152 relaxdata = SCIPrelaxGetData(relax); 153 assert(relaxdata != NULL); 154 assert(relaxdata->objrelax != NULL); 155 156 /* call virtual method of relax object */ 157 SCIP_CALL( relaxdata->objrelax->scip_initsol(scip, relax) ); 158 159 return SCIP_OKAY; 160 } 161 162 163 /** solving process deinitialization method of relaxator (called before branch and bound process data is freed) */ 164 static 165 SCIP_DECL_RELAXEXITSOL(relaxExitsolObj) 166 { /*lint --e{715}*/ 167 SCIP_RELAXDATA* relaxdata; 168 169 relaxdata = SCIPrelaxGetData(relax); 170 assert(relaxdata != NULL); 171 assert(relaxdata->objrelax != NULL); 172 173 /* call virtual method of relax object */ 174 SCIP_CALL( relaxdata->objrelax->scip_exitsol(scip, relax) ); 175 176 return SCIP_OKAY; 177 } 178 179 180 /** execution method of relaxator */ 181 static 182 SCIP_DECL_RELAXEXEC(relaxExecObj) 183 { /*lint --e{715}*/ 184 SCIP_RELAXDATA* relaxdata; 185 186 relaxdata = SCIPrelaxGetData(relax); 187 assert(relaxdata != NULL); 188 assert(relaxdata->objrelax != NULL); 189 190 /* call virtual method of relax object */ 191 SCIP_CALL( relaxdata->objrelax->scip_exec(scip, relax, lowerbound, result) ); 192 193 return SCIP_OKAY; 194 } 195 } 196 197 198 199 /* 200 * relaxator specific interface methods 201 */ 202 203 /** creates the relaxator for the given relaxator object and includes it in SCIP */ 204 SCIP_RETCODE SCIPincludeObjRelax( 205 SCIP* scip, /**< SCIP data structure */ 206 scip::ObjRelax* objrelax, /**< relaxator object */ 207 SCIP_Bool deleteobject /**< should the relaxator object be deleted when relaxator is freed? */ 208 ) 209 { 210 SCIP_RELAXDATA* relaxdata; 211 212 assert(scip != NULL); 213 assert(objrelax != NULL); 214 215 /* create relaxator data */ 216 relaxdata = new SCIP_RELAXDATA; 217 relaxdata->objrelax = objrelax; 218 relaxdata->deleteobject = deleteobject; 219 220 /* include relaxator */ 221 SCIP_CALL( SCIPincludeRelax(scip, objrelax->scip_name_, objrelax->scip_desc_, 222 objrelax->scip_priority_, objrelax->scip_freq_, relaxCopyObj, 223 relaxFreeObj, relaxInitObj, relaxExitObj, 224 relaxInitsolObj, relaxExitsolObj, relaxExecObj, 225 relaxdata) ); /*lint !e429*/ 226 227 return SCIP_OKAY; /*lint !e429*/ 228 } 229 230 /** returns the relax object of the given name, or 0 if not existing */ 231 scip::ObjRelax* SCIPfindObjRelax( 232 SCIP* scip, /**< SCIP data structure */ 233 const char* name /**< name of relaxator */ 234 ) 235 { 236 SCIP_RELAX* relax; 237 SCIP_RELAXDATA* relaxdata; 238 239 relax = SCIPfindRelax(scip, name); 240 if( relax == NULL ) 241 return 0; 242 243 relaxdata = SCIPrelaxGetData(relax); 244 assert(relaxdata != NULL); 245 246 return relaxdata->objrelax; 247 } 248 249 /** returns the relax object for the given relaxator */ 250 scip::ObjRelax* SCIPgetObjRelax( 251 SCIP* scip, /**< SCIP data structure */ 252 SCIP_RELAX* relax /**< relaxator */ 253 ) 254 { 255 SCIP_RELAXDATA* relaxdata; 256 257 assert(scip != NULL); 258 relaxdata = SCIPrelaxGetData(relax); 259 assert(relaxdata != NULL); 260 261 return relaxdata->objrelax; 262 } 263