Scippy

SCIP

Solving Constraint Integer Programs

heur_trivialnegation.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-2016 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_trivialnegation.c
17  * @brief trivialnegation primal heuristic
18  * @author Jakob Witzig
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 
27 #include "scip/pub_reopt.h"
28 #include "scip/struct_scip.h"
29 
30 #define HEUR_NAME "trivialnegation"
31 #define HEUR_DESC "negate solution entries if an objective coefficient changes the sign, enters or leaves the objective."
32 #define HEUR_DISPCHAR 'j'
33 #define HEUR_PRIORITY 40000
34 #define HEUR_FREQ -1
35 #define HEUR_FREQOFS 0
36 #define HEUR_MAXDEPTH 0
37 #define HEUR_TIMING SCIP_HEURTIMING_BEFORENODE
38 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
39 
40 /*
41  * Local methods
42  */
43 
44 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
45 static
46 SCIP_DECL_HEURCOPY(heurCopyTrivialnegation)
47 { /*lint --e{715}*/
48  assert(scip != NULL);
49  assert(heur != NULL);
50  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
51 
52  /* call inclusion method of primal heuristic */
54 
55  return SCIP_OKAY;
56 }
57 
58 
59 /** execution method of primal heuristic */
60 static
61 SCIP_DECL_HEUREXEC(heurExecTrivialnegation)
62 { /*lint --e{715}*/
63  SCIP_SOL* lastbestsol; /* best solution from last run */
64  SCIP_SOL* allchanged; /* solution with all entries negated */
65  SCIP_SOL* feasiblechanged; /* solution with all feasible entries negated */
66  SCIP_SOL* singlenegatedsol; /* solution with exactly one negated entry */
67  SCIP_VAR** vars;
68  int nvars;
69  int i;
70 
71  SCIP_Real solval;
72 
73  *result = SCIP_DIDNOTRUN;
74 
75  if( !SCIPisReoptEnabled(scip) )
76  return SCIP_OKAY;
77 
78  vars = SCIPgetVars(scip);
79  nvars = SCIPgetNVars(scip);
80 
81  *result = SCIP_DIDNOTFIND;
82 
83  /* get best solution from the run */
84  lastbestsol = SCIPgetReoptLastOptSol(scip);
85 
86  if( lastbestsol == NULL )
87  return SCIP_OKAY;
88 
89  /* initialize data structure */
90  SCIP_CALL( SCIPcreateSol(scip, &allchanged, heur) );
91  SCIP_CALL( SCIPcreateSol(scip, &feasiblechanged, heur) );
92  SCIP_CALL( SCIPcreateSol(scip, &singlenegatedsol, heur) );
93 
94  /* copy the solutions */
95  for( i = 0; i < nvars; i++ )
96  {
97  solval = SCIPgetSolVal(scip, lastbestsol, vars[i]);
98  SCIP_CALL( SCIPsetSolVal(scip, allchanged, vars[i], solval) );
99  SCIP_CALL( SCIPsetSolVal(scip, feasiblechanged, vars[i], solval) );
100  SCIP_CALL( SCIPsetSolVal(scip, singlenegatedsol, vars[i], solval) );
101  }
102 
103  assert(SCIPsolGetHeur(allchanged) == heur);
104  assert(SCIPsolGetHeur(feasiblechanged) == heur);
105  assert(SCIPsolGetHeur(singlenegatedsol) == heur);
106 
107  /* change the entries */
108  for( i = 0; i < nvars; i++ )
109  {
110  SCIP_VAR* transvar;
111 
112  assert(SCIPvarIsActive(vars[i]));
113 
114  transvar = vars[i];
115 
116  if( SCIPvarGetType(vars[i]) == SCIP_VARTYPE_BINARY )
117  {
118  SCIP_Real obj;
119  SCIP_Real newcoef;
120  SCIP_Real oldcoef;
121  SCIP_Bool changed;
122 
124  SCIP_CALL( SCIPgetReoptOldObjCoef(scip, transvar, SCIPgetNReoptRuns(scip)-1, &newcoef));
125 
126  /* check if variable entered or left the objective, or if its objective coefficient changed sign */
127  changed = FALSE;
128  if( !SCIPisFeasEQ(scip, oldcoef, newcoef) )
129  {
130  changed = SCIPisZero(scip, oldcoef) != SCIPisZero(scip, newcoef);
131  changed |= SCIPisPositive(scip, oldcoef) == SCIPisNegative(scip, newcoef); /*lint !e514*/
132  }
133 
134  SCIPdebugMessage("check variable <%s> which has %schanged from %g to %g\n", SCIPvarGetName(transvar), changed ? "" : "not ", oldcoef, newcoef);
135 
136  if( changed )
137  {
138  SCIP_Bool success;
139 
140  solval = SCIPgetSolVal(scip, lastbestsol, vars[i]);
141 
142  /* change solution value */
143  SCIP_CALL( SCIPsetSolVal(scip, allchanged, vars[i], 1 - solval) );
144  SCIP_CALL( SCIPsetSolVal(scip, feasiblechanged, vars[i], 1 - solval) );
145  SCIP_CALL( SCIPsetSolVal(scip, singlenegatedsol, vars[i], 1 - solval) );
146 
147  /* try solution with all changes */
148  success = FALSE;
149  obj = SCIPgetSolTransObj(scip, allchanged);
151  {
152  SCIPdebugMessage("try solution with all negations\n");
153  SCIP_CALL( SCIPtrySol(scip, allchanged, FALSE, FALSE, FALSE, TRUE, &success) );
154 
155  if( success )
156  {
157  SCIPdebugMessage("found feasible solution solution:\n");
158  SCIPdebug( SCIP_CALL( SCIPprintSol(scip, allchanged, NULL, FALSE) ) );
159 
160  *result = SCIP_FOUNDSOL;
161  }
162  }
163 
164  /* try solution with feasible changes */
165  success = FALSE;
166  obj = SCIPgetSolTransObj(scip, feasiblechanged);
168  {
169  SCIPdebugMessage("try solution with feasible negations\n");
170  SCIP_CALL( SCIPtrySol(scip, feasiblechanged, FALSE, FALSE, FALSE, TRUE, &success) );
171 
172  if( success )
173  {
174  SCIPdebugMessage("found feasible solution solution:\n");
175  SCIPdebug( SCIP_CALL( SCIPprintSol(scip, feasiblechanged, NULL, FALSE) ) );
176 
177  *result = SCIP_FOUNDSOL;
178  }
179  }
180 
181  if( !success )
182  {
183  /* reset solution with feasible changes */
184  SCIP_CALL( SCIPsetSolVal(scip, feasiblechanged, vars[i], solval) );
185  }
186 
187  /* try solution with exactly one changed value */
188  obj = SCIPgetSolTransObj(scip, singlenegatedsol);
190  {
191  success = FALSE;
192  SCIPdebugMessage("try solution with a single negation\n");
193  SCIP_CALL( SCIPtrySol(scip, singlenegatedsol, FALSE, FALSE, FALSE, TRUE, &success) );
194 
195  if( success )
196  {
197  SCIPdebugMessage("found feasible solution:\n");
198  SCIPdebug( SCIP_CALL( SCIPprintSol(scip, singlenegatedsol, NULL, FALSE) ) );
199 
200  *result = SCIP_FOUNDSOL;
201  }
202  }
203 
204  /* reset solution with exactly one changed value */
205  SCIP_CALL( SCIPsetSolVal(scip, singlenegatedsol, vars[i], solval) );
206  }
207  }
208  }
209 
210  /* free solutions */
211  SCIP_CALL( SCIPfreeSol(scip, &allchanged) );
212  SCIP_CALL( SCIPfreeSol(scip, &feasiblechanged) );
213  SCIP_CALL( SCIPfreeSol(scip, &singlenegatedsol) );
214 
215  return SCIP_OKAY;
216 }
217 
218 
219 /*
220  * primal heuristic specific interface methods
221  */
222 
223 /** creates the trivialnegation primal heuristic and includes it in SCIP */
225  SCIP* scip /**< SCIP data structure */
226  )
227 {
228  SCIP_HEURDATA* heurdata;
229  SCIP_HEUR* heur;
230 
231  /* no primal heuristic data */
232  heurdata = NULL;
233 
234  /* include heuristic */
235  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
237  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecTrivialnegation, heurdata) );
238 
239  assert(heur != NULL);
240 
241  /* set non fundamental callbacks via setter functions */
242  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyTrivialnegation) );
243 
244 
245  return SCIP_OKAY;
246 }
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
Definition: scip.c:41685
int SCIPgetNVars(SCIP *scip)
Definition: scip.c:10698
#define HEUR_NAME
#define HEUR_TIMING
SCIP_HEUR * SCIPsolGetHeur(SCIP_SOL *sol)
Definition: sol.c:2252
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:16443
trivialnegation primal heuristic
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1147
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:7297
SCIP_Bool SCIPisFeasLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41920
SCIP_VAR ** SCIPgetVars(SCIP *scip)
Definition: scip.c:10653
#define NULL
Definition: lpi_spx.cpp:130
static SCIP_DECL_HEURCOPY(heurCopyTrivialnegation)
#define HEUR_DISPCHAR
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
Definition: scip.c:34843
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, unsigned int timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:7252
#define FALSE
Definition: def.h:56
SCIP_Real SCIPgetSolTransObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:35113
#define TRUE
Definition: def.h:55
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPgetReoptOldObjCoef(SCIP *scip, SCIP_VAR *var, int run, SCIP_Real *objcoef)
Definition: scip.c:14910
#define SCIP_CALL(x)
Definition: def.h:266
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
SCIP_Real SCIPgetCutoffbound(SCIP *scip)
Definition: scip.c:38561
static SCIP_DECL_HEUREXEC(heurExecTrivialnegation)
public methods for reoptimization
#define SCIPdebugMessage
Definition: pub_message.h:77
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:34983
#define HEUR_FREQOFS
#define HEUR_FREQ
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:41709
#define HEUR_DESC
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:34002
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41907
SCIP main data structure.
#define HEUR_PRIORITY
int SCIPgetNReoptRuns(SCIP *scip)
Definition: scip.c:37345
SCIP_SOL * SCIPgetReoptLastOptSol(SCIP *scip)
Definition: scip.c:14883
#define SCIP_Bool
Definition: def.h:53
SCIP_RETCODE SCIPincludeHeurTrivialnegation(SCIP *scip)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:16608
#define SCIP_Real
Definition: def.h:127
SCIP_Bool SCIPvarIsActive(SCIP_VAR *var)
Definition: var.c:16730
#define SCIPdebug(x)
Definition: pub_message.h:74
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:41697
SCIP_RETCODE SCIPtrySol(SCIP *scip, SCIP_SOL *sol, SCIP_Bool printreason, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip.c:36217
SCIP_Bool SCIPisReoptEnabled(SCIP *scip)
Definition: scip.c:15481
#define HEUR_MAXDEPTH
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:35397
#define HEUR_USESSUBSCIP
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:34607