|
| 1 | +function vertex_enumeration(g::NormalFormGame{2}; plib=getlibraryfor(2, Float64)) |
| 2 | + |
| 3 | + c = Channel(0) |
| 4 | + task = vertex_enumeration_task(c, g, plib) |
| 5 | + bind(c, task) |
| 6 | + schedule(task) |
| 7 | + NEs = Tuple{Vector{Real}, Vector{Real}}[NE for NE in c] |
| 8 | + |
| 9 | + return NEs |
| 10 | + |
| 11 | +end |
| 12 | + |
| 13 | +function vertex_enumeration_task(c::Channel, |
| 14 | + g::NormalFormGame{2}, |
| 15 | + plib) |
| 16 | + |
| 17 | + task = Task( |
| 18 | + () -> _vertex_enumeration_producer(c, g, plib) |
| 19 | + ) |
| 20 | + |
| 21 | + return task |
| 22 | + |
| 23 | +end |
| 24 | + |
| 25 | +function _vertex_enumeration_producer(c::Channel, |
| 26 | + g::NormalFormGame{2}, |
| 27 | + plib) |
| 28 | + |
| 29 | + n, m = size(g.players[1].payoff_array) |
| 30 | + BRSv = [_BRS_vertices(g, opp_idx, plib) for opp_idx in 1:2] |
| 31 | + ZERO_LABELING = BitArray(vcat(zeros(m), ones(n))) |
| 32 | + COMPLETE_LABELING = trues(n+m) |
| 33 | + |
| 34 | + for i in 1:size(BRSv[1][3])[1] |
| 35 | + v1 = BRSv[1][3][i, :] |
| 36 | + labeling1 = *(BRSv[1][1], v1) .≈ BRSv[1][2] |
| 37 | + |
| 38 | + if labeling1 == ZERO_LABELING |
| 39 | + continue |
| 40 | + end |
| 41 | + |
| 42 | + for j in 1:size(BRSv[2][3])[1] |
| 43 | + v2 = BRSv[2][3][j, :] |
| 44 | + labeling2 = *(BRSv[2][1], v2) .≈ BRSv[2][2] |
| 45 | + |
| 46 | + if xor.(labeling1, labeling2) == COMPLETE_LABELING |
| 47 | + put!(c, (_get_mixed_action(v1), |
| 48 | + _get_mixed_action(v2))) |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +function _BRS_vertices(g::NormalFormGame, idx::Integer, plib) |
| 55 | + |
| 56 | + opp_idx = idx % 2 + 1 |
| 57 | + B = g.players[opp_idx].payoff_array |
| 58 | + n, m = size(B) |
| 59 | + |
| 60 | + arr = [B, -eye(m)] |
| 61 | + D = vcat(arr[idx], arr[opp_idx]) |
| 62 | + vec = [ones(Float64, n), zeros(Float64, m)] |
| 63 | + b = vcat(vec[idx], vec[opp_idx]) |
| 64 | + |
| 65 | + hrep = SimpleHRepresentation(D, b) |
| 66 | + p = polyhedron(hrep, plib) |
| 67 | + vertices = SimpleVRepresentation(p).V |
| 68 | + |
| 69 | + return D, b, vertices |
| 70 | +end |
| 71 | + |
| 72 | +function _get_mixed_action(a) |
| 73 | + return a ./ sum(a) |
| 74 | +end |
0 commit comments