Kaggle competition: Conway's Reverse Game of Life 2020
def hyperopt_gb_score(params):
clf = GradientBoostingClassifier(**params)
 current_score = cross_val_score(clf, X_train_feature_creation2, target, cv=10 ).mean()
print(current_score, params)
 return current_score
space_gb = {
'n_estimators': hp.choice('n_estimators', range(100, 1000)) ,
'max_depth': hp.choice('max_depth', np.arange(2, 10, dtype=int))
}
best = fmin(fn=hyperopt_gb_score, space=space_gb, algo=tpe.suggest, max_evals=10)
 print('best:')
 print(best )<compute_train_metric>
sample_submission_df = pd.read_csv('.. /input/conways-reverse-game-of-life-2020/sample_submission.csv', index_col='id')
 test_df = pd.read_csv('.. /input/conways-reverse-game-of-life-2020/test.csv', index_col='id')
 deltas = test_df['delta'].values
boards = csv_to_numpy_list(test_df )
params2 = space_eval(space_gb, best)
 params2<train_model>
SIZE = 25
empty_board = np.zeros(( SIZE,SIZE), dtype=bool)
 def v(c):
return -(SIZE * c[0] + c[1] + 1)
 def dead_clauses(res, c, x):
for i1 in range(0, 6):
for i2 in range(i1+1, 7):
for i3 in range(i2+1, 8):
a = [v(x[i])for i in range(8)]
a[i1], a[i2], a[i3] = -a[i1], -a[i2], -a[i3]
res.append(a)
 for i1 in range(0, 7):
for i2 in range(i1+1, 8):
a = [v(x[i])if i < 8 else -v(c)for i in range(9)]
a[i1], a[i2] = -a[i1], -a[i2]
res.append(a)
 def live_clauses(res, c, x):
for i1 in range(0, 5):
for i2 in range(i1+1, 6):
for i3 in range(i2+1, 7):
for i4 in range(i3+1, 8):
res.append([-v(x[i1]), -v(x[i2]), -v(x[i3]), -v(x[i4])])
 for i1 in range(0, 7):
for i2 in range(i1+1, 8):
a = [v(x[i])if i < 8 else v(c)for i in range(9)if i != i1 and i != i2]
res.append(a)
 for i1 in range(0, 8):
a = [v(x[i])for i in range(8)if i != i1]
res.append(a)
 def board_clauses(board, use_opt = True):
res, opt1, opt2 = [], [], []
for i in range(SIZE):
for j in range(SIZE):
x = [(( i + k % 3 - 1)% SIZE,(j + k // 3 - 1)% SIZE)for k in range(9)if k != 4]
if board[i,j]:
live_clauses(res,(i, j), x)
 else:
dead_clauses(res,(i, j), x)
 if use_opt:
y = [(( i + k % 5 - 2)% SIZE,(j + k // 5 - 2)% SIZE)for k in range(25)]
if sum(board[ii,jj] for ii,jj in y)< 1:
res.append([-v(( i, j)) ])
 elif sum(board[ii,jj] for ii,jj in x)< 1:
opt1.append([-v(( i, j)) ])
 elif sum(board[ii,jj] for ii,jj in x)< 2:
opt2.append([-v(( i, j)) ])
 return res, opt1, opt2
gradient_boosting2 = GradientBoostingClassifier(**params2)
 gradient_boosting2.fit(X_train_feature_creation2, target)
 Y_pred2 = gradient_boosting2.predict(X_test_feature_creation2 ).astype(int)
 gradient_boosting2.score(X_train_feature_creation2, target)
 acc_gradient_boosting2 = round(gradient_boosting2.score(X_train_feature_creation2, target)* 100, 2)
 acc_gradient_boosting2<save_to_csv>
N = len(deltas)
 score = 0
for n in tqdm(range(N), total=N):
clauses, opt1, opt2 = board_clauses(boards[n], use_opt = False)
 solution = pycosat.solve(clauses)
 if isinstance(solution, str):
print(f'{n} not solved!')
 continue
board = np.array(solution[:SIZE**2])< 0
sample_submission_df.loc[test_df.index[n]] = 1 * board
board = life_step(board.reshape(SIZE,SIZE))
d = np.sum(board ^ boards[n])
 score += d / 625
print(score/N )
pd.DataFrame({'Survived': test2['Survived_LR'].astype(int)}, \
index=testdf.index ).reset_index().to_csv('survived_LR3.csv', index=False)
 pd.DataFrame({'Survived': test2['Survived_AF'].astype(int)}, \
index=testdf.index ).reset_index().to_csv('survived_Autofeat3.csv', index=False)
 pd.DataFrame({'Survived': Y_pred2}, \
index=testdf.index ).reset_index().to_csv('survived_GBC3.csv', index=False )<init_hyperparams>
N = 1
for n in tqdm(range(N), total=N):
T = min(deltas[n], 3)
 board = np.tile(empty_board,(T+1, 1, 1))
board[0] = boards[n]
solvers = [None for _ in range(T)]
opt = [None for _ in range(T)]
os = [0 for _ in range(T)]
oe = [0 for _ in range(T)]
t = 0
while 0 <= t and t < T:
if solvers[t] is None:
clauses, opt1, opt2 = board_clauses(board[t])
 solution = pycosat.solve(clauses)
 if not isinstance(solution, str):
if t == T - 1:
print(t, '!!', end=" ")
 t += 1
board[t] = np.array(solution[:SIZE**2] ).reshape(SIZE,SIZE)< 0
continue
else:
print(t, '??', end=" ")
 random.shuffle(opt1)
 random.shuffle(opt2)
 opt[t] = opt1 + opt2
os[t] = len(opt[t])+1
oe[t] = len(opt[t])+1
solvers[t] = pycosat.itersolve(clauses + opt[t])
 print(len(opt[t]), end=" ")
 try:
solution = next(solvers[t])
 if oe[t] - os[t] > 1:
os[t] =(os[t]+oe[t])//2
solvers[t] = pycosat.itersolve(clauses + opt[t][:(os[t]+oe[t])//2])
 print(( os[t]+oe[t])//2, end=" ")
 else:
print(t, '++', end=" ")
 t += 1
board[t] = np.array(solution[:SIZE**2] ).reshape(SIZE,SIZE)< 0
except Exception as err:
if solvers[t] is not None and(os[t]+oe[t])//2 > 0:
if os[t] == oe[t]:
os[t] = 0
oe[t] = len(opt[t])+1
elif oe[t] - os[t] > 1:
oe[t] =(os[t]+oe[t])//2
else:
os[t] -= 1
oe[t] -= 1
solvers[t] = pycosat.itersolve(clauses + opt[t][:(os[t]+oe[t])//2])
 print(( os[t]+oe[t])//2, end=" ")
 else:
print(t, '--', end=" ")
 solvers[t] = None
opt[t] = None
t -= 1
if t == T:
sample_submission_df.loc[test_df.index[n]] = 1 * board[T].ravel()
plot_3d(board, index )
LB_LR3 = 0.67942
LB_Autofeat3 = 0.69377
LB_GBC3 = 0.83253<create_dataframe>
sample_submission_df.to_csv("submission.csv", index=True )
models = pd.DataFrame({
'Model': ['Simple rule','Linear Regression without Autofeat', 'Linear Regression with Autofeat',
'GradientBoostingClassifier with Autofeat'],
'LB_for_16_features': [LB_simple_rule, LB_LR16, LB_Autofeat16, LB_GBC16],
'LB_for_3opt_features': [LB_simple_rule, LB_LR3, LB_Autofeat3, LB_GBC3]} )<sort_values>
submission_df = pd.read_csv('./submission.csv', index_col='id' )