// File: 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:
subq $0x78, %rsp
movq %rdi, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x68(%rsp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
movl $0x1, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x20(%rsp), %rax
subq $0x2, %rcx
cmpq %rcx, %rax
jae 0xacf7
movq 0x60(%rsp), %rdi
movl 0x4c(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x48(%rsp)
movq 0x60(%rsp), %rdi
movl 0x4c(%rsp), %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x44(%rsp)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
movl 0x40(%rsp), %eax
movq %rax, 0x18(%rsp)
movq 0x60(%rsp), %rdi
callq 0x7c40
movq %rax, %rcx
movq 0x18(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0xace5
movq 0x60(%rsp), %rdi
movl 0x40(%rsp), %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0x60(%rsp), %rdi
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x87d0
movl (%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movslq 0x48(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x44(%rsp), %rsi
callq 0x8810
movsd (%rax), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, (%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movslq 0x3c(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x38(%rsp), %rsi
callq 0x8810
movsd (%rsp), %xmm0
subsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movslq 0x48(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x3c(%rsp), %rsi
callq 0x8810
movsd 0x8(%rsp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x68(%rsp), %rdi
addq $0x8, %rdi
movslq 0x44(%rsp), %rsi
callq 0x87f0
movq %rax, %rdi
movslq 0x38(%rsp), %rsi
callq 0x8810
movsd 0x10(%rsp), %xmm0
movq 0x28(%rsp), %rdi
addsd (%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x4c(%rsp), %esi
movl 0x40(%rsp), %edx
callq 0xb080
testb $0x1, %al
jne 0xacd3
movsd 0x30(%rsp), %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xacd1
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x4c(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, (%rax)
movl 0x40(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x4(%rax)
jmp 0xacd3
jmp 0xacd5
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0xab5e
jmp 0xace7
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xaafa
movsd 0x50(%rsp), %xmm0
addq $0x78, %rsp
retq
nopw %cs:(%rax,%rax)