Scippy

SCIP

Solving Constraint Integer Programs

objvardata.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* SCIP --- Solving Constraint Integer Programs */
5/* */
6/* Copyright (c) 2002-2024 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 */
44struct 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
57extern "C"
58{
59
60/** frees user data of original variable (called when the original variable is freed) */
61static
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 */
86static
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) */
110static
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 */
132static
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 */
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 */
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
C++ wrapper for user variable data.
Definition: objvardata.h:53
#define NULL
Definition: def.h:267
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:173
#define TRUE
Definition: def.h:93
#define SCIP_CALL(x)
Definition: def.h:374
SCIP_VARDATA * SCIPvarGetData(SCIP_VAR *var)
Definition: var.c:17439
SCIP_RETCODE SCIPcreateVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
Definition: scip_var.c:114
static SCIP_DECL_VARDELTRANS(varDeltransObj)
Definition: objvardata.cpp:111
static SCIP_DECL_VARCOPY(varCopyObj)
Definition: objvardata.cpp:133
SCIP_RETCODE SCIPcreateObjVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, scip::ObjVardata *objvardata, SCIP_Bool deleteobject)
Definition: objvardata.cpp:175
static SCIP_DECL_VARTRANS(varTransObj)
Definition: objvardata.cpp:87
scip::ObjVardata * SCIPgetObjVardata(SCIP *scip, SCIP_VAR *var)
Definition: objvardata.cpp:207
static SCIP_DECL_VARDELORIG(varDelorigObj)
Definition: objvardata.cpp:62
C++ wrapper for user variable data.
@ SCIP_DIDNOTRUN
Definition: type_result.h:42
@ SCIP_SUCCESS
Definition: type_result.h:58
@ SCIP_OKAY
Definition: type_retcode.h:42
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
struct SCIP_VarData SCIP_VARDATA
Definition: type_var.h:120
enum SCIP_Vartype SCIP_VARTYPE
Definition: type_var.h:73