-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreinforcement-subtract-borda-set.h
177 lines (155 loc) · 5.13 KB
/
reinforcement-subtract-borda-set.h
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
unsigned int REINF_SUB_A_VOTES[V][C];
unsigned int REINF_SUB_A_RESULT[C];
unsigned int V_REINF_SUB_A;
unsigned int REINF_SUB_B_VOTES[V][C];
unsigned int REINF_SUB_B_RESULT[C];
unsigned int V_REINF_SUB_B;
struct subsubset { unsigned int arr[V]; };
unsigned int reinforcementSubtractCheck(unsigned int votes[V][C], unsigned int res[C], struct subsubset sub, unsigned int sep, unsigned int numVotes, unsigned int print, unsigned int negated, unsigned int blocking) {
unsigned int i = 0;
unsigned int j = 0;
unsigned int s = 0;
unsigned int taken[V];
unsigned int rest[V - sep];
for (i = 0; i < V; i++) {
taken[i] = 0;
}
unsigned int votes1[V][C];
for (i = 0; i < sep; i++) {
s = sub.arr[i];
taken[s] = 1;
for (j = 0; j < C; j++) {
votes1[i][j] = votes[s][j];
}
}
for (i = sep; i < V; i++) {
for (j = 0; j < C; j++) {
votes1[i][j] = C;
}
}
for (i = 0, j = 0; i < numVotes; i++) {
if (taken[i] == 0) {
rest[j] = i;
j++;
}
}
unsigned int votes2[V][C];
for (i = 0; i < numVotes - sep; i++) {
s = rest[i];
for (j = 0; j < C; j++) {
votes2[i][j] = votes[s][j];
}
}
for (i = numVotes - sep; i < V; i++) {
for (j = 0; j < C; j++) {
votes2[i][j] = C;
}
}
struct result elect = voting2(votes1, sep);
unsigned int full = 1;
for (i = 0; i < C; i++) {
if (elect.arr[i] == 0) {
full = 0;
}
}
if (!full && !blocking) { return 0; }
assume (full);
struct result elect2 = voting2(votes2, numVotes - sep);
unsigned int nonemptyOutcome = 0;
for (i = 0; i < C; i++) {
if (elect2.arr[i] != 0) {
nonemptyOutcome = 1;
}
}
if (!nonemptyOutcome && !blocking) { return 0; }
assume (nonemptyOutcome);
unsigned int exists = 0;
for (i = 0; i < C; i++) {
assume (negated || res[i] == elect2.arr[i]);
if (res[i] != elect2.arr[i]) {
exists = 1;
}
}
assume (!negated || exists);
if (print) {
for (i = 0; i < sep; i++) {
for (j = 0; j < C; j++) {
REINF_SUB_A_VOTES[i][j] = votes1[i][j];
}
}
for (i = sep; i < V; i++) {
for (j = 0; j < C; j++) {
REINF_SUB_A_VOTES[i][j] = votes1[i][j];
}
}
for (i = 0; i < C; i++) {
REINF_SUB_A_RESULT[i] = elect.arr[i];
}
V_REINF_SUB_A = sep;
unsigned int diff = numVotes - sep;
for (i = 0; i < diff; i++) {
for (j = 0; j < C; j++) {
REINF_SUB_B_VOTES[i][j] = votes2[i][j];
}
}
for (i = diff; i < V; i++) {
for (j = 0; j < C; j++) {
REINF_SUB_B_VOTES[i][j] = votes2[i][j];
}
}
for (i = 0; i < C; i++) {
REINF_SUB_B_RESULT[i] = elect2.arr[i];
}
V_REINF_SUB_B = diff;
return 1;
}
return 0;
}
unsigned int reinforceSubRec(unsigned int votes[V][C], unsigned int res[C], struct subsubset sub,
unsigned int start, unsigned int end, unsigned int index,
unsigned int sep, unsigned int numVotes,
unsigned int print, unsigned int negated, unsigned int blocking) {
unsigned int i = 0;
unsigned int printNow = nondet_uint();
assume (printNow == 0 || printNow == 1);
unsigned int hasPrinted = 0;
if (index == sep) {
printNow = (printNow && print);
assume (hasPrinted == 0 || printNow == 0);
printNow = reinforcementSubtractCheck(votes, res, sub, sep, numVotes, printNow, negated, blocking);
return (printNow || hasPrinted);
}
for (i = start; i <= end && sep - index <= end - i + 1; i++) {
sub.arr[index] = i;
printNow = nondet_uint();
assume (printNow == 0 || printNow == 1);
printNow = (printNow && print);
assume (hasPrinted == 0 || printNow == 0);
printNow = reinforceSubRec(votes, res, sub, i + 1, end, index + 1, sep, numVotes, printNow, negated, blocking);
hasPrinted = (printNow || hasPrinted);
}
return hasPrinted;
}
unsigned int reinforcement_subtract(unsigned int votes[V][C], unsigned int res[C], unsigned int sep, unsigned int numVotes, unsigned int print, unsigned int negated, unsigned int blocking) {
assume (0 < sep && sep * 2 <= numVotes);
unsigned int i = 0;
unsigned int s = 0;
struct subsubset sub;
unsigned int taken[V];
for (i = 0; i < V; i++) {
taken[i] = 0;
}
if (negated) {
for (i = 0; i < sep; i++) {
s = nondet_uint();
assume (0 <= s && s < sep);
sub.arr[i] = s;
assume (taken[s] == 0);
taken[s] = 1;
}
print = reinforcementSubtractCheck(votes, res, sub, sep, numVotes, print, negated, blocking);
} else {
print = reinforceSubRec(votes, res, sub, 0, numVotes - 1, 0, sep, numVotes, print, negated, blocking);
}
return print;
}