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  int i;
163 
164  /********************
165  * Parse parameters *
166  ********************/
167 
168  quiet = FALSE;
169  paramerror = FALSE;
170  interactive = FALSE;
171  onlyversion = FALSE;
172  for( i = 1; i < argc; ++i )
173  {
174  if( strcmp(argv[i], "-l") == 0 )
175  {
176  i++;
177  if( i < argc )
178  logname = argv[i];
179  else
180  {
181  printf("missing log filename after parameter '-l'\n");
182  paramerror = TRUE;
183  }
184  }
185  else if( strcmp(argv[i], "-q") == 0 )
186  quiet = TRUE;
187  else if( strcmp(argv[i], "-v") == 0 )
188  onlyversion = TRUE;
189  else if( strcmp(argv[i], "--version") == 0 )
190  onlyversion = TRUE;
191  else if( strcmp(argv[i], "-s") == 0 )
192  {
193  i++;
194  if( i < argc )
195  settingsname = argv[i];
196  else
197  {
198  printf("missing settings filename after parameter '-s'\n");
199  paramerror = TRUE;
200  }
201  }
202  else if( strcmp(argv[i], "-f") == 0 )
203  {
204  i++;
205  if( i < argc )
206  probname = argv[i];
207  else
208  {
209  printf("missing problem filename after parameter '-f'\n");
210  paramerror = TRUE;
211  }
212  }
213  else if( strcmp(argv[i], "-c") == 0 )
214  {
215  i++;
216  if( i < argc )
217  {
218  SCIP_CALL( SCIPaddDialogInputLine(scip, argv[i]) );
219  interactive = TRUE;
220  }
221  else
222  {
223  printf("missing command line after parameter '-c'\n");
224  paramerror = TRUE;
225  }
226  }
227  else if( strcmp(argv[i], "-b") == 0 )
228  {
229  i++;
230  if( i < argc )
231  {
232  SCIP_FILE* file;
233 
234  file = SCIPfopen(argv[i], "r");
235  if( file == NULL )
236  {
237  printf("cannot read command batch file <%s>\n", argv[i]);
238  SCIPprintSysError(argv[i]);
239  paramerror = TRUE;
240  }
241  else
242  {
243  while( !SCIPfeof(file) )
244  {
245  char buffer[SCIP_MAXSTRLEN];
246 
247  (void)SCIPfgets(buffer, (int) sizeof(buffer), file);
248  if( buffer[0] != '\0' )
249  {
250  SCIP_CALL_FINALLY( SCIPaddDialogInputLine(scip, buffer), SCIPfclose(file) );
251  }
252  }
253  SCIPfclose(file);
254  interactive = TRUE;
255  }
256  }
257  else
258  {
259  printf("missing command batch filename after parameter '-b'\n");
260  paramerror = TRUE;
261  }
262  }
263  else
264  {
265  printf("invalid parameter <%s>\n", argv[i]);
266  paramerror = TRUE;
267  }
268  }
269  if( interactive && probname != NULL )
270  {
271  printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
272  paramerror = TRUE;
273  }
274 
275  if( !paramerror )
276  {
277  /***********************************
278  * create log file message handler *
279  ***********************************/
280 
281  if( quiet )
282  {
283  SCIPsetMessagehdlrQuiet(scip, quiet);
284  }
285 
286  if( logname != NULL )
287  {
288  SCIPsetMessagehdlrLogfile(scip, logname);
289  }
290 
291  /***********************************
292  * Version and library information *
293  ***********************************/
294 
295  SCIPprintVersion(scip, NULL);
296  SCIPinfoMessage(scip, NULL, "\n");
297 
299  SCIPinfoMessage(scip, NULL, "\n");
300 
301  if( onlyversion )
302  {
304  SCIPinfoMessage(scip, NULL, "\n");
305  return SCIP_OKAY;
306  }
307 
308  /*****************
309  * Load settings *
310  *****************/
311 
312  if( settingsname != NULL )
313  {
314  SCIP_CALL( readParams(scip, settingsname) );
315  }
316  else if( defaultsetname != NULL )
317  {
318  SCIP_CALL( readParams(scip, defaultsetname) );
319  }
320 
321  /**************
322  * Start SCIP *
323  **************/
324 
325  if( probname != NULL )
326  {
327  SCIP_CALL( fromCommandLine(scip, probname) );
328  }
329  else
330  {
331  SCIPinfoMessage(scip, NULL, "\n");
333  }
334  }
335  else
336  {
337  printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-f <problem>] [-b <batchfile>] [-c \"command\"]\n"
338  " -v, --version : print version and build options\n"
339  " -l <logfile> : copy output into log file\n"
340  " -q : suppress screen messages\n"
341  " -s <settings> : load parameter settings (.set) file\n"
342  " -f <problem> : load and solve problem file\n"
343  " -b <batchfile>: load and execute dialog command batch file (can be used multiple times)\n"
344  " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n\n",
345  argv[0]);
346  }
347 
348  return SCIP_OKAY;
349 }
350 
351 /** creates a SCIP instance with default plugins, evaluates command line parameters, runs SCIP appropriately,
352  * and frees the SCIP instance
353  */
355  int argc, /**< number of shell parameters */
356  char** argv, /**< array with shell parameters */
357  const char* defaultsetname /**< name of default settings file */
358  )
359 {
360  SCIP* scip = NULL;
361 
362  /*********
363  * Setup *
364  *********/
365 
366  /* initialize SCIP */
367  SCIP_CALL( SCIPcreate(&scip) );
368 
369  /* we explicitly enable the use of a debug solution for this main SCIP instance */
370  SCIPenableDebugSol(scip);
371 
372  /* include default SCIP plugins */
374 
375  /**********************************
376  * Process command line arguments *
377  **********************************/
378 
379  SCIP_CALL( SCIPprocessShellArguments(scip, argc, argv, defaultsetname) );
380 
381 
382  /********************
383  * Deinitialization *
384  ********************/
385 
386  SCIP_CALL( SCIPfree(&scip) );
387 
389 
390  return SCIP_OKAY;
391 }
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
Definition: scip.c:38997
#define BMScheckEmptyMemory()
Definition: memory.h:110
default message handler
#define SCIP_MAXSTRLEN
Definition: def.h:215
#define SCIP_CALL_FINALLY(x, y)
Definition: def.h:348
#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:9526
#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:44425
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:37295
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:15777
SCIP command line interface.
SCIP_RETCODE SCIPreadProb(SCIP *scip, const char *filename, const char *extension)
Definition: scip.c:9998
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:4369
#define NULL
Definition: lpi_spx1.cpp:137
SCIP_RETCODE SCIPrunShell(int argc, char **argv, const char *defaultsetname)
Definition: scipshell.c:354
#define SCIP_CALL(x)
Definition: def.h:306
#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
void SCIPsetMessagehdlrQuiet(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:1248
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:37631
SCIP_RETCODE SCIPretransformSol(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:39109
SCIP_RETCODE SCIPprocessShellArguments(SCIP *scip, int argc, char **argv, const char *defaultsetname)
Definition: scipshell.c:148
SCIP_RETCODE SCIPstartInteraction(SCIP *scip)
Definition: scip.c:9740
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:38931
void SCIPsetMessagehdlrLogfile(SCIP *scip, const char *filename)
Definition: scip.c:1236
SCIP_RETCODE SCIPreadParams(SCIP *scip, const char *filename)
Definition: scip.c:4889
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:9690
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:38421