Scippy

SCIP

Solving Constraint Integer Programs

reader_lop.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-2020 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 scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file reader_lop.c
17  * @brief linear ordering file reader
18  * @author Marc Pfetsch
19  *
20  * This file implements the reader/parser used to read linear ordering problems. For more details see \ref READER. The
21  * data should be given in LOLIB format, see <a href="http://www.optsicom.es/lolib/">LOLIB</a>.
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include <assert.h>
27 #include <string.h>
28 
29 #include "cons_lop.h"
30 #include "reader_lop.h"
31 
32 #define READER_NAME "lopreader"
33 #define READER_DESC "file reader for linear ordering problems"
34 #define READER_EXTENSION "lop"
35 
36 
37 /* ----------------- auxiliary functions ------------------------ */
38 
39 /** read weight matrix from file (in LOLIB format)
40  *
41  * Format:
42  * comment line
43  * n = # of elements
44  * weight matrix (n times n double matrix)
45  */
46 static
48  SCIP* scip, /**< SCIP data structure */
49  const char* filename, /**< name of file to read */
50  int* n, /**< pointer to store the number of elements on exit */
51  SCIP_Real*** W /**< pointer to store the weight matrix on exit */
52  )
53 {
54  char s[SCIP_MAXSTRLEN];
55  FILE *file;
56  int status;
57  int i, j;
58 
59  assert( n != NULL );
60  assert( W != NULL );
61 
62  /* open file */
63  file = fopen(filename, "r");
64  if ( file == NULL )
65  {
66  SCIPerrorMessage("Could not open file <%s>.\n", filename);
67  SCIPprintSysError(filename);
68  return SCIP_NOFILE;
69  }
70 
71  /* skip comment line */
72  if ( fgets(s, SCIP_MAXSTRLEN, file) == NULL )
73  {
74  SCIPerrorMessage("Error reading file <%s>.\n", filename);
75  return SCIP_READERROR;
76  }
77 
78  /* read number of elements */
79  status = fscanf(file, "%d", n);
80  if ( ! status )
81  {
82  SCIPerrorMessage("Reading of number of elements failed.\n");
83  return SCIP_READERROR;
84  }
85  assert( 0 < *n );
86 
87  /* set up matrix */
88  SCIP_CALL( SCIPallocBlockMemoryArray(scip, W, *n) );
89  for (i = 0; i < *n; ++i)
90  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &((*W)[i]), *n) ); /*lint !e866*/
91 
92  /* read weight matrix */
93  for (i = 0; i < *n; ++i)
94  {
95  for (j = 0; j < *n; ++j)
96  {
97  SCIP_Real val;
98  status = fscanf(file, "%lf", &val);
99  if ( ! status )
100  {
101  SCIPerrorMessage("Reading failed.\n");
102  return SCIP_READERROR;
103  }
104  (*W)[i][j] = val;
105  }
106  }
107  fclose( file );
108 
109  return SCIP_OKAY;
110 }
111 
112 /** get problem name
113  *
114  * Returns NULL on error
115  */
116 static
118  const char* filename, /**< input filename */
119  char* probname, /**< output problemname */
120  int maxSize /**< maximum size of probname */
121  )
122 {
123  int i = 0;
124  int j = 0;
125  int l;
126 
127  /* first find end of string */
128  while ( filename[i] != 0)
129  ++i;
130  l = i;
131 
132  /* go back until '.' or '/' or '\' appears */
133  while ((i > 0) && (filename[i] != '.') && (filename[i] != '/') && (filename[i] != '\\'))
134  --i;
135 
136  /* if we found '.', search for '/' or '\\' */
137  if (filename[i] == '.')
138  {
139  l = i;
140  while ((i > 0) && (filename[i] != '/') && (filename[i] != '\\'))
141  --i;
142  }
143 
144  /* correct counter */
145  if ((filename[i] == '/') || (filename[i] == '\\'))
146  ++i;
147 
148  /* copy name */
149  while ( (i < l) && (filename[i] != 0) )
150  {
151  probname[j++] = filename[i++];
152  if (j > maxSize-1)
153  return SCIP_ERROR;
154  }
155  probname[j] = 0;
156 
157  return SCIP_OKAY;
158 }
159 
160 
161 
162 /**@name Callback methods
163  *
164  * @{
165  */
166 
167 /** problem reading method of reader */
168 static
169 SCIP_DECL_READERREAD(LOPreaderRead)
170 { /*lint --e{715}*/
171  char name[SCIP_MAXSTRLEN];
172  SCIP_VAR*** vars;
173  SCIP_CONS* cons;
174  SCIP_Real** W;
175  int n;
176  int i;
177  int j;
178 
179  assert( scip != NULL );
180  assert( result != NULL );
181 
182  *result = SCIP_DIDNOTRUN;
183 
184  SCIPinfoMessage(scip, NULL, "File name:\t\t%s\n", filename);
185 
186  /* read problem */
187  SCIP_CALL( LOPreadFile(scip, filename, &n, &W) );
188 
189  /* generate problem name from filename */
190  SCIP_CALL( getProblemName(filename, name, SCIP_MAXSTRLEN) );
191 
192  SCIPinfoMessage(scip, NULL, "Problem name:\t\t%s\n", name);
193  SCIPinfoMessage(scip, NULL, "Number of elements:\t%d\n\n", n);
194 
195  /* create problem */
197 
198  /* set maximization */
200 
201  /* generate variables */
203  for (i = 0; i < n; ++i)
204  {
205  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(vars[i]), n) );
206  for (j = 0; j < n; ++j)
207  {
208  if (j != i)
209  {
210  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "x#%d#%d", i, j);
211  SCIP_CALL( SCIPcreateVar(scip, &(vars[i][j]), name, 0.0, 1.0, W[i][j], SCIP_VARTYPE_BINARY,
212  TRUE, FALSE, NULL, NULL, NULL, NULL, NULL));
213  SCIP_CALL( SCIPaddVar(scip, vars[i][j]) );
214  }
215  else
216  vars[i][j] = NULL;
217  }
218  }
219 
220  /* generate linear ordering constraint */
221  SCIP_CALL( SCIPcreateConsLOP(scip, &cons, "LOP", n, vars, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE,
222  FALSE, FALSE, FALSE, FALSE));
223  SCIP_CALL( SCIPaddCons(scip, cons) );
224  SCIP_CALL( SCIPreleaseCons(scip, &cons) );
225 
226  /* print small model */
227  if ( n <= 10 )
228  {
230  SCIPinfoMessage(scip, NULL, "\n");
231  }
232 
233  /* free memory */
234  for (i = 0; i < n; ++i)
235  {
236  for (j = 0; j < n; ++j)
237  {
238  if (j != i)
239  {
240  SCIP_CALL( SCIPreleaseVar(scip, &(vars[i][j])) );
241  }
242  }
243  SCIPfreeBlockMemoryArray(scip, &(vars[i]), n);
244  SCIPfreeBlockMemoryArray(scip, &(W[i]), n);
245  }
246  SCIPfreeBlockMemoryArray(scip, &vars, n);
248 
249  *result = SCIP_SUCCESS;
250 
251  return SCIP_OKAY;
252 }
253 
254 /**@} */
255 
256 
257 /**@name Interface methods
258  *
259  * @{
260  */
261 
262 /** includes the linear ordering file reader in SCIP */
264  SCIP* scip /**< SCIP data structure */
265  )
266 {
267  SCIP_READER* reader;
268 
269  /* include reader */
271  assert( reader != NULL );
272 
273  SCIP_CALL( SCIPsetReaderRead(scip, reader, LOPreaderRead) );
274 
275  return SCIP_OKAY;
276 }
277 
278 /**@} */
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:97
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:80
SCIP_RETCODE SCIPcreateConsLOP(SCIP *scip, SCIP_CONS **cons, const char *name, int n, SCIP_VAR ***vars, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
Definition: cons_lop.c:1188
#define SCIP_MAXSTRLEN
Definition: def.h:273
SCIP_RETCODE SCIPsetObjsense(SCIP *scip, SCIP_OBJSENSE objsense)
Definition: scip_prob.c:1240
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:186
#define FALSE
Definition: def.h:73
static SCIP_RETCODE LOPreadFile(SCIP *scip, const char *filename, int *n, SCIP_Real ***W)
Definition: reader_lop.c:47
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
SCIP_RETCODE SCIPincludeReaderLOP(SCIP *scip)
Definition: reader_lop.c:263
#define SCIPerrorMessage
Definition: pub_message.h:55
constraint handler for linear ordering constraints
#define READER_DESC
Definition: reader_lop.c:33
#define NULL
Definition: lpi_spx1.cpp:155
SCIP_RETCODE SCIPprintOrigProblem(SCIP *scip, FILE *file, const char *extension, SCIP_Bool genericnames)
#define SCIP_CALL(x)
Definition: def.h:364
SCIP_RETCODE SCIPcreateVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
Definition: scip_var.c:105
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip_reader.c:100
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1666
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10590
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1252
#define SCIP_Real
Definition: def.h:163
static SCIP_DECL_READERREAD(LOPreaderRead)
Definition: reader_lop.c:169
void SCIPprintSysError(const char *message)
Definition: misc.c:10499
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2764
#define READER_NAME
Definition: reader_lop.c:32
linear ordering file reader
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:199
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1110
#define READER_EXTENSION
Definition: reader_lop.c:34
static SCIP_RETCODE getProblemName(const char *filename, char *probname, int maxSize)
Definition: reader_lop.c:117
SCIP_RETCODE SCIPcreateProbBasic(SCIP *scip, const char *name)
Definition: scip_prob.c:170