// File: /workspace/github_cpp_makefile_star2_O3/EduBic[P]TSP-MetaheuristicSolvers/TabuSearchSolver.cpp
// Function: TabuSearchSolver::findFirstBestNeighbor(TSP const&, TSPSolution const&, move&)
double TabuSearchSolver::findFirstBestNeighbor(const TSP& tsp , const TSPSolution& currSol, TSPMove& move) {
    double bestCostVariation = tsp.infinite;

    // N.B. intial and final position are fixed (initial/final node remains 0)

    // Slice all the current solution vector
    for (uint a = 1 ; a < currSol.sequence.size() - 2; a++) {

        // prev node
        int h = currSol.sequence[a-1];
        // choose a starting node
        int i = currSol.sequence[a];

        for (uint b = a + 1 ; b < currSol.sequence.size() - 1 ; b++) {

            int j = currSol.sequence[b];        // choose a finishing node
            int l = currSol.sequence[b+1];      // prev node

            // prof. implementation it discards other moves
            /*if ( ( (currIter - tabuList[i] <= tabuLength) &&
                    (currIter - tabuList[j] <= tabuLength)) ) continue */

            double neighCostVariation = - tsp.cost[h][i] - tsp.cost[j][l]
                                        + tsp.cost[h][j] + tsp.cost[i][l];


            if (isTabuMove(a, b) && !satisfiedAspirationCriteria(neighCostVariation)) {
                // DEBUG
                //std::cout << "\t" << "discard move: " << a << ", " << b << "\t";
            }
            else {

                // DEBUG
                /*if (isTabuMove(a, b) && satisfiedAspirationCriteria(neighCostVariation)) {
                    std::cout << "\t" << "in tabu but satisfied AC: " << a << ", " << b << "\t";
                }*/

                if (neighCostVariation < bestCostVariation) {
                    bestCostVariation = neighCostVariation;
                    move.from = a;
                    move.to = b;

                    double currentBestValueFound = currSol.evaluateObjectiveFunction(tsp);

                    // on first improvement exit
                    if (currentBestValueFound + bestCostVariation < currentBestValueFound) {
                        return bestCostVariation;
                    }

                    //std::cout << "\t" << "better move: " << a << ", " << b << "\t";
                }
                else {
                    //std::cout << "\t" << "no better move: " << a << ", " << b << "\t";
                }
            }
        }
    }

    return bestCostVariation;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, 0x30(%rsp)
movq %rdx, %r14
movq %rsi, 0x18(%rsp)
movsd 0x20(%rsi), %xmm0
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
andq $-0x8, %rdx
cmpq $0x8, %rdx
jne 0x6bba
movapd %xmm0, %xmm2
movapd %xmm2, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r12
movl $0x1, 0x4(%rsp)
movl $0x1, %edx
movq %rdx, 0x10(%rsp)
movl $0x3, (%rsp)
movl 0x4(%rsp), %edx
movl %edx, %r13d
movq 0x10(%rsp), %rsi
leal 0x1(%r13), %edx
movl %edx, 0x4(%rsp)
movl %edx, %ebx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
leaq -0x1(%rdx), %rdi
cmpq %rbx, %rdi
movq %rbx, 0x10(%rsp)
jbe 0x6d31
movslq (%rax,%rsi,4), %rdx
leal -0x1(%r13), %ecx
movslq (%rax,%rcx,4), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x40(%rsp)
movq %rdx, %r15
leaq (%rdx,%rdx,2), %rcx
movq %rcx, 0x38(%rsp)
movl (%rsp), %ebp
movsd %xmm0, 0x8(%rsp)
movslq (%rax,%rbx,4), %rcx
movl %ebp, %ebx
movslq (%rax,%rbx,4), %rax
movq 0x18(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x40(%rsp), %rsi
movq (%rdx,%rsi,8), %rsi
movsd (%rsi,%r15,8), %xmm0
xorpd 0x1674(%rip), %xmm0     # 0x82d0
leaq (%rcx,%rcx,2), %rdi
movq (%rdx,%rdi,8), %rdi
subsd (%rdi,%rax,8), %xmm0
addsd (%rsi,%rcx,8), %xmm0
decl %ebp
movq 0x38(%rsp), %rcx
movq (%rdx,%rcx,8), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, 0x20(%rsp)
movq %r12, %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x6d4c
testb %al, %al
je 0x6cc3
movapd 0x20(%rsp), %xmm1
movsd 0x78(%r12), %xmm0
addsd 0x161e(%rip), %xmm0     # 0x82c8
cmpb $0x1, 0x80(%r12)
jne 0x6d0a
ucomisd %xmm1, %xmm0
jbe 0x6d0a
movsd 0x8(%rsp), %xmm0
jmp 0x6ccf
movsd 0x8(%rsp), %xmm0
movapd 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6d10
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
movl %ebp, 0x4(%rax)
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x52e0
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm0
movapd %xmm2, %xmm0
jbe 0x6d10
jmp 0x6ba7
movsd 0x8(%rsp), %xmm0
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
leaq -0x1(%rdx), %rsi
leal 0x1(%rbx), %ebp
cmpq %rbx, %rsi
ja 0x6c2c
addq $-0x2, %rdx
incl (%rsp)
movapd %xmm0, %xmm2
cmpq 0x10(%rsp), %rdx
ja 0x6bd6
jmp 0x6ba7