// File: /workspace/github_cpp_makefile_star2_O1/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)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x20dba(%rip), %rax     # 0x43c88
movl (%rax), %ebx
cmpl %edx, %ebx
movl %edx, 0xc(%rsp)
cmovbel %edx, %ebx
testl %ebx, %ebx
je 0x22f6c
xorl %r14d, %r14d
leaq 0x20d85(%rip), %r15     # 0x43c70
leaq 0x28(%rsp), %r12
leaq 0x40(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r12, %rdi
movq %r15, %rsi
callq 0xecfe
movl %r14d, %eax
xorl %edx, %edx
leaq 0x20d77(%rip), %rcx     # 0x43c88
divl (%rcx)
movq 0x28(%rsp), %rax
movl (%rax,%rdx,4), %esi
movq %r13, %rdi
callq 0x21df4
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x22f3a
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x53c0
movq %r13, %rdi
callq 0x2202c
movq %rbp, %rdi
movq %r13, %rsi
callq 0x239a4
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x22f64
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x53c0
incl %r14d
cmpl %r14d, %ebx
jne 0x22efa
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
cmpq %r14, %r15
je 0x22fe7
movq %r14, %rbx
subq %r15, %rbx
movq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x23261
cmpq $0x181, %rbx            # imm = 0x181
jl 0x22fdc
leaq 0x180(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x237ee
cmpq %r14, %r12
je 0x22fe7
movq %r12, %rdi
callq 0x238ee
addq $0x18, %r12
jmp 0x22fc9
movq %r15, %rdi
movq %r14, %rsi
callq 0x237ee
leaq 0x20cba(%rip), %r14     # 0x43ca8
movq %r14, %rdi
callq 0x2231a
movq %rax, %rdi
callq 0xf1c0
movl %eax, %ebp
movq 0x10(%rsp), %rdi
callq 0x2231a
movq %rax, %rdi
callq 0xf1c0
cmpl %eax, %ebp
jle 0x23044
movq 0x10(%rsp), %rsi
leaq 0x20c86(%rip), %rdi     # 0x43ca8
callq 0xb27a
leaq 0x20c7a(%rip), %rdi     # 0x43ca8
callq 0x2231a
movq %rax, %rdi
callq 0xf1c0
leaq 0x20c5e(%rip), %rcx     # 0x43ca0
movl %eax, (%rcx)
imull $0xcccccccd, 0x20c76(%rip), %eax # imm = 0xCCCCCCCD
addl $0x19999999, %eax       # imm = 0x19999999
cmpl $0x33333333, %eax       # imm = 0x33333333
jae 0x2305f
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2231a
movq %rax, %rdi
callq 0x22902
movq (%rsp), %rdi
callq 0xeac4
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x230ba
movl %eax, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
xorl %r15d, %r15d
movq (%rsp), %rdi
callq 0xead4
movq %rax, %r14
movq 0x10(%rsp), %rdi
addq %r15, %rdi
callq 0x2231a
movq %r14, %rdi
movq %rax, %rsi
callq 0xb24e
addq $0x18, %r15
cmpq %r15, %rbx
jne 0x2308d
incl 0x20c04(%rip)           # 0x43cc4
leaq 0x10(%rsp), %rdi
callq 0x239d0
movq (%rsp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x230e1
jmp 0x230e1
movq %rax, %r14
jmp 0x23122
movq %rax, %r14
movq 0x28(%rsp), %rdi
testq %rdi, %rdi
je 0x23122
movq 0x38(%rsp), %rsi
jmp 0x2310c
movq %rax, %r14
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x23122
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x53c0
jmp 0x23122
movq %rax, %r14
movq (%rsp), %rdi
callq 0x8102
leaq 0x10(%rsp), %rdi
callq 0x239d0
movq %r14, %rdi
callq 0x5670