Scippy

GCG

Branch-and-Price & Column Generation for Everyone

event_solvingstats.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_solvingstats.c
30 * @brief eventhdlr for writing various types of information during the solving process
31 * @author Gerald Gamrath
32 *
33 * If the filename is specified, a file is created and the eventhandler is installed to catch all events announcing that
34 * a node was solved or that a new best solution was found.
35 * Whenever one of these things happens, a line is printed to the file with the following information:
36 * 1) solving time
37 * 2) number of processed nodes (including the current node)
38 * 3) number of open nodes
39 * 4) number of LP iterations
40 * 5) number of variables in the master problem
41 * 6) current global dual bound
42 * 7) current primal bound
43 */
44
45/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
46
48#include "gcg/pricer_gcg.h"
49#include <string.h>
50
51#define EVENTHDLR_NAME "solvingstats"
52#define EVENTHDLR_DESC "event handler for best solutions found"
53
54#define DEFAULT_FILENAME "" /**< filename to write to */
55
56/*
57 * Data structures
58 */
59
60/** event handler data */
62{
63 SCIP* origprob; /**< pointer to the original SCIP instance */
64 FILE* file; /**< file to which statistics should be written */
65 char* filename; /**< name of file to which statistics should be written */
66};
67
68/** destructor of event handler to free user data (called when SCIP is exiting) */
69static
70SCIP_DECL_EVENTFREE(eventFreeSolvingstats)
71{ /*lint --e{715}*/
72 SCIP_EVENTHDLRDATA* eventhdlrdata;
73
74 assert(scip != NULL);
75 assert(eventhdlr != NULL);
76
77 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
78 assert(eventhdlrdata != NULL);
79 assert(eventhdlrdata->file == NULL);
80
81 SCIPfreeMemory(scip, &eventhdlrdata);
82
83 return SCIP_OKAY;
84}
85
86/** initialization method of event handler (called after problem was transformed) */
87static
88SCIP_DECL_EVENTINIT(eventInitSolvingstats)
89{ /*lint --e{715}*/
90 SCIP_EVENTHDLRDATA* eventhdlrdata;
91
92 assert(scip != NULL);
93 assert(eventhdlr != NULL);
94 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
95
96 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
97 assert(eventhdlrdata != NULL);
98
99 /* check if we need to open the file */
100 if( strlen(eventhdlrdata->filename) > 0 )
101 {
102 assert(eventhdlrdata->origprob != NULL);
103 assert(eventhdlrdata->file == NULL);
104
105 eventhdlrdata->file = fopen(eventhdlrdata->filename, "w");
106
107 if( eventhdlrdata->file == NULL )
108 {
109 SCIPerrorMessage("cannot create file <%s> for writing\n", eventhdlrdata->filename);
110 SCIPprintSysError(eventhdlrdata->filename);
112 }
113
114 /* notify SCIP that your event handler wants to react on the event types best solution found and node solved */
116 }
117
118 return SCIP_OKAY;
119}
120
121/** deinitialization method of event handler (called before transformed problem is freed) */
122static
123SCIP_DECL_EVENTEXIT(eventExitSolvingstats)
124{ /*lint --e{715}*/
125 SCIP_EVENTHDLRDATA* eventhdlrdata;
126
127 assert(scip != NULL);
128 assert(eventhdlr != NULL);
129 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
130
131 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
132 assert(eventhdlrdata != NULL);
133
134 if( eventhdlrdata->file != NULL )
135 {
136 (void) fclose(eventhdlrdata->file);
137
138 /* notify SCIP that your event handler wants to drop the event types best solution found and node solved */
140
141 eventhdlrdata->file = NULL;
142 }
143
144 return SCIP_OKAY;
145}
146
147/** execution method of event handler */
148static
149SCIP_DECL_EVENTEXEC(eventExecSolvingstats)
150{ /*lint --e{715}*/
151 SCIP_EVENTHDLRDATA* eventhdlrdata;
152
153 assert(eventhdlr != NULL);
154 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
155 assert(event != NULL);
156 assert(scip != NULL);
157 /*assert(SCIPeventGetType(event) & (SCIP_EVENTTYPE_BESTSOLFOUND | SCIP_EVENTTYPE_NODESOLVED) != 0);*/
158
159 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
160 assert(eventhdlrdata != NULL);
161 assert(eventhdlrdata->file != NULL);
162
163 SCIPdebugMessage("exec method of event handler for writing information during the solving process\n");
164
165 SCIPinfoMessage(scip, eventhdlrdata->file, "%8.2f %7"SCIP_LONGINT_FORMAT" %7d %10"SCIP_LONGINT_FORMAT" %d %16.9g %16.9g\n",
169
170 return SCIP_OKAY;
171}
172
173/** includes event handler for best solution found */
175 GCG* gcg /**< GCG data structure */
176 )
177{
178 SCIP_EVENTHDLRDATA* eventhdlrdata;
179 SCIP_EVENTHDLR* eventhdlr;
180 SCIP* masterprob = GCGgetMasterprob(gcg);
181
182 /* create bounds reader data */
183 SCIP_CALL( SCIPallocMemory(masterprob, &eventhdlrdata) );
184 eventhdlrdata->origprob = GCGgetOrigprob(gcg);
185 eventhdlrdata->file = NULL;
186 eventhdlrdata->filename = NULL;
187 eventhdlr = NULL;
188
189 /* create event handler for events on watched variables */
190 SCIP_CALL( SCIPincludeEventhdlrBasic(masterprob, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecSolvingstats, eventhdlrdata) );
191 assert(eventhdlr != NULL);
192
193 SCIP_CALL( SCIPsetEventhdlrFree(masterprob, eventhdlr, eventFreeSolvingstats) );
194 SCIP_CALL( SCIPsetEventhdlrInit(masterprob, eventhdlr, eventInitSolvingstats) );
195 SCIP_CALL( SCIPsetEventhdlrExit(masterprob, eventhdlr, eventExitSolvingstats) );
196
197 /* add boundwriting parameters */
198 SCIP_CALL( SCIPaddStringParam(eventhdlrdata->origprob,
199 "eventhdlr/"EVENTHDLR_NAME"/filename",
200 "filename to write all bounds to",
201 &eventhdlrdata->filename, FALSE, DEFAULT_FILENAME, NULL, NULL) );
202
203 return SCIP_OKAY;
204}
#define SCIP_LONGINT_FORMAT
#define SCIP_CALL(x)
#define FALSE
static SCIP_DECL_EVENTFREE(eventFreeSolvingstats)
#define DEFAULT_FILENAME
static SCIP_DECL_EVENTEXIT(eventExitSolvingstats)
SCIP_RETCODE GCGincludeEventHdlrSolvingstats(GCG *gcg)
static SCIP_DECL_EVENTEXEC(eventExecSolvingstats)
#define EVENTHDLR_DESC
static SCIP_DECL_EVENTINIT(eventInitSolvingstats)
#define EVENTHDLR_NAME
eventhdlr for writing various types of information during the solving process
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
int SCIPgetNVars(SCIP *scip)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPaddStringParam(SCIP *scip, const char *name, const char *desc, char **valueptr, SCIP_Bool isadvanced, const char *defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
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 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_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)
#define SCIPallocMemory(scip, ptr)
#define SCIPfreeMemory(scip, ptr)
SCIP_Real SCIPretransformObj(SCIP *scip, SCIP_Real obj)
SCIP_Real SCIPgetPrimalbound(SCIP *scip)
SCIP_Longint SCIPgetNNodes(SCIP *scip)
SCIP_Real SCIPgetDualbound(SCIP *scip)
SCIP_Longint SCIPgetNLPIterations(SCIP *scip)
SCIP_Real SCIPgetSolvingTime(SCIP *scip)
int SCIPgetNNodesLeft(SCIP *scip)
void SCIPprintSysError(const char *message)
#define NULL
GCG variable pricer.
#define SCIPerrorMessage
#define SCIPdebugMessage
Definition: struct_gcg.h:55
#define SCIP_EVENTTYPE_NODESOLVED
#define SCIP_EVENTTYPE_BESTSOLFOUND
SCIP_FILECREATEERROR
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE