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