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