import os
import numpy as np
from hm2_ogd import ogd, ogd_sliding

#######
# I/O #
#######


# Create text file
def create_file(filename):
    f = open(filename, 'w')
    f.close()


# Write array to a row in the given file
def write_to_file(filename, arr):
    with open(filename, 'a') as f:
        np.savetxt(f, [arr], delimiter=', ', fmt='%1.6f')

########
# Main #
########

num_repeats = 5
learning_rate = 0.02
momentum = 0.9
hidden_activation_fun = 'tanh'
hidden_units = 8

# sliding window
flag_sliding = True
window_size = 1

# load data
data = np.genfromtxt('sea.csv', delimiter=',')

simulation_time = data.shape[0]

# output file
temp_name = '_sliding{}'.format(window_size) if flag_sliding else ''
out_name = 'exps{}_{}_{}_{}_{}'.format(temp_name, learning_rate, momentum, hidden_activation_fun, hidden_units)
filename_acc = os.path.join(os.getcwd(), out_name + '.txt')

# start
for r in range(num_repeats):
    print('Repetition: ', r)

    # start
    if flag_sliding:
        preq_general_accs = ogd_sliding(data=data, simulation_time=simulation_time, hidden_activation_fun=hidden_activation_fun, hidden_units=hidden_units,
                                        learning_rate=learning_rate, momentum=momentum, batch_size=window_size)
    else:
        preq_general_accs = ogd(data=data, simulation_time=simulation_time, hidden_activation_fun=hidden_activation_fun, hidden_units=hidden_units,
                                learning_rate=learning_rate, momentum=momentum)

    # store
    write_to_file(filename_acc, preq_general_accs)
