Scippy

SCIP

Solving Constraint Integer Programs

objheur.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-2022 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 scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file objheur.cpp
17  * @brief C++ wrapper for primal heuristics
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 "objheur.h"
26 
27 
28 
29 
30 /*
31  * Data structures
32  */
33 
34 /** primal heuristic data */
35 struct SCIP_HeurData
36 {
37  scip::ObjHeur* objheur; /**< primal heuristic object */
38  SCIP_Bool deleteobject; /**< should the primal heuristic object be deleted when heuristic is freed? */
39 };
40 
41 
42 
43 
44 /*
45  * Callback methods of primal heuristic
46  */
47 
48 extern "C"
49 {
50 
51 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
52 static
53 SCIP_DECL_HEURCOPY(heurCopyObj)
54 { /*lint --e{715}*/
55  SCIP_HEURDATA* heurdata;
56 
57  assert(scip != NULL);
58 
59  heurdata = SCIPheurGetData(heur);
60  assert(heurdata != NULL);
61  assert(heurdata->objheur != NULL);
62  assert(heurdata->objheur->scip_ != scip);
63 
64  if( heurdata->objheur->iscloneable() )
65  {
66  scip::ObjHeur* newobjheur;
67  newobjheur = dynamic_cast<scip::ObjHeur*> (heurdata->objheur->clone(scip));
68 
69  /* call include method of primal heuristic object */
70  SCIP_CALL( SCIPincludeObjHeur(scip, newobjheur, TRUE) );
71  }
72 
73  return SCIP_OKAY;
74 }
75 
76 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
77 static
78 SCIP_DECL_HEURFREE(heurFreeObj)
79 { /*lint --e{715}*/
80  SCIP_HEURDATA* heurdata;
81 
82  heurdata = SCIPheurGetData(heur);
83  assert(heurdata != NULL);
84  assert(heurdata->objheur != NULL);
85  assert(heurdata->objheur->scip_ == scip);
86 
87  /* call virtual method of heur object */
88  SCIP_CALL( heurdata->objheur->scip_free(scip, heur) );
89 
90  /* free heur object */
91  if( heurdata->deleteobject )
92  delete heurdata->objheur;
93 
94  /* free heur data */
95  delete heurdata;
96  SCIPheurSetData(heur, NULL); /*lint !e64*/
97 
98  return SCIP_OKAY;
99 }
100 
101 
102 /** initialization method of primal heuristic (called after problem was transformed) */
103 static
104 SCIP_DECL_HEURINIT(heurInitObj)
105 { /*lint --e{715}*/
106  SCIP_HEURDATA* heurdata;
107 
108  heurdata = SCIPheurGetData(heur);
109  assert(heurdata != NULL);
110  assert(heurdata->objheur != NULL);
111  assert(heurdata->objheur->scip_ == scip);
112 
113  /* call virtual method of heur object */
114  SCIP_CALL( heurdata->objheur->scip_init(scip, heur) );
115 
116  return SCIP_OKAY;
117 }
118 
119 
120 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
121 static
122 SCIP_DECL_HEUREXIT(heurExitObj)
123 { /*lint --e{715}*/
124  SCIP_HEURDATA* heurdata;
125 
126  heurdata = SCIPheurGetData(heur);
127  assert(heurdata != NULL);
128  assert(heurdata->objheur != NULL);
129 
130  /* call virtual method of heur object */
131  SCIP_CALL( heurdata->objheur->scip_exit(scip, heur) );
132 
133  return SCIP_OKAY;
134 }
135 
136 
137 /** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
138 static
139 SCIP_DECL_HEURINITSOL(heurInitsolObj)
140 { /*lint --e{715}*/
141  SCIP_HEURDATA* heurdata;
142 
143  heurdata = SCIPheurGetData(heur);
144  assert(heurdata != NULL);
145  assert(heurdata->objheur != NULL);
146 
147  /* call virtual method of heur object */
148  SCIP_CALL( heurdata->objheur->scip_initsol(scip, heur) );
149 
150  return SCIP_OKAY;
151 }
152 
153 
154 /** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
155 static
156 SCIP_DECL_HEUREXITSOL(heurExitsolObj)
157 { /*lint --e{715}*/
158  SCIP_HEURDATA* heurdata;
159 
160  heurdata = SCIPheurGetData(heur);
161  assert(heurdata != NULL);
162  assert(heurdata->objheur != NULL);
163 
164  /* call virtual method of heur object */
165  SCIP_CALL( heurdata->objheur->scip_exitsol(scip, heur) );
166 
167  return SCIP_OKAY;
168 }
169 
170 
171 /** execution method of primal heuristic */
172 static
173 SCIP_DECL_HEUREXEC(heurExecObj)
174 { /*lint --e{715}*/
175  SCIP_HEURDATA* heurdata;
176 
177  heurdata = SCIPheurGetData(heur);
178  assert(heurdata != NULL);
179  assert(heurdata->objheur != NULL);
180 
181  /* call virtual method of heur object */
182  SCIP_CALL( heurdata->objheur->scip_exec(scip, heur, heurtiming, nodeinfeasible, result) );
183 
184  return SCIP_OKAY;
185 }
186 }
187 
188 
189 
190 /*
191  * primal heuristic specific interface methods
192  */
193 
194 /** creates the primal heuristic for the given primal heuristic object and includes it in SCIP */
196  SCIP* scip, /**< SCIP data structure */
197  scip::ObjHeur* objheur, /**< primal heuristic object */
198  SCIP_Bool deleteobject /**< should the primal heuristic object be deleted when heuristic is freed? */
199  )
200 {
201  SCIP_HEURDATA* heurdata;
202 
203  assert(scip != NULL);
204  assert(objheur != NULL);
205 
206  /* create primal heuristic data */
207  heurdata = new SCIP_HEURDATA;
208  heurdata->objheur = objheur;
209  heurdata->deleteobject = deleteobject;
210 
211  /* include primal heuristic */
212  SCIP_CALL( SCIPincludeHeur(scip, objheur->scip_name_, objheur->scip_desc_, objheur->scip_dispchar_,
213  objheur->scip_priority_, objheur->scip_freq_, objheur->scip_freqofs_, objheur->scip_maxdepth_,
214  objheur->scip_timingmask_, objheur->scip_usessubscip_,
215  heurCopyObj,
216  heurFreeObj, heurInitObj, heurExitObj,
217  heurInitsolObj, heurExitsolObj, heurExecObj,
218  heurdata) ); /*lint !e429*/
219 
220  return SCIP_OKAY; /*lint !e429*/
221 }
222 
223 /** returns the heur object of the given name, or 0 if not existing */
225  SCIP* scip, /**< SCIP data structure */
226  const char* name /**< name of primal heuristic */
227  )
228 {
229  SCIP_HEUR* heur;
230  SCIP_HEURDATA* heurdata;
231 
232  heur = SCIPfindHeur(scip, name);
233  if( heur == NULL )
234  return 0;
235 
236  heurdata = SCIPheurGetData(heur);
237  assert(heurdata != NULL);
238 
239  return heurdata->objheur;
240 }
241 
242 /** returns the heur object for the given primal heuristic */
244  SCIP* scip, /**< SCIP data structure */
245  SCIP_HEUR* heur /**< primal heuristic */
246  )
247 {
248  SCIP_HEURDATA* heurdata;
249 
250  assert(scip != NULL);
251  heurdata = SCIPheurGetData(heur);
252  assert(heurdata != NULL);
253 
254  return heurdata->objheur;
255 }
const int scip_priority_
Definition: objheur.h:61
static SCIP_DECL_HEURINIT(heurInitObj)
Definition: objheur.cpp:104
static SCIP_DECL_HEUREXEC(heurExecObj)
Definition: objheur.cpp:173
C++ wrapper for primal heuristics.
Definition: objheur.h:43
static SCIP_DECL_HEURFREE(heurFreeObj)
Definition: objheur.cpp:78
static SCIP_DECL_HEURCOPY(heurCopyObj)
Definition: objheur.cpp:53
#define TRUE
Definition: def.h:86
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_RETCODE SCIPincludeHeur(SCIP *scip, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEURCOPY((*heurcopy)), SCIP_DECL_HEURFREE((*heurfree)), SCIP_DECL_HEURINIT((*heurinit)), SCIP_DECL_HEUREXIT((*heurexit)), SCIP_DECL_HEURINITSOL((*heurinitsol)), SCIP_DECL_HEUREXITSOL((*heurexitsol)), SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip_heur.c:58
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:67
const SCIP_Bool scip_usessubscip_
Definition: objheur.h:76
const int scip_freqofs_
Definition: objheur.h:67
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1362
static SCIP_DECL_HEUREXIT(heurExitObj)
Definition: objheur.cpp:122
static SCIP_DECL_HEURINITSOL(heurInitsolObj)
Definition: objheur.cpp:139
SCIP_RETCODE SCIPincludeObjHeur(SCIP *scip, scip::ObjHeur *objheur, SCIP_Bool deleteobject)
Definition: objheur.cpp:195
const int scip_freq_
Definition: objheur.h:64
SCIP_HEUR * SCIPfindHeur(SCIP *scip, const char *name)
Definition: scip_heur.c:249
static SCIP_DECL_HEUREXITSOL(heurExitsolObj)
Definition: objheur.cpp:156
const int scip_maxdepth_
Definition: objheur.h:70
#define NULL
Definition: lpi_spx1.cpp:155
#define SCIP_CALL(x)
Definition: def.h:384
scip::ObjHeur * SCIPgetObjHeur(SCIP *scip, SCIP_HEUR *heur)
Definition: objheur.cpp:243
#define SCIP_Bool
Definition: def.h:84
C++ wrapper for primal heuristics.
const char scip_dispchar_
Definition: objheur.h:58
char * scip_desc_
Definition: objheur.h:55
char * scip_name_
Definition: objheur.h:52
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1352
scip::ObjHeur * SCIPfindObjHeur(SCIP *scip, const char *name)
Definition: objheur.cpp:224
const SCIP_HEURTIMING scip_timingmask_
Definition: objheur.h:73