Scippy

SCIP

Solving Constraint Integer Programs

event_bestsol.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-2015 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 event_bestsol.c
17  * @brief eventhdlr for best solution found
18  * @author Gerald Gamrath
19  * @author Daniel Rehfeldt
20  *
21  * Event handler for printing DIMACS solution file.
22  */
23 
24 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include "event_bestsol.h"
27 #include "probdata_stp.h"
28 #include "grph.h"
29 
30 #include <string.h>
31 
32 #define EVENTHDLR_NAME "bestsol"
33 #define EVENTHDLR_DESC "event handler for best solutions found"
34 
35 /** copy method for event handler plugins (called when SCIP copies plugins) */
36 static
37 SCIP_DECL_EVENTCOPY(eventCopyBestsol)
38 { /*lint --e{715}*/
39  assert(scip != NULL);
40  assert(eventhdlr != NULL);
41  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
42 
43  /* call inclusion method of event handler */
44  SCIP_CALL( SCIPincludeEventHdlrBestsol(scip) );
45 
46  return SCIP_OKAY;
47 }
48 
49 /** initialization method of event handler (called after problem was transformed) */
50 static
51 SCIP_DECL_EVENTINIT(eventInitBestsol)
52 { /*lint --e{715}*/
53  assert(scip != NULL);
54  assert(eventhdlr != NULL);
55  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
56 
57  /* notify SCIP that your event handler wants to react on the event type best solution found */
58  SCIP_CALL( SCIPcatchEvent( scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, NULL) );
59 
60  return SCIP_OKAY;
61 }
62 
63 /** deinitialization method of event handler (called before transformed problem is freed) */
64 static
65 SCIP_DECL_EVENTEXIT(eventExitBestsol)
66 { /*lint --e{715}*/
67  assert(scip != NULL);
68  assert(eventhdlr != NULL);
69  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
70 
71  /* notify SCIP that your event handler wants to drop the event type best solution found */
72  SCIP_CALL( SCIPdropEvent( scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, -1) );
73 
74  return SCIP_OKAY;
75 }
76 
77 /** execution method of event handler */
78 static
79 SCIP_DECL_EVENTEXEC(eventExecBestsol)
80 { /*lint --e{715}*/
81  SCIP_SOL* bestsol;
82  SCIP_Real solvalue;
83  SCIP_Real factor = 1.0;
84 
86  factor = -1.0;
87 
88  assert(eventhdlr != NULL);
89  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
90  assert(event != NULL);
91  assert(scip != NULL);
92  assert(SCIPeventGetType(event) == SCIP_EVENTTYPE_BESTSOLFOUND);
93 
94  SCIPdebugMessage("exec method of event handler for best solution found\n");
95 
96  bestsol = SCIPgetBestSol(scip);
97  assert(bestsol != NULL);
98  solvalue = factor * SCIPgetSolOrigObj(scip, bestsol);
99 
100  SCIPprobdataWriteLogLine(scip, "Solution %.1f %16.9g\n", SCIPgetTotalTime(scip), solvalue);
101 
102  SCIP_CALL( SCIPprobdataWriteIntermediateSolution(scip) );
103 
104 #if 0
105  /* perform bound-based reduction tests to fix variables */
106  if( SCIPprobdataGetType(scip) == STP_HOP_CONS )
107  {
108  SCIP_PROBDATA* probdata;
109  GRAPH* g;
110  PATH* vnoi;
111  SCIP_Real* cost;
112  SCIP_Real* radius;
113  SCIP_Real* costrev;
114  SCIP_Real primalobj;
115  SCIP_Real offset;
116  int* heap;
117  int* state;
118  int* vbase;
119  int* pathedge;
120  int nnodes;
121  int nedges;
122  int hcrcnelims;
123 
124  /* get problem data */
125  probdata = SCIPgetProbData(scip);
126  assert(probdata != NULL);
127 
128  /* get graph */
129  g = SCIPprobdataGetGraph(probdata);
130  assert(g != NULL);
131  assert(g->stp_type == STP_HOP_CONS);
132 
133  offset = SCIPprobdataGetOffset(scip);
134  primalobj = SCIPgetPrimalbound(scip);
135  nnodes = g->knots;
136  nedges = g->edges;
137 
138  /* allocate memory */
139  SCIP_CALL( SCIPallocBufferArray(scip, &heap, nnodes + 1) );
140  SCIP_CALL( SCIPallocBufferArray(scip, &state, nnodes) );
141  SCIP_CALL( SCIPallocBufferArray(scip, &cost, nedges) );
142  SCIP_CALL( SCIPallocBufferArray(scip, &radius, nnodes) );
143  SCIP_CALL( SCIPallocBufferArray(scip, &costrev, nedges) );
144  SCIP_CALL( SCIPallocBufferArray(scip, &vbase, nnodes) );
145  SCIP_CALL( SCIPallocBufferArray(scip, &pathedge, nnodes) );
146  SCIP_CALL( SCIPallocBufferArray(scip, &vnoi, nnodes) );
147 
148  printf("perfrom HC %f\n", offset);
149  SCIP_CALL( hcrcbound_reduce(scip, g, vnoi, cost, costrev, radius, offset, primalobj, heap, state, vbase, &hcrcnelims, pathedge, TRUE) );
150 
151  /* free memory */
152  SCIPfreeBufferArray(scip, &vnoi);
153  SCIPfreeBufferArray(scip, &pathedge);
154  SCIPfreeBufferArray(scip, &vbase);
155  SCIPfreeBufferArray(scip, &costrev);
156  SCIPfreeBufferArray(scip, &radius);
157  SCIPfreeBufferArray(scip, &cost);
158  SCIPfreeBufferArray(scip, &state);
159  SCIPfreeBufferArray(scip, &heap);
160  }
161 #endif
162  return SCIP_OKAY;
163 }
164 
165 /** includes event handler for best solution found */
167  SCIP* scip /**< SCIP data structure */
168  )
169 {
170  SCIP_EVENTHDLRDATA* eventhdlrdata;
171  SCIP_EVENTHDLR* eventhdlr;
172  eventhdlrdata = NULL;
173 
174  eventhdlr = NULL;
175  /* create event handler for events on watched variables */
176  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecBestsol, eventhdlrdata) );
177  assert(eventhdlr != NULL);
178 
179  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopyBestsol) );
180  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitBestsol) );
181  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitBestsol) );
182 
183  return SCIP_OKAY;
184 }
SCIP_RETCODE hcrcbound_reduce(SCIP *, GRAPH *, PATH *, SCIP_Real *, SCIP_Real *, SCIP_Real *, SCIP_Real, int *, int *, int *, int *, int *, SCIP_Bool)
Definition: reduce_bnd.c:1878
Definition: grph.h:55
#define TRUE
Definition: portab.h:34
#define EVENTHDLR_NAME
Definition: event_bestsol.c:32
static SCIP_DECL_EVENTINIT(eventInitBestsol)
Definition: event_bestsol.c:51
#define STP_HOP_CONS
Definition: grph.h:48
static SCIP_DECL_EVENTEXIT(eventExitBestsol)
Definition: event_bestsol.c:65
SCIP_RETCODE SCIPprobdataWriteIntermediateSolution(SCIP *scip)
Problem data for stp problem.
GRAPH * SCIPprobdataGetGraph(SCIP_PROBDATA *probdata)
void SCIPprobdataWriteLogLine(SCIP *scip, const char *formatstr,...)
int stp_type
Definition: grph.h:123
SCIP_Real SCIPprobdataGetOffset(SCIP *scip)
#define EVENTHDLR_DESC
Definition: event_bestsol.c:33
int knots
Definition: grph.h:61
#define STP_MAX_NODE_WEIGHT
Definition: grph.h:47
int SCIPprobdataGetType(SCIP *scip)
eventhdlr for best solution found
static SCIP_DECL_EVENTCOPY(eventCopyBestsol)
Definition: event_bestsol.c:37
includes various files containing graph methods used for Steiner problems
SCIP_RETCODE SCIPincludeEventHdlrBestsol(SCIP *scip)
int edges
Definition: grph.h:89
static SCIP_DECL_EVENTEXEC(eventExecBestsol)
Definition: event_bestsol.c:79