-
Notifications
You must be signed in to change notification settings - Fork 1
/
meanpayoff.cpp
186 lines (146 loc) · 4.28 KB
/
meanpayoff.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
// Author: Romain Brenguier <rbrengui@cs.ox.ac.uk>
// A mean payoff solver using Gain and Bias equations.
// This assumes the library Z3 is installed on your computer.
#include<vector>
#include<set>
#include<list>
#include<iostream>
#include<fstream>
#include<string>
#include"z3++.h"
using namespace z3;
/* Encoding of Mean payoff games:
States are represented by integers.
Even states are minimizer states.
Odd states are maximizer states.
The initial state is 0.
*/
class Transition {
int source;
int dest;
int weight;
public :
Transition(int s,int d, int w) {
dest=d ;
source=s;
weight=w;
}
int get_source(){ return source; }
int get_dest(){ return dest; }
int get_weight(){ return weight; }
};
class Game{
std::vector<std::list<Transition> > succ;
public:
void add_transition(Transition t) {
int s = t.get_source();
succ.resize(s+1);
succ[s].push_back(t);
}
void add_transition(int s,int d, int w=0) {
add_transition(Transition(s,d,w));
}
std::set<int> states() {
std::set<int> s;
for(unsigned i=0;i<succ.size();i++) {
s.insert(i);
}
return s;
}
std::list<Transition> successors(int state) {
return succ[state];
}
};
expr fold(func_decl f, std::list<expr> e){
std::list<expr>::iterator it = e.begin();
expr res = *it;
for(++it; it != e.end(); ++it)
res = f(res, *it);
return res;
}
expr disj(std::list<expr> e){
std::list<expr>::iterator it = e.begin();
expr res(*it);
for(++it; it != e.end(); ++it)
res = res || *it;
return res;
}
expr conj(std::list<expr> e){
std::list<expr>::iterator it = e.begin();
expr res(*it);
for(++it; it != e.end(); ++it)
res = res && *it;
return res;
}
void solve_game(Game g){
context c;
sort R = c.real_sort();
sort state_sort = c.int_sort();
func_decl mp = c.function("mp",1,&state_sort,R);
func_decl bias = c.function("bias",1,&state_sort,R);
func_decl max = c.function("max",R,R,R);
func_decl min = c.function("min",R,R,R);
expr x = c.real_const("x");
expr y = c.real_const("y");
std::set<int> states = g.states();
solver s(c);
// s.add(forall(x,y,max(x,y) == (ite(x < y,y,x))));
//s.add(forall(x,y,min(x,y) == (ite(x < y,x,y))));
for(std::set<int>::iterator it = states.begin();
it != states.end(); ++it)
{
std::list<expr> e;
std::list<expr> f;
std::list<Transition> successors = g.successors(*it);
for(std::list<Transition>::iterator sit=successors.begin(); sit!=successors.end(); ++sit)
{
if (*it % 2 == 0)
e.push_back(mp(*it) <= mp(sit->get_dest()));
else
e.push_back(mp(*it) >= mp(sit->get_dest()));
// TO CHANGE: We need to take the max/min of the bias
f.push_back((mp(*it) == mp(sit->get_dest())) && (mp(*it) + bias(*it)) == sit->get_weight() + bias(sit->get_dest()));
}
// if (*it % 2 == 0) s.add(mp(*it) == fold(min,e)); else s.add(mp(*it) == fold(max,e));
s.add(conj(e));
if(! f.empty())
s.add(disj(f));
}
//std::cout << "Equations : " << std::endl << s.to_smt2() << "\n";
std::cout << "Equations : " << std::endl << s<< "\n";
switch (s.check()) {
case unsat: std::cout << "equations are not satisfiable\n"; break;
case sat: std::cout << "equations are satisfiable\n"; break;
case unknown: std::cout << "unknown\n"; break;
}
model m = s.get_model();
std::cout << "Model: "<< std::endl << m << std::endl;
std::cout << std::endl << " Results: " << std::endl;
set_param("pp.decimal", true);
for(std::set<int>::iterator it=states.begin(); it!= states.end(); ++it)
std::cout << "State: "<< *it << " MP: " << m.eval(mp(*it)) << " Bias: " << m.eval(bias(*it)) << std::endl;
}
int main(int argc, char* argv[]) {
Game g;
if(argc < 2) {
std::cout << "usage: meanpayoff <file>" << std::endl;
std::cout << "using a dummy game instead" << std::endl;
g.add_transition(0,1,1);
g.add_transition(1,0,2);
}
else {
std::ifstream file(argv[1]);
if(file.is_open()) {
int a,b,c,line=1;
while(file >> a >> b >> c) {
line++;
g.add_transition(a,b,c);
}
file.close();
std::cout << line-1 << " lines read" << std::endl;
}
else std::cout << "Unable to open file";
}
solve_game(g);
return 0;
}