Scippy

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 /* */
6 /* Copyright (C) 2002-2019 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 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 "scip/heur_trysol.h"
29 #include "scip/pub_heur.h"
30 #include "scip/pub_message.h"
31 #include "scip/pub_sol.h"
32 #include "scip/scip_heur.h"
33 #include "scip/scip_mem.h"
34 #include "scip/scip_message.h"
35 #include "scip/scip_numerics.h"
36 #include "scip/scip_prob.h"
37 #include "scip/scip_sol.h"
38 #include <string.h>
39 
40 #define HEUR_NAME "trysol"
41 #define HEUR_DESC "try solution heuristic"
42 #define HEUR_DISPCHAR 'y'
43 #define HEUR_PRIORITY -3000000 /* should process after all other heuristics */
44 #define HEUR_FREQ 1
45 #define HEUR_FREQOFS 0
46 #define HEUR_MAXDEPTH -1
47 #define HEUR_TIMING SCIP_HEURTIMING_DURINGLPLOOP | SCIP_HEURTIMING_BEFOREPRESOL | SCIP_HEURTIMING_BEFORENODE
48 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
49 
50 
51 /*
52  * Data structures
53  */
54 
55 
56 /** primal heuristic data */
57 struct SCIP_HeurData
58 {
59  SCIP_SOL* trysol; /**< storing solution passed to heuristic which has to tried (NULL if none) */
60  SCIP_SOL* addsol; /**< storing solution passed to heuristic which can be added without checking (NULL if none) */
61  SCIP_Bool rec; /**< whether we are within our own call */
62 };
63 
64 
65 /*
66  * Callback methods of primal heuristic
67  */
68 
69 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
70 static
71 SCIP_DECL_HEURCOPY(heurCopyTrySol)
72 { /*lint --e{715}*/
73  assert(scip != NULL);
74  assert(heur != NULL);
75  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
76 
77  /* call inclusion method of primal heuristic */
79 
80  return SCIP_OKAY;
81 }
82 
83 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
84 static
85 SCIP_DECL_HEURFREE(heurFreeTrySol)
86 { /*lint --e{715}*/
87  SCIP_HEURDATA* heurdata;
88 
89  assert( heur != NULL );
90  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
91  assert( scip != NULL );
92 
93  SCIPdebugMsg(scip, "free method of trysol primal heuristic.\n");
94 
95  /* get heuristic data */
96  heurdata = SCIPheurGetData(heur);
97  assert(heurdata != NULL);
98 
99  SCIPfreeBlockMemory(scip, &heurdata);
100 
101  return SCIP_OKAY;
102 }
103 
104 
105 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
106 static
107 SCIP_DECL_HEUREXITSOL(heurExitTrySol)
108 { /*lint --e{715}*/
109  SCIP_HEURDATA* heurdata;
110 
111  assert( heur != NULL );
112  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
113  assert( scip != NULL );
114 
115  SCIPdebugMsg(scip, "exit method of trysol primal heuristic.\n");
116 
117  /* get heuristic data */
118  heurdata = SCIPheurGetData(heur);
119  assert(heurdata != NULL);
120 
121  /* free solution if one is still present */
122  if( heurdata->trysol != NULL )
123  SCIP_CALL( SCIPfreeSol(scip, &heurdata->trysol) );
124  assert( heurdata->trysol == NULL );
125 
126  /* free solution if one is still present */
127  if( heurdata->addsol != NULL )
128  SCIP_CALL( SCIPfreeSol(scip, &heurdata->addsol) );
129  assert( heurdata->trysol == NULL );
130 
131  return SCIP_OKAY;
132 }
133 
134 
135 /** execution method of primal heuristic */
136 static
137 SCIP_DECL_HEUREXEC(heurExecTrySol)
138 { /*lint --e{715}*/
139  SCIP_HEURDATA* heurdata;
140  SCIP_Bool stored;
141 #ifdef SCIP_DEBUG
142  SCIP_Real obj;
143 #endif
144 
145  assert( heur != NULL );
146  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
147  assert( scip != NULL );
148  assert( result != NULL );
149 
150  *result = SCIP_DIDNOTRUN;
151 
152  /* get heuristic data */
153  heurdata = SCIPheurGetData(heur);
154  assert(heurdata != NULL);
155 
156  /* only run if solution present */
157  if( heurdata->addsol == NULL && heurdata->trysol == NULL )
158  return SCIP_OKAY;
159 
160  SCIPdebugMsg(scip, "exec method of trysol primal heuristic.\n");
161  *result = SCIP_DIDNOTFIND;
162  heurdata->rec = TRUE;
163 
164  if( heurdata->trysol != NULL )
165  {
166  /* try solution and free it - check everything, because we are not sure */
167 #ifdef SCIP_DEBUG
168  obj = SCIPgetSolOrigObj(scip, heurdata->trysol);
169 #endif
170 
171  SCIP_CALL( SCIPtrySolFree(scip, &heurdata->trysol, FALSE, FALSE, TRUE, TRUE, TRUE, &stored) );
172 
173  if( stored )
174  {
175 #ifdef SCIP_DEBUG
176  SCIPdebugMsg(scip, "Found feasible solution of value %g.\n", obj);
177 #endif
178  *result = SCIP_FOUNDSOL;
179  }
180  }
181 
182  if( heurdata->addsol != NULL )
183  {
184 #ifdef SCIP_DEBUG
185  obj = SCIPgetSolOrigObj(scip, heurdata->addsol);
186 #endif
187 
188  SCIP_CALL( SCIPaddSolFree(scip, &heurdata->addsol, &stored) );
189 
190  if( stored )
191  {
192 #ifdef SCIP_DEBUG
193  SCIPdebugMsg(scip, "Found feasible solution of value %g.\n", obj);
194 #endif
195  *result = SCIP_FOUNDSOL;
196  }
197  }
198 
199  assert( heurdata->trysol == NULL );
200  assert( heurdata->addsol == NULL );
201 
202  heurdata->rec = FALSE;
203 
204  return SCIP_OKAY;
205 }
206 
207 /*
208  * primal heuristic specific interface methods
209  */
210 
211 /** creates the trysol primal heuristic and includes it in SCIP */
213  SCIP* scip /**< SCIP data structure */
214  )
215 {
216  SCIP_HEURDATA* heurdata;
217  SCIP_HEUR* heur;
218 
219  /* create heuristic data */
220  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
221  heurdata->trysol = NULL;
222  heurdata->addsol = NULL;
223  heurdata->rec = FALSE;
224 
225  /* include primal heuristic */
226  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
228  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecTrySol, heurdata) );
229 
230  assert(heur != NULL);
231 
232  /* set non-NULL pointers to callback methods */
233  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyTrySol) );
234  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeTrySol) );
235  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitTrySol) );
236 
237  return SCIP_OKAY;
238 }
239 
240 
241 /** pass solution to trysol heuristic */
243  SCIP* scip, /**< SCIP data structure */
244  SCIP_HEUR* heur, /**< trysol heuristic */
245  SCIP_SOL* sol /**< solution to be passed */
246  )
247 {
248  SCIP_HEURDATA* heurdata;
249 
250  assert( scip != NULL );
251  assert( heur != NULL );
252  assert( sol != NULL );
253  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
254 
255  /* get heuristic data */
256  heurdata = SCIPheurGetData(heur);
257  assert(heurdata != NULL);
258 
259  /* only store solution if we are not within our own SCIPtrySol() call */
260  if( ! heurdata->rec )
261  {
262  if( heurdata->trysol == NULL || (SCIPgetObjsense(scip) == SCIP_OBJSENSE_MAXIMIZE &&
263  SCIPisGT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->trysol))) ||
264  SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->trysol)) )
265  {
266  if( heurdata->trysol != NULL )
267  {
268  /* free previous solution */
269  SCIP_CALL( SCIPfreeSol(scip, &heurdata->trysol) );
270  }
271 
272  SCIPdebugMsg(scip, "Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
273  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->trysol, sol) );
274  SCIP_CALL( SCIPunlinkSol(scip, heurdata->trysol) );
275  SCIPsolSetHeur(heurdata->trysol, heur);
276  }
277  }
278 
279  return SCIP_OKAY;
280 }
281 
282 /** pass solution to trysol heuristic which just gets added (without checking feasibility */
284  SCIP* scip, /**< SCIP data structure */
285  SCIP_HEUR* heur, /**< trysol heuristic */
286  SCIP_SOL* sol /**< solution to be passed */
287  )
288 {
289  SCIP_HEURDATA* heurdata;
290 
291  assert( scip != NULL );
292  assert( heur != NULL );
293  assert( sol != NULL );
294  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
295 
296  /* get heuristic data */
297  heurdata = SCIPheurGetData(heur);
298  assert(heurdata != NULL);
299 
300  /* only store solution if we are not within our own SCIPtrySol() call */
301  if( ! heurdata->rec )
302  {
303  if( heurdata->addsol == NULL || (SCIPgetObjsense(scip) == SCIP_OBJSENSE_MAXIMIZE &&
304  SCIPisGT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->addsol))) ||
305  SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->addsol)) )
306  {
307  if( heurdata->addsol != NULL )
308  {
309  /* free previous solution */
310  SCIP_CALL( SCIPfreeSol(scip, &heurdata->addsol) );
311  }
312 
313  SCIPdebugMsg(scip, "Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
314  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->addsol, sol) );
315  SCIP_CALL( SCIPunlinkSol(scip, heurdata->addsol) );
316  SCIPsolSetHeur(heurdata->addsol, heur);
317  }
318  }
319 
320  return SCIP_OKAY;
321 }
#define HEUR_FREQ
Definition: heur_trysol.c:44
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:976
#define NULL
Definition: def.h:253
SCIP_RETCODE SCIPheurPassSolTrySol(SCIP *scip, SCIP_HEUR *heur, SCIP_SOL *sol)
Definition: heur_trysol.c:242
primal heuristic that tries a given solution
#define HEUR_PRIORITY
Definition: heur_trysol.c:43
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1254
public methods for memory management
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1165
#define HEUR_DESC
Definition: heur_trysol.c:41
SCIP_RETCODE SCIPincludeHeurTrySol(SCIP *scip)
Definition: heur_trysol.c:212
#define FALSE
Definition: def.h:73
#define TRUE
Definition: def.h:72
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_mem.h:95
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
SCIP_RETCODE SCIPunlinkSol(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1181
#define SCIPdebugMsg
Definition: scip_message.h:69
public methods for numerical tolerances
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_heur.c:107
SCIP_RETCODE SCIPcreateSolCopy(SCIP *scip, SCIP_SOL **sol, SCIP_SOL *sourcesol)
Definition: scip_sol.c:609
#define HEUR_FREQOFS
Definition: heur_trysol.c:45
SCIP_RETCODE SCIPheurPassSolAddSol(SCIP *scip, SCIP_HEUR *heur, SCIP_SOL *sol)
Definition: heur_trysol.c:283
SCIP_EXPORT void SCIPsolSetHeur(SCIP_SOL *sol, SCIP_HEUR *heur)
Definition: sol.c:2594
static SCIP_DECL_HEURFREE(heurFreeTrySol)
Definition: heur_trysol.c:85
SCIP_OBJSENSE SCIPgetObjsense(SCIP *scip)
Definition: scip_prob.c:1224
public methods for primal CIP solutions
#define SCIP_CALL(x)
Definition: def.h:365
public methods for primal heuristic plugins and divesets
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition: scip_sol.c:3014
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:200
static SCIP_DECL_HEUREXEC(heurExecTrySol)
Definition: heur_trysol.c:137
#define HEUR_DISPCHAR
Definition: heur_trysol.c:42
#define HEUR_TIMING
Definition: heur_trysol.c:47
static SCIP_DECL_HEURCOPY(heurCopyTrySol)
Definition: heur_trysol.c:71
public methods for solutions
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:152
public methods for message output
static SCIP_DECL_HEUREXITSOL(heurExitTrySol)
Definition: heur_trysol.c:107
#define SCIP_Real
Definition: def.h:164
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
public methods for message handling
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define HEUR_USESSUBSCIP
Definition: heur_trysol.c:48
#define HEUR_MAXDEPTH
Definition: heur_trysol.c:46
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:168
public methods for primal heuristics
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_sol.c:3218
public methods for global and local (sub)problems
#define HEUR_NAME
Definition: heur_trysol.c:40
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1435