GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
event_bestsol.c
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file event_bestsol.c
30 * @brief eventhdlr to record the best primal bound for each heuristic
31 * @author Christian Puchert
32 */
33
34/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
36#include <string.h>
37#include "gcg/event_bestsol.h"
38
39#ifdef SCIP_STATISTIC
40#define EVENTHDLR_NAME "bestsol"
41#define EVENTHDLR_DESC "event handler to record the best primal bound for each heuristic"
42
43/*
44 * Data structures
45 */
46
47/** event handler data */
49{
50 SCIP_HEUR** heurs; /**< heuristics known to this event handler */
51 SCIP_Real* bestprimalbd; /**< array to store best primal bounds for each heuristic */
52};
53
54/*
55 * Callback methods of event handler
56 */
57
58/** destructor of event handler to free user data (called when SCIP is exiting) */
59static
60SCIP_DECL_EVENTFREE(eventFreeBestsol)
61{ /*lint --e{715}*/
62 SCIP_EVENTHDLRDATA* eventhdlrdata;
63
64 assert(scip != NULL);
65 assert(eventhdlr != NULL);
66
67 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
68 assert(eventhdlrdata != NULL);
69
70 SCIPfreeMemory(scip, &eventhdlrdata);
71
72 return SCIP_OKAY;
73}
74
75/** initialization method of event handler (called after problem was transformed) */
76static
77SCIP_DECL_EVENTINIT(eventInitBestsol)
78{ /*lint --e{715}*/
79 SCIP_EVENTHDLRDATA* eventhdlrdata;
80 int nheurs;
81 int i;
82
83 assert(scip != NULL);
84 assert(eventhdlr != NULL);
85 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
86
87 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
88 assert(eventhdlrdata != NULL);
89
90 nheurs = SCIPgetNHeurs(scip);
91
92 /* allocate memory */
93 SCIP_CALL( SCIPduplicateMemoryArray(scip, &eventhdlrdata->heurs, SCIPgetHeurs(scip), nheurs) );
94 SCIP_CALL( SCIPallocMemoryArray(scip, &eventhdlrdata->bestprimalbd, nheurs) );
95
96 for( i = 0; i < nheurs; ++i )
97 eventhdlrdata->bestprimalbd[i] = SCIPinfinity(scip);
98
99 /* notify SCIP that your event handler wants to react on the event types best solution found and node solved */
101
102 return SCIP_OKAY;
103}
104
105/** deinitialization method of event handler (called before transformed problem is freed) */
106static
107SCIP_DECL_EVENTEXIT(eventExitBestsol)
108{ /*lint --e{715}*/
109 SCIP_EVENTHDLRDATA* eventhdlrdata;
110
111 assert(scip != NULL);
112 assert(eventhdlr != NULL);
113 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
114
115 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
116 assert(eventhdlrdata != NULL);
117
118 /* free memory */
119 SCIPfreeMemoryArray(scip, &eventhdlrdata->heurs);
120 SCIPfreeMemoryArray(scip, &eventhdlrdata->bestprimalbd);
121
122 /* notify SCIP that your event handler wants to drop the event types best solution found and node solved */
124
125 return SCIP_OKAY;
126}
127
128/** solving process deinitialization method of event handler (called before branch and bound process data is freed) */
129static
130SCIP_DECL_EVENTEXITSOL(eventExitsolBestsol)
131{ /*lint --e{715}*/
132 SCIP_EVENTHDLRDATA* eventhdlrdata;
133 const char* probname;
134 int nheurs;
135 int i;
136
137 assert(scip != NULL);
138 assert(eventhdlr != NULL);
139 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
140
141 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
142 assert(eventhdlrdata != NULL);
143
144 nheurs = SCIPgetNHeurs(scip);
145 probname = SCIPgetProbName(scip);
146
147 /* output statistics */
148 for( i = 0; i < nheurs; ++i )
149 {
150 SCIPstatisticPrintf("Heuristic statistics (%s) -- %s : bestprimalbound = %13.6e\n",
151 strncmp(probname, "master", 6) == 0 ? "master" : "original",
152 SCIPheurGetName(eventhdlrdata->heurs[i]), eventhdlrdata->bestprimalbd[i]);
153 }
154
155 return SCIP_OKAY;
156}
157
158/** execution method of event handler */
159static
160SCIP_DECL_EVENTEXEC(eventExecBestsol)
161{ /*lint --e{715}*/
162 SCIP_EVENTHDLRDATA* eventhdlrdata;
163 int nheurs;
164 SCIP_SOL* sol;
165 SCIP_HEUR* solheur;
167 int i;
168
169 assert(scip != NULL);
170 assert(eventhdlr != NULL);
171 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
172
173 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
174 assert(eventhdlrdata != NULL);
175
176 nheurs = SCIPgetNHeurs(scip);
177
178 /* get new primal solution */
179 sol = SCIPeventGetSol(event);
180 assert(sol != NULL);
181
182 /* get the heuristic that found the solution */
183 solheur = SCIPgetSolHeur(scip, sol);
184
185 /* get the objective value */
187
188 /* if the solution was found by a relaxation, there is nothing to do */
189 if( solheur == NULL )
190 return SCIP_OKAY;
191
192 /* search the heuristic that found the solution */
193 for( i = 0; i < nheurs && eventhdlrdata->heurs[i] != solheur; ++i ) ;
194
195 /* if the heuristic was not found in the problem, then the solution comes
196 * from another problem; in that case, no statistics are collected here
197 */
198 if( i == nheurs )
199 return SCIP_OKAY;
200
201 /* update the best objective value for that heuristic */
202 if( obj < eventhdlrdata->bestprimalbd[i] )
203 eventhdlrdata->bestprimalbd[i] = obj;
204
205 return SCIP_OKAY;
206}
207#endif
208
209/** creates event handler for bestsol event */
211 SCIP* scip /**< SCIP data structure */
212 )
213{
214#ifdef SCIP_STATISTIC
215 SCIP_EVENTHDLRDATA* eventhdlrdata;
216 SCIP_EVENTHDLR* eventhdlr;
217 assert(scip != NULL);
218
219 /* create bestsol event handler data */
220 eventhdlrdata = NULL;
221 SCIP_CALL( SCIPallocMemory(scip, &eventhdlrdata) );
222 assert(eventhdlrdata != NULL);
223
224 eventhdlr = NULL;
225
226 /* include event handler into SCIP */
228 eventExecBestsol, eventhdlrdata) );
229 assert(eventhdlr != NULL);
230
231 /* set non fundamental callbacks via setter functions */
232 SCIP_CALL( SCIPsetEventhdlrFree(scip, eventhdlr, eventFreeBestsol) );
233 SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitBestsol) );
234 SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitBestsol) );
235 SCIP_CALL( SCIPsetEventhdlrExitsol(scip, eventhdlr, eventExitsolBestsol) );
236#endif
237
238 return SCIP_OKAY;
239}
SCIP_DECL_EVENTEXEC(EventhdlrNewSol::scip_exec)
SCIP_DECL_EVENTINIT(EventhdlrNewSol::scip_init)
SCIP_DECL_EVENTEXIT(EventhdlrNewSol::scip_exit)
SCIP_DECL_EVENTEXITSOL(EventhdlrNewSol::scip_exitsol)
SCIP_DECL_EVENTFREE(EventhdlrNewSol::scip_free)
#define EVENTHDLR_DESC
#define EVENTHDLR_NAME
#define SCIP_Real
SCIP_RETCODE GCGincludeEventHdlrBestsol(SCIP *scip)
eventhdlr to record the best primal bound for each heuristic
const char * SCIPgetProbName(SCIP *scip)
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
SCIP_RETCODE SCIPsetEventhdlrExitsol(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXITSOL((*eventexitsol)))
SCIP_RETCODE SCIPsetEventhdlrFree(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTFREE((*eventfree)))
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
SCIP_SOL * SCIPeventGetSol(SCIP_EVENT *event)
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
SCIP_HEUR ** SCIPgetHeurs(SCIP *scip)
int SCIPgetNHeurs(SCIP *scip)
const char * SCIPheurGetName(SCIP_HEUR *heur)
#define SCIPallocMemoryArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
#define SCIPduplicateMemoryArray(scip, ptr, source, num)
#define SCIPfreeMemoryArray(scip, ptr)
SCIP_HEUR * SCIPgetSolHeur(SCIP *scip, SCIP_SOL *sol)
SCIP_Real SCIPgetSolTransObj(SCIP *scip, SCIP_SOL *sol)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
int i
SCIP_SOL * sol
SCIP_Real obj
#define NULL
#define SCIPstatisticPrintf
struct SCIP_Eventhdlr SCIP_EVENTHDLR
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
#define SCIP_EVENTTYPE_SOLFOUND
struct SCIP_Heur SCIP_HEUR
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Sol SCIP_SOL