// File: 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:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x70(%rsp)
movq %rdi, %rax
movq %rax, 0x78(%rsp)
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movl %edx, 0x104(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x51900
leaq 0x4de39(%rip), %rax     # 0x9dda8
movl (%rax), %edx
leaq 0xe0(%rsp), %rdi
movl $0x1, %esi
callq 0x10800
jmp 0x4ff85
movl 0x104(%rsp), %eax
leaq 0x4de15(%rip), %rcx     # 0x9dda8
cmpl (%rcx), %eax
jbe 0x4ffa4
movl 0x104(%rsp), %eax
movl %eax, 0x6c(%rsp)
jmp 0x4ffb1
leaq 0x4ddfd(%rip), %rax     # 0x9dda8
movl (%rax), %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jae 0x500e0
leaq 0x4ddae(%rip), %rsi     # 0x9dd90
leaq 0x98(%rsp), %rdi
callq 0x195b0
jmp 0x4fff1
movl 0xcc(%rsp), %eax
leaq 0x4dda9(%rip), %rcx     # 0x9dda8
movl (%rcx), %ecx
xorl %edx, %edx
divl %ecx
movl %edx, %esi
leaq 0x98(%rsp), %rdi
callq 0x10830
movl (%rax), %esi
leaq 0xb0(%rsp), %rdi
callq 0x4c730
jmp 0x50025
leaq 0x98(%rsp), %rdi
callq 0x9af0
leaq 0xb0(%rsp), %rdi
callq 0x4ca70
jmp 0x50041
leaq 0xe8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x51910
jmp 0x50058
leaq 0xb0(%rsp), %rdi
callq 0x4fbc0
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0x4ffc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
jmp 0x50322
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x9af0
jmp 0x50322
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x4fbc0
jmp 0x50322
leaq 0xe8(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x51980
movq 0x60(%rsp), %rdi
movq %rax, 0x90(%rsp)
callq 0x519b0
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rdi
movq 0x88(%rsp), %rsi
callq 0x50340
jmp 0x50123
leaq 0x4dc9e(%rip), %rdi     # 0x9ddc8
callq 0x4cf60
movq %rax, 0x58(%rsp)
jmp 0x50136
movq 0x58(%rsp), %rdi
callq 0x1b240
movl %eax, 0x54(%rsp)
jmp 0x50146
leaq 0xe8(%rsp), %rdi
callq 0x519e0
movq %rax, %rdi
callq 0x4cf60
movq %rax, 0x48(%rsp)
jmp 0x50162
movq 0x48(%rsp), %rdi
callq 0x1b240
movl %eax, 0x44(%rsp)
jmp 0x50172
movl 0x54(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jle 0x501cc
leaq 0xe8(%rsp), %rdi
callq 0x519e0
movq %rax, %rsi
leaq 0x4dc33(%rip), %rdi     # 0x9ddc8
callq 0x51a10
jmp 0x5019c
leaq 0x4dc25(%rip), %rdi     # 0x9ddc8
callq 0x4cf60
movq %rax, 0x38(%rsp)
jmp 0x501af
movq 0x38(%rsp), %rdi
callq 0x1b240
movl %eax, 0x34(%rsp)
jmp 0x501bf
movl 0x34(%rsp), %ecx
leaq 0x4dbf6(%rip), %rax     # 0x9ddc0
movl %ecx, (%rax)
movl 0x4dc12(%rip), %eax     # 0x9dde4
movl $0x5, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x50207
leaq 0xe8(%rsp), %rdi
callq 0x519e0
movq %rax, %rdi
callq 0x4cf60
movq %rax, 0x28(%rsp)
jmp 0x501fb
movq 0x28(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x50226
leaq 0x4dbba(%rip), %rdi     # 0x9ddc8
callq 0x4cf60
movq %rax, 0x18(%rsp)
jmp 0x5021a
movq 0x18(%rsp), %rax
movq %rax, 0x20(%rsp)
jmp 0x50226
movq 0x20(%rsp), %rdi
callq 0x4f5f0
jmp 0x50232
movq 0x70(%rsp), %rdi
movb $0x0, 0x86(%rsp)
callq 0x19220
jmp 0x50246
movl $0x0, 0x80(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x104(%rsp), %eax
jae 0x502dd
movq 0x70(%rsp), %rdi
callq 0x19230
movq %rax, 0x10(%rsp)
jmp 0x50272
movl 0x80(%rsp), %eax
movl %eax, %esi
leaq 0xe8(%rsp), %rdi
callq 0x51a40
movq %rax, %rdi
callq 0x4cf60
movq %rax, 0x8(%rsp)
jmp 0x50297
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x10190
jmp 0x502a8
jmp 0x502aa
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x50251
movq 0x70(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
callq 0x9440
jmp 0x50322
movl 0x4db01(%rip), %eax     # 0x9dde4
addl $0x1, %eax
movl %eax, 0x4daf8(%rip)     # 0x9dde4
movb $0x1, 0x86(%rsp)
testb $0x1, 0x86(%rsp)
jne 0x50308
movq 0x70(%rsp), %rdi
callq 0x9440
leaq 0xe8(%rsp), %rdi
callq 0x51a60
movq 0x78(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
retq
leaq 0xe8(%rsp), %rdi
callq 0x51a60
movq 0xd8(%rsp), %rdi
callq 0x6850
nopl (%rax)