# SCIP

Solving Constraint Integer Programs

heur_trysol.c
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 /* */
7 /* fuer Informationstechnik Berlin */
8 /* */
10 /* */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15
16 /**@file heur_trysol.c
17  * @brief primal heuristic that tries a given solution
18  * @author Marc Pfetsch
19  *
20  * This heuristic takes a solution from somewhere else via the function SCIPheurPassSolTrySol(). It
21  * then tries to commit this solution. It is mainly used by cons_indicator, which tries to correct a
22  * given solution, but cannot directly submit this solution, because it is a constraint handler and
23  * not a heuristic.
24  */
25
26 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
27
28 #include <assert.h>
29 #include <string.h>
30
31 #include "scip/heur_trysol.h"
32
33
34 #define HEUR_NAME "trysol"
35 #define HEUR_DESC "try solution heuristic"
36 #define HEUR_DISPCHAR 'y'
37 #define HEUR_PRIORITY -3000000 /* should process after all other heuristics */
38 #define HEUR_FREQ 1
39 #define HEUR_FREQOFS 0
40 #define HEUR_MAXDEPTH -1
41 #define HEUR_TIMING SCIP_HEURTIMING_DURINGLPLOOP | SCIP_HEURTIMING_BEFOREPRESOL | SCIP_HEURTIMING_BEFORENODE
42 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
43
44
45 /*
46  * Data structures
47  */
48
49
50 /** primal heuristic data */
51 struct SCIP_HeurData
52 {
53  SCIP_SOL* trysol; /**< storing solution passed to heuristic which has to tried (NULL if none) */
54  SCIP_SOL* addsol; /**< storing solution passed to heuristic which can be added without checking (NULL if none) */
55  SCIP_Bool rec; /**< whether we are within our own call */
56 };
57
58
59 /*
60  * Callback methods of primal heuristic
61  */
62
63 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
64 static
65 SCIP_DECL_HEURCOPY(heurCopyTrySol)
66 { /*lint --e{715}*/
67  assert(scip != NULL);
68  assert(heur != NULL);
69  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
70
71  /* call inclusion method of primal heuristic */
73
74  return SCIP_OKAY;
75 }
76
77 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
78 static
79 SCIP_DECL_HEURFREE(heurFreeTrySol)
80 { /*lint --e{715}*/
81  SCIP_HEURDATA* heurdata;
82
83  assert( heur != NULL );
84  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
85  assert( scip != NULL );
86
87  SCIPdebugMsg(scip, "free method of trysol primal heuristic.\n");
88
89  /* get heuristic data */
90  heurdata = SCIPheurGetData(heur);
91  assert(heurdata != NULL);
92
93  SCIPfreeBlockMemory(scip, &heurdata);
94
95  return SCIP_OKAY;
96 }
97
98
99 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
100 static
101 SCIP_DECL_HEUREXITSOL(heurExitTrySol)
102 { /*lint --e{715}*/
103  SCIP_HEURDATA* heurdata;
104
105  assert( heur != NULL );
106  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
107  assert( scip != NULL );
108
109  SCIPdebugMsg(scip, "exit method of trysol primal heuristic.\n");
110
111  /* get heuristic data */
112  heurdata = SCIPheurGetData(heur);
113  assert(heurdata != NULL);
114
115  /* free solution if one is still present */
116  if( heurdata->trysol != NULL )
117  SCIP_CALL( SCIPfreeSol(scip, &heurdata->trysol) );
118  assert( heurdata->trysol == NULL );
119
120  /* free solution if one is still present */
121  if( heurdata->addsol != NULL )
123  assert( heurdata->trysol == NULL );
124
125  return SCIP_OKAY;
126 }
127
128
129 /** execution method of primal heuristic */
130 static
131 SCIP_DECL_HEUREXEC(heurExecTrySol)
132 { /*lint --e{715}*/
133  SCIP_HEURDATA* heurdata;
134  SCIP_Bool stored;
135 #ifdef SCIP_DEBUG
136  SCIP_Real obj;
137 #endif
138
139  assert( heur != NULL );
140  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
141  assert( scip != NULL );
142  assert( result != NULL );
143
144  *result = SCIP_DIDNOTRUN;
145
146  /* get heuristic data */
147  heurdata = SCIPheurGetData(heur);
148  assert(heurdata != NULL);
149
150  /* only run if solution present */
151  if( heurdata->addsol == NULL && heurdata->trysol == NULL )
152  return SCIP_OKAY;
153
154  SCIPdebugMsg(scip, "exec method of trysol primal heuristic.\n");
155  *result = SCIP_DIDNOTFIND;
156  heurdata->rec = TRUE;
157
158  if( heurdata->trysol != NULL )
159  {
160  /* try solution and free it - check everything, because we are not sure */
161 #ifdef SCIP_DEBUG
162  obj = SCIPgetSolOrigObj(scip, heurdata->trysol);
163 #endif
164
165  SCIP_CALL( SCIPtrySolFree(scip, &heurdata->trysol, FALSE, FALSE, TRUE, TRUE, TRUE, &stored) );
166
167  if( stored )
168  {
169 #ifdef SCIP_DEBUG
170  SCIPdebugMsg(scip, "Found feasible solution of value %g.\n", obj);
171 #endif
172  *result = SCIP_FOUNDSOL;
173  }
174  }
175
176  if( heurdata->addsol != NULL )
177  {
178 #ifdef SCIP_DEBUG
180 #endif
181
183
184  if( stored )
185  {
186 #ifdef SCIP_DEBUG
187  SCIPdebugMsg(scip, "Found feasible solution of value %g.\n", obj);
188 #endif
189  *result = SCIP_FOUNDSOL;
190  }
191  }
192
193  assert( heurdata->trysol == NULL );
194  assert( heurdata->addsol == NULL );
195
196  heurdata->rec = FALSE;
197
198  return SCIP_OKAY;
199 }
200
201 /*
202  * primal heuristic specific interface methods
203  */
204
205 /** creates the trysol primal heuristic and includes it in SCIP */
207  SCIP* scip /**< SCIP data structure */
208  )
209 {
210  SCIP_HEURDATA* heurdata;
211  SCIP_HEUR* heur;
212
213  /* create heuristic data */
214  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
215  heurdata->trysol = NULL;
217  heurdata->rec = FALSE;
218
219  /* include primal heuristic */
220  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
222  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecTrySol, heurdata) );
223
224  assert(heur != NULL);
225
226  /* set non-NULL pointers to callback methods */
227  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyTrySol) );
228  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeTrySol) );
229  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitTrySol) );
230
231  return SCIP_OKAY;
232 }
233
234
235 /** pass solution to trysol heuristic */
237  SCIP* scip, /**< SCIP data structure */
238  SCIP_HEUR* heur, /**< trysol heuristic */
239  SCIP_SOL* sol /**< solution to be passed */
240  )
241 {
242  SCIP_HEURDATA* heurdata;
243
244  assert( scip != NULL );
245  assert( heur != NULL );
246  assert( sol != NULL );
247  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
248
249  /* get heuristic data */
250  heurdata = SCIPheurGetData(heur);
251  assert(heurdata != NULL);
252
253  /* only store solution if we are not within our own SCIPtrySol() call */
254  if( ! heurdata->rec )
255  {
256  if( heurdata->trysol == NULL || (SCIPgetObjsense(scip) == SCIP_OBJSENSE_MAXIMIZE &&
257  SCIPisGT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->trysol))) ||
258  SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->trysol)) )
259  {
260  if( heurdata->trysol != NULL )
261  {
262  /* free previous solution */
263  SCIP_CALL( SCIPfreeSol(scip, &heurdata->trysol) );
264  }
265
266  SCIPdebugMsg(scip, "Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
267  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->trysol, sol) );
269  SCIPsolSetHeur(heurdata->trysol, heur);
270  }
271  }
272
273  return SCIP_OKAY;
274 }
275
276 /** pass solution to trysol heuristic which just gets added (without checking feasibility */
278  SCIP* scip, /**< SCIP data structure */
279  SCIP_HEUR* heur, /**< trysol heuristic */
280  SCIP_SOL* sol /**< solution to be passed */
281  )
282 {
283  SCIP_HEURDATA* heurdata;
284
285  assert( scip != NULL );
286  assert( heur != NULL );
287  assert( sol != NULL );
288  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
289
290  /* get heuristic data */
291  heurdata = SCIPheurGetData(heur);
292  assert(heurdata != NULL);
293
294  /* only store solution if we are not within our own SCIPtrySol() call */
295  if( ! heurdata->rec )
296  {
297  if( heurdata->addsol == NULL || (SCIPgetObjsense(scip) == SCIP_OBJSENSE_MAXIMIZE &&
298  SCIPisGT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->addsol))) ||
299  SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->addsol)) )
300  {
301  if( heurdata->addsol != NULL )
302  {
303  /* free previous solution */
305  }
306
307  SCIPdebugMsg(scip, "Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
308  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->addsol, sol) );
311  }
312  }
313
314  return SCIP_OKAY;
315 }
#define HEUR_FREQ
Definition: heur_trysol.c:38
primal heuristic that tries a given solution
#define HEUR_PRIORITY
Definition: heur_trysol.c:37
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition: scip.c:39639
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:8092
#define HEUR_DESC
Definition: heur_trysol.c:35
#define FALSE
Definition: def.h:64
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:21907
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:7999
SCIP_RETCODE SCIPheurPassSolAddSol(SCIP *scip, SCIP_HEUR *heur, SCIP_SOL *sol)
Definition: heur_trysol.c:277
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:21890
#define SCIPdebugMsg
Definition: scip.h:451
SCIP_RETCODE SCIPheurPassSolTrySol(SCIP *scip, SCIP_HEUR *heur, SCIP_SOL *sol)
Definition: heur_trysol.c:236
SCIP_RETCODE SCIPincludeHeurTrySol(SCIP *scip)
Definition: heur_trysol.c:206
#define HEUR_FREQOFS
Definition: heur_trysol.c:39
SCIP_RETCODE SCIPcreateSolCopy(SCIP *scip, SCIP_SOL **sol, SCIP_SOL *sourcesol)
Definition: scip.c:37295
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
static SCIP_DECL_HEURFREE(heurFreeTrySol)
Definition: heur_trysol.c:79
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8060
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:45764
#define NULL
Definition: lpi_spx1.cpp:137
#define SCIP_CALL(x)
Definition: def.h:306
#define SCIP_Bool
Definition: def.h:61
static SCIP_DECL_HEUREXEC(heurExecTrySol)
Definition: heur_trysol.c:131
#define HEUR_DISPCHAR
Definition: heur_trysol.c:36
void SCIPsolSetHeur(SCIP_SOL *sol, SCIP_HEUR *heur)
Definition: sol.c:2423
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip.c:39843
#define HEUR_TIMING
Definition: heur_trysol.c:41
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:37631
static SCIP_DECL_HEURCOPY(heurCopyTrySol)
Definition: heur_trysol.c:65
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:38090
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:45790
static SCIP_DECL_HEUREXITSOL(heurExitTrySol)
Definition: heur_trysol.c:101
#define SCIP_Real
Definition: def.h:135