def main ():
    print 'n = ' + str(n_val)
    libsvm_filename = count_mistakes_files[n_val]
    ##################### Generate D1,D2 from libsvm #############################
    dataset = file_read (libsvm_filename)
    partition = partition_data (dataset)
    training_set = partition.training
    testing_set = partition.testing
    
    ######## Learn best performing hyperparameters for all four algorithms #######
    best_hyperparams = generate_best_hyperparams (n_val, training_set, testing_set)
    best_hyperparams_perc = best_hyperparams[0]
    best_hyperparams_perc_margin = best_hyperparams[1]
    best_hyperparams_winnow = best_hyperparams[2]
    best_hyperparams_winnow_margin = best_hyperparams[3]
    
    ######## Count cumulative mistakes for all four algorithms on 50K dataset ####
    print '\ncounting cumulative mistakes on N = 50,000 dataset...'
    print 'current algorithm: perceptron without margin'
    cumulative_mistakes_perc = perceptron_test (dataset, best_hyperparams_perc.weight_vector, best_hyperparams_perc.bias, instance_intervals)
    print 'mistakes made, M: ' + str(cumulative_mistakes_perc[len(cumulative_mistakes_perc) - 1])
    print 'current algorithm: perceptron with margin'
    cumulative_mistakes_perc_margin = perceptron_test (dataset, best_hyperparams_perc_margin.weight_vector, best_hyperparams_perc_margin.bias, instance_intervals)
    print 'mistakes made, M: ' + str(cumulative_mistakes_perc[len(cumulative_mistakes_perc) - 1])
    print 'current algorithm: winnow without margin'
    cumulative_mistakes_winnow = winnow_test (dataset, best_hyperparams_winnow.weight_vector, instance_intervals)
    print 'mistakes made, M: ' + str(cumulative_mistakes_perc[len(cumulative_mistakes_perc) - 1])
    print 'current algorithm: winnow with margin'
    cumulative_mistakes_winnow_margin = winnow_test (dataset, best_hyperparams_winnow_margin.weight_vector, instance_intervals)
    print 'mistakes made, M: ' + str(cumulative_mistakes_perc[len(cumulative_mistakes_perc) - 1])
    ################ Plot mistakes versus instances for current n value ##########
    plot_cumulative_mistakes_versus_instances (cumulative_mistakes_perc, cumulative_mistakes_perc_margin, cumulative_mistakes_winnow, cumulative_mistakes_winnow_margin)
def learn_batch_performance_params (m_val):
    testing_file = batch_performance_datasets[m_val][0]
    training_file = batch_performance_datasets[m_val][1]
    
    testing_set = file_read (testing_file)
    training_set = file_read (training_file)
    
    partition = partition_data (testing_set)
    D1 = partition.training
    D2 = partition.testing
    
    perceptron_params = perceptron_learn_batch_performance_params (D1, D2)
    perceptron_params_without_margin = perceptron_params.get_params (0)
    perceptron_params_with_margin = perceptron_params.get_params (1)
    print 'perceptron without margin acc(D2): ' + str(perceptron_params_without_margin.accuracy)
    print 'perceptron with margin acc(D2): ' + str(perceptron_params_with_margin.accuracy)
    
    print '\n\nRunning perceptron on Test set...'
    
    perceptron_trained_without_margin = perceptron_train (batch_performance_n, training_set, perceptron_params_without_margin.gamma, perceptron_params_without_margin.eta)
    perceptron_trained_with_margin = perceptron_train (batch_performance_n, training_set, perceptron_params_with_margin.gamma, perceptron_params_with_margin.eta)
    perceptron_mistakes_without_margin = perceptron_test (testing_set, perceptron_trained_without_margin[0], perceptron_trained_without_margin[1])
    perceptron_mistakes_with_margin = perceptron_test (testing_set, perceptron_trained_with_margin[0], perceptron_trained_with_margin[1])
    print 'perecptron without margin acc(Test): ' + str(1.0 - float(perceptron_mistakes_without_margin[len(perceptron_mistakes_without_margin)-1]) / len (testing_set))
    print 'perecptron with margin acc(Test): ' + str(1.0 - float(perceptron_mistakes_with_margin[len(perceptron_mistakes_with_margin)-1]) / len (testing_set))
    
    winnow_params = winnow_learn_batch_performance_params (D1, D2)
    winnow_params_without_margin = winnow_params.get_params (0)
    winnow_params_with_margin = winnow_params.get_params (1)
    print 'winnow without margin acc(D2): ' + str(winnow_params_without_margin.accuracy)
    print 'winnow with margin acc(D2): ' + str(winnow_params_with_margin.accuracy)
    
    print '\n\nRunning winnow on Test set...'
    
    winnow_trained_without_margin = winnow_train (batch_performance_n, training_set, winnow_params_without_margin.gamma, winnow_params_with_margin.eta)
    winnow_trained_with_margin = winnow_train (batch_performance_n, training_set, winnow_params_with_margin.gamma, winnow_params_with_margin.eta)
    winnow_mistakes_without_margin = winnow_test (testing_set, winnow_trained_without_margin)
    winnow_mistakes_with_margin = winnow_test (testing_set, winnow_trained_with_margin)
    print 'winnow without margin acc(Test): ' + str(1.0 - float(winnow_mistakes_without_margin[len(winnow_mistakes_without_margin)-1]) / len(testing_set))
    print 'winnow with margin acc(Test): ' + str(1.0 - float(winnow_mistakes_with_margin[len(winnow_mistakes_with_margin)-1]) / len(testing_set))