Skip to content

Commit

Permalink
first pass at ideal lexico normal form algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Doolittle committed Aug 26, 2020
1 parent b03fb22 commit 78cdb2b
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion src/LocalPolytope/generators.jl
Expand Up @@ -32,7 +32,7 @@ function generator_facet(BG :: BellGame, PM :: PrepareAndMeasure) :: BellGame
end

"""
Helper function for `generator_facet`.
Helper function for `generator_facet`.
"""
function _perm_increase_lexico_score(
game::Matrix{Int64},
Expand Down Expand Up @@ -140,3 +140,106 @@ function lexico_score(BG :: BellGame) :: Vector{Int64}

map(row -> QMath.base_n_val(row, base), eachrow(game_copy))
end


# function generator(BG :: BellGame, PM :: PrepareAndMeasure) :: BellGame
#
# _lexico_reduce(m :: Matrix{Int64}, target_row :: Int64, )
#
# end


function _lexico_reduce(
m :: Matrix{Int64},
target_row :: Int64,
max_perms :: Vector{Tuple{Vector{Int64},Vector{Int64}}},
allowed_col_perms :: Vector{Vector{Int64}}
# allowed_row_perms :: Vector{Int64},
# PM :: PrepareAndMeasure
)
num_rows = size(m,2)
allowed_row_perms = collect(target_row:num_rows)

println("m : ", m)
println("target_row : ", target_row)
println("allowed_col_perms : ", allowed_col_perms)

current_best_row = m[max_perms[1][1][target_row], max_perms[1][2]]

for allowed_col_perm in allowed_col_perms

new_max_perms = Vector{Tuple{Vector{Int64},Vector{Int64}}}(undef,0)
for perm in max_perms


# find the col permutation ids which maximize each row
max_col_perms = map( b -> sort(allowed_col_perm, by=x->m[b,x], rev=true), perm[1][allowed_row_perms])

# sort row ids by their maximal col permutation
sorted_row_perms = sort( allowed_row_perms, by=b -> m[b,max_col_perms[b-target_row+1]], rev=true)

example_best = m[sorted_row_perms[1],max_col_perms[sorted_row_perms[1]-target_row + 1 ]]
example_best_row = copy(current_best_row)
example_best_row[allowed_col_perm] = example_best

# find all row ids which achieve the maximal permutation
max_row_perms = filter(b -> isequal(example_best, m[b,max_col_perms[b-target_row+1]]), sorted_row_perms)

if current_best_row < example_best_row
current_best_row = example_best_row
new_max_perms = Vector{Tuple{Vector{Int64},Vector{Int64}}}(undef,0)
end

if current_best_row == example_best_row
# update and branch the existing permutation
for max_row_perm in max_row_perms

new_max_perm = copy.(perm)

# swap optimal row into its optimal position
new_max_perm[1][target_row] = max_row_perm
new_max_perm[1][max_row_perm] = target_row

# update allowed columns with the corresponding permutation ids
new_max_perm[2][allowed_col_perm] = max_col_perms[max_row_perm-target_row+1]

push!(new_max_perms, new_max_perm)
end

end
end

if length(new_max_perms) > 0
max_perms = new_max_perms
end
end

best_row_id = max_perms[1][1][target_row]
best_col_ids = max_perms[1][2]
best_m_row = m[best_row_id,best_col_ids]


if target_row < PM.B
# updating the allowed column permutations
new_allowed_col_perms = Vector{Vector{Int64}}(undef,0)
for col_perm in allowed_col_perms
if length(col_perm) == 1
# if we couldn't permute before we still can't
push!(new_allowed_col_perms, col_perm)
elseif length(unique(best_m_row[col_perm])) == 1
# if all elements are the same, we continue to consider the perm group
push!(new_allowed_col_perms, col_perm)
else
for n in unique(best_m_row[col_perm])
sub_group_ids = findall( el -> el == n, best_m_row[col_perm])

push!(new_allowed_col_perms, col_perm[sub_group_ids])
end
end
end

_lexico_reduce(m, target_row + 1, max_perms, new_allowed_col_perms )
end

max_perms
end

0 comments on commit 78cdb2b

Please sign in to comment.