// File: /workspace/github_cpp_makefile_star2_O2/sebastienmeyer2[P]mst-clustering/src/compare_clustering.cpp
// Function: compare_inconsistency(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, double)
void compare_inconsistency(std::string file_to_read, double cutoff)
{
    /*!
     * @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 Inconsistency method 

    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 inc_clustering = std::chrono::steady_clock::now();

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

    std::unordered_map<Node*, int> clusters_size;
    for (auto kv : clusters) {
        if(clusters_size.find(kv.second) != clusters_size.end()) {
            clusters_size.at(kv.second) += 1;
        } else {
            clusters_size.insert(std::pair<Node*, int>(kv.second, 1));
        }
    }
    std::cout << "In total, there are " << clusters_size.size() << " clusters when cutoff = " << cutoff << std::endl;

    int big_clusters = 0;
    for (auto kv : clusters_size) {
        if (kv.second > 1) {
            big_clusters += 1;
        }
    }
    std::cout << "There are " << big_clusters << " clusters of size at least 2" << std::endl;

    std::chrono::steady_clock::time_point inc_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, clusters.size());

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

    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 Inconsistency clustering including graph creation: ";
    std::cout << std::chrono::duration_cast<std::chrono::microseconds>(inc_end - graph_creation).count() << " µs" << std::endl;

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

    double inc_variance = intracluster_variance(clusters);
    std::cout << "Intracluster variance with Inconsistency clustering: ";
    std::cout << inc_variance << 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;

    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
leaq 0x280(%rsp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x3440
leaq 0x2f8(%rsp), %rdi
callq 0x3080
testb %al, %al
je 0xe0f7
leaq 0xa0(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x280(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3070
leaq 0x90(%rsp), %rdi
callq 0xd560
movl %eax, %ebp
decl %ebp
leaq 0x96ba(%rip), %rax      # 0x172a8
movl %ebp, (%rax)
leaq 0xe8(%rsp), %rdi
movq %rbx, %rsi
callq 0x33b0
leaq 0x18(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
movl %ebp, %edx
callq 0x3960
leaq 0xe8(%rsp), %rdi
callq 0x3110
movq 0x20(%rsp), %r15
subq 0x18(%rsp), %r15
callq 0x33a0
movq %rax, %rbx
movq 0x9376(%rip), %rdi      # 0x16fb0
leaq 0x1dd1(%rip), %rsi      # 0xfa12
callq 0x32a0
movl $0xe8, %edi
callq 0x32b0
movq %rax, %r12
leaq 0x78(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0xe1f8
leaq 0x78(%rsp), %rsi
movq %r12, %rdi
callq 0x4350
leaq 0x78(%rsp), %rdi
callq 0x3ade
movq 0x9330(%rip), %rdi      # 0x16fb0
leaq 0x1adc(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movl %ebp, 0x14(%rsp)
callq 0x33a0
movq %rax, %r14
leaq 0x178(%rsp), %rdi
movq %r12, %rsi
callq 0xb3a4
leaq 0x178(%rsp), %rdi
callq 0xb3ba
movq %r14, 0x58(%rsp)
movq %rbx, 0x60(%rsp)
callq 0x33a0
leaq 0xb0(%rsp), %rdi
leaq 0x178(%rsp), %rsi
movsd 0x8(%rsp), %xmm0
callq 0x96d6
shrq $0x3, %r15
leaq 0x138(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
leaq 0x118(%rsp), %r14
xorps %xmm0, %xmm0
movups %xmm0, (%r14)
movl $0x3f800000, 0x10(%r14) # imm = 0x3F800000
movups %xmm0, 0x18(%r14)
movq 0xc0(%rsp), %rbp
leaq 0x38(%rsp), %rbx
leaq 0x108(%rsp), %r12
leaq 0x68(%rsp), %r13
testq %rbp, %rbp
je 0xdd85
movups 0x8(%rbp), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x80ec
testq %rax, %rax
je 0xdd62
movq %r12, %rdi
movq %rbx, %rsi
callq 0x8168
incl (%rax)
jmp 0xdd7f
movq 0x38(%rsp), %rax
movq %rax, 0x68(%rsp)
movl $0x1, 0x70(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x8186
movq (%rbp), %rbp
jmp 0xdd35
movq 0x9224(%rip), %rdi      # 0x16fb0
leaq 0x1df9(%rip), %rsi      # 0xfb8c
callq 0x32a0
movq 0x120(%rsp), %rsi
movq %rax, %rdi
callq 0x31d0
leaq 0x15d7(%rip), %rsi      # 0xf386
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
xorl %ebx, %ebx
movq (%r14), %r14
testq %r14, %r14
je 0xdde5
xorl %eax, %eax
cmpl $0x2, 0x10(%r14)
setge %al
addl %eax, %ebx
jmp 0xddcf
movq 0x91c4(%rip), %rdi      # 0x16fb0
leaq 0x15db(%rip), %rsi      # 0xf3ce
callq 0x32a0
movq %rax, %rdi
movl %ebx, %esi
callq 0x34a0
leaq 0x1d98(%rip), %rsi      # 0xfba1
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
callq 0x33a0
movq %rax, %rbx
callq 0x33a0
movq %rax, %r12
movq 0x9180(%rip), %rdi      # 0x16fb0
leaq 0x1bee(%rip), %rsi      # 0xfa25
callq 0x32a0
movl 0x14(%rsp), %esi
movl 0xc8(%rsp), %ecx
leaq 0x30(%rsp), %rdi
movl %r15d, %edx
callq 0xc73c
xorl %r14d, %r14d
testl %r15d, %r15d
cmovlel %r14d, %r15d
leaq 0x30(%rsp), %r13
xorl %ebp, %ebp
cmpq %r14, %r15
je 0xde94
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %r13, %rdi
movl %ebp, %edx
callq 0xc918
incl %ebp
movslq %ebp, %rax
xorl %edx, %edx
divq 0xc8(%rsp)
incq %r14
movq %rdx, %rbp
jmp 0xde65
movq 0x9115(%rip), %rdi      # 0x16fb0
leaq 0x18c1(%rip), %rsi      # 0xf763
callq 0x32a0
movq %rax, %rdi
callq 0x3130
callq 0x33a0
movq %rax, %r15
leaq 0x30(%rsp), %rdi
callq 0xcd16
callq 0x33a0
movq %rax, %r13
movq 0x90e0(%rip), %rdi      # 0x16fb0
leaq 0x1ce7(%rip), %rsi      # 0xfbbe
callq 0x32a0
movq %rbx, %rax
subq 0x60(%rsp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq 0x90bb(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x189c(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq 0x9095(%rip), %rdi      # 0x16fb0
leaq 0x1ce5(%rip), %rsi      # 0xfc07
callq 0x32a0
subq 0x58(%rsp), %rbx
movl $0x3e8, %ecx            # imm = 0x3E8
movq %rbx, %rax
cqto
idivq %rcx
movq 0x9070(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x1851(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0x140(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0xe246
leaq 0x140(%rsp), %rdi
callq 0x84cc
movsd %xmm0, 0x8(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x7e78
movq 0x9015(%rip), %rdi      # 0x16fb0
leaq 0x1c94(%rip), %rsi      # 0xfc36
callq 0x32a0
movq 0x9002(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
movq 0x8fe8(%rip), %rdi      # 0x16fb0
leaq 0x1b00(%rip), %rsi      # 0xfacf
callq 0x32a0
movq %r13, %rax
subq %r12, %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq 0x8fc5(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x17a6(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
movq 0x8f9f(%rip), %rdi      # 0x16fb0
leaq 0x1aee(%rip), %rsi      # 0xfb06
callq 0x32a0
subq %r15, %r13
movl $0x3e8, %ecx            # imm = 0x3E8
movq %r13, %rax
cqto
idivq %rcx
movq 0x8f7c(%rip), %rdi      # 0x16fb0
movq %rax, %rsi
callq 0x3290
leaq 0x175d(%rip), %rsi      # 0xf7a0
movq %rax, %rdi
callq 0x32a0
movq %rax, %rdi
callq 0x3130
leaq 0x30(%rsp), %rdi
callq 0xc9f0
movsd %xmm0, 0x8(%rsp)
movq 0x8f46(%rip), %rdi      # 0x16fb0
leaq 0x1ab2(%rip), %rsi      # 0xfb23
callq 0x32a0
movq 0x8f33(%rip), %rdi      # 0x16fb0
movsd 0x8(%rsp), %xmm0
callq 0x3430
movq %rax, %rdi
callq 0x3130
leaq 0x30(%rsp), %rdi
callq 0xc870
leaq 0x108(%rsp), %rdi
callq 0x7dd0
leaq 0xb0(%rsp), %rdi
callq 0x7e78
leaq 0x178(%rsp), %rdi
callq 0x7cec
leaq 0x18(%rsp), %rdi
callq 0x3ade
leaq 0x90(%rsp), %rdi
callq 0x3110
leaq 0x280(%rsp), %rdi
callq 0x30a0
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x18c2(%rip), %rdi      # 0xf9c0
leaq 0x18c8(%rip), %rsi      # 0xf9cd
leaq 0x1a50(%rip), %rcx      # 0xfb5c
movl $0x82, %edx
callq 0x31b0
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x7e78
jmp 0xe196
jmp 0xe193
jmp 0xe1a2
jmp 0xe1a2
jmp 0xe130
movq %rax, %rbx
jmp 0xe1bf
jmp 0xe182
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x3ade
jmp 0xe14c
movq %rax, %rbx
movl $0xe8, %esi
movq %r12, %rdi
callq 0x32c0
jmp 0xe1cc
jmp 0xe182
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x3110
jmp 0xe1d6
jmp 0xe171
movq %rax, %rbx
jmp 0xe1d6
jmp 0xe193
jmp 0xe193
jmp 0xe193
jmp 0xe193
jmp 0xe193
jmp 0xe193
movq %rax, %rbx
jmp 0xe1cc
jmp 0xe193
jmp 0xe1a2
jmp 0xe1a2
jmp 0xe193
jmp 0xe1a2
jmp 0xe1a2
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xc870
jmp 0xe1a5
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x7dd0
leaq 0xb0(%rsp), %rdi
callq 0x7e78
leaq 0x178(%rsp), %rdi
callq 0x7cec
leaq 0x18(%rsp), %rdi
callq 0x3ade
leaq 0x90(%rsp), %rdi
callq 0x3110
leaq 0x280(%rsp), %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x34b0