// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rdi, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
movsd 0x20(%rsi), %xmm0
movsd %xmm0, (%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
pushq $0x1
popq %r13
movl %r13d, %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
addq $-0x2, %rsi
cmpq %rdx, %rsi
jbe 0x5c61
leal -0x1(%r13), %esi
movslq (%rax,%rsi,4), %rsi
movslq (%rax,%rdx,4), %rbx
leal 0x1(%r13), %edx
imulq $0x18, %rsi, %rsi
movq %rsi, 0x28(%rsp)
imulq $0x18, %rbx, %r12
movl %edx, 0xc(%rsp)
movl %edx, %ebp
movl %ebp, %edx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x2, %rsi
decq %rsi
cmpq %rdx, %rsi
jbe 0x5c55
movslq (%rax,%rdx,4), %rcx
leal 0x1(%rbp), %r15d
movslq (%rax,%r15,4), %rax
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x28(%rsp), %rsi
movq (%rdx,%rsi), %rsi
movsd (%rsi,%rbx,8), %xmm0
xorpd 0x16bf(%rip), %xmm0     # 0x72b0
imulq $0x18, %rcx, %rdi
movq (%rdx,%rdi), %rdi
subsd (%rdi,%rax,8), %xmm0
addsd (%rsi,%rcx,8), %xmm0
movq (%rdx,%r12), %rcx
addsd (%rcx,%rax,8), %xmm0
movapd %xmm0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x5e60
testb %al, %al
jne 0x5c46
movsd (%rsp), %xmm0
movapd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5c46
movq 0x10(%rsp), %rax
movl %r13d, (%rax)
movl %ebp, 0x4(%rax)
movsd %xmm1, (%rsp)
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl %r15d, %ebp
jmp 0x5bae
movl 0xc(%rsp), %edx
movl %edx, %r13d
jmp 0x5b71
movsd (%rsp), %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop