Scippy

GCG

Branch-and-Price & Column Generation for Everyone

main.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 main.c
30 * @brief Main file for C compilation
31 * @author Gerald Gamrath
32 * @author Martin Bergner
33 * @author Christian Puchert
34 */
35
36/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
37#include <string.h>
38
39#include "scip/scip.h"
40#include "scip/scipdefplugins.h"
41#include "scip/scipshell.h"
42
43#include "gcg/gcgplugins.h"
44#include "gcg/cons_decomp.h"
45#include "gcg/relax_gcg.h"
46#include "gcg/cons_decomp.h"
47#include "gcg/gcg.h"
48
49#if SCIP_VERSION < 801
50#error GCG can only be compiled with SCIP version 8.0.1 or higher
51#endif
52
53
54/** read in parameter file
55 * @returns SCIP return code */
56static
58 SCIP* scip, /**< SCIP data structure */
59 const char* filename /**< parameter file name */
60 )
61{
62 if( SCIPfileExists(filename) )
63 {
64 SCIPinfoMessage(scip, NULL, "reading user parameter file <%s>\n", filename);
65 SCIP_CALL( SCIPreadParams(scip, filename) );
66 }
67 else
68 SCIPinfoMessage(scip, NULL, "user parameter file <%s> not found - using default parameters\n", filename);
69
70 return SCIP_OKAY;
71}
72
73/** runs GCG from the command line
74 * @returns SCIPreturn code */
75static
77 GCG* gcg, /**< GCG data structure */
78 const char* filename, /**< input file name */
79 const char* decname /**< decomposition file name (or NULL) */
80 )
81{
83 /********************
84 * Problem Creation *
85 ********************/
86
87 SCIPinfoMessage(scip, NULL, "\nread problem <%s>\n", filename);
88 SCIPinfoMessage(scip, NULL, "============\n\n");
89 SCIP_CALL( SCIPreadProb(scip, filename, NULL) );
90
92
93 if( decname != NULL )
94 {
95 SCIPinfoMessage(scip, NULL, "\nread decomposition <%s>\n", decname);
96 SCIPinfoMessage(scip, NULL, "==================\n\n");
97 SCIP_CALL( SCIPreadProb(scip, decname, NULL) );
98 }
99
100 /*******************
101 * Problem Solving *
102 *******************/
103 SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
104 SCIPinfoMessage(scip, NULL, "=============\n\n");
105
107
108 SCIPinfoMessage(scip, NULL, "\nprimal solution:\n");
109 SCIPinfoMessage(scip, NULL, "================\n\n");
111
112 /**************
113 * Statistics *
114 **************/
115
116 SCIPinfoMessage(scip, NULL, "\nStatistics\n");
117 SCIPinfoMessage(scip, NULL, "==========\n");
118
120
121 return SCIP_OKAY;
122}
123
124/** evaluates command line parameters and runs GCG appropriately in the given SCIP instance
125 * @returns SCIP return code */
126static
128 GCG* gcg, /**< GCG data structure */
129 int argc, /**< number of shell parameters */
130 char** argv, /**< array with shell parameters */
131 const char* defaultsetname /**< name of default settings file */
132 )
133{ /*lint --e{850}*/
134 char* probname = NULL;
135 char* decname = NULL;
136 char* settingsname = NULL;
137 char* mastersetname = NULL;
138 char* logname = NULL;
139 SCIP_Bool quiet;
140 SCIP_Bool paramerror;
141 SCIP_Bool interactive;
142 SCIP_Real primalreference = SCIP_UNKNOWN;
143 SCIP_Real dualreference = SCIP_UNKNOWN;
144 SCIP_Bool onlyversion = FALSE;
145 const char* dualrefstring;
146 const char* primalrefstring;
147 int i;
148 SCIP* scip;
149
150
151 /********************
152 * Parse parameters *
153 ********************/
154
156 quiet = FALSE;
157 paramerror = FALSE;
158 interactive = FALSE;
159 primalrefstring = NULL;
160 dualrefstring = NULL;
161 for( i = 1; i < argc; ++i )
162 {
163 if( strcmp(argv[i], "-l") == 0 )
164 {
165 i++;
166 if( i < argc )
167 logname = argv[i];
168 else
169 {
170 SCIPinfoMessage(scip, NULL, "missing log filename after parameter '-l'\n");
171 paramerror = TRUE;
172 }
173 }
174 else if( strcmp(argv[i], "-q") == 0 )
175 quiet = TRUE;
176 else if( strcmp(argv[i], "-s") == 0 )
177 {
178 i++;
179 if( i < argc )
180 settingsname = argv[i];
181 else
182 {
183 SCIPinfoMessage(scip, NULL, "missing settings filename after parameter '-s'\n");
184 paramerror = TRUE;
185 }
186 }
187 else if( strcmp(argv[i], "-m") == 0 )
188 {
189 i++;
190 if( i < argc )
191 mastersetname = argv[i];
192 else
193 {
194 SCIPinfoMessage(scip, NULL, "missing settings filename for master program after parameter '-m'\n");
195 paramerror = TRUE;
196 }
197 }
198 else if( strcmp(argv[i], "-f") == 0 )
199 {
200 i++;
201 if( i < argc )
202 probname = argv[i];
203 else
204 {
205 SCIPinfoMessage(scip, NULL, "missing problem filename after parameter '-f'\n");
206 paramerror = TRUE;
207 }
208 }
209 else if( strcmp(argv[i], "-d") == 0 )
210 {
211 i++;
212 if( i < argc )
213 decname = argv[i];
214 else
215 {
216 SCIPinfoMessage(scip, NULL, "missing decomposition filename after parameter '-d'\n");
217 paramerror = TRUE;
218 }
219 }
220 else if( strcmp(argv[i], "-c") == 0 )
221 {
222 i++;
223 if( i < argc )
224 {
226 interactive = TRUE;
227 }
228 else
229 {
230 SCIPinfoMessage(scip, NULL, "missing command line after parameter '-c'\n");
231 paramerror = TRUE;
232 }
233 }
234 else if( strcmp(argv[i], "-b") == 0 )
235 {
236 i++;
237 if( i < argc )
238 {
239 SCIP_FILE* file;
240
241 file = SCIPfopen(argv[i], "r");
242 if( file == NULL )
243 {
244 SCIPinfoMessage(scip, NULL, "cannot read command batch file <%s>\n", argv[i]);
245 SCIPprintSysError(argv[i]);
246 paramerror = TRUE;
247 }
248 else
249 {
250 while( !SCIPfeof(file) )
251 {
252 char buffer[SCIP_MAXSTRLEN];
253
254 (void)SCIPfgets(buffer, SCIP_MAXSTRLEN, file);
255 if( buffer[0] != '\0' )
256 {
258 }
259 }
260 SCIPfclose(file);
261 interactive = TRUE;
262 }
263 }
264 else
265 {
266 SCIPinfoMessage(scip, NULL, "missing command batch filename after parameter '-b'\n");
267 paramerror = TRUE;
268 }
269 }
270 else if( strcmp(argv[i], "-o") == 0 )
271 {
272 if( i >= argc - 2 )
273 {
274 printf("wrong usage of reference objective parameter '-o': -o <primref> <dualref>\n");
275 paramerror = TRUE;
276 }
277 else
278 {
279 /* do not parse the strings directly, the settings could still influence the value of +-infinity */
280 primalrefstring = argv[i + 1];
281 dualrefstring = argv[i+2];
282 }
283 i += 2;
284 }
285 else if( strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0 )
286 {
287 onlyversion = TRUE;
288 }
289 else
290 {
291 SCIPinfoMessage(scip, NULL, "invalid parameter <%s>\n", argv[i]);
292 paramerror = TRUE;
293 }
294 }
295 if( interactive && probname != NULL )
296 {
297 SCIPinfoMessage(scip, NULL, "cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
298 paramerror = TRUE;
299 }
300 if( probname == NULL && decname != NULL )
301 {
302 SCIPinfoMessage(scip, NULL, "cannot read decomposition file without given problem\n");
303 paramerror = TRUE;
304 }
305
306 if( !paramerror )
307 {
308
309 /***********************************
310 * create log file message handler *
311 ***********************************/
312
313 if( quiet )
314 {
316 }
317
318 if( logname != NULL )
319 {
321 }
322
323
324 /***********************************
325 * Version and library information *
326 ***********************************/
327
329 SCIPinfoMessage(scip, NULL, "\n");
330
332 SCIPinfoMessage(scip, NULL, "\n");
333
334 if( onlyversion )
335 {
336 /* @todo: build options, see SCIPprintBuildOptions() */
337 return SCIP_OKAY;
338 }
339
340 /*****************
341 * Load settings *
342 *****************/
343
344 if( settingsname != NULL )
345 {
346 SCIP_CALL( readParams(scip, settingsname) );
347 }
348 else if( defaultsetname != NULL )
349 {
350 SCIP_CALL( readParams(scip, defaultsetname) );
351 }
352
353 if( mastersetname != NULL )
354 {
355 SCIP_CALL( readParams(GCGgetMasterprob(gcg), mastersetname) );
356 }
357
358 /**************
359 * Start GCG *
360 **************/
361
362 if( probname != NULL )
363 {
364 SCIP_Bool validatesolve = FALSE;
365
366 if( primalrefstring != NULL && dualrefstring != NULL )
367 {
368 char *endptr;
369 if( ! SCIPparseReal(scip, primalrefstring, &primalreference, &endptr) ||
370 ! SCIPparseReal(scip, dualrefstring, &dualreference, &endptr) )
371 {
372 printf("error parsing primal and dual reference values for validation: %s %s\n", primalrefstring, dualrefstring);
373 return SCIP_ERROR;
374 }
375 else
376 validatesolve = TRUE;
377 }
378 SCIP_CALL( fromCommandLine(gcg, probname, decname) );
379
380 /* validate the solve */
381 if( validatesolve )
382 {
383 SCIP_CALL( SCIPvalidateSolve(scip, primalreference, dualreference, SCIPfeastol(scip), FALSE, NULL, NULL, NULL) );
384 }
385 }
386 else
387 {
388 SCIPinfoMessage(scip, NULL, "\n");
390 }
391 }
392 else
393 {
394 SCIPinfoMessage(scip, NULL, "\nsyntax: %s [-v] [-l <logfile>] [-q] [-s <settings>] [-f <problem>] [-m <mastersettings>] [-d <decomposition>] [-b <batchfile>] [-c \"command\"]\n"
395 " -v, --version : print version\n"
396 " -l <logfile> : copy output into log file\n"
397 " -q : suppress screen messages\n"
398 " -s <settings> : load parameter settings (.set) file\n"
399 " -m <mastersettings> : load parameter settings for master program (.set) file\n",
400 argv[0]);
401 SCIPinfoMessage(scip, NULL, " -f <problem> : load and solve problem file\n"
402 " -d <decomposition> : load decomposition file\n"
403 " -o <primref> <dualref> : pass primal and dual objective reference values for validation at the end of the solve\n"
404 " -b <batchfile> : load and execute dialog command batch file (can be used multiple times)\n"
405 " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n\n");
406 }
407
408 return SCIP_OKAY;
409}
410
411/** runs the interactive shell
412 * @returns SCIP return code */
413static
415 int argc, /**< number of shell parameters */
416 char** argv, /**< array with shell parameters */
417 const char* defaultsetname /**< name of default settings file */
418 )
419{
420 GCG* gcg = NULL;
421
422 /*********
423 * Setup *
424 *********/
425
426 /* initialize GCG */
428
430
431
432 /**********************************
433 * Process command line arguments *
434 **********************************/
435 SCIP_CALL( GCGprocessGCGShellArguments(gcg, argc, argv, defaultsetname) );
436
437
438 /********************
439 * Deinitialization *
440 ********************/
441
442 SCIP_CALL( GCGfree(&gcg) );
443
445
446 return SCIP_OKAY;
447}
448
449/** main function called first
450 * @returns 0 if success, -1 otherwise */
451int
453 int argc, /**< number of arguments */
454 char** argv /**< array of arguments */
455 )
456{
457 SCIP_RETCODE retcode;
458
459 retcode = GCGrunGCGShell(argc, argv, "gcg.set");
460
461 if( retcode != SCIP_OKAY )
462 {
463 SCIPprintError(retcode);
464 return -1;
465 }
466
467 return 0;
468}
constraint handler for structure detection
#define SCIP_MAXSTRLEN
#define SCIP_Bool
#define SCIP_Real
#define SCIP_UNKNOWN
#define SCIP_CALL(x)
#define TRUE
#define FALSE
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
int SCIPfeof(SCIP_FILE *stream)
int SCIPfclose(SCIP_FILE *fp)
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
GCG interface methods.
void GCGprintVersion(GCG *gcg, FILE *file)
Definition: gcg_general.c:101
SCIP plugins for generic column generation.
SCIP_Bool SCIPfileExists(const char *filename)
GCG_EXPORT SCIP_RETCODE GCGtransformProb(GCG *gcg)
Definition: relax_gcg.c:5331
SCIP_RETCODE GCGprintStatistics(GCG *gcg, FILE *file)
Definition: misc.c:719
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
SCIP_RETCODE GCGcreate(GCG **gcg)
Definition: gcg.cpp:45
SCIP_RETCODE GCGfree(GCG **gcg)
Definition: gcg.cpp:68
GCG_EXPORT SCIP_RETCODE GCGsolve(GCG *gcg)
Definition: relax_gcg.c:5480
SCIP_RETCODE SCIPreadProb(SCIP *scip, const char *filename, const char *extension)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
void SCIPsetMessagehdlrLogfile(SCIP *scip, const char *filename)
void SCIPsetMessagehdlrQuiet(SCIP *scip, SCIP_Bool quiet)
void SCIPprintError(SCIP_RETCODE retcode)
void SCIPprintVersion(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPreadParams(SCIP *scip, const char *filename)
SCIP_RETCODE SCIPstartInteraction(SCIP *scip)
SCIP_RETCODE SCIPaddDialogInputLine(SCIP *scip, const char *inputline)
void SCIPprintExternalCodes(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
SCIP_Real SCIPfeastol(SCIP *scip)
SCIP_Bool SCIPparseReal(SCIP *scip, const char *str, SCIP_Real *value, char **endptr)
SCIP_RETCODE SCIPvalidateSolve(SCIP *scip, SCIP_Real primalreference, SCIP_Real dualreference, SCIP_Real reftol, SCIP_Bool quiet, SCIP_Bool *feasible, SCIP_Bool *primalboundcheck, SCIP_Bool *dualboundcheck)
void SCIPprintSysError(const char *message)
#define NULL
static SCIP_RETCODE GCGprocessGCGShellArguments(GCG *gcg, int argc, char **argv, const char *defaultsetname)
Definition: main.c:127
int main(int argc, char **argv)
Definition: main.c:452
static SCIP_RETCODE GCGrunGCGShell(int argc, char **argv, const char *defaultsetname)
Definition: main.c:414
static SCIP_RETCODE fromCommandLine(GCG *gcg, const char *filename, const char *decname)
Definition: main.c:76
static SCIP_RETCODE readParams(SCIP *scip, const char *filename)
Definition: main.c:57
#define BMScheckEmptyMemory()
struct SCIP_File SCIP_FILE
GCG relaxator.
Definition: struct_gcg.h:55
SCIP_OKAY
SCIP_ERROR
enum SCIP_Retcode SCIP_RETCODE