-
Notifications
You must be signed in to change notification settings - Fork 2
/
grouping.cpp
268 lines (226 loc) · 6.1 KB
/
grouping.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright (c) 2006 James R. Bruce
RoboCup Small-Size League grouping program
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Compile with:
// g++ -Wall -O2 -o grouping grouping.cpp
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
static int NumGroups = 2;
static const int MaxAllowableCountryBias = 0;
static const char *TeamFile = "teams.txt";
static const bool Debug = false;
//====================================================================//
template <class data>
void swap(data &a,data &b)
{
data t;
t = a;
a = b;
b = t;
}
//====================================================================//
// RNG from Knuth's TAOCP(2), Seminumerical Algorithms, pp 108
//--------------------------------------------------------------------//
class KnuthRng{
static const uint64_t A1 = 271828183ULL;
static const uint64_t A2 = 314159269ULL;
static const uint64_t M = 2147483647ULL; // 2^31-1
uint32_t state0,state1;
public:
KnuthRng()
{state0=1; state1=1;}
void seed(uint32_t s0,uint32_t s1);
uint32_t get();
};
void KnuthRng::seed(uint32_t s0,uint32_t s1)
{
if(s0 == 0) s0 = 1;
if(s1 == 0) s1 = 1;
state0 = (uint64_t)s0 % M;
state1 = (uint64_t)s1 % M;
}
uint32_t KnuthRng::get()
{
uint32_t state2 = (A1*(uint64_t)state1 + A2*(uint64_t)state0) % M;
state0 = state1;
state1 = state2;
return(state2);
}
//====================================================================//
struct TeamEntry{
int rank;
char *country;
char *name;
};
std::vector<TeamEntry> teams;
KnuthRng rng;
const char *GetInt(const char *str,uint32_t &val)
{
char *end = NULL;
val = 0;
if(!str) return(NULL);
errno = 0;
val = strtoul(str,&end,0);
return(errno? NULL : end);
}
int ReadTeamFile(const char *filename)
{
static const int MaxBuf = 256;
char buf[MaxBuf];
char country[MaxBuf];
char name[MaxBuf];
int line = 1;
int last_rank = 1;
FILE *in = fopen(filename,"rt");
if(!in) return(0);
teams.clear();
while(fgets(buf,MaxBuf,in)){
char ch = buf[0];
if(ch>='0' && ch<='9'){
TeamEntry te;
if(sscanf(buf,"%d %256s %256s\n",&te.rank,country,name) == 3){
if(te.rank < last_rank){
printf("Error: non-monotonic rank at line %d\n",line);
return(0);
}
te.country = strdup(country);
te.name = strdup(name);
teams.push_back(te);
last_rank = te.rank;
}else{
printf("Error at line %d\n",line);
return(0);
}
}else if(ch=='#' || ch=='\n'){
// skip comments and empty lines
}else{
printf("Junk at line %d\n",line);
return(0);
}
line++;
}
fclose(in);
return((int)teams.size());
}
bool RandomizeGroups()
{
int n = teams.size();
int i=0,j=0;
while(i < n){
// find subset of list at next rank
while(j<n && teams[i].rank==teams[j].rank) j++;
int r = teams[i].rank;
int nr = j - i;
if(Debug) printf("rank %d: start=%d num=%d\n",r,i,nr);
if(nr % NumGroups != 0){
printf("Error: teams at rank %d not divisible by number of groups\n",r);
printf(" (add dummy entries if needed)\n");
return(false);
}
// permute entries at this rank
for(int k=0; k<nr-1; k++){
uint32_t l = rng.get() % (nr-k);
swap(teams[i+k],teams[i+k+l]);
if(Debug) printf(" %d:[0..%d]\n",l,nr-k-1);
}
if(Debug) printf("\n");
// advance indicies
i = j;
}
return(true);
}
int CalcCountryBias()
{
int bias = 0;
int n = teams.size();
for(int g=0; g<NumGroups; g++){
for(int i=g; i<n; i+=NumGroups){
for(int j=i+NumGroups; j<n; j+=NumGroups){
if(strcmp(teams[i].country,teams[j].country) == 0) bias++;
}
}
}
if(Debug){
printf("country bias = %d\n\n",bias);
}else{
printf(" %d",bias);
fflush(stdout);
}
return(bias);
}
void PrintTeams()
{
for(unsigned i=0; i<teams.size(); i++){
const TeamEntry &te = teams[i];
printf("%d %s (%s)\n",te.rank,te.name,te.country);
}
}
void PrintGroups()
{
int n = teams.size();
for(int g=0; g<NumGroups; g++){
printf("Group %c:\n",'A'+g);
for(int i=g; i<n; i+=NumGroups){
const TeamEntry &te = teams[i];
printf("\t%s (%s)\n",te.name,te.country);
}
}
}
int main(int argc,char **argv)
{
if(argc != 4){
printf("usage:\n grouping <nrOfGroups> <seed 1> <seed 2>\n");
return(1);
}
uint32_t localGroups;
if(!GetInt(argv[1], localGroups)){
printf("invalid #groups\n");
return(3);
}
NumGroups = localGroups;
uint32_t seed0 = 0;
uint32_t seed1 = 0;
if(!GetInt(argv[2],seed0) || !GetInt(argv[3],seed1)){
printf("invalid seed\n");
return(2);
}
printf("seed: 0x%08X 0x%08X\n",seed0,seed1);
int n = ReadTeamFile(TeamFile);
if(n <= 0){
printf("error reading teams\n");
return(3);
}
printf("\n===== Teams (%d) ====\n",n);
PrintTeams();
printf("\n==== Randomizing ====\n");
rng.seed(seed0,seed1);
do{
RandomizeGroups();
}while(CalcCountryBias() > MaxAllowableCountryBias);
if(!Debug) printf("\n");
printf("\n==== Grouping ====\n");
PrintGroups();
return(0);
}