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