// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rdi, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movsd 0x20(%rsi), %xmm0
movsd %xmm0, (%rsp)
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
subq %rax, %rcx
andq $-0x8, %rcx
cmpq $0x8, %rcx
je 0x6ba5
movq %rdx, %r14
movl $0x1, %r13d
movl $0x1, %edx
movl $0x3, %edi
leal 0x1(%r13), %r15d
movq (%r14), %rcx
movq 0x8(%r14), %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
decq %rsi
cmpq %r15, %rsi
movl %edi, 0xc(%rsp)
movq %r15, 0x10(%rsp)
jbe 0x6b7c
movslq (%rax,%rdx,4), %rsi
leal -0x1(%r13), %edx
movslq (%rax,%rdx,4), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x38(%rsp)
movq %rsi, %r12
leaq (%rsi,%rsi,2), %rax
movq %rax, 0x30(%rsp)
movl %edi, %ebx
movslq (%rcx,%r15,4), %rax
movl %ebx, %r15d
movslq (%rcx,%r15,4), %rcx
movq 0x28(%rsp), %rdx
movq 0x8(%rdx), %rdx
movq 0x38(%rsp), %rsi
movq (%rdx,%rsi,8), %rsi
movsd (%rsi,%r12,8), %xmm0
xorpd 0x17ed(%rip), %xmm0     # 0x82f0
leaq (%rax,%rax,2), %rdi
movq (%rdx,%rdi,8), %rdi
subsd (%rdi,%rcx,8), %xmm0
addsd (%rsi,%rax,8), %xmm0
leal -0x1(%rbx), %ebp
movq 0x30(%rsp), %rax
movq (%rdx,%rax,8), %rax
addsd (%rax,%rcx,8), %xmm0
movapd %xmm0, 0x40(%rsp)
movq 0x20(%rsp), %rdi
movl %r13d, %esi
movl %ebp, %edx
callq 0x6e72
testb %al, %al
jne 0x6b60
movsd (%rsp), %xmm0
movapd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6b60
movq 0x18(%rsp), %rax
movl %r13d, (%rax)
movl %ebp, 0x4(%rax)
movsd %xmm1, (%rsp)
movq (%r14), %rcx
movq 0x8(%r14), %rax
subq %rcx, %rax
sarq $0x2, %rax
decq %rax
incl %ebx
cmpq %r15, %rax
ja 0x6ad8
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
addq $-0x2, %rcx
movl 0xc(%rsp), %edi
incl %edi
movq 0x10(%rsp), %r13
movq %r13, %rdx
cmpq %r13, %rcx
ja 0x6a8e
movsd (%rsp), %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop