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-2014 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 
60  /********************
61  * Problem Creation *
62  ********************/
63 
64  /** @note The message handler should be only fed line by line such the message has the chance to add string in front
65  * of each message
66  */
67  SCIPinfoMessage(scip, NULL, "\n");
68  SCIPinfoMessage(scip, NULL, "read problem <%s>\n", filename);
69  SCIPinfoMessage(scip, NULL, "============\n");
70  SCIPinfoMessage(scip, NULL, "\n");
71 
72 
73  retcode = SCIPreadProb(scip, filename, NULL);
74 
75  switch( retcode )
76  {
77  case SCIP_NOFILE:
78  SCIPinfoMessage(scip, NULL, "file <%s> not found\n", filename);
79  return SCIP_OKAY;
81  SCIPinfoMessage(scip, NULL, "no reader for input file <%s> available\n", filename);
82  return SCIP_OKAY;
83  case SCIP_READERROR:
84  SCIPinfoMessage(scip, NULL, "error reading file <%s>\n", filename);
85  return SCIP_OKAY;
86  default:
87  SCIP_CALL( retcode );
88  } /*lint !e788*/
89 
90  /*******************
91  * Problem Solving *
92  *******************/
93 
94  /* solve problem */
95  SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
96  SCIPinfoMessage(scip, NULL, "=============\n\n");
97 
98  SCIP_CALL( SCIPsolve(scip) );
99 
100  SCIPinfoMessage(scip, NULL, "\nprimal solution:\n");
101  SCIPinfoMessage(scip, NULL, "================\n\n");
103 
104 
105  /**************
106  * Statistics *
107  **************/
108 
109  SCIPinfoMessage(scip, NULL, "\nStatistics\n");
110  SCIPinfoMessage(scip, NULL, "==========\n\n");
111 
113 
114  return SCIP_OKAY;
115 }
116 
117 /** evaluates command line parameters and runs SCIP appropriately in the given SCIP instance */
119  SCIP* scip, /**< SCIP data structure */
120  int argc, /**< number of shell parameters */
121  char** argv, /**< array with shell parameters */
122  const char* defaultsetname /**< name of default settings file */
123  )
124 { /*lint --e{850}*/
125  char* probname = NULL;
126  char* settingsname = NULL;
127  char* logname = NULL;
128  SCIP_Bool quiet;
129  SCIP_Bool paramerror;
130  SCIP_Bool interactive;
131  int i;
132 
133  /********************
134  * Parse parameters *
135  ********************/
136 
137  quiet = FALSE;
138  paramerror = FALSE;
139  interactive = FALSE;
140  for( i = 1; i < argc; ++i )
141  {
142  if( strcmp(argv[i], "-l") == 0 )
143  {
144  i++;
145  if( i < argc )
146  logname = argv[i];
147  else
148  {
149  printf("missing log filename after parameter '-l'\n");
150  paramerror = TRUE;
151  }
152  }
153  else if( strcmp(argv[i], "-q") == 0 )
154  quiet = TRUE;
155  else if( strcmp(argv[i], "-s") == 0 )
156  {
157  i++;
158  if( i < argc )
159  settingsname = argv[i];
160  else
161  {
162  printf("missing settings filename after parameter '-s'\n");
163  paramerror = TRUE;
164  }
165  }
166  else if( strcmp(argv[i], "-f") == 0 )
167  {
168  i++;
169  if( i < argc )
170  probname = argv[i];
171  else
172  {
173  printf("missing problem filename after parameter '-f'\n");
174  paramerror = TRUE;
175  }
176  }
177  else if( strcmp(argv[i], "-c") == 0 )
178  {
179  i++;
180  if( i < argc )
181  {
182  SCIP_CALL( SCIPaddDialogInputLine(scip, argv[i]) );
183  interactive = TRUE;
184  }
185  else
186  {
187  printf("missing command line after parameter '-c'\n");
188  paramerror = TRUE;
189  }
190  }
191  else if( strcmp(argv[i], "-b") == 0 )
192  {
193  i++;
194  if( i < argc )
195  {
196  SCIP_FILE* file;
197 
198  file = SCIPfopen(argv[i], "r");
199  if( file == NULL )
200  {
201  printf("cannot read command batch file <%s>\n", argv[i]);
202  SCIPprintSysError(argv[i]);
203  paramerror = TRUE;
204  }
205  else
206  {
207  while( !SCIPfeof(file) )
208  {
209  char buffer[SCIP_MAXSTRLEN];
210 
211  (void)SCIPfgets(buffer, (int) sizeof(buffer), file);
212  if( buffer[0] != '\0' )
213  {
214  SCIP_CALL( SCIPaddDialogInputLine(scip, buffer) );
215  }
216  }
217  SCIPfclose(file);
218  interactive = TRUE;
219  }
220  }
221  else
222  {
223  printf("missing command batch filename after parameter '-b'\n");
224  paramerror = TRUE;
225  }
226  }
227  else
228  {
229  printf("invalid parameter <%s>\n", argv[i]);
230  paramerror = TRUE;
231  }
232  }
233  if( interactive && probname != NULL )
234  {
235  printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
236  paramerror = TRUE;
237  }
238 
239  if( !paramerror )
240  {
241  /***********************************
242  * create log file message handler *
243  ***********************************/
244 
245  if( quiet )
246  {
247  SCIPsetMessagehdlrQuiet(scip, quiet);
248  }
249 
250  if( logname != NULL )
251  {
252  SCIPsetMessagehdlrLogfile(scip, logname);
253  }
254 
255  /***********************************
256  * Version and library information *
257  ***********************************/
258 
259  SCIPprintVersion(scip, NULL);
260  SCIPinfoMessage(scip, NULL, "\n");
261 
263  SCIPinfoMessage(scip, NULL, "\n");
264 
265  /*****************
266  * Load settings *
267  *****************/
268 
269  if( settingsname != NULL )
270  {
271  SCIP_CALL( readParams(scip, settingsname) );
272  }
273  else if( defaultsetname != NULL )
274  {
275  SCIP_CALL( readParams(scip, defaultsetname) );
276  }
277 
278  /**************
279  * Start SCIP *
280  **************/
281 
282  if( probname != NULL )
283  {
284  SCIP_CALL( fromCommandLine(scip, probname) );
285  }
286  else
287  {
288  SCIPinfoMessage(scip, NULL, "\n");
290  }
291  }
292  else
293  {
294  printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-f <problem>] [-b <batchfile>] [-c \"command\"]\n"
295  " -l <logfile> : copy output into log file\n"
296  " -q : suppress screen messages\n"
297  " -s <settings> : load parameter settings (.set) file\n"
298  " -f <problem> : load and solve problem file\n"
299  " -b <batchfile>: load and execute dialog command batch file (can be used multiple times)\n"
300  " -c \"command\" : execute single line of dialog commands (can be used multiple times)\n\n",
301  argv[0]);
302  }
303 
304  return SCIP_OKAY;
305 }
306 
307 /** creates a SCIP instance with default plugins, evaluates command line parameters, runs SCIP appropriately,
308  * and frees the SCIP instance
309  */
311  int argc, /**< number of shell parameters */
312  char** argv, /**< array with shell parameters */
313  const char* defaultsetname /**< name of default settings file */
314  )
315 {
316  SCIP* scip = NULL;
317 
318  /*********
319  * Setup *
320  *********/
321 
322  /* initialize SCIP */
323  SCIP_CALL( SCIPcreate(&scip) );
324 
325  /* include default SCIP plugins */
327 
328  /**********************************
329  * Process command line arguments *
330  **********************************/
331 
332  SCIP_CALL( SCIPprocessShellArguments(scip, argc, argv, defaultsetname) );
333 
334 
335  /********************
336  * Deinitialization *
337  ********************/
338 
339  SCIP_CALL( SCIPfree(&scip) );
340 
342 
343  return SCIP_OKAY;
344 }
345