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