Scippy

SCIP

Solving Constraint Integer Programs

genRandomLOPInstance.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 genRandomLOPInstance.c
17  * @brief generate a random linear ordering problem instance
18  * @author Marc Pfetsch
19  */
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 
25 
26 /* the following is copied from <scip/misc.c> */
27 
28 /* define own random numbers or take library version depending on the following define */
29 #ifdef NO_RAND_R
30 
31 #define SCIP_RAND_MAX 32767
32 
33 /** returns a random number between 0 and SCIP_RAND_MAX */
34 static
35 int getRand(
36  unsigned int* seedp /**< pointer to seed value */
37  )
38 {
39  SCIP_Longint nextseed;
40 
41  assert(seedp != NULL);
42 
43  nextseed = (*seedp) * 1103515245 + 12345;
44  *seedp = (unsigned int)nextseed;
45 
46  return (int)((unsigned int)(nextseed/(2*(SCIP_RAND_MAX+1))) % (SCIP_RAND_MAX+1));
47 }
48 
49 #else
50 
51 #define SCIP_RAND_MAX RAND_MAX
52 
53 /** returns a random number between 0 and SCIP_RAND_MAX */
54 static
55 int getRand(
56  unsigned int* seedp /**< pointer to seed value */
57  )
58 {
59  return rand_r(seedp);
60 }
61 
62 #endif
63 
64 /** returns a random integer between minrandval and maxrandval */
65 static
67  int minrandval, /**< minimal value to return */
68  int maxrandval, /**< maximal value to return */
69  unsigned int* seedp /**< pointer to seed value */
70  )
71 {
72  return minrandval + (int) ((maxrandval - minrandval + 1)*(double)getRand(seedp)/(SCIP_RAND_MAX+1.0));
73 }
74 
75 int main(int argc, char** argv)
76 {
77  int n;
78  int d;
79  int i;
80  int j;
81  unsigned int seed;
82  FILE *file;
83 
84  if ( argc != 4 )
85  {
86  printf("usage: %s <filename> <n> <d>.\n", argv[0]);
87  return 1;
88  }
89 
90  n = atoi(argv[2]);
91  d = atoi(argv[3]);
92  seed = 0;
93  assert( n > 0 );
94  assert( d > 0 );
95 
96  /* open file */
97  file = fopen(argv[1], "w");
98  if ( file == NULL )
99  {
100  printf("Could not open file %s.\n", argv[1]);
101  return 1;
102  }
103 
104  /* write comment line and size*/
105  fprintf(file, "Randomly generated LOP instance.\n");
106  fprintf(file, "%d\n", n);
107  for (i = 0; i < n; ++i)
108  {
109  for (j = 0; j < n; ++j)
110  fprintf(file, "%d ", getRandomInt(0, d, &seed));
111  fprintf(file, "\n");
112  }
113 
114  printf("Wrote random LOP instance to %s\n", argv[1]);
115  printf("Size: %d\n", n);
116  printf("Entries: {0, ..., %d}\n", d);
117 
118  return 0;
119 }
#define NULL
Definition: def.h:246
int main(int argc, char **argv)
static int getRand(unsigned int *seedp)
#define SCIP_RAND_MAX
static int getRandomInt(int minrandval, int maxrandval, unsigned int *seedp)
#define SCIP_Longint
Definition: def.h:142