// File: /workspace/github_cpp_makefile_star2_O2/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, 0x20(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x28(%rsp), %rdi
movq %rdx, %rsi
callq 0x5562
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x476c
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0x5caa(%rip), %r13      # 0xafd0
leaq 0x1f2a(%rip), %rbx      # 0x7257
xorl %r12d, %r12d
testb $0x1, %r12b
jne 0x5455
cmpl $0x13, (%r14)
leaq 0x28(%rsp), %r12
jg 0x5350
movq %r12, %rdi
movq %r13, %rsi
callq 0x4f56
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3250
incl %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x33a0
movq %rax, %rdi
leaq 0x1ee9(%rip), %rsi      # 0x725a
callq 0x3250
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3320
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3250
movq %rax, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x3320
movq %rax, %rdi
leaq 0x1ebc(%rip), %rsi      # 0x7263
callq 0x3250
movq 0x8(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %r12, %rdx
leaq 0x18(%rsp), %rcx
callq *(%rax)
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
leaq 0x1e95(%rip), %rsi      # 0x7265
callq 0x3250
movl 0x18(%rsp), %esi
movq %rax, %rdi
callq 0x33a0
movq %rax, %rdi
leaq 0x1e83(%rip), %rsi      # 0x726e
callq 0x3250
movl 0x1c(%rsp), %esi
movq %rax, %rdi
callq 0x33a0
movq %rax, %rdi
callq 0x3130
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm0
setbe %r12b
jbe 0x5330
movsd %xmm1, 0x10(%rsp)
leaq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x5508
leaq 0x28(%rsp), %rdi
movq %rdi, %rsi
callq 0x5610
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x5330
leaq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x5610
movl %ebp, 0x48(%r14)
movq %rbx, %rdi
callq 0x5640
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5493
movq %rdx, %r14
movq %rax, %rbx
jmp 0x54a3
jmp 0x5493
movq %rdx, %r14
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5640
cmpl $0x1, %r14d
jne 0x54f7
movq %rbx, %rdi
callq 0x3100
movq %rax, %rbx
movq 0x5b15(%rip), %rdi      # 0xafd0
leaq 0x1db0(%rip), %rsi      # 0x7272
callq 0x3250
movq %rax, %r14
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
movq %r14, %rdi
movq %rax, %rsi
callq 0x3250
movq %rax, %rdi
callq 0x3130
callq 0x3360
xorl %eax, %eax
jmp 0x5478
movq %rax, %rbx
callq 0x3360
movq %rbx, %rdi
callq 0x33b0
movq %rax, %rdi
callq 0x4a0a
nop