SCIP Doxygen Documentation
 
Loading...
Searching...
No Matches
scipshell.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-2023 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file scipshell.c
26 * @ingroup OTHER_CFILES
27 * @brief SCIP command line interface
28 * @author Tobias Achterberg
29 */
30
31/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32
33#include <stdio.h>
34#include <string.h>
35#include <ctype.h>
36
37#include "scip/scip.h"
38#include "scip/scipdefplugins.h"
39#include "scip/scipshell.h"
41#include "scip/reader_nl.h"
42
43/*
44 * Message Handler
45 */
46
47static
49 SCIP* scip, /**< SCIP data structure */
50 const char* filename /**< parameter file name */
51 )
52{
53 if( SCIPfileExists(filename) )
54 {
55 SCIPinfoMessage(scip, NULL, "reading user parameter file <%s>\n", filename);
56 SCIP_CALL( SCIPreadParams(scip, filename) );
57 }
58 else
59 SCIPinfoMessage(scip, NULL, "user parameter file <%s> not found - using default parameters\n", filename);
60
61 return SCIP_OKAY;
62}
63
64static
66 SCIP* scip, /**< SCIP data structure */
67 const char* filename /**< input file name */
68 )
69{
70 SCIP_RETCODE retcode;
71 SCIP_Bool outputorigsol = FALSE;
72
73 /********************
74 * Problem Creation *
75 ********************/
76
77 /** @note The message handler should be only fed line by line such the message has the chance to add string in front
78 * of each message
79 */
81 SCIPinfoMessage(scip, NULL, "read problem <%s>\n", filename);
82 SCIPinfoMessage(scip, NULL, "============\n");
84
85 retcode = SCIPreadProb(scip, filename, NULL);
86
87 switch( retcode )
88 {
89 case SCIP_NOFILE:
90 SCIPinfoMessage(scip, NULL, "file <%s> not found\n", filename);
91 return SCIP_OKAY;
93 SCIPinfoMessage(scip, NULL, "no reader for input file <%s> available\n", filename);
94 return SCIP_OKAY;
95 case SCIP_READERROR:
96 SCIPinfoMessage(scip, NULL, "error reading file <%s>\n", filename);
97 return SCIP_OKAY;
98 default:
99 SCIP_CALL( retcode );
100 } /*lint !e788*/
101
102 /*******************
103 * Problem Solving *
104 *******************/
105
106 /* solve problem */
107 SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
108 SCIPinfoMessage(scip, NULL, "=============\n\n");
109
111
112 /*******************
113 * Solution Output *
114 *******************/
115
116 SCIP_CALL( SCIPgetBoolParam(scip, "misc/outputorigsol", &outputorigsol) );
117 if ( outputorigsol )
118 {
119 SCIP_SOL* bestsol;
120
121 SCIPinfoMessage(scip, NULL, "\nprimal solution (original space):\n");
122 SCIPinfoMessage(scip, NULL, "=================================\n\n");
123
124 bestsol = SCIPgetBestSol(scip);
125 if ( bestsol == NULL )
126 SCIPinfoMessage(scip, NULL, "no solution available\n");
127 else
128 {
130
135 }
136 }
137 else
138 {
139 SCIPinfoMessage(scip, NULL, "\nprimal solution (transformed space):\n");
140 SCIPinfoMessage(scip, NULL, "====================================\n\n");
141
143 }
144
145 /**************
146 * Statistics *
147 **************/
148
149 SCIPinfoMessage(scip, NULL, "\nStatistics\n");
150 SCIPinfoMessage(scip, NULL, "==========\n\n");
151
153
154 return SCIP_OKAY;
155}
156
157/** runs SCIP as if it was called by AMPL */
158static
160 SCIP* scip, /**< SCIP data structure */
161 char* nlfilename, /**< name of .nl file, without the .nl */
162 const char* defaultsetname /**< name of default settings file */
163 )
164{
165#ifdef SCIP_WITH_AMPL
167 char* logfile;
168 SCIP_Bool printstat;
169 size_t nlfilenamelen;
170
171 SCIP_CALL( SCIPaddBoolParam(scip, "display/statistics",
172 "whether to print statistics on a solve",
173 &printstat, FALSE, FALSE, NULL, NULL) );
174
175 SCIP_CALL( SCIPaddStringParam(scip, "display/logfile",
176 "name of file to write SCIP log to (additionally to writing to stdout)",
177 NULL, FALSE, "", NULL, NULL) );
178
180 SCIPinfoMessage(scip, NULL, "\n");
181
183 SCIPinfoMessage(scip, NULL, "\n");
184
185 if( defaultsetname != NULL )
186 {
188 {
189 SCIPinfoMessage(scip, NULL, "reading user parameter file <%s>\n", defaultsetname);
190 SCIPinfoMessage(scip, NULL, "===========================\n\n");
193 SCIPinfoMessage(scip, NULL, "\n");
194 }
195 else
196 {
197 SCIPinfoMessage(scip, NULL, "user parameter file <%s> not found - using default parameters\n", defaultsetname);
198 }
199 }
200
201 SCIP_CALL( SCIPgetStringParam(scip, "display/logfile", &logfile) );
202 if( *logfile )
204
205 /* AMPL calls solver with file without .nl extension, but others (Pyomo) may not
206 * so add .nl only if not already present
207 */
209 if( nlfilenamelen > 3 && strcmp(nlfilename + (nlfilenamelen-3), ".nl") == 0 )
211 else
213 SCIPinfoMessage(scip, NULL, "read problem <%s>\n", fullnlfilename);
214 SCIPinfoMessage(scip, NULL, "============\n\n");
215
217
218 SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
219 SCIPinfoMessage(scip, NULL, "=============\n\n");
220
222
223 SCIP_CALL( SCIPgetBoolParam(scip, "display/statistics", &printstat) );
224 if( printstat )
225 {
226 SCIPinfoMessage(scip, NULL, "\nStatistics\n");
227 SCIPinfoMessage(scip, NULL, "==========\n\n");
228
230 }
231
233
234 return SCIP_OKAY;
235
236#else /* SCIP_WITH_AMPL */
237 SCIPerrorMessage("SCIP has been compiled without AMPL support.\n");
238 return SCIP_PLUGINNOTFOUND;
239#endif
240}
241
242/** evaluates command line parameters and runs SCIP appropriately in the given SCIP instance */
244 SCIP* scip, /**< SCIP data structure */
245 int argc, /**< number of shell parameters */
246 char** argv, /**< array with shell parameters */
247 const char* defaultsetname /**< name of default settings file */
248 )
249{ /*lint --e{850}*/
250 char* probname = NULL;
251 char* settingsname = NULL;
252 char* logname = NULL;
253 int randomseed;
254 SCIP_Bool randomseedread;
255 SCIP_Bool quiet;
256 SCIP_Bool paramerror;
257 SCIP_Bool interactive;
258 SCIP_Bool onlyversion;
259 SCIP_Real primalreference = SCIP_UNKNOWN;
260 SCIP_Real dualreference = SCIP_UNKNOWN;
261 const char* dualrefstring;
262 const char* primalrefstring;
263 int i;
264
265 /********************
266 * Parse parameters *
267 ********************/
268
269 /* recognize and handle case where we were called from AMPL first */
270 if( argc >= 3 && strcmp(argv[2], "-AMPL") == 0 )
271 {
273
274 return SCIP_OKAY;
275 }
276
277 quiet = FALSE;
282 randomseed = 0;
285
286 for( i = 1; i < argc; ++i )
287 {
288 if( strcmp(argv[i], "-l") == 0 )
289 {
290 i++;
291 if( i < argc )
292 logname = argv[i];
293 else
294 {
295 printf("missing log filename after parameter '-l'\n");
297 }
298 }
299 else if( strcmp(argv[i], "-q") == 0 )
300 quiet = TRUE;
301 else if( strcmp(argv[i], "-v") == 0 )
303 else if( strcmp(argv[i], "--version") == 0 )
305 else if( strcmp(argv[i], "-s") == 0 )
306 {
307 i++;
308 if( i < argc )
310 else
311 {
312 printf("missing settings filename after parameter '-s'\n");
314 }
315 }
316 else if( strcmp(argv[i], "-f") == 0 )
317 {
318 i++;
319 if( i < argc )
320 probname = argv[i];
321 else
322 {
323 printf("missing problem filename after parameter '-f'\n");
325 }
326 }
327 else if( strcmp(argv[i], "-c") == 0 )
328 {
329 i++;
330 if( i < argc )
331 {
334 }
335 else
336 {
337 printf("missing command line after parameter '-c'\n");
339 }
340 }
341 else if( strcmp(argv[i], "-b") == 0 )
342 {
343 i++;
344 if( i < argc )
345 {
346 SCIP_FILE* file;
347
348 file = SCIPfopen(argv[i], "r");
349 if( file == NULL )
350 {
351 printf("cannot read command batch file <%s>\n", argv[i]);
354 }
355 else
356 {
357 while( !SCIPfeof(file) )
358 {
359 char buffer[SCIP_MAXSTRLEN];
360
361 (void)SCIPfgets(buffer, (int) sizeof(buffer), file);
362 if( buffer[0] != '\0' )
363 {
365 }
366 }
367 SCIPfclose(file);
369 }
370 }
371 else
372 {
373 printf("missing command batch filename after parameter '-b'\n");
375 }
376 }
377 else if( strcmp(argv[i], "-r") == 0 )
378 {
379 /*read a random seed from the command line */
380 i++;
381 if( i < argc && isdigit(argv[i][0]) )
382 {
383 randomseed = atoi(argv[i]);
385 }
386 else
387 {
388 printf("Random seed parameter '-r' followed by something that is not an integer\n");
390 }
391 }
392 else if( strcmp(argv[i], "-o") == 0 )
393 {
394 if( i >= argc - 2 )
395 {
396 printf("wrong usage of reference objective parameter '-o': -o <primref> <dualref>\n");
398 }
399 else
400 {
401 /* do not parse the strings directly, the settings could still influence the value of +-infinity */
402 primalrefstring = argv[i + 1];
403 dualrefstring = argv[i+2];
404 }
405 i += 2;
406 }
407 else
408 {
409 printf("invalid parameter <%s>\n", argv[i]);
411 }
412 }
413
414 if( interactive && probname != NULL )
415 {
416 printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
418 }
419
420 if( !paramerror )
421 {
422 /***********************************
423 * create log file message handler *
424 ***********************************/
425
426 if( quiet )
427 {
429 }
430
431 if( logname != NULL )
432 {
434 }
435
436 /***********************************
437 * Version and library information *
438 ***********************************/
439
441 SCIPinfoMessage(scip, NULL, "\n");
442
444 SCIPinfoMessage(scip, NULL, "\n");
445
446 if( onlyversion )
447 {
449 SCIPinfoMessage(scip, NULL, "\n");
450 return SCIP_OKAY;
451 }
452
453 /*****************
454 * Load settings *
455 *****************/
456
457 if( settingsname != NULL )
458 {
460 }
461 else if( defaultsetname != NULL )
462 {
464 }
465
466 /************************************
467 * Change random seed, if specified *
468 ***********************************/
469 if( randomseedread )
470 {
471 SCIP_CALL( SCIPsetIntParam(scip, "randomization/randomseedshift", randomseed) );
472 }
473
474 /**************
475 * Start SCIP *
476 **************/
477
478 if( probname != NULL )
479 {
480 SCIP_Bool validatesolve = FALSE;
481
483 {
484 char *endptr;
487 {
488 printf("error parsing primal and dual reference values for validation: %s %s\n", primalrefstring, dualrefstring);
489 return SCIP_ERROR;
490 }
491 else
493 }
494 SCIP_CALL( fromCommandLine(scip, probname) );
495
496 /* validate the solve */
497 if( validatesolve )
498 {
500 }
501 }
502 else
503 {
504 SCIPinfoMessage(scip, NULL, "\n");
506 }
507 }
508 else
509 {
510 printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-r <randseed>] [-f <problem>] [-b <batchfile>] [-c \"command\"]\n"
511 " -v, --version : print version and build options\n"
512 " -l <logfile> : copy output into log file\n"
513 " -q : suppress screen messages\n"
514 " -s <settings> : load parameter settings (.set) file\n"
515 " -f <problem> : load and solve problem file\n"
516 " -o <primref> <dualref> : pass primal and dual objective reference values for validation at the end of the solve\n"
517 " -b <batchfile>: load and execute dialog command batch file (can be used multiple times)\n"
518 " -r <randseed> : nonnegative integer to be used as random seed. "
519 "Has priority over random seed specified through parameter settings (.set) file\n"
520 " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n",
521 argv[0]);
522#ifdef SCIP_WITH_AMPL
523 printf("\nas AMPL solver: %s <.nl-file without the .nl> -AMPL\n", argv[0]);
524#endif
525 printf("\n");
526 }
527
528 return SCIP_OKAY;
529}
530
531/** creates a SCIP instance with default plugins, evaluates command line parameters, runs SCIP appropriately,
532 * and frees the SCIP instance
533 */
535 int argc, /**< number of shell parameters */
536 char** argv, /**< array with shell parameters */
537 const char* defaultsetname /**< name of default settings file */
538 )
539{
540 SCIP* scip = NULL;
541
542 /*********
543 * Setup *
544 *********/
545
546 /* initialize SCIP */
548
549 /* we explicitly enable the use of a debug solution for this main SCIP instance */
551
552 /* include default SCIP plugins */
554
555 /**********************************
556 * Process command line arguments *
557 **********************************/
558
560
561 /********************
562 * Deinitialization *
563 ********************/
565
567
568 return SCIP_OKAY;
569}
#define SCIP_MAXSTRLEN
Definition def.h:302
#define SCIP_UNKNOWN
Definition def.h:207
#define TRUE
Definition def.h:95
#define FALSE
Definition def.h:96
#define SCIP_CALL(x)
Definition def.h:388
#define SCIP_CALL_FINALLY(x, y)
Definition def.h:430
static SCIP_RETCODE interactive(SCIP *scip)
Definition cmain.c:99
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition fileio.c:153
int SCIPfeof(SCIP_FILE *stream)
Definition fileio.c:227
int SCIPfclose(SCIP_FILE *fp)
Definition fileio.c:232
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition fileio.c:200
void SCIPenableDebugSol(SCIP *scip)
Definition scip_debug.c:57
SCIP_Bool SCIPfileExists(const char *filename)
Definition misc.c:10991
SCIP_RETCODE SCIPfree(SCIP **scip)
SCIP_RETCODE SCIPcreate(SCIP **scip)
SCIP_RETCODE SCIPreadProb(SCIP *scip, const char *filename, const char *extension)
Definition scip_prob.c:339
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
void SCIPsetMessagehdlrLogfile(SCIP *scip, const char *filename)
void SCIPsetMessagehdlrQuiet(SCIP *scip, SCIP_Bool quiet)
void SCIPprintBuildOptions(SCIP *scip, FILE *file)
void SCIPprintVersion(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition scip_param.c:250
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)
Definition scip_param.c:194
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition scip_param.c:487
SCIP_RETCODE SCIPreadParams(SCIP *scip, const char *filename)
Definition scip_param.c:751
SCIP_RETCODE SCIPwriteParams(SCIP *scip, const char *filename, SCIP_Bool comments, SCIP_Bool onlychanged)
Definition scip_param.c:792
SCIP_RETCODE SCIPgetStringParam(SCIP *scip, const char *name, char **value)
Definition scip_param.c:345
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition scip_param.c:57
SCIP_RETCODE SCIPstartInteraction(SCIP *scip)
SCIP_RETCODE SCIPaddDialogInputLine(SCIP *scip, const char *inputline)
void SCIPprintExternalCodes(SCIP *scip, FILE *file)
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition scip_sol.c:2313
SCIP_RETCODE SCIPcreateSolCopy(SCIP *scip, SCIP_SOL **sol, SCIP_SOL *sourcesol)
Definition scip_sol.c:618
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
Definition scip_sol.c:2379
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition scip_sol.c:1775
SCIP_RETCODE SCIPretransformSol(SCIP *scip, SCIP_SOL *sol)
Definition scip_sol.c:2491
SCIP_RETCODE SCIPsolve(SCIP *scip)
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
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)
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition misc.c:10788
void SCIPprintSysError(const char *message)
Definition misc.c:10680
return SCIP_OKAY
SCIPfreeSol(scip, &heurdata->sol))
#define NULL
Definition lpi_spx1.cpp:161
#define BMScheckEmptyMemory()
Definition memory.h:157
default message handler
struct SCIP_File SCIP_FILE
Definition pub_fileio.h:43
#define SCIPerrorMessage
Definition pub_message.h:64
SCIP_RETCODE SCIPwriteSolutionNl(SCIP *scip)
AMPL .nl file reader.
SCIP callable library.
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
default SCIP plugins
SCIP_RETCODE SCIPrunShell(int argc, char **argv, const char *defaultsetname)
Definition scipshell.c:534
static SCIP_RETCODE fromAmpl(SCIP *scip, char *nlfilename, const char *defaultsetname)
Definition scipshell.c:159
static SCIP_RETCODE fromCommandLine(SCIP *scip, const char *filename)
Definition scipshell.c:65
static SCIP_RETCODE readParams(SCIP *scip, const char *filename)
Definition scipshell.c:48
SCIP_RETCODE SCIPprocessShellArguments(SCIP *scip, int argc, char **argv, const char *defaultsetname)
Definition scipshell.c:243
SCIP command line interface.
@ SCIP_NOFILE
@ SCIP_READERROR
@ SCIP_PLUGINNOTFOUND
@ SCIP_ERROR
enum SCIP_Retcode SCIP_RETCODE