def main():
    # num_cores = multiprocessing.cpu_count()
    # results = Parallel(n_jobs=num_cores)(delayed(run_testcase)(n[i], d[i], x[i], y[i])
    #                      for i in trange(t, desc='Total', ascii=True))
    #
    # with open("output.txt", 'w') as outfile:
    #     for case, val, chromosome in enumerate(results):
    #         outfile.write('Case: %d \n' % (i+1))
    #         for f in chromosome:
    #             outfile.write(str(f) + ' ')
    #         outfile.write(" value: %d \n" % val)


    infile = open('input_examples.txt', 'r')
    outfile = open('output.txt', 'w')
    # outfile2 = open('output2.txt', 'w')

    graph = Graph()

    test_cases = int(infile.readline())
    for i in range(test_cases):
        (n, d) = infile.readline().split()
        n_points, degree = int(n), int(d)

        points = []
        for j in range(n_points):
            (x, y) = infile.readline().split()
            points.append(Object(float(x), float(y)))

        x_axis = [i.x for i in points]
        y_axis = [i.y for i in points]
        graph.update_org(x_axis, y_axis)

        population = create_population(degree)
        max_val, max_chromosome, count = 0.0, [], 0

        for y in range(max_generations):
            (population, value, chromosome) = genetic_algorithm(population, points, y)
            count += 1
            if value > max_val:
                count = 0
                max_val, max_chromosome = value, chromosome
                y_calculated = ff.calculate_y(max_chromosome, points)
                graph.update_pred(x_axis, y_calculated)
                # print(max_chromosome)
            print('%s / %s: %s' % (y, max_generations, max_chromosome), end='\r')

            # if count == 250:
            #     break
            # print(" value: %d" % max_val)
        print('%s / %s: %s' % (y, max_generations, max_chromosome))
        
        outfile.write('Case: %d \n' % (i+1))
        for f in max_chromosome:
            outfile.write(str(f) + ' ')
        outfile.write(" value: %f \n" % max_val)
        print(" value: %f" % max_val)
        x_axis = [i.x for i in points]
        y_axis = [i.y for i in points]
        plt.plot(x_axis, y_axis, 'ro')
        # outfile2.write('case: %d \n' % (i+1))
        y_calculated = ff.calculate_y(max_chromosome, points)
        # for z in range(len(points)):
        #    outfile2.write('%f %f\n' % (points[z].x , y_calculated[z]))
        plt.plot(x_axis, y_calculated, linestyle='-.')
        plt.show()
    
    outfile.close()