-
Notifications
You must be signed in to change notification settings - Fork 1
/
z3.cpp
259 lines (228 loc) · 5.11 KB
/
z3.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
#include<iostream>
#include<vector>
#include<algorithm>
#include "z3++.h"
using namespace std;
using namespace z3;
#define nyi { cout<<"nyi "<<__LINE__<<"\n"; exit(44); }
#define PRINT(X) cout<<""#X<<":"<<(X)<<"\n";
template<typename T>
vector<T> sorted(vector<T> a){
std::sort(begin(a),end(a));
return a;
}
template<typename T>
void print_lines(T t){
for(auto elem:t){
cout<<elem<<"\n";
}
}
template<typename T>
vector<T>& operator|=(vector<T> &a,T t){
a.push_back(t);
return a;
}
template<typename T>
vector<T> range(T start,T lim){
vector<T> r;
for(auto i=start;i<lim;i++){
r|=i;
}
return r;
}
template<typename T>
vector<T> range(T lim){
return range(T{0},lim);
}
template<typename Func,typename T>
auto mapf(Func f,vector<T> v){
vector<decltype(f(v[0]))> r;
for(auto a:v){
r|=f(a);
}
return r;
}
template<typename T>
string as_string(T t){
stringstream ss;
ss<<t;
return ss.str();
}
template<typename A,typename B>
vector<B> seconds(vector<pair<A,B>> a){
vector<B> r;
for(auto elem:a) r|=elem.second;
return r;
}
template<typename A,typename B>
ostream& operator<<(ostream& o,pair<A,B> const& p){
return o<<"("<<p.first<<","<<p.second<<")";
}
template<typename T>
T sum(vector<T> a){
T r{0};
for(auto elem:a){
r+=elem;
}
return r;
}
template<typename T>
vector<T> tail(vector<T> a){
vector<T> r;
for(auto i:range(size_t{1},a.size())){
r|=a[i];
}
return r;
}
//start program-specific code
using Team=int;
unsigned qual_pts(int event_size,int rank){
//TODO: FIXME
assert(rank>0);
assert(rank<=event_size);
assert(event_size>0);
return event_size-rank;
}
void add_event(string event_code,vector<Team> teams,context &c,solver &s){
auto prefix=event_code+"_";
//qual
auto team_ranks=mapf(
[&](auto team){
auto name=prefix+"rank_"+as_string(team);
auto t=c.int_const(name.c_str());
s.add(t>=1 && t<=int(teams.size()));
return make_pair(team,t);
},
teams
);
auto declare_not_equal=[&](auto a){
/*for(auto i:range(a.size())){
for(auto j:range(i)){
s.add(a[i]!=a[j]);
}
}*/
//distinct(a);
expr_vector t(c);
for(auto &elem:a){
t.push_back(elem);
}
s.add(distinct(t));
};
declare_not_equal(seconds(team_ranks));
auto ranks=range(size_t(1),1+teams.size());
mapf(
[&](auto const& rank_var){
//PRINT(rank_var);
auto x=c.int_const( (prefix+"qual_pts_"+as_string(rank_var.first)).c_str() );
for(auto rank:ranks){
s.add(x==int(qual_pts(teams.size(),rank)) || rank_var.second!=int(rank));
}
return 0;
},
team_ranks
);
auto decl_team_number=[&](string name){
auto x=c.int_const(name.c_str());
auto v=(x==teams[0]);
for(auto a:tail(teams)){
//v|=(x==a);
v= v || (x==a);
}
s.add(v);
return x;
};
auto decl_maybe_team_number=[&](string name){
auto x=c.int_const(name.c_str());
auto v=(x==teams[0]);
for(auto a:tail(teams)){
//v|=(x==a);
v= v || (x==a);
}
//v= v||(x==0); //if the number is 0 then there was not a team there.
v= v||0; //if the number is 0 then there was not a team there.
s.add(v);
return x;
};
//alliance selection
vector<z3::expr> places;
for(auto i:range(1,9)){
places|=decl_team_number(prefix+"capt"+as_string(i));
places|=decl_team_number(prefix+"a"+as_string(i)+"_p1");
places|=decl_team_number(prefix+"a"+as_string(i)+"_p2");
}
declare_not_equal(places);
vector<z3::expr> backup_slots;
for(auto i:range(1,9)){
backup_slots|=decl_maybe_team_number(prefix+"a"+as_string(i)+"_backup");
}
for(auto i:range(backup_slots.size())){
for(auto j:range(i+1,backup_slots.size())){
s.add(backup_slots[i]==0 || backup_slots[i]!=backup_slots[j]);
}
}
for(auto& elem:places){
for(auto &x:backup_slots){
s.add(elem!=x);
}
}
//next, put in all the logic to make who can be captain correct
//0r could just skip this and say that lots of captains could decide to take themselves out of the
//tournament.
//captains need to be ranked lower than the captains above them.
//and all captains need to be ranked higher than all non-included teams
//all the places exist
//teams are all different
//elims
/*
tournament structure:
1v8
2v7
3v6
4v5
1/8 v 4/5
2/7 v 3/6
1/4/5/8 v 2/3/6/7
*/
//q1 in r2/r3/b2/b3 (how many matches to win)
//awards
//total points
}
void demo(){
/*for each of the events:
* add event
*for each of the events that has finished:
* add details about how it went
* including who's no longer eligible for chairmans
*after have all of the constraints, need to run it with the push/pop
logic that will ask each of the interesting questions
* */
context c;
solver s(c);
add_event("2018wase",range(100,124),c,s);
switch(s.check()){
case unsat:
cout<<"unsat\n";
break;
case sat:{
cout<<"sat\n";
model m=s.get_model();
vector<pair<string,string>> data;
for(unsigned i=0;i<m.size();i++){
func_decl v=m[i];
assert(v.arity()==0);//don't know why
//cout<<v.name()<<" = "<<m.get_const_interp(v)<<"\n";
data|=make_pair(as_string(v.name()),as_string(m.get_const_interp(v)));
}
print_lines(sorted(data));
break;
}
case unknown:
cout<<"Unknown\n";
break;
default:
assert(0);
}
}
int main(){
demo();
}