// File: GAStudyGroup[P]GPX2/src/base/sources/GAUtils.cpp
// Function: GAUtils::stop(Population&, std::ostream&)
bool GAUtils::stop(Population &pop, ostream &out) {
    unsigned static generationsWithoutChange{0};
    unsigned static bestFitness{(unsigned)pop.bestFitness()};
    
    unsigned currentFitness{(unsigned)pop.bestFitness()};

    unsigned totalEqual{0};
    //how many routes have fitness equal to the best route, to determine stagnation
    for (vector<int> t : pop.getPopulation()) {
        if ((unsigned)getFitness(t) == bestFitness) {
            totalEqual++;
        }
    }

    //zero the generationsWithoutChange counter if a better fitness is found
    if (bestFitness > currentFitness) {
        bestFitness = currentFitness;
        generationsWithoutChange = 0;
    } else if (totalEqual == Config::POP_SIZE) {
        //if all elements are equal ends GA
        out << "\nPopulation converged!";
        out.flush();
        return (false);
    } else {
        generationsWithoutChange++;
    }

    //If the current fitness is equal to or less than the best known, it decreases the number of generations to stop AG
    if(bestFitness <= Config::BEST_FITNESS){
        // Config::GENERATION_LIMIT = Config::AFTER_BEST;
        out << "\nFound best fitness!\n";
        out.flush();
        return (false);
    }

    if (generationsWithoutChange >= Config::GENERATION_LIMIT) {
        out << "\nGeneration limit reached!";
        out.flush();
        return (false);
    } else {
        return (true);
    }
}

// Assembly:
subq $0x78, %rsp
movq %rdi, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
cmpb $0x0, 0x91c9b(%rip)     # 0x9c960
jne 0xacfe
leaq 0x91c92(%rip), %rdi     # 0x9c960
callq 0x6890
cmpl $0x0, %eax
je 0xacfe
movq 0x68(%rsp), %rdi
callq 0x19240
movl %eax, 0x14(%rsp)
jmp 0xace8
movl 0x14(%rsp), %eax
movl %eax, 0x91c66(%rip)     # 0x9c958
leaq 0x91c67(%rip), %rdi     # 0x9c960
callq 0x6330
movq 0x68(%rsp), %rdi
callq 0x19240
movl %eax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movq 0x68(%rsp), %rdi
callq 0x19230
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
callq 0xfc20
movq %rax, 0x38(%rsp)
movq 0x40(%rsp), %rdi
callq 0xfc50
movq %rax, 0x30(%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xfc80
testb $0x1, %al
jne 0xad59
jmp 0xadf3
leaq 0x38(%rsp), %rdi
callq 0xfcc0
movq %rax, %rsi
leaq 0x18(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0xfcd0
movq 0x8(%rsp), %rdi
callq 0x1b240
movl %eax, 0x10(%rsp)
jmp 0xad85
movl 0x10(%rsp), %eax
cmpl 0x91bc9(%rip), %eax     # 0x9c958
jne 0xadda
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xadda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x91bad(%rip), %rdi     # 0x9c960
callq 0x6320
jmp 0xaecd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x9af0
jmp 0xaecd
leaq 0x18(%rsp), %rdi
callq 0x9af0
leaq 0x38(%rsp), %rdi
callq 0xfdd0
jmp 0xad41
movl 0x91b5f(%rip), %eax     # 0x9c958
cmpl 0x50(%rsp), %eax
jbe 0xae15
movl 0x50(%rsp), %eax
movl %eax, 0x91b4f(%rip)     # 0x9c958
movl $0x0, 0x91b41(%rip)     # 0x9c954
jmp 0xae57
movl 0x4c(%rsp), %eax
leaq 0x91be0(%rip), %rcx     # 0x9ca00
cmpl (%rcx), %eax
jne 0xae46
movq 0x60(%rsp), %rdi
leaq 0x694c8(%rip), %rsi     # 0x742f8
callq 0x64a0
movq 0x60(%rsp), %rdi
callq 0x6340
movb $0x0, 0x77(%rsp)
jmp 0xaec2
movl 0x91b08(%rip), %eax     # 0x9c954
addl $0x1, %eax
movl %eax, 0x91aff(%rip)     # 0x9c954
jmp 0xae57
movl 0x91afb(%rip), %eax     # 0x9c958
leaq 0x91ba0(%rip), %rcx     # 0x9ca04
cmpl (%rcx), %eax
ja 0xae8a
movq 0x60(%rsp), %rdi
leaq 0x6949b(%rip), %rsi     # 0x7430f
callq 0x64a0
movq 0x60(%rsp), %rdi
callq 0x6340
movb $0x0, 0x77(%rsp)
jmp 0xaec2
movl 0x91ac4(%rip), %eax     # 0x9c954
leaq 0x91a51(%rip), %rcx     # 0x9c8e8
cmpl (%rcx), %eax
jb 0xaebd
movq 0x60(%rsp), %rdi
leaq 0x6947e(%rip), %rsi     # 0x74325
callq 0x64a0
movq 0x60(%rsp), %rdi
callq 0x6340
movb $0x0, 0x77(%rsp)
jmp 0xaec2
movb $0x1, 0x77(%rsp)
movb 0x77(%rsp), %al
andb $0x1, %al
addq $0x78, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x6850
nopw (%rax,%rax)