// File: /workspace/github_cpp_makefile_star2_O3/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 0x61ec
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
decq %rcx
movq %rbx, 0x18(%rsp)
je 0x5f18
movq 0x8(%r14), %rdx
movl (%rax), %edi
xorpd %xmm0, %xmm0
movl $0x1, %esi
movl %esi, %r8d
movslq %edi, %rdi
movslq (%rax,%r8,4), %r9
leaq (%rdi,%rdi,2), %rdi
movq (%rdx,%rdi,8), %rdi
addsd (%rdi,%r9,8), %xmm0
incl %esi
movl %r9d, %edi
cmpq %r8, %rcx
ja 0x5ef4
jmp 0x5f1c
xorpd %xmm0, %xmm0
movsd %xmm0, (%rsp)
movl $0x1, %ebp
movq 0x50a3(%rip), %r13      # 0xafd0
leaq 0x28(%rsp), %r12
leaq 0x10(%rsp), %r15
cmpl $0x13, (%r14)
jg 0x5f48
movq %r12, %rdi
movq %r13, %rsi
callq 0x5a02
movl $0x2, %edx
movq %r13, %rdi
leaq 0x232a(%rip), %rsi      # 0x8281
callq 0x32f0
movq %r13, %rdi
movl %ebp, %esi
callq 0x3400
movq %rax, %rbx
movl $0x8, %edx
movq %rax, %rdi
leaq 0x230c(%rip), %rsi      # 0x8284
callq 0x32f0
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x3380
movq %rax, %rbx
movl $0x2, %edx
movq %rax, %rdi
leaq 0x22e5(%rip), %rsi      # 0x8281
callq 0x32f0
movq %rbx, %rdi
movsd (%rsp), %xmm0
callq 0x3380
movl $0x1, %edx
movq %rax, %rdi
leaq 0x22d0(%rip), %rsi      # 0x828d
callq 0x32f0
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq *(%rax)
movsd %xmm0, 0x8(%rsp)
movl $0x8, %edx
movq %r13, %rdi
leaq 0x22a1(%rip), %rsi      # 0x828f
callq 0x32f0
movl 0x10(%rsp), %esi
movq %r13, %rdi
callq 0x3400
movq %rax, %rbx
movl $0x3, %edx
movq %rax, %rdi
leaq 0x2287(%rip), %rsi      # 0x8298
callq 0x32f0
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0x3400
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x32b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31f0
movsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6084
incl %ebp
movq %r12, %rsi
movq %r15, %rdx
movsd %xmm1, 0x8(%rsp)
callq 0x6170
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsp)
jmp 0x5f37
movq 0x18(%rsp), %r8
cmpq %r8, %r12
je 0x60b5
movq (%r8), %rax
movq 0x8(%r8), %rcx
subq %rax, %rcx
je 0x60b5
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 0x60a5
movl %ebp, 0x48(%r8)
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x60cc
callq 0x3290
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x60db
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 0x6106
jmp 0x60f6
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x62c2
cmpl $0x1, %r14d
jne 0x6160
movq %rbx, %rdi
callq 0x3130
movq %rax, %rbx
movq 0x4eb2(%rip), %rdi      # 0xafd0
leaq 0x2177(%rip), %rsi      # 0x829c
movl $0xe, %edx
callq 0x32f0
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq 0x4e91(%rip), %rdi      # 0xafd0
movq %rax, %rsi
callq 0x32c0
movq %rax, %rdi
callq 0x3150
callq 0x33c0
xorl %eax, %eax
jmp 0x60dd
movq %rax, %rbx
callq 0x33c0
movq %rbx, %rdi
callq 0x3410
movq %rax, %rdi
callq 0x62e8