// File: /workspace/github_cpp_makefile_star2_O3/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 $0x68, %rsp
movq %rdi, %r13
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
leaq 0x20d33(%rip), %rax     # 0x43c78
movl (%rax), %ebx
cmpl %edx, %ebx
movl %edx, 0xc(%rsp)
cmovbel %edx, %ebx
testl %ebx, %ebx
je 0x23068
movq %r13, 0x30(%rsp)
xorl %r14d, %r14d
leaq 0x20cf9(%rip), %r15     # 0x43c60
leaq 0x38(%rsp), %r12
leaq 0x50(%rsp), %r13
leaq 0x10(%rsp), %rbp
movq %r12, %rdi
movq %r15, %rsi
callq 0xece6
movl %r14d, %eax
xorl %edx, %edx
leaq 0x20ceb(%rip), %rcx     # 0x43c78
divl (%rcx)
movq 0x38(%rsp), %rax
movl (%rax,%rdx,4), %esi
movq %r13, %rdi
callq 0x21e74
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x22fb6
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x53b0
movq %r13, %rdi
callq 0x22096
movq %rbp, %rdi
movq %r13, %rsi
callq 0x23b40
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x22fe0
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x53b0
incl %r14d
cmpl %r14d, %ebx
jne 0x22f76
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %r14
cmpq %r14, %r15
movq 0x30(%rsp), %r13
je 0x23068
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 0x232df
cmpq $0x181, %rbx            # imm = 0x181
jl 0x2305d
leaq 0x180(%r15), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x23964
cmpq %r14, %r12
je 0x23068
movq %r12, %rdi
callq 0x23a63
addq $0x18, %r12
jmp 0x2304a
movq %r15, %rdi
movq %r14, %rsi
callq 0x23964
leaq 0x20c29(%rip), %r14     # 0x43c98
movq %r14, %rdi
callq 0x2236c
movq %rax, %rdi
callq 0xf1cc
movl %eax, %ebp
movq 0x10(%rsp), %rdi
callq 0x2236c
movq %rax, %rdi
callq 0xf1cc
cmpl %eax, %ebp
jle 0x230c5
movq 0x10(%rsp), %rsi
leaq 0x20bf5(%rip), %rdi     # 0x43c98
callq 0xb296
leaq 0x20be9(%rip), %rdi     # 0x43c98
callq 0x2236c
movq %rax, %rdi
callq 0xf1cc
leaq 0x20bcd(%rip), %rcx     # 0x43c90
movl %eax, (%rcx)
imull $0xcccccccd, 0x20be5(%rip), %eax # imm = 0xCCCCCCCD
addl $0x19999999, %eax       # imm = 0x19999999
cmpl $0x33333333, %eax       # imm = 0x33333333
jae 0x230e0
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2236c
movq %rax, %rdi
callq 0x2295e
movq %r13, %rdi
callq 0xeaac
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x23139
movl %eax, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rbx
xorl %r15d, %r15d
movq %r13, %rdi
callq 0xeabc
movq %rax, %r14
movq 0x10(%rsp), %rdi
addq %r15, %rdi
callq 0x2236c
movq %r14, %rdi
movq %rax, %rsi
callq 0xb26a
addq $0x18, %r15
cmpq %r15, %rbx
jne 0x2310d
incl 0x20b75(%rip)           # 0x43cb4
leaq 0x10(%rsp), %rdi
callq 0x23b6c
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2315f
jmp 0x2315f
movq %rax, %r14
jmp 0x2319f
movq %rax, %r14
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x2319f
movq 0x48(%rsp), %rsi
jmp 0x2318a
movq %rax, %r14
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x2319f
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x53b0
jmp 0x2319f
movq %rax, %r14
movq %r13, %rdi
callq 0x8122
leaq 0x10(%rsp), %rdi
callq 0x23b6c
movq %r14, %rdi
callq 0x5660
nop