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