Scippy

SCIP

Solving Constraint Integer Programs

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-2017 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 scipshell.c
17  * @brief SCIP command line interface
18  * @author Tobias Achterberg
19  */
20 
21 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "scip/scip.h"
27 #include "scip/scipdefplugins.h"
28 #include "scip/scipshell.h"
29 #include "scip/message_default.h"
30 
31 /*
32  * Message Handler
33  */
34 
35 static
37  SCIP* scip, /**< SCIP data structure */
38  const char* filename /**< parameter file name */
39  )
40 {
41  if( SCIPfileExists(filename) )
42  {
43  SCIPinfoMessage(scip, NULL, "reading user parameter file <%s>\n", filename);
44  SCIP_CALL( SCIPreadParams(scip, filename) );
45  }
46  else
47  SCIPinfoMessage(scip, NULL, "user parameter file <%s> not found - using default parameters\n", filename);
48 
49  return SCIP_OKAY;
50 }
51 
52 static
54  SCIP* scip, /**< SCIP data structure */
55  const char* filename /**< input file name */
56  )
57 {
58  SCIP_RETCODE retcode;
59  SCIP_Bool outputorigsol = FALSE;
60 
61  /********************
62  * Problem Creation *
63  ********************/
64 
65  /** @note The message handler should be only fed line by line such the message has the chance to add string in front
66  * of each message
67  */
68  SCIPinfoMessage(scip, NULL, "\n");
69  SCIPinfoMessage(scip, NULL, "read problem <%s>\n", filename);
70  SCIPinfoMessage(scip, NULL, "============\n");
71  SCIPinfoMessage(scip, NULL, "\n");
72 
73 
74  retcode = SCIPreadProb(scip, filename, NULL);
75 
76  switch( retcode )
77  {
78  case SCIP_NOFILE:
79  SCIPinfoMessage(scip, NULL, "file <%s> not found\n", filename);
80  return SCIP_OKAY;
82  SCIPinfoMessage(scip, NULL, "no reader for input file <%s> available\n", filename);
83  return SCIP_OKAY;
84  case SCIP_READERROR:
85  SCIPinfoMessage(scip, NULL, "error reading file <%s>\n", filename);
86  return SCIP_OKAY;
87  default:
88  SCIP_CALL( retcode );
89  } /*lint !e788*/
90 
91  /*******************
92  * Problem Solving *
93  *******************/
94 
95  /* solve problem */
96  SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
97  SCIPinfoMessage(scip, NULL, "=============\n\n");
98 
99  SCIP_CALL( SCIPsolve(scip) );
100 
101  /*******************
102  * Solution Output *
103  *******************/
104 
105  SCIP_CALL( SCIPgetBoolParam(scip, "misc/outputorigsol", &outputorigsol) );
106  if ( outputorigsol )
107  {
108  SCIP_SOL* bestsol;
109 
110  SCIPinfoMessage(scip, NULL, "\nprimal solution (original space):\n");
111  SCIPinfoMessage(scip, NULL, "=================================\n\n");
112 
113  bestsol = SCIPgetBestSol(scip);
114  if ( bestsol == NULL )
115  SCIPinfoMessage(scip, NULL, "no solution available\n");
116  else
117  {
118  SCIP_SOL* origsol;
119 
120  SCIP_CALL( SCIPcreateSolCopy(scip, &origsol, bestsol) );
121  SCIP_CALL( SCIPretransformSol(scip, origsol) );
122  SCIP_CALL( SCIPprintSol(scip, origsol, NULL, FALSE) );
123  SCIP_CALL( SCIPfreeSol(scip, &origsol) );
124  }
125  }
126  else
127  {
128  SCIPinfoMessage(scip, NULL, "\nprimal solution (transformed space):\n");
129  SCIPinfoMessage(scip, NULL, "====================================\n\n");
130 
132  }
133 
134 
135  /**************
136  * Statistics *
137  **************/
138 
139  SCIPinfoMessage(scip, NULL, "\nStatistics\n");
140  SCIPinfoMessage(scip, NULL, "==========\n\n");
141 
143 
144  return SCIP_OKAY;
145 }
146 
147 /** evaluates command line parameters and runs SCIP appropriately in the given SCIP instance */
149  SCIP* scip, /**< SCIP data structure */
150  int argc, /**< number of shell parameters */
151  char** argv, /**< array with shell parameters */
152  const char* defaultsetname /**< name of default settings file */
153  )
154 { /*lint --e{850}*/
155  char* probname = NULL;
156  char* settingsname = NULL;
157  char* logname = NULL;
158  SCIP_Bool quiet;
159  SCIP_Bool paramerror;
161  SCIP_Bool onlyversion;
162  SCIP_Real primalreference = SCIP_UNKNOWN;
163  SCIP_Real dualreference = SCIP_UNKNOWN;
164  const char* dualrefstring;
165  const char* primalrefstring;
166  int i;
167 
168  /********************
169  * Parse parameters *
170  ********************/
171 
172  quiet = FALSE;
173  paramerror = FALSE;
174  interactive = FALSE;
175  onlyversion = FALSE;
176  primalrefstring = NULL;
177  dualrefstring = NULL;
178 
179  for( i = 1; i < argc; ++i )
180  {
181  if( strcmp(argv[i], "-l") == 0 )
182  {
183  i++;
184  if( i < argc )
185  logname = argv[i];
186  else
187  {
188  printf("missing log filename after parameter '-l'\n");
189  paramerror = TRUE;
190  }
191  }
192  else if( strcmp(argv[i], "-q") == 0 )
193  quiet = TRUE;
194  else if( strcmp(argv[i], "-v") == 0 )
195  onlyversion = TRUE;
196  else if( strcmp(argv[i], "--version") == 0 )
197  onlyversion = TRUE;
198  else if( strcmp(argv[i], "-s") == 0 )
199  {
200  i++;
201  if( i < argc )
202  settingsname = argv[i];
203  else
204  {
205  printf("missing settings filename after parameter '-s'\n");
206  paramerror = TRUE;
207  }
208  }
209  else if( strcmp(argv[i], "-f") == 0 )
210  {
211  i++;
212  if( i < argc )
213  probname = argv[i];
214  else
215  {
216  printf("missing problem filename after parameter '-f'\n");
217  paramerror = TRUE;
218  }
219  }
220  else if( strcmp(argv[i], "-c") == 0 )
221  {
222  i++;
223  if( i < argc )
224  {
225  SCIP_CALL( SCIPaddDialogInputLine(scip, argv[i]) );
226  interactive = TRUE;
227  }
228  else
229  {
230  printf("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  printf("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, (int) sizeof(buffer), file);
255  if( buffer[0] != '\0' )
256  {
257  SCIP_CALL_FINALLY( SCIPaddDialogInputLine(scip, buffer), SCIPfclose(file) );
258  }
259  }
260  SCIPfclose(file);
261  interactive = TRUE;
262  }
263  }
264  else
265  {
266  printf("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
286  {
287  printf("invalid parameter <%s>\n", argv[i]);
288  paramerror = TRUE;
289  }
290  }
291 
292  if( interactive && probname != NULL )
293  {
294  printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
295  paramerror = TRUE;
296  }
297 
298  if( !paramerror )
299  {
300  /***********************************
301  * create log file message handler *
302  ***********************************/
303 
304  if( quiet )
305  {
306  SCIPsetMessagehdlrQuiet(scip, quiet);
307  }
308 
309  if( logname != NULL )
310  {
311  SCIPsetMessagehdlrLogfile(scip, logname);
312  }
313 
314  /***********************************
315  * Version and library information *
316  ***********************************/
317 
318  SCIPprintVersion(scip, NULL);
319  SCIPinfoMessage(scip, NULL, "\n");
320 
322  SCIPinfoMessage(scip, NULL, "\n");
323 
324  if( onlyversion )
325  {
327  SCIPinfoMessage(scip, NULL, "\n");
328  return SCIP_OKAY;
329  }
330 
331  /*****************
332  * Load settings *
333  *****************/
334 
335  if( settingsname != NULL )
336  {
337  SCIP_CALL( readParams(scip, settingsname) );
338  }
339  else if( defaultsetname != NULL )
340  {
341  SCIP_CALL( readParams(scip, defaultsetname) );
342  }
343 
344  /**************
345  * Start SCIP *
346  **************/
347 
348  if( probname != NULL )
349  {
350  SCIP_Bool validatesolve = FALSE;
351 
352  if( primalrefstring != NULL && dualrefstring != NULL )
353  {
354  char *endptr;
355  if( ! SCIPparseReal(scip, primalrefstring, &primalreference, &endptr) ||
356  ! SCIPparseReal(scip, dualrefstring, &dualreference, &endptr) )
357  {
358  printf("error parsing primal and dual reference values for validation: %s %s\n", primalrefstring, dualrefstring);
359  return SCIP_ERROR;
360  }
361  else
362  validatesolve = TRUE;
363  }
364  SCIP_CALL( fromCommandLine(scip, probname) );
365 
366  /* validate the solve */
367  if( validatesolve )
368  {
369  SCIP_CALL( SCIPvalidateSolve(scip, primalreference, dualreference, SCIPfeastol(scip), FALSE, NULL, NULL, NULL) );
370  }
371  }
372  else
373  {
374  SCIPinfoMessage(scip, NULL, "\n");
376  }
377  }
378  else
379  {
380  printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-f <problem>] [-b <batchfile>] [-c \"command\"]\n"
381  " -v, --version : print version and build options\n"
382  " -l <logfile> : copy output into log file\n"
383  " -q : suppress screen messages\n"
384  " -s <settings> : load parameter settings (.set) file\n"
385  " -f <problem> : load and solve problem file\n"
386  " -o <primref> <dualref> : pass primal and dual objective reference values for validation at the end of the solve"
387  " -b <batchfile>: load and execute dialog command batch file (can be used multiple times)\n"
388  " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n\n",
389  argv[0]);
390  }
391 
392  return SCIP_OKAY;
393 }
394 
395 /** creates a SCIP instance with default plugins, evaluates command line parameters, runs SCIP appropriately,
396  * and frees the SCIP instance
397  */
399  int argc, /**< number of shell parameters */
400  char** argv, /**< array with shell parameters */
401  const char* defaultsetname /**< name of default settings file */
402  )
403 {
404  SCIP* scip = NULL;
405 
406  /*********
407  * Setup *
408  *********/
409 
410  /* initialize SCIP */
411  SCIP_CALL( SCIPcreate(&scip) );
412 
413  /* we explicitly enable the use of a debug solution for this main SCIP instance */
414  SCIPenableDebugSol(scip);
415 
416  /* include default SCIP plugins */
418 
419  /**********************************
420  * Process command line arguments *
421  **********************************/
422 
423  SCIP_CALL( SCIPprocessShellArguments(scip, argc, argv, defaultsetname) );
424 
425 
426  /********************
427  * Deinitialization *
428  ********************/
429 
430  SCIP_CALL( SCIPfree(&scip) );
431 
433 
434  return SCIP_OKAY;
435 }
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:39224
#define BMScheckEmptyMemory()
Definition: memory.h:114
SCIP_Real SCIPfeastol(SCIP *scip)
Definition: scip.c:45508
default message handler
#define SCIP_MAXSTRLEN
Definition: def.h:225
SCIP_Bool SCIPparseReal(SCIP *scip, const char *str, SCIP_Real *value, char **endptr)
Definition: scip.c:45705
#define SCIP_CALL_FINALLY(x, y)
Definition: def.h:358
#define FALSE
Definition: def.h:64
static SCIP_RETCODE fromCommandLine(SCIP *scip, const char *filename)
Definition: scipshell.c:53
void SCIPprintExternalCodes(SCIP *scip, FILE *file)
Definition: scip.c:9561
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_RETCODE readParams(SCIP *scip, const char *filename)
Definition: scipshell.c:36
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:696
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
Definition: scip.c:44659
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1336
SCIP_Bool SCIPfileExists(const char *filename)
Definition: misc.c:9513
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
SCIP_RETCODE SCIPcreateSolCopy(SCIP *scip, SCIP_SOL **sol, SCIP_SOL *sourcesol)
Definition: scip.c:37468
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:15846
SCIP command line interface.
SCIP_RETCODE SCIPreadProb(SCIP *scip, const char *filename, const char *extension)
Definition: scip.c:10033
int SCIPfeof(SCIP_FILE *stream)
Definition: fileio.c:214
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:187
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip.c:4404
#define NULL
Definition: lpi_spx1.cpp:137
SCIP_RETCODE SCIPrunShell(int argc, char **argv, const char *defaultsetname)
Definition: scipshell.c:398
#define SCIP_CALL(x)
Definition: def.h:316
#define SCIP_UNKNOWN
Definition: def.h:166
#define SCIP_Bool
Definition: def.h:61
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
void SCIPprintVersion(SCIP *scip, FILE *file)
Definition: scip.c:609
void SCIPprintSysError(const char *message)
Definition: misc.c:9276
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)
Definition: scip.c:47457
void SCIPsetMessagehdlrQuiet(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:1248
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:37806
SCIP_RETCODE SCIPretransformSol(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:39336
SCIP_RETCODE SCIPprocessShellArguments(SCIP *scip, int argc, char **argv, const char *defaultsetname)
Definition: scipshell.c:148
SCIP_RETCODE SCIPstartInteraction(SCIP *scip)
Definition: scip.c:9775
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:39158
void SCIPsetMessagehdlrLogfile(SCIP *scip, const char *filename)
Definition: scip.c:1236
SCIP_RETCODE SCIPreadParams(SCIP *scip, const char *filename)
Definition: scip.c:4924
#define SCIP_Real
Definition: def.h:145
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
default SCIP plugins
void SCIPprintBuildOptions(SCIP *scip, FILE *file)
Definition: scip.c:644
static SCIP_RETCODE interactive(SCIP *scip)
Definition: cmain.c:92
SCIP_RETCODE SCIPaddDialogInputLine(SCIP *scip, const char *inputline)
Definition: scip.c:9725
SCIP callable library.
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:774
void SCIPenableDebugSol(SCIP *scip)
Definition: scip.c:1155
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:38601