from crosscorrelate import test_against_real_data
import numpy as np
import random
import time
import csv
import os
#import requests
from multiprocessing import Pool


def create_population(size):
    params = [0.443, 0.0107, -0.0159, 0.0062, 0.302, -0.0056, 0.196, 1.29, -0.024, 18.9, -0.391, 0.0035, 4.27, 238, 53, 77, 59, 177]
    pop_size = size

    pop_list = []
    for i in range(pop_size):
        newparam = []
        for k in params:
            k = k * np.random.normal(1, 0.3)
            k = round(k, 4)
            newparam.append(k)

        pop_list.append(newparam)
    return pop_list


def create_new_set(setlength):
    new_set = []
    for i in range(setlength):
        new_set.append(round(random.random(),4))
    count = 0
    for k in new_set:
        i = round(k, 4)
        new_set[count] = i
        count += 1

        return new_set


def create_new_population(size, setlength):
    pop_size = size
    pop_list = []
    for i in range(pop_size):
        newparam = create_new_set(setlength)
        pop_list.append(newparam)
    return pop_list


def test_population(pop, best, second, one, two):

    best_fitness = best[1]
    second_fitness = second[1]

    fittest_set = one
    second_set = two
    #print "best: " + str(best_fitness)

    #switching between the tuple and value is confusing and i'm getting upset
    pop_list = []
    for i in pop:
        try:
            result = multi_core_test(cores, max, i)
            fitness = result[1]

            pop_list.append([fitness, i, result])

        except:
            result = (99, 99, 99)




    pop_list.sort()