Scippy

SCIP

Solving Constraint Integer Programs

objrelax.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 2002-2022 Zuse Institute Berlin */
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 objrelax.cpp
26  * @brief C++ wrapper for relaxators
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 "objrelax.h"
35 
36 
37 
38 
39 /*
40  * Data structures
41  */
42 
43 /** relaxator data */
44 struct SCIP_RelaxData
45 {
46  scip::ObjRelax* objrelax; /**< relaxator object */
47  SCIP_Bool deleteobject; /**< should the relaxator object be deleted when relaxator is freed? */
48 };
49 
50 
51 
52 
53 /*
54  * Callback methods of relaxator
55  */
56 
57 extern "C"
58 {
59 
60 /** copy method for relaxator plugins (called when SCIP copies plugins) */
61 static
62 SCIP_DECL_RELAXCOPY(relaxCopyObj)
63 { /*lint --e{715}*/
64  SCIP_RELAXDATA* relaxdata;
65 
66  assert(scip != NULL);
67 
68  relaxdata = SCIPrelaxGetData(relax);
69  assert(relaxdata != NULL);
70  assert(relaxdata->objrelax != NULL);
71  assert(relaxdata->objrelax->scip_ != scip);
72 
73  if( relaxdata->objrelax->iscloneable() )
74  {
75  scip::ObjRelax* newobjrelax;
76  newobjrelax = dynamic_cast<scip::ObjRelax*> (relaxdata->objrelax->clone(scip));
77 
78  /* call include method of relaxator object */
79  SCIP_CALL( SCIPincludeObjRelax(scip, newobjrelax, TRUE) );
80  }
81 
82  return SCIP_OKAY;
83 }
84 
85 /** destructor of relaxator to free user data (called when SCIP is exiting) */
86 static
87 SCIP_DECL_RELAXFREE(relaxFreeObj)
88 { /*lint --e{715}*/
89  SCIP_RELAXDATA* relaxdata;
90 
91  relaxdata = SCIPrelaxGetData(relax);
92  assert(relaxdata != NULL);
93  assert(relaxdata->objrelax != NULL);
94  assert(relaxdata->objrelax->scip_ == scip);
95 
96  /* call virtual method of relax object */
97  SCIP_CALL( relaxdata->objrelax->scip_free(scip, relax) );
98 
99  /* free relax object */
100  if( relaxdata->deleteobject )
101  delete relaxdata->objrelax;
102 
103  /* free relax data */
104  delete relaxdata;
105  SCIPrelaxSetData(relax, NULL); /*lint !e64*/
106 
107  return SCIP_OKAY;
108 }
109 
110 
111 /** initialization method of relaxator (called after problem was transformed) */
112 static
113 SCIP_DECL_RELAXINIT(relaxInitObj)
114 { /*lint --e{715}*/
115  SCIP_RELAXDATA* relaxdata;
116 
117  relaxdata = SCIPrelaxGetData(relax);
118  assert(relaxdata != NULL);
119  assert(relaxdata->objrelax != NULL);
120  assert(relaxdata->objrelax->scip_ == scip);
121 
122  /* call virtual method of relax object */
123  SCIP_CALL( relaxdata->objrelax->scip_init(scip, relax) );
124 
125  return SCIP_OKAY;
126 }
127 
128 
129 /** deinitialization method of relaxator (called before transformed problem is freed) */
130 static
131 SCIP_DECL_RELAXEXIT(relaxExitObj)
132 { /*lint --e{715}*/
133  SCIP_RELAXDATA* relaxdata;
134 
135  relaxdata = SCIPrelaxGetData(relax);
136  assert(relaxdata != NULL);
137  assert(relaxdata->objrelax != NULL);
138 
139  /* call virtual method of relax object */
140  SCIP_CALL( relaxdata->objrelax->scip_exit(scip, relax) );
141 
142  return SCIP_OKAY;
143 }
144 
145 
146 /** solving process initialization method of relaxator (called when branch and bound process is about to begin) */
147 static
148 SCIP_DECL_RELAXINITSOL(relaxInitsolObj)
149 { /*lint --e{715}*/
150  SCIP_RELAXDATA* relaxdata;
151 
152  relaxdata = SCIPrelaxGetData(relax);
153  assert(relaxdata != NULL);
154  assert(relaxdata->objrelax != NULL);
155 
156  /* call virtual method of relax object */
157  SCIP_CALL( relaxdata->objrelax->scip_initsol(scip, relax) );
158 
159  return SCIP_OKAY;
160 }
161 
162 
163 /** solving process deinitialization method of relaxator (called before branch and bound process data is freed) */
164 static
165 SCIP_DECL_RELAXEXITSOL(relaxExitsolObj)
166 { /*lint --e{715}*/
167  SCIP_RELAXDATA* relaxdata;
168 
169  relaxdata = SCIPrelaxGetData(relax);
170  assert(relaxdata != NULL);
171  assert(relaxdata->objrelax != NULL);
172 
173  /* call virtual method of relax object */
174  SCIP_CALL( relaxdata->objrelax->scip_exitsol(scip, relax) );
175 
176  return SCIP_OKAY;
177 }
178 
179 
180 /** execution method of relaxator */
181 static
182 SCIP_DECL_RELAXEXEC(relaxExecObj)
183 { /*lint --e{715}*/
184  SCIP_RELAXDATA* relaxdata;
185 
186  relaxdata = SCIPrelaxGetData(relax);
187  assert(relaxdata != NULL);
188  assert(relaxdata->objrelax != NULL);
189 
190  /* call virtual method of relax object */
191  SCIP_CALL( relaxdata->objrelax->scip_exec(scip, relax, lowerbound, result) );
192 
193  return SCIP_OKAY;
194 }
195 }
196 
197 
198 
199 /*
200  * relaxator specific interface methods
201  */
202 
203 /** creates the relaxator for the given relaxator object and includes it in SCIP */
205  SCIP* scip, /**< SCIP data structure */
206  scip::ObjRelax* objrelax, /**< relaxator object */
207  SCIP_Bool deleteobject /**< should the relaxator object be deleted when relaxator is freed? */
208  )
209 {
210  SCIP_RELAXDATA* relaxdata;
211 
212  assert(scip != NULL);
213  assert(objrelax != NULL);
214 
215  /* create relaxator data */
216  relaxdata = new SCIP_RELAXDATA;
217  relaxdata->objrelax = objrelax;
218  relaxdata->deleteobject = deleteobject;
219 
220  /* include relaxator */
221  SCIP_CALL( SCIPincludeRelax(scip, objrelax->scip_name_, objrelax->scip_desc_,
222  objrelax->scip_priority_, objrelax->scip_freq_, relaxCopyObj,
223  relaxFreeObj, relaxInitObj, relaxExitObj,
224  relaxInitsolObj, relaxExitsolObj, relaxExecObj,
225  relaxdata) ); /*lint !e429*/
226 
227  return SCIP_OKAY; /*lint !e429*/
228 }
229 
230 /** returns the relax object of the given name, or 0 if not existing */
232  SCIP* scip, /**< SCIP data structure */
233  const char* name /**< name of relaxator */
234  )
235 {
236  SCIP_RELAX* relax;
237  SCIP_RELAXDATA* relaxdata;
238 
239  relax = SCIPfindRelax(scip, name);
240  if( relax == NULL )
241  return 0;
242 
243  relaxdata = SCIPrelaxGetData(relax);
244  assert(relaxdata != NULL);
245 
246  return relaxdata->objrelax;
247 }
248 
249 /** returns the relax object for the given relaxator */
251  SCIP* scip, /**< SCIP data structure */
252  SCIP_RELAX* relax /**< relaxator */
253  )
254 {
255  SCIP_RELAXDATA* relaxdata;
256 
257  assert(scip != NULL);
258  relaxdata = SCIPrelaxGetData(relax);
259  assert(relaxdata != NULL);
260 
261  return relaxdata->objrelax;
262 }
static SCIP_DECL_RELAXEXEC(relaxExecObj)
Definition: objrelax.cpp:182
#define TRUE
Definition: def.h:95
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
SCIP_RETCODE SCIPincludeRelax(SCIP *scip, const char *name, const char *desc, int priority, int freq, SCIP_DECL_RELAXCOPY((*relaxcopy)), SCIP_DECL_RELAXFREE((*relaxfree)), SCIP_DECL_RELAXINIT((*relaxinit)), SCIP_DECL_RELAXEXIT((*relaxexit)), SCIP_DECL_RELAXINITSOL((*relaxinitsol)), SCIP_DECL_RELAXEXITSOL((*relaxexitsol)), SCIP_DECL_RELAXEXEC((*relaxexec)), SCIP_RELAXDATA *relaxdata)
Definition: scip_relax.c:61
static SCIP_DECL_RELAXCOPY(relaxCopyObj)
Definition: objrelax.cpp:62
static SCIP_DECL_RELAXFREE(relaxFreeObj)
Definition: objrelax.cpp:87
scip::ObjRelax * SCIPfindObjRelax(SCIP *scip, const char *name)
Definition: objrelax.cpp:231
#define NULL
Definition: lpi_spx1.cpp:164
SCIP_RELAXDATA * SCIPrelaxGetData(SCIP_RELAX *relax)
Definition: relax.c:455
#define SCIP_CALL(x)
Definition: def.h:393
char * scip_desc_
Definition: objrelax.h:64
SCIP_RETCODE SCIPincludeObjRelax(SCIP *scip, scip::ObjRelax *objrelax, SCIP_Bool deleteobject)
Definition: objrelax.cpp:204
#define SCIP_Bool
Definition: def.h:93
static SCIP_DECL_RELAXINIT(relaxInitObj)
Definition: objrelax.cpp:113
void SCIPrelaxSetData(SCIP_RELAX *relax, SCIP_RELAXDATA *relaxdata)
Definition: relax.c:465
static SCIP_DECL_RELAXINITSOL(relaxInitsolObj)
Definition: objrelax.cpp:148
char * scip_name_
Definition: objrelax.h:61
static SCIP_DECL_RELAXEXITSOL(relaxExitsolObj)
Definition: objrelax.cpp:165
struct SCIP_RelaxData SCIP_RELAXDATA
Definition: type_relax.h:47
scip::ObjRelax * SCIPgetObjRelax(SCIP *scip, SCIP_RELAX *relax)
Definition: objrelax.cpp:250
const int scip_freq_
Definition: objrelax.h:70
const int scip_priority_
Definition: objrelax.h:67
C++ wrapper for relaxation handlers.
Definition: objrelax.h:52
static SCIP_DECL_RELAXEXIT(relaxExitObj)
Definition: objrelax.cpp:131
C++ wrapper for relaxation handlers.
SCIP_RELAX * SCIPfindRelax(SCIP *scip, const char *name)
Definition: scip_relax.c:234