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 objvardata.cpp 26 * @brief C++ wrapper for user variable data 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 "objvardata.h" 35 36 37 38 39 /* 40 * Data structures 41 */ 42 43 /** user variable data */ 44 struct SCIP_VarData 45 { 46 scip::ObjVardata* objvardata; /**< user variable data object */ 47 SCIP_Bool deleteobject; /**< should the user variable data object be deleted when variable is freed? */ 48 }; 49 50 51 52 53 /* 54 * Callback methods of user variable data 55 */ 56 57 extern "C" 58 { 59 60 /** frees user data of original variable (called when the original variable is freed) */ 61 static 62 SCIP_DECL_VARDELORIG(varDelorigObj) 63 { /*lint --e{715}*/ 64 assert(vardata != NULL); 65 assert(*vardata != NULL); 66 assert((*vardata)->objvardata != NULL); 67 68 /* call virtual method of vardata object */ 69 SCIP_CALL( (*vardata)->objvardata->scip_delorig(scip, var) ); 70 71 /* free vardata object */ 72 if( (*vardata)->deleteobject ) 73 delete (*vardata)->objvardata; 74 75 /* free vardata data */ 76 delete *vardata; 77 *vardata = 0; /*lint !e64*/ 78 79 return SCIP_OKAY; 80 } 81 82 83 /** creates user data of transformed variable by transforming the original user variable data 84 * (called after variable was transformed) 85 */ 86 static 87 SCIP_DECL_VARTRANS(varTransObj) 88 { /*lint --e{715}*/ 89 scip::ObjVardata* objvardata; /*lint !e78 !e40 !e55 !e530 !e522*/ 90 SCIP_Bool deleteobject; 91 92 assert(sourcedata != NULL); 93 assert(sourcedata->objvardata != NULL); 94 assert(targetdata != NULL); 95 assert(*targetdata == NULL); 96 97 /* call virtual method of vardata object */ 98 SCIP_CALL( sourcedata->objvardata->scip_trans(scip, targetvar, &objvardata, &deleteobject) ); /*lint !e40*/ 99 100 /* create transformed user variable data */ 101 *targetdata = new SCIP_VARDATA; 102 (*targetdata)->objvardata = objvardata; /*lint !e40*/ 103 (*targetdata)->deleteobject = deleteobject; 104 105 return SCIP_OKAY; 106 } 107 108 109 /** frees user data of transformed variable (called when the transformed variable is freed) */ 110 static 111 SCIP_DECL_VARDELTRANS(varDeltransObj) 112 { /*lint --e{715}*/ 113 assert(vardata != NULL); 114 assert(*vardata != NULL); 115 assert((*vardata)->objvardata != NULL); 116 117 /* call virtual method of vardata object */ 118 SCIP_CALL( (*vardata)->objvardata->scip_deltrans(scip, var) ); 119 120 /* free vardata object */ 121 if( (*vardata)->deleteobject ) 122 delete (*vardata)->objvardata; 123 124 /* free vardata data */ 125 delete *vardata; 126 *vardata = 0; /*lint !e64*/ 127 128 return SCIP_OKAY; 129 } 130 131 /** copies user data if you want to copy it to a subscip */ 132 static 133 SCIP_DECL_VARCOPY(varCopyObj) 134 { /*lint --e{715}*/ 135 scip::ObjVardata* objvardata; /*lint !e78 !e40 !e55 !e530 !e522*/ 136 137 assert(sourcedata != NULL); 138 assert(sourcedata->objvardata != NULL); 139 assert(targetdata != NULL); 140 assert(*targetdata == NULL); 141 142 /* call virtual method of probdata object */ 143 SCIP_CALL( sourcedata->objvardata->scip_copy(scip, sourcescip, sourcevar, varmap, consmap, targetvar, &objvardata, result) ); /*lint !e40*/ 144 145 if( objvardata != 0 ) 146 { 147 assert(*result == SCIP_SUCCESS); 148 149 /* create traget user problem data */ 150 *targetdata = new SCIP_VARDATA; 151 (*targetdata)->objvardata = objvardata; /*lint !e40*/ 152 (*targetdata)->deleteobject = TRUE; /* always delete object, because we created it */ 153 } 154 else 155 { 156 assert(*result == SCIP_DIDNOTRUN); 157 *targetdata = 0; 158 } 159 160 return SCIP_OKAY; 161 } 162 163 } 164 165 166 167 168 /* 169 * user variable data specific interface methods 170 */ 171 172 /** create and capture problem variable and associates the given variable data with the variable; 173 * if variable is of integral type, fractional bounds are automatically rounded 174 */ 175 SCIP_RETCODE SCIPcreateObjVar( 176 SCIP* scip, /**< SCIP data structure */ 177 SCIP_VAR** var, /**< pointer to variable object */ 178 const char* name, /**< name of variable, or NULL for automatic name creation */ 179 SCIP_Real lb, /**< lower bound of variable */ 180 SCIP_Real ub, /**< upper bound of variable */ 181 SCIP_Real obj, /**< objective function value */ 182 SCIP_VARTYPE vartype, /**< type of variable */ 183 SCIP_Bool initial, /**< should var's column be present in the initial root LP? */ 184 SCIP_Bool removable, /**< is var's column removable from the LP (due to aging or cleanup)? */ 185 scip::ObjVardata* objvardata, /**< user variable data object */ 186 SCIP_Bool deleteobject /**< should the user variable data object be deleted when variable is freed? */ 187 ) 188 { 189 SCIP_VARDATA* vardata; 190 191 /* create user variable data */ 192 vardata = new SCIP_VARDATA; 193 vardata->objvardata = objvardata; 194 vardata->deleteobject = deleteobject; 195 196 /* create variable */ 197 SCIP_CALL( SCIPcreateVar(scip, var, name, lb, ub, obj, vartype, initial, removable, 198 varDelorigObj, varTransObj, varDeltransObj, varCopyObj, vardata) ); /*lint !e429*/ 199 200 return SCIP_OKAY; /*lint !e429*/ 201 } 202 203 /** gets user variable data object for given problem variable 204 * Warning! This method should only be called after a variable was created with SCIPcreateObjVar(). 205 * Otherwise, a segmentation fault may arise, or an undefined pointer is returned. 206 */ 207 scip::ObjVardata* SCIPgetObjVardata( 208 SCIP* scip, /**< SCIP data structure */ 209 SCIP_VAR* var /**< problem variable */ 210 ) 211 { 212 SCIP_VARDATA* vardata; 213 214 vardata = SCIPvarGetData(var); 215 assert(vardata != NULL); 216 217 return vardata->objvardata; 218 } 219 220