Scippy

SCIP

Solving Constraint Integer Programs

objprobdata.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-2018 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 visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file objprobdata.cpp
17  * @brief C++ wrapper for user problem 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 "objprobdata.h"
26 
27 
28 
29 
30 /*
31  * Data structures
32  */
33 
34 /** user problem data */
35 struct SCIP_ProbData
36 {
37  scip::ObjProbData* objprobdata; /**< user problem data object */
38  SCIP_Bool deleteobject; /**< should the user problem data object be deleted when problem is freed? */
39 };
40 
41 
42 
43 
44 /*
45  * Callback methods of user problem data
46  */
47 
48 extern "C"
49 {
50 
51 /** frees user data of original problem (called when the original problem is freed) */
52 static
53 SCIP_DECL_PROBDELORIG(probDelorigObj)
54 { /*lint --e{715}*/
55  assert(probdata != NULL);
56  assert(*probdata != NULL);
57  assert((*probdata)->objprobdata != NULL);
58 
59  /* call virtual method of probdata object */
60  SCIP_CALL( (*probdata)->objprobdata->scip_delorig(scip) );
61 
62  /* free probdata object */
63  if( (*probdata)->deleteobject )
64  delete (*probdata)->objprobdata;
65 
66  /* free probdata data */
67  delete *probdata;
68  *probdata = 0; /*lint !e64*/
69 
70  return SCIP_OKAY;
71 }
72 
73 
74 /** creates user data of transformed problem by transforming the original user problem data
75  * (called after problem was transformed)
76  */
77 static
78 SCIP_DECL_PROBTRANS(probTransObj)
79 { /*lint --e{715}*/
80  scip::ObjProbData* objprobdata; /*lint !e78 !e40 !e55 !e530 !e522*/
81  SCIP_Bool deleteobject;
82 
83  assert(sourcedata != NULL);
84  assert(sourcedata->objprobdata != NULL);
85  assert(targetdata != NULL);
86  assert(*targetdata == NULL);
87 
88  /* call virtual method of probdata object */
89  SCIP_CALL( sourcedata->objprobdata->scip_trans(scip, &objprobdata, &deleteobject) ); /*lint !e40*/
90 
91  /* create transformed user problem data */
92  *targetdata = new SCIP_PROBDATA;
93  (*targetdata)->objprobdata = objprobdata; /*lint !e40*/
94  (*targetdata)->deleteobject = deleteobject;
95 
96  return SCIP_OKAY;
97 }
98 
99 
100 /** frees user data of transformed problem (called when the transformed problem is freed) */
101 static
102 SCIP_DECL_PROBDELTRANS(probDeltransObj)
103 { /*lint --e{715}*/
104  assert(probdata != NULL);
105  assert(*probdata != NULL);
106  assert((*probdata)->objprobdata != NULL);
107 
108  /* call virtual method of probdata object */
109  SCIP_CALL( (*probdata)->objprobdata->scip_deltrans(scip) );
110 
111  /* free probdata object */
112  if( (*probdata)->deleteobject )
113  delete (*probdata)->objprobdata;
114 
115  /* free probdata data */
116  delete *probdata;
117  *probdata = 0; /*lint !e64*/
118 
119  return SCIP_OKAY;
120 }
121 
122 
123 /** solving process initialization method of transformed data (called before the branch and bound process begins) */
124 static
125 SCIP_DECL_PROBINITSOL(probInitsolObj)
126 { /*lint --e{715}*/
127  assert(probdata != NULL);
128  assert(probdata->objprobdata != NULL);
129 
130  /* call virtual method of probdata object */
131  SCIP_CALL( probdata->objprobdata->scip_initsol(scip) );
132 
133  return SCIP_OKAY;
134 }
135 
136 
137 /** solving process deinitialization method of transformed data (called before the branch and bound data is freed) */
138 static
139 SCIP_DECL_PROBEXITSOL(probExitsolObj)
140 { /*lint --e{715}*/
141  assert(probdata != NULL);
142  assert(probdata->objprobdata != NULL);
143 
144  /* call virtual method of probdata object */
145  SCIP_CALL( probdata->objprobdata->scip_exitsol(scip, restart) );
146 
147  return SCIP_OKAY;
148 }
149 
150 /** copies user data if you want to copy it to a subscip */
151 static
152 SCIP_DECL_PROBCOPY(probCopyObj)
153 { /*lint --e{715}*/
154  scip::ObjProbData* objprobdata; /*lint !e78 !e40 !e55 !e530 !e522*/
155 
156  assert(sourcedata != NULL);
157  assert(sourcedata->objprobdata != NULL);
158  assert(targetdata != NULL);
159  assert(*targetdata == NULL);
160 
161  /* call virtual method of probdata object */
162  SCIP_CALL( sourcedata->objprobdata->scip_copy(scip, sourcescip, varmap, consmap, &objprobdata, global, result) ); /*lint !e40*/
163 
164  if( objprobdata != 0 )
165  {
166  assert(*result == SCIP_SUCCESS);
167 
168  /* create trarget user problem data */
169  *targetdata = new SCIP_PROBDATA;
170  (*targetdata)->objprobdata = objprobdata; /*lint !e40*/
171  (*targetdata)->deleteobject = TRUE; /* always delete object, because we created it */
172  }
173  else
174  {
175  assert(*result == SCIP_DIDNOTRUN);
176  *targetdata = 0;
177  }
178 
179  return SCIP_OKAY;
180 }
181 
182 }
183 
184 
185 
186 /*
187  * user problem data specific interface methods
188  */
189 
190 /** creates empty problem, initializes all solving data structures, and sets the user problem data to point to the
191  * given user data object
192  */
194  SCIP* scip, /**< SCIP data structure */
195  const char* name, /**< problem name */
196  scip::ObjProbData* objprobdata, /**< user problem data object */
197  SCIP_Bool deleteobject /**< should the user problem data object be deleted when problem is freed? */
198  )
199 {
200  SCIP_PROBDATA* probdata;
201 
202  /* create user problem data */
203  probdata = new SCIP_PROBDATA;
204  probdata->objprobdata = objprobdata;
205  probdata->deleteobject = deleteobject;
206 
207  /* create problem */
208  SCIP_CALL( SCIPcreateProb(scip, name, probDelorigObj, probTransObj, probDeltransObj,
209  probInitsolObj, probExitsolObj, probCopyObj, probdata) ); /*lint !e429*/
210 
211  return SCIP_OKAY; /*lint !e429*/
212 }
213 
214 /** gets user problem data object
215  * Warning! This method should only be called after a problem was created with SCIPcreateObjProb().
216  * Otherwise, a segmentation fault may arise, or an undefined pointer is returned.
217  */
219  SCIP* scip /**< SCIP data structure */
220  )
221 {
222  SCIP_PROBDATA* probdata;
223 
224  probdata = SCIPgetProbData(scip);
225  assert(probdata != NULL);
226 
227  return probdata->objprobdata;
228 }
229 
#define NULL
Definition: def.h:239
SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
Definition: scip_prob.c:162
static SCIP_DECL_PROBDELTRANS(probDeltransObj)
#define TRUE
Definition: def.h:64
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
C++ wrapper for user problem data.
Definition: objprobdata.h:43
static SCIP_DECL_PROBCOPY(probCopyObj)
static SCIP_DECL_PROBDELORIG(probDelorigObj)
Definition: objprobdata.cpp:53
#define SCIP_CALL(x)
Definition: def.h:351
#define SCIP_Bool
Definition: def.h:62
static SCIP_DECL_PROBEXITSOL(probExitsolObj)
scip::ObjProbData * SCIPgetObjProbData(SCIP *scip)
struct SCIP_ProbData SCIP_PROBDATA
Definition: type_prob.h:44
SCIP_PROBDATA * SCIPgetProbData(SCIP *scip)
Definition: scip_prob.c:1020
SCIP_RETCODE SCIPcreateObjProb(SCIP *scip, const char *name, scip::ObjProbData *objprobdata, SCIP_Bool deleteobject)
static SCIP_DECL_PROBINITSOL(probInitsolObj)
C++ wrapper for user problem data.
static SCIP_DECL_PROBTRANS(probTransObj)
Definition: objprobdata.cpp:78