Skip to content

Commit 8ee9e73

Browse files
committed
Using HRepIterator and hyperplane to check binding inequalities.
1 parent 2f38cc3 commit 8ee9e73

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

src/vertex_enumeration.jl

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
function vertex_enumeration(g::NormalFormGame{2}; plib=getlibraryfor(2, Float64))
1+
import Polyhedra: hyperplane
2+
3+
function vertex_enumeration(g::NormalFormGame{2};
4+
plib=getlibraryfor(2, Float64))
25

36
c = Channel(0)
47
task = vertex_enumeration_task(c, g, plib)
@@ -22,53 +25,73 @@ function vertex_enumeration_task(c::Channel,
2225

2326
end
2427

25-
function _vertex_enumeration_producer(c::Channel,
26-
g::NormalFormGame{2},
27-
plib)
28+
function _vertex_enumeration_producer{T}(c::Channel,
29+
g::NormalFormGame{2, T},
30+
plib)
2831

2932
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)
3333

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]
34+
# create Representation for player 1
35+
H1, V1, H2, V2 = construction_BRP(g, plib)
36+
37+
ZERO_LABELING_BITS = (1 << (n+m)) - (1 << m)
38+
COMPLETE_LABELING_BITS = 1 << (n+m) - 1
3739

38-
if labeling1 == ZERO_LABELING
40+
for v1 in vreps(V1)
41+
labelings_bits1 = labelings_bits(v1, H1)
42+
if labelings_bits1 == ZERO_LABELING_BITS
3943
continue
4044
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
45+
for v2 in vreps(V2)
46+
labelings_bits2 = labelings_bits(v2, H2)
47+
if xor(labelings_bits1, labelings_bits2) == COMPLETE_LABELING_BITS
4748
put!(c, (_get_mixed_action(v1),
4849
_get_mixed_action(v2)))
4950
end
5051
end
5152
end
52-
end
5353

54-
function _BRS_vertices(g::NormalFormGame, idx::Integer, plib)
54+
end
5555

56-
opp_idx = idx % 2 + 1
57-
B = g.players[opp_idx].payoff_array
58-
n, m = size(B)
56+
function construction_BRP{T}(g::NormalFormGame{2, T}, plib)
5957

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])
58+
n, m = size(g.players[1].payoff_array)
6459

65-
hrep = SimpleHRepresentation(D, b)
66-
p = polyhedron(hrep, plib)
67-
vertices = SimpleVRepresentation(p).V
60+
# create Representation for player 1
61+
C = Matrix{T}(n+m, n)
62+
C[1:m, :] = g.players[2].payoff_array
63+
C[m+1:end, :] = -eye(T, n)
64+
b1 = Vector{T}(n+m)
65+
b1[1:m] = one(T)
66+
b1[m+1:end] = zero(T)
67+
H1 = SimpleHRepresentation(C, b1)
68+
p1 = polyhedron(H1, plib)
69+
V1 = SimpleVRepresentation(p1)
70+
71+
# create Representation for player 2
72+
D = Matrix{T}(n+m, m)
73+
D[1:m, :] = -eye(T, m)
74+
D[m+1:end, :] = g.players[1].payoff_array
75+
b2 = Vector{T}(n+m)
76+
b2[1:m] = zero(T)
77+
b2[m+1:end] = one(T)
78+
H2 = SimpleHRepresentation(D, b2)
79+
p2 = polyhedron(H2, plib)
80+
V2 = SimpleVRepresentation(p2)
81+
82+
return H1, V1, H2, V2
83+
end
6884

69-
return D, b, vertices
85+
function labelings_bits(v::VRepElement, p::HRep)
86+
b = 0
87+
for (i, h) in enumerate(hreps(p))
88+
if v in hyperplane(h)
89+
b += 1 << (i-1)
90+
end
91+
end
92+
return b
7093
end
7194

72-
function _get_mixed_action(a)
95+
function _get_mixed_action(a::Vector)
7396
return a ./ sum(a)
7497
end

0 commit comments

Comments
 (0)