// File: /workspace/github_cpp_makefile_star2_O2/GAStudyGroup[P]GPX2/src/cross/AntColony/AntColony.cpp
// Function: AntColony::run(unsigned int)
Population AntColony::run(unsigned popToFill){
    vector<Ant> colony;
    uniform_int_distribution<int> dist(1,Globals::TOUR_SIZE);
    unsigned numberOfAnts{popToFill>Globals::TOUR_SIZE?popToFill:Globals::TOUR_SIZE};

    for(unsigned i=0;i<numberOfAnts;i++){
        //create ant
        Ant newAnt(Globals::map.getCityList()[i%Globals::TOUR_SIZE]);
        // Ant newAnt(dist(Globals::urng));
        //create path
        newAnt.run();
        //store in vector
        colony.push_back(newAnt);
    }

    //sort colony by fitness of ant path
    sort(colony.begin(),colony.end(),[](Ant& a, Ant&b){return getFitness(a.getPath())<getFitness(b.getPath());});

    if(getFitness(AntMap::bestAntYet.getPath())>getFitness(colony.front().getPath())){
        AntMap::bestAntYet = colony.front();
        AntMap::bestFitnessYet = getFitness(AntMap::bestAntYet.getPath());
    }

    // cout<<"iterations without improvement: "<<iterationsWithoutImprovement<<endl;
    //update pheromone with best ant of run
    AntMap::updatePheromoneMap(iteration%5==0?colony.front().getPath():AntMap::bestAntYet.getPath());

    // bestFitnessYet = Config::BEST_FITNESS!=0?Config::BEST_FITNESS:(bestFitnessYet>getFitness(colony.front().getPath())?(getFitness(colony.front().getPath())):(bestFitnessYet));

    Population newPop;
    for(unsigned i=0;i<popToFill;i++){
        newPop.getPopulation().push_back(colony[i].getPath());
    }

    iteration++;
    return newPop;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, (%rsp)
leaq 0x10(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
leaq 0x21d5b(%rip), %rbx     # 0x3dc68
movl (%rbx), %r14d
cmpl %edx, %r14d
movl %edx, 0xc(%rsp)
cmovbel %edx, %r14d
xorl %r15d, %r15d
leaq 0x21d2b(%rip), %r12     # 0x3dc50
leaq 0x28(%rsp), %r13
leaq 0x40(%rsp), %rbp
cmpl %r15d, %r14d
je 0x1bf80
movq %r13, %rdi
movq %r12, %rsi
callq 0xbb16
movl %r15d, %eax
xorl %edx, %edx
divl (%rbx)
movq 0x28(%rsp), %rax
movl (%rax,%rdx,4), %esi
movq %rbp, %rdi
callq 0x1b048
movq %r13, %rdi
callq 0x6e4c
movq %rbp, %rdi
callq 0x1b234
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1c6e8
movq %rbp, %rdi
callq 0x6e4c
incl %r15d
jmp 0x1bf2f
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
cmpq %r14, %r15
je 0x1bff1
movq %r14, %r12
subq %r15, %r12
pushq $0x18
popq %rcx
movq %r12, %rax
cqto
idivq %rcx
bsrq %rax, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x1c1c9
cmpq $0x181, %r12            # imm = 0x181
jl 0x1bfe6
leaq 0x180(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x1c5c9
cmpq %r14, %r12
je 0x1bff1
movq %r12, %rdi
callq 0x1c668
addq $0x18, %r12
jmp 0x1bfd3
movq %r15, %rdi
movq %r14, %rsi
callq 0x1c5c9
leaq 0x21c90(%rip), %r14     # 0x3dc88
movq %r14, %rdi
callq 0x1b43c
movq %rax, %rdi
callq 0xc060
movl %eax, %ebp
movq 0x10(%rsp), %rdi
callq 0x1b43c
movq %rax, %rdi
callq 0xc060
cmpl %eax, %ebp
jle 0x1c04e
movq 0x10(%rsp), %rsi
leaq 0x21c5c(%rip), %rdi     # 0x3dc88
callq 0x93b6
leaq 0x21c50(%rip), %rdi     # 0x3dc88
callq 0x1b43c
movq %rax, %rdi
callq 0xc060
leaq 0x21c34(%rip), %rcx     # 0x3dc80
movl %eax, (%rcx)
movl 0x21c50(%rip), %eax     # 0x3dca4
pushq $0x5
popq %rcx
cltd
idivl %ecx
testl %edx, %edx
jne 0x1c063
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x1b43c
movq %rax, %rdi
callq 0x1ba56
movq (%rsp), %rdi
callq 0xb97c
movl 0xc(%rsp), %eax
imulq $0x18, %rax, %rbx
xorl %r15d, %r15d
cmpq %r15, %rbx
je 0x1c0b6
movq (%rsp), %rdi
callq 0xb988
movq %rax, %r14
movq 0x10(%rsp), %rdi
addq %r15, %rdi
callq 0x1b43c
movq %r14, %rdi
movq %rax, %rsi
callq 0x938a
addq $0x18, %r15
jmp 0x1c087
incl 0x21be8(%rip)           # 0x3dca4
leaq 0x10(%rsp), %rdi
callq 0x1c714
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1c0dd
jmp 0x1c0dd
movq %rax, %r14
jmp 0x1c107
movq %rax, %r14
leaq 0x28(%rsp), %rdi
jmp 0x1c0f4
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x6e4c
jmp 0x1c107
movq %rax, %r14
movq (%rsp), %rdi
callq 0x6dde
leaq 0x10(%rsp), %rdi
callq 0x1c714
movq %r14, %rdi
callq 0x5610
nop