// File: /workspace/github_cpp_makefile_star2_O2/sebastienmeyer2[P]mst-clustering/src/compare_clustering.cpp
// Function: compare_clustering(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int)
void compare_clustering(std::string file_to_read, int k)
{
    /*!
     * @brief From a file given as entry and an integer k for the number of 
     * clusters to draw, establishes a comparison between kmeans and MST clustering
     * 
     * @param file_to_read Path to the data
     * @param k Number of clusters to draw
     * 
     */

	// get points from datafile

	std::ifstream is(file_to_read);
	assert(is.is_open());

	std::string header_line;
	std::getline(is, header_line);

	const int d = nb_columns(header_line) - 1;
    Point::d = d;

    std::vector<Point*> points = Point::read_points_from_file(file_to_read, d);
    int n = points.size();

    // run MST clustering 

    std::chrono::steady_clock::time_point graph_creation = std::chrono::steady_clock::now();

    std::cout << "\nCreating graph...";
    Graph* g = new Graph(points);
    std::cout << "[OK]" << std::endl;

    std::chrono::steady_clock::time_point kruskal_algo = std::chrono::steady_clock::now();

    KruskalAlgorithm kruskal = KruskalAlgorithm(g);
    kruskal.compute_mst();

    std::chrono::steady_clock::time_point kruskal_clustering = std::chrono::steady_clock::now();

    std::unordered_map<Node*, Node*> clusters = kruskal.compute_clustering(k);

    std::chrono::steady_clock::time_point mst_end = std::chrono::steady_clock::now();

    // run kmeans 

    std::chrono::steady_clock::time_point cloud_creation = std::chrono::steady_clock::now();

    std::cout << "\nCreating cloud...";

    Cloud cloud = Cloud(d, n, k);

    int label = 0;
    for (int i = 0; i < n; i ++) {
        cloud.add_point(*points[i], label);
        label = (label+1) % k;
    }

    std::cout << "[OK]" << std::endl;

    std::chrono::steady_clock::time_point kmeans_algo = std::chrono::steady_clock::now();

    cloud.kmeans();

    std::chrono::steady_clock::time_point kmeans_end = std::chrono::steady_clock::now();

    // comparison

    std::cout << "\nTotal time spent by MST clustering including graph creation: ";
    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(mst_end - graph_creation).count() << " µs" << std::endl;

    std::cout << "Total time spent by MST clustering: ";
    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(mst_end - kruskal_algo).count() << " µs" << std::endl;

    double mst_variance = intracluster_variance(clusters);
    std::cout << "Intracluster variance in MST: ";
    std::cout << mst_variance << std::endl;

    double mst_silhouette = silhouette(clusters);
    std::cout << "Silhouette in MST: ";
    std::cout << mst_silhouette << std::endl;

    std::cout << "\nTotal time spent by kmeans including cloud creation: ";
    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(kmeans_end - cloud_creation).count() << " µs" << std::endl;

    std::cout << "Total time spent by kmeans: ";
    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(kmeans_end - kmeans_algo).count() << " µs" << std::endl;

    double kmeans_variance = cloud.intracluster_variance();
    std::cout << "Intracluster variance in kmeans: ";
    std::cout << kmeans_variance << std::endl;

    double kmeans_silhouette = cloud.silhouette();
    std::cout << "Silhouette in kmeans: ";
    std::cout << kmeans_silhouette << std::endl;

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x468, %rsp            # imm = 0x468
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x260(%rsp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x3440
leaq 0x2d8(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0xda78
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x260(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3070
leaq 0x70(%rsp), %rdi
callq 0xd560
movl %eax, %r15d
decl %r15d
leaq 0x9c8e(%rip), %rax      # 0x172a8
movl %r15d, (%rax)
leaq 0x90(%rsp), %rdi
movq %rbx, %rsi
callq 0x33b0
leaq 0x10(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movl %r15d, %edx
callq 0x3960
leaq 0x90(%rsp), %rdi
callq 0x3110
movq 0x18(%rsp), %r14
subq 0x10(%rsp), %r14
callq 0x33a0
movq %rax, %r12
movq 0x9948(%rip), %rdi      # 0x16fb0
leaq 0x23a3(%rip), %rsi      # 0xfa12
callq 0x32a0
movl $0xe8, %edi
callq 0x32b0
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xe1f8
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0x4350
leaq 0x58(%rsp), %rdi
callq 0x3ade
movq 0x9902(%rip), %rdi      # 0x16fb0
leaq 0x20ae(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
callq 0x33a0
movq %rax, 0x30(%rsp)
leaq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0xb3a4
leaq 0x158(%rsp), %rdi
callq 0xb3ba
callq 0x33a0
leaq 0xb0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
movl %ebp, %edx
callq 0x9454
callq 0x33a0
movq %rax, %rbx
callq 0x33a0
movq %rax, 0x28(%rsp)
movq 0x9892(%rip), %rdi      # 0x16fb0
leaq 0x2300(%rip), %rsi      # 0xfa25
callq 0x32a0
movq %r12, 0x8(%rsp)
shrq $0x3, %r14
leaq 0x38(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
movl %ebp, %ecx
callq 0xc73c
xorl %r13d, %r13d
testl %r14d, %r14d
cmovlel %r13d, %r14d
leaq 0x38(%rsp), %r15
xorl %r12d, %r12d
cmpq %r13, %r14
je 0xd781
movq 0x10(%rsp), %rax
movq (%rax,%r13,8), %rsi
movq %r15, %rdi
movl %r12d, %edx
callq 0xc918
incl %r12d
movl %r12d, %eax
cltd
idivl %ebp
incq %r13
movl %edx, %r12d
jmp 0xd757
movq 0x9828(%rip), %rdi      # 0x16fb0
leaq 0x1fd4(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
callq 0x33a0
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0xcd16
movq 0x8(%rsp), %r12
callq 0x33a0
movq %rax, %r15
movq 0x97ee(%rip), %rdi      # 0x16fb0
leaq 0x226f(%rip), %rsi      # 0xfa38
callq 0x32a0
movq %rbx, %rax
subq %r12, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq 0x97cb(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x1fac(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq 0x97a5(%rip), %rdi      # 0x16fb0
leaq 0x2265(%rip), %rsi      # 0xfa77
callq 0x32a0
subq 0x30(%rsp), %rbx
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
cqto
idivq %rcx
movq 0x9780(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x1f61(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0x120(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe246
leaq 0x120(%rsp), %rdi
callq 0x84cc
movsd %xmm0, 0x8(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x7e78
movq 0x9725(%rip), %rdi      # 0x16fb0
leaq 0x220a(%rip), %rsi      # 0xfa9c
callq 0x32a0
movq 0x9712(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
leaq 0xe8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe246
leaq 0xe8(%rsp), %rdi
callq 0x87fb
movsd %xmm0, 0x8(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x7e78
movq 0x96c3(%rip), %rdi      # 0x16fb0
leaq 0x21c7(%rip), %rsi      # 0xfabb
callq 0x32a0
movq 0x96b0(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
movq 0x9696(%rip), %rdi      # 0x16fb0
leaq 0x21ae(%rip), %rsi      # 0xfacf
callq 0x32a0
movq %r15, %rax
subq 0x28(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq 0x9671(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x1e52(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq 0x964b(%rip), %rdi      # 0x16fb0
leaq 0x219a(%rip), %rsi      # 0xfb06
callq 0x32a0
subq %r14, %r15
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r15, %rax
cqto
idivq %rcx
movq 0x9628(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x1e09(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0x38(%rsp), %rdi
callq 0xc9f0
movsd %xmm0, 0x8(%rsp)
movq 0x95f2(%rip), %rdi      # 0x16fb0
leaq 0x215e(%rip), %rsi      # 0xfb23
callq 0x32a0
movq 0x95df(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
leaq 0x38(%rsp), %rdi
callq 0xd2dc
movsd %xmm0, 0x8(%rsp)
movq 0x95b5(%rip), %rdi      # 0x16fb0
leaq 0x2143(%rip), %rsi      # 0xfb45
callq 0x32a0
movq 0x95a2(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
leaq 0x38(%rsp), %rdi
callq 0xc870
leaq 0xb0(%rsp), %rdi
callq 0x7e78
leaq 0x158(%rsp), %rdi
callq 0x7cec
leaq 0x10(%rsp), %rdi
callq 0x3ade
leaq 0x70(%rsp), %rdi
callq 0x3110
leaq 0x260(%rsp), %rdi
callq 0x30a0
addq $0x468, %rsp            # imm = 0x468
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f41(%rip), %rdi      # 0xf9c0
leaq 0x1f47(%rip), %rsi      # 0xf9cd
leaq 0x1f5b(%rip), %rcx      # 0xf9e8
pushq $0x1f
popq %rdx
callq 0x31b0
movq %rax, %r14
leaq 0xe8(%rsp), %rdi
jmp 0xdaad
movq %rax, %r14
leaq 0x120(%rsp), %rdi
callq 0x7e78
jmp 0xdb1e
jmp 0xdb1b
jmp 0xdab8
movq %rax, %r14
jmp 0xdb28
jmp 0xdabf
movq %rax, %r14
jmp 0xdb35
jmp 0xdb0e
movq %rax, %r14
leaq 0x58(%rsp), %rdi
callq 0x3ade
jmp 0xdad8
movq %rax, %r14
movl $0xe8, %esi
movq %rbx, %rdi
callq 0x32c0
jmp 0xdb42
jmp 0xdb0e
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x3110
jmp 0xdb4c
jmp 0xdafd
movq %rax, %r14
jmp 0xdb4c
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
movq %rax, %r14
jmp 0xdb42
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
jmp 0xdb1b
movq %rax, %r14
leaq 0x38(%rsp), %rdi
callq 0xc870
leaq 0xb0(%rsp), %rdi
callq 0x7e78
leaq 0x158(%rsp), %rdi
callq 0x7cec
leaq 0x10(%rsp), %rdi
callq 0x3ade
leaq 0x70(%rsp), %rdi
callq 0x3110
leaq 0x260(%rsp), %rdi
callq 0x30a0
movq %r14, %rdi
callq 0x34b0