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