// File: /workspace/github_cpp_makefile_star2_O1/EduBic[P]TSP-MetaheuristicSolvers/LocalSearchSolver.cpp
// Function: LocalSearchSolver::solve(TSP const&, TSPSolution const&, TSPSolution&)
bool LocalSearchSolver::solve( const TSP& tsp , const TSPSolution& initSol , TSPSolution& bestSol ) {

    try {
        bool stop = false;
        int  iter = 0;

        TSPSolution currSol(initSol);
        double bestValue, currValue;

        bestValue = currValue = currSol.evaluateObjectiveFunction(tsp);
        TSPMove move;

        while (!stop) {
            if ( tsp.n < 20 ) {
                currSol.print(std::cout); //log current solution (only small instances)
            }

            std::cout << " (" << ++iter << ") value " << currValue << " (" << bestValue << ")";

            // first improvement
            // neigh = findFirstBestNeighbor(tsp, currSol, )
            // neighValue = compute neight obj fun value
            // if neighValue < currentBest then bestFound = currentBest = neightValue


            // incremental evaluation: findBestNeighbour returns the cost increment
            double bestNeighValue = currValue + findNeighbor->execute(tsp, currSol, move);

            std::cout << "\t move: " << move.from << " , " << move.to << std::endl;

            // stop criteria
            if (bestNeighValue < currValue) {
                bestValue = currValue = bestNeighValue;
                currSol = swap(currSol,move);
            }
            else {
                stop = true;    // exit from cycle
            }
        }

        bestSol = currSol;
        bestSol.iterations = iter;
    }
    catch (std::exception& e) {
        std::cout << ">>>EXCEPTION: " << e.what() << std::endl;
        return false;
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x625a
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
decq %rcx
movq %rbx, 0x18(%rsp)
je 0x5f7e
movq 0x8(%r14), %rdx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movslq (%rax,%rsi,4), %rdi
incq %rsi
movl %esi, %r8d
movslq (%rax,%r8,4), %r9
leaq (%rdi,%rdi,2), %rdi
movq (%rdx,%rdi,8), %rdi
addsd (%rdi,%r9,8), %xmm0
cmpq %r8, %rcx
ja 0x5f5b
jmp 0x5f82
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %ebx, %ebx
movq 0x5040(%rip), %r13      # 0xafd0
leaq 0x28(%rsp), %r12
xorl %ebp, %ebp
cmpl $0x13, (%r14)
jg 0x5fa8
movq %r12, %rdi
movq %r13, %rsi
callq 0x5a76
movl $0x2, %edx
movq %r13, %rdi
leaq 0x22ea(%rip), %rsi      # 0x82a1
callq 0x32f0
incl %ebp
movq %r13, %rdi
movl %ebp, %esi
callq 0x3400
movq %rax, %r15
movl $0x8, %edx
movq %rax, %rdi
leaq 0x22ca(%rip), %rsi      # 0x82a4
callq 0x32f0
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x3380
movq %rax, %r15
movl $0x2, %edx
movq %rax, %rdi
leaq 0x22a3(%rip), %rsi      # 0x82a1
callq 0x32f0
movq %r15, %rdi
movsd (%rsp), %xmm0
callq 0x3380
movl $0x1, %edx
movq %rax, %rdi
leaq 0x228e(%rip), %rsi      # 0x82ad
callq 0x32f0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r12, %rdx
leaq 0x10(%rsp), %rcx
callq *(%rax)
movsd %xmm0, 0x8(%rsp)
movl $0x8, %edx
movq %r13, %rdi
leaq 0x225d(%rip), %rsi      # 0x82af
callq 0x32f0
movl 0x10(%rsp), %esi
movq %r13, %rdi
callq 0x3400
movq %rax, %r15
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2243(%rip), %rsi      # 0x82b8
callq 0x32f0
movl 0x14(%rsp), %esi
movq %r15, %rdi
callq 0x3400
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31f0
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
movb $0x1, %al
movsd %xmm1, 0x8(%rsp)
ucomisd %xmm1, %xmm0
jbe 0x60e7
movq %r12, %rsi
leaq 0x10(%rsp), %rdx
callq 0x61de
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsp)
movb %bl, %al
movl %eax, %ebx
testb $0x1, %al
je 0x5f97
movq 0x18(%rsp), %r8
cmpq %r8, %r12
je 0x6122
movq (%r8), %rax
movq 0x8(%r8), %rcx
subq %rax, %rcx
je 0x6122
sarq $0x2, %rcx
movq 0x28(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movl %edi, (%rax,%rsi,4)
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0x6112
movl %ebp, 0x48(%r8)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x6139
callq 0x3290
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x6148
callq 0x3290
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdx, %r14
movq %rax, %rbx
jmp 0x6173
jmp 0x6163
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x6330
cmpl $0x1, %r14d
jne 0x61cd
movq %rbx, %rdi
callq 0x3130
movq %rax, %rbx
movq 0x4e45(%rip), %rdi      # 0xafd0
leaq 0x212a(%rip), %rsi      # 0x82bc
movl $0xe, %edx
callq 0x32f0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x4e24(%rip), %rdi      # 0xafd0
movq %rax, %rsi
callq 0x32c0
movq %rax, %rdi
callq 0x3150
callq 0x33c0
xorl %eax, %eax
jmp 0x614a
movq %rax, %rbx
callq 0x33c0
movq %rbx, %rdi
callq 0x3410
movq %rax, %rdi
callq 0x6356
nop