def cross_over_for_genetic(pop, result_cluster, movie_name_arr, movie_had_seen):
    payoff_list = []
    name_list = []
    new_name_list = []
    new_payoff_list = []
    list_for_cross_use = []
    payoff_list, name_list = split_payoff_and_name( pop )
    new_pop = []
    cross_over_location = random.randint(1,len(name_list)-2)
    
    for i in range( len(name_list) ):
        name_coll = name_list[i]
        for j in range( len(result_cluster) ):
            # use a flag variable to mark if there is a match
            flag = 0
            for k in result_cluster[j]:
                if name_coll == movie_name_arr[k]:
                    flag = 1
            if flag == 1:
                for i in range(10000):
                    rand_loc = random.randint(0, len(result_cluster[j])-1 )
                    location = result_cluster[j][rand_loc]
                    if movie_name_arr[location] not in movie_had_seen.keys():
                        list_for_cross_use.append(movie_name_arr[location])
                        break
    
    new_name_list = name_list[0:cross_over_location] + list_for_cross_use[cross_over_location:]
    new_payoff_list = gt.gen_payoff_list(new_name_list)
    new_pop = zip( new_payoff_list, new_name_list )
    
    return new_pop
def cross_over_for_genetic(pop, result_cluster, movie_name_arr,
                           movie_had_seen):
    payoff_list = []
    name_list = []
    new_name_list = []
    new_payoff_list = []
    list_for_cross_use = []
    payoff_list, name_list = split_payoff_and_name(pop)
    new_pop = []
    cross_over_location = random.randint(1, len(name_list) - 2)

    for i in range(len(name_list)):
        name_coll = name_list[i]
        for j in range(len(result_cluster)):
            # use a flag variable to mark if there is a match
            flag = 0
            for k in result_cluster[j]:
                if name_coll == movie_name_arr[k]:
                    flag = 1
            if flag == 1:
                for i in range(10000):
                    rand_loc = random.randint(0, len(result_cluster[j]) - 1)
                    location = result_cluster[j][rand_loc]
                    if movie_name_arr[location] not in movie_had_seen.keys():
                        list_for_cross_use.append(movie_name_arr[location])
                        break

    new_name_list = name_list[0:cross_over_location] + list_for_cross_use[
        cross_over_location:]
    new_payoff_list = gt.gen_payoff_list(new_name_list)
    new_pop = zip(new_payoff_list, new_name_list)

    return new_pop
def mutate_for_genetic( pop, result_cluster, movie_name_arr, movie_had_seen ):
    payoff_list = []
    name_list = []
    new_name_list = []
    new_payoff_list = []
    payoff_list, name_list = split_payoff_and_name( pop )
    new_pop = []
    mutate_location = random.randint( 0, len(name_list)-1 )

    name = name_list[mutate_location]
    for j in range( len(result_cluster) ):
        # use a flag variable to mark if there is a match
        flag = 0
        for k in result_cluster[j]:
            if name == movie_name_arr[k]:
                flag = 1
        if flag == 1:
            for i in range(10000):
                rand_loc = random.randint(0, len(result_cluster[j])-1 )
                location = result_cluster[j][rand_loc]
                if movie_name_arr[location] not in movie_had_seen.keys():
                    new_name_list = name_list[0:mutate_location] + [movie_name_arr[location]] + \
                            name_list[mutate_location+1:]
                    break
                
    new_payoff_list = gt.gen_payoff_list(new_name_list)
    new_pop = zip(new_payoff_list, new_name_list)
    
    return new_pop
def mutate_for_genetic(pop, result_cluster, movie_name_arr, movie_had_seen):
    payoff_list = []
    name_list = []
    new_name_list = []
    new_payoff_list = []
    payoff_list, name_list = split_payoff_and_name(pop)
    new_pop = []
    mutate_location = random.randint(0, len(name_list) - 1)

    name = name_list[mutate_location]
    for j in range(len(result_cluster)):
        # use a flag variable to mark if there is a match
        flag = 0
        for k in result_cluster[j]:
            if name == movie_name_arr[k]:
                flag = 1
        if flag == 1:
            for i in range(10000):
                rand_loc = random.randint(0, len(result_cluster[j]) - 1)
                location = result_cluster[j][rand_loc]
                if movie_name_arr[location] not in movie_had_seen.keys():
                    new_name_list = name_list[0:mutate_location] + [movie_name_arr[location]] + \
                            name_list[mutate_location+1:]
                    break

    new_payoff_list = gt.gen_payoff_list(new_name_list)
    new_pop = zip(new_payoff_list, new_name_list)

    return new_pop