import numpy as np
import sympy as sp
import random
import time
SIZE = 30
random.seed(0)
def gen_diag():
return [random.randint(0, 1) for i in range(SIZE*2 - 1)]
def diag_to_mat(diag):
return [diag[a:a+SIZE] for a in range(SIZE-1, -1, -1)]
def diag_to_det(diag):
matrix = diag_to_mat(diag)
return np.linalg.det(matrix)
def best_diags(diags, keep):
return sorted(diags, key=diag_to_det, reverse=True)[:keep]
def improve(diag):
old_diag = diag
really_old_diag = []
while really_old_diag != old_diag:
really_old_diag = old_diag
for flip_at in range(SIZE * 2 - 1):
new_diag = old_diag[:]
new_diag[flip_at] ^= 1
old_diag = max(old_diag, new_diag, key=diag_to_det)
return old_diag
overall_best_score = 0
time.clock()
while time.clock() < 500:
best = improve(gen_diag())
best_score = diag_to_det(best)
if best_score > overall_best_score:
overall_best_score = best_score
overall_best = best
print(time.clock(), sp.Matrix(diag_to_mat(overall_best)).det(), ''.join(map(str,overall_best)))
mat = diag_to_mat(overall_best)
sym_mat = sp.Matrix(mat)
print(overall_best)
print(sym_mat.det())