Scippy

SCIP

Solving Constraint Integer Programs

main.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-2019 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 visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file CycleClustering/src/main.c
17  * @brief Main file for C compilation
18  * @author Leon Eifler
19  */
20 
21 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 #include "scip/scip.h"
23 #include "scip/scipdefplugins.h"
24 #include "scip/scipshell.h"
25 #include "scip/message_default.h"
26 #include <string.h>
27 
28 #if defined(_WIN32) || defined(_WIN64)
29 #include <windows.h>
30 #else
31 #include <unistd.h>
32 #endif
33 
34 #include "cycplugins.h"
35 #include "probdata_cyc.h"
36 #include "reader_cyc.h"
37 #include "scip/debug.h"
38 
39 #define COL_MAX_LINELEN 1024
40 
41 /** Read the parameters from the command Line */
42 static
44  SCIP* scip, /**< SCIP data structure */
45  const char* filename /**< parameter file name */
46  )
47 {
48  if( SCIPfileExists(filename) )
49  {
50  /* read params from settingsfile */
51  SCIPinfoMessage(scip, NULL, "reading user parameter file <%s>\n", filename);
52  SCIP_CALL( SCIPreadParams(scip, filename) );
53  }
54  else
55  SCIPinfoMessage(scip, NULL, "user parameter file <%s> not found - using default parameters\n", filename);
56 
57  return SCIP_OKAY;
58 }
59 
60 /** execute the scip-program from the command-line */
61 static
63  SCIP* scip, /**< SCIP data structure */
64  const char* filename, /**< input file name */
65  const char* soluname /**< input file name */
66  )
67 {
68  SCIP_RETCODE retcode;
69  /********************
70  * Problem Creation *
71  ********************/
72 
73  /** @note The message handler should be only fed line by line such the message has the chance to add string in front
74  * of each message
75  */
76  SCIPinfoMessage(scip, NULL, "\n");
77  SCIPinfoMessage(scip, NULL, "read problem <%s>\n", filename);
78  SCIPinfoMessage(scip, NULL, "============\n");
79  SCIPinfoMessage(scip, NULL, "\n");
80 
81  retcode = SCIPreadProb(scip, filename, NULL);
82 
83  switch( retcode )
84  {
85  case SCIP_NOFILE:
86  SCIPinfoMessage(scip, NULL, "file <%s> not found\n", filename);
87  return SCIP_OKAY;
89  SCIPinfoMessage(scip, NULL, "no reader for input file <%s> available\n", filename);
90  return SCIP_OKAY;
91  case SCIP_READERROR:
92  SCIPinfoMessage(scip, NULL, "error reading file <%s>\n", filename);
93  return SCIP_OKAY;
94  default:
95  SCIP_CALL( retcode );
96  } /*lint !e788*/
97 
98  if( soluname != NULL )
99  {
100  retcode = SCIPreadProb(scip, soluname, NULL);
101 
102  switch( retcode )
103  {
104  case SCIP_NOFILE:
105  SCIPinfoMessage(scip, NULL, "file <%s> not found\n", filename);
106  return SCIP_OKAY;
107  case SCIP_PLUGINNOTFOUND:
108  SCIPinfoMessage(scip, NULL, "no reader for input file <%s> available\n", filename);
109  return SCIP_OKAY;
110  case SCIP_READERROR:
111  SCIPinfoMessage(scip, NULL, "error reading file <%s>\n", filename);
112  return SCIP_OKAY;
113  default:
114  SCIP_CALL( retcode );
115  } /*lint !e788*/
116  }
117 
118  /*******************
119  * Problem Solving *
120  *******************/
121 
122  /* solve problem */
123  SCIPinfoMessage(scip, NULL, "\nsolve problem\n");
124  SCIPinfoMessage(scip, NULL, "=============\n\n");
125 
126  SCIP_CALL( SCIPsolve(scip) );
127 
128  /*******************
129  * Solution Output *
130  *******************/
131 
132  SCIPinfoMessage(scip, NULL, "\nprimal solution (transformed space):\n");
133  SCIPinfoMessage(scip, NULL, "====================================\n\n");
134 
135  /**************
136  * Statistics *
137  **************/
138 
139  SCIPinfoMessage(scip, NULL, "\nStatistics\n");
140  SCIPinfoMessage(scip, NULL, "==========\n\n");
142 
143  return SCIP_OKAY;
144 }
145 
146 /** process the arguments and set up the problem */
147 static
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 {
155  char* probname = NULL;
156  char* soluname = NULL;
157  char* settingsname = NULL;
158  char* logname = NULL;
159  char name_file[COL_MAX_LINELEN];
160 
161  SCIP_Bool quiet;
162  SCIP_Bool paramerror;
164  int i;
165 
166  /********************
167  * Parse parameters *
168  ********************/
169 
170  quiet = FALSE;
171  paramerror = FALSE;
172  interactive = (argc == 0);
173 
174  /*lint -e{850} read the arguments from commandLine */
175  for( i = 1; i < argc; ++i )
176  {
177  if( strcmp(argv[i], "-l") == 0 )
178  {
179  i++;
180  if( i < argc )
181  logname = argv[i];
182  else
183  {
184  printf("missing log filename after parameter '-l'\n");
185  paramerror = TRUE;
186  }
187  }
188  else if( strcmp(argv[i], "-q") == 0 )
189  quiet = TRUE;
190  else if( strcmp(argv[i], "-s") == 0 )
191  {
192  i++;
193  if( i < argc )
194  settingsname = argv[i];
195  else
196  {
197  printf("missing settings filename after parameter '-s'\n");
198  paramerror = TRUE;
199  }
200  }
201  else if( strcmp(argv[i], "-f") == 0 )
202  {
203  i++;
204  if( i < argc )
205  {
206  probname = argv[i];
207  (void)SCIPsnprintf( name_file, SCIP_MAXSTRLEN, argv[i] );
208  }
209  else
210  {
211  printf("missing problem filename after parameter '-f'\n");
212  paramerror = TRUE;
213  }
214  }
215  else if( strcmp(argv[i], "-c") == 0 )
216  {
217  i++;
218  if( i < argc )
219  {
220  SCIP_CALL( SCIPaddDialogInputLine(scip, argv[i]) );
221  interactive = TRUE;
222  }
223  else
224  {
225  printf("missing command line after parameter '-c'\n");
226  paramerror = TRUE;
227  }
228  }
229  else if( strcmp(argv[i], "-x") == 0 )
230  {
231  i++;
232  if( i < argc )
233  {
234  soluname = argv[i];
235  }
236  else
237  {
238  printf("missing solution filename after parameter '-x'\n");
239  paramerror = TRUE;
240  }
241  }
242  else if( strcmp(argv[i], "-b") == 0 )
243  {
244  i++;
245  if( i < argc )
246  {
247  SCIP_FILE* file;
248 
249  file = SCIPfopen(argv[i], "r");
250  if( file == NULL )
251  {
252  printf("cannot read command batch file <%s>\n", argv[i]);
253  SCIPprintSysError(argv[i]);
254  paramerror = TRUE;
255  }
256  else
257  {
258  while( !SCIPfeof(file) )
259  {
260  char buffer[SCIP_MAXSTRLEN];
261 
262  (void)SCIPfgets(buffer, (int) sizeof(buffer), file);
263  if( buffer[0] != '\0' )
264  {
265  SCIP_CALL( SCIPaddDialogInputLine(scip, buffer) );
266  }
267  }
268  SCIPfclose(file);
269  interactive = TRUE;
270  }
271  }
272  else
273  {
274  printf("missing command batch filename after parameter '-b'\n");
275  paramerror = TRUE;
276  }
277  }
278  }
279 
280  if( interactive && probname != NULL )
281  {
282  printf("cannot mix batch mode '-c' and '-b' with file mode '-f'\n");
283  paramerror = TRUE;
284  }
285 
286  if( !paramerror )
287  {
288  /***********************************
289  * create log file message handler *
290  ***********************************/
291 
292  if( quiet )
293  {
294  SCIPsetMessagehdlrQuiet(scip, quiet);
295  }
296 
297  if( logname != NULL )
298  {
299  SCIPsetMessagehdlrLogfile(scip, logname);
300  }
301 
302  /***********************************
303  * Version and library information *
304  ***********************************/
305 
306  SCIPprintVersion(scip, NULL);
307  SCIPinfoMessage(scip, NULL, "\n");
308 
310  SCIPinfoMessage(scip, NULL, "\n");
311 
312  /*****************
313  * Load settings *
314  *****************/
315 
316  if( settingsname != NULL )
317  {
318  SCIP_CALL( readParams(scip, settingsname) );
319  }
320  else if( defaultsetname != NULL )
321  {
322  SCIP_CALL( readParams(scip, defaultsetname) );
323  }
324  /**************
325  * Start SCIP *
326  **************/
327 
328  if( probname != NULL )
329  {
330  /* run scip */
331  SCIP_CALL( fromCommandLine(scip, probname, soluname) );
332 
333  }
334  else
335  {
336  SCIPinfoMessage(scip, NULL, "\n");
338  }
339  }
340  else
341  {
342  printf("\nsyntax: %s [-l <logfile>] [-q] [-s <settings>] [-f <problem>]\n"
343  " -l <logfile> : copy output into log file\n"
344  " -q : suppress screen messages\n"
345  " -s <settings> : load parameter settings (.set) file\n"
346  " -f <problem> : load and solve problem file\n\n",
347  argv[0]);
348  }
349 
350  return SCIP_OKAY;
351 }
352 
353 /** Set up the problem-structure and solve the clustering problem */
354 static
356  int argc, /**< number of shell parameters */
357  char** argv, /**< array with shell parameters */
358  const char* defaultsetname /**< name of default settings file */
359  )
360 {
361  SCIP* scip = NULL;
362  /*********
363  * Setup *
364  *********/
365 
366  /* initialize SCIP */
367  SCIP_CALL( SCIPcreate(&scip) );
368 
369 #ifdef WITH_DEBUG_SOLUTION
370  SCIPdebugSolEnable(scip);
371 #endif
372  /* include reader, problemdata*/
374 
375  /**********************************
376  * Process command line arguments *
377  **********************************/
378 
379  SCIP_CALL( processArguments(scip, argc, argv, defaultsetname) );
380 
381  SCIPinfoMessage(scip, NULL, "\n");
382 
383  /* free scip */
384  SCIP_CALL( SCIPfree(&scip) );
385 
387 
388  return SCIP_OKAY;
389 }
390 
391 /** main method */
392 int
394  int argc,
395  char** argv
396  )
397 {
398  SCIP_RETCODE retcode;
399 
400  retcode = SCIPrunCyc(argc, argv, "scip.set");
401 
402  if( retcode != SCIP_OKAY )
403  {
404  SCIPprintError(retcode);
405  return -1;
406  }
407 
408  return 0;
409 }
static SCIP_RETCODE fromCommandLine(SCIP *scip, const char *filename, const char *soluname)
Definition: main.c:62
#define NULL
Definition: def.h:246
#define BMScheckEmptyMemory()
Definition: memory.h:144
default message handler
#define SCIP_MAXSTRLEN
Definition: def.h:267
#define FALSE
Definition: def.h:72
void SCIPprintExternalCodes(SCIP *scip, FILE *file)
Definition: scip_general.c:804
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10253
#define TRUE
Definition: def.h:71
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:338
static SCIP_RETCODE interactive(SCIP *scip)
Definition: cmain.c:90
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
static SCIP_RETCODE readParams(SCIP *scip, const char *filename)
Definition: main.c:43
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:279
static SCIP_RETCODE processArguments(SCIP *scip, int argc, char **argv, const char *defaultsetname)
Definition: main.c:148
SCIP_Bool SCIPfileExists(const char *filename)
Definition: misc.c:10456
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip_solve.c:2577
SCIP command line interface.
SCIP_RETCODE SCIPreadProb(SCIP *scip, const char *filename, const char *extension)
Definition: scip_prob.c:382
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
#define SCIP_CALL(x)
Definition: def.h:358
SCIP_RETCODE SCIPincludeCycPlugins(SCIP *scip)
Definition: cycplugins.c:36
#define SCIP_Bool
Definition: def.h:69
void SCIPprintVersion(SCIP *scip, FILE *file)
Definition: scip_general.c:201
void SCIPprintSysError(const char *message)
Definition: misc.c:10162
void SCIPsetMessagehdlrQuiet(SCIP *scip, SCIP_Bool quiet)
Definition: scip_message.c:191
methods for debugging
problem data for cycle clustering problem
SCIP_RETCODE SCIPstartInteraction(SCIP *scip)
Definition: scip_dialog.c:313
#define COL_MAX_LINELEN
Definition: main.c:39
SCIP plugins for cycle clustering.
file reader for cycle clustering instances
void SCIPsetMessagehdlrLogfile(SCIP *scip, const char *filename)
Definition: scip_message.c:179
SCIP_RETCODE SCIPreadParams(SCIP *scip, const char *filename)
Definition: scip_param.c:842
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
void SCIPprintError(SCIP_RETCODE retcode)
Definition: scip_general.c:266
static SCIP_RETCODE SCIPrunCyc(int argc, char **argv, const char *defaultsetname)
Definition: main.c:355
#define SCIPdebugSolEnable(scip)
Definition: debug.h:281
int main(int argc, char **argv)
Definition: main.c:49
default SCIP plugins
SCIP_RETCODE SCIPaddDialogInputLine(SCIP *scip, const char *inputline)
Definition: scip_dialog.c:263
SCIP callable library.
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:370