// File: /workspace/github_cpp_makefile_star2_O3/EduBic[P]TSP-MetaheuristicSolvers/TabuSearchSolver.cpp
// Function: TabuSearchSolver::findBestNeighbor(TSP const&, TSPSolution const&, move&)
double TabuSearchSolver::findBestNeighbor( const TSP& tsp , const TSPSolution& currSol, TSPMove& move ) {
    // Determine the NON-TABU *move* yielding the best 2-opt neigbor solution
    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)*/) {
                if (neighCostVariation < bestCostVariation) {
                    bestCostVariation = neighCostVariation;
                    move.from = a;
                    move.to = b;
                }
            }

            /*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;

                    //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 $0x58, %rsp
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rdi, 0x28(%rsp)
movq %rsi, 0x30(%rsp)
movsd 0x20(%rsi), %xmm0
movsd %xmm0, 0x8(%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
andq $-0x8, %rdx
cmpq $0x8, %rdx
je 0x6ada
movl $0x1, %edx
movl $0x1, %esi
movl $0x3, %ebp
movl %edx, %r13d
leal 0x1(%r13), %edx
movl %edx, 0x10(%rsp)
movl %edx, %ebx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x2, %rdx
leaq -0x1(%rdx), %rdi
cmpq %rbx, %rdi
movl %ebp, 0x14(%rsp)
movq %rbx, 0x18(%rsp)
jbe 0x6abe
movslq (%rax,%rsi,4), %rdx
leal -0x1(%r13), %ecx
movslq (%rax,%rcx,4), %rcx
leaq (%rcx,%rcx,2), %rcx
movq %rcx, 0x38(%rsp)
movq %rdx, %r15
leaq (%rdx,%rdx,2), %r12
movslq (%rax,%rbx,4), %rcx
movl %ebp, %ebx
movslq (%rax,%rbx,4), %rax
movq 0x30(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x38(%rsp), %rsi
movq (%rdx,%rsi,8), %rsi
movsd (%rsi,%r15,8), %xmm0
xorpd 0x188c(%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 (%rdx,%r12,8), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, 0x40(%rsp)
movq 0x28(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x6d4c
testb %al, %al
jne 0x6a9d
movsd 0x8(%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6a9d
movq 0x20(%rsp), %rax
movl %r13d, (%rax)
movl %ebp, 0x4(%rax)
movsd %xmm1, 0x8(%rsp)
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 0x6a1a
addq $-0x2, %rdx
movl 0x14(%rsp), %ebp
incl %ebp
movq 0x18(%rsp), %rsi
cmpq %rsi, %rdx
movl 0x10(%rsp), %edx
ja 0x69d1
movsd 0x8(%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop