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-2014 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 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  SCIPdebugMessage("free method of trysol primal heuristic.\n");
88 
89  /* get heuristic data */
90  heurdata = SCIPheurGetData(heur);
91  assert(heurdata != NULL);
92 
93  SCIPfreeMemory(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  SCIPdebugMessage("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 )
122  SCIP_CALL( SCIPfreeSol(scip, &heurdata->addsol) );
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  SCIPdebugMessage("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, TRUE, TRUE, TRUE, &stored) );
166 
167  if( stored )
168  {
169 #ifdef SCIP_DEBUG
170  SCIPdebugMessage("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
179  obj = SCIPgetSolOrigObj(scip, heurdata->addsol);
180 #endif
181 
182  SCIP_CALL( SCIPaddSolFree(scip, &heurdata->addsol, &stored) );
183 
184  if( stored )
185  {
186 #ifdef SCIP_DEBUG
187  SCIPdebugMessage("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( SCIPallocMemory(scip, &heurdata) );
215  heurdata->trysol = NULL;
216  heurdata->addsol = 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 || SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->trysol)) )
257  {
258  if( heurdata->trysol != NULL )
259  {
260  /* free previous solution */
261  SCIP_CALL( SCIPfreeSol(scip, &heurdata->trysol) );
262  }
263 
264  SCIPdebugMessage("Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
265  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->trysol, sol) );
266  SCIP_CALL( SCIPunlinkSol(scip, heurdata->trysol) );
267  SCIPsolSetHeur(heurdata->trysol, heur);
268  }
269  }
270 
271  return SCIP_OKAY;
272 }
273 
274 /** pass solution to trysol heuristic which just gets added (without checking feasibility */
276  SCIP* scip, /**< SCIP data structure */
277  SCIP_HEUR* heur, /**< trysol heuristic */
278  SCIP_SOL* sol /**< solution to be passed */
279  )
280 {
281  SCIP_HEURDATA* heurdata;
282 
283  assert( scip != NULL );
284  assert( heur != NULL );
285  assert( sol != NULL );
286  assert( strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0 );
287 
288  /* get heuristic data */
289  heurdata = SCIPheurGetData(heur);
290  assert(heurdata != NULL);
291 
292  /* only store solution if we are not within our own SCIPtrySol() call */
293  if( ! heurdata->rec )
294  {
295  if( heurdata->addsol == NULL || SCIPisLT(scip, SCIPgetSolOrigObj(scip, sol), SCIPgetSolOrigObj(scip, heurdata->addsol)) )
296  {
297  if( heurdata->addsol != NULL )
298  {
299  /* free previous solution */
300  SCIP_CALL( SCIPfreeSol(scip, &heurdata->addsol) );
301  }
302 
303  SCIPdebugMessage("Received solution of value %g.\n", SCIPgetSolOrigObj(scip, sol));
304  SCIP_CALL( SCIPcreateSolCopy(scip, &heurdata->addsol, sol) );
305  SCIP_CALL( SCIPunlinkSol(scip, heurdata->addsol) );
306  SCIPsolSetHeur(heurdata->addsol, heur);
307  }
308  }
309 
310  return SCIP_OKAY;
311 }
312