// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::sendMigrationSetDE(unsigned int, std::vector<std::vector<unsigned int, std::allocator<unsigned int>>, std::allocator<std::vector<unsigned int, std::allocator<unsigned int>>>>, std::map<unsigned int, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>>>>&)
void Swarm::sendMigrationSetDE(unsigned int island, vector<vector<unsigned int> > islandTopology, map<unsigned int, vector<vector<double> > > &migrationSets) {
	if (options.verbosity >= 3) {
		cout << "Sending migration set from island " << island << endl;
	}

	// Generate a random number between 1 and the number of neighbors
	// in the island topology
	boost::random::uniform_int_distribution<int> unif(0, islandTopology[island].size() - 1);
	int parID = 0;
	// Fill a vector with particles from this island, starting with best fits and ending with worst
	vector<unsigned int> particlesToSend;
	//Raquel: if the user provided constraints, then use the ranks calculated from the fit values and constraints combined in order to select the best particle
	if(options.constraints_.size()>0){

		//the ranks are sorted from the best particle to the worst, so the for loop will stop in the first time it finds a particle that belongs to the current iland
		for (auto fitIt = subParRankFinal.begin(); fitIt != subParRankFinal.end(); ++fitIt) {
			parID = fcalcParID(fitIt->first, options.models.size());

			if (particleToIsland_[parID] == island) {
				particlesToSend.push_back(parID);
				if (particlesToSend.size() == options.numToMigrate) {
					break;
				}
			}
		}

	}else{

		for (auto fitIt = particleBestFitsByFit_.begin(); fitIt != particleBestFitsByFit_.end(); ++fitIt) {
			if (particleToIsland_[fitIt->second] == island) {
				particlesToSend.push_back(fitIt->second);
				if (particlesToSend.size() == options.numToMigrate) {
					break;
				}
			}
		}

	}

	// Choose the index of our receiver
	unsigned int receivingIslandIndex = unif(generalRand);

	// Loop through particles to send
	vector<double> migrationSet;
	//for (auto particle = particlesToSend.begin(); particle != particlesToSend.end(); ++particle) {
	for (unsigned int i = 0; i < options.numToMigrate; ++i) {
		if (options.verbosity >= 3) {
			cout << "sending set " << i + 1 << " from particle " << particlesToSend[i] << endl;
		}
		// Fill migrationSet with this particles current parameters
		for (auto param = particleCurrParamSets_[particlesToSend[i]].begin(); param != particleCurrParamSets_[particlesToSend[i]].end(); ++param) {
			migrationSet.push_back(*param);
		}
		// Add that migration set to the list of the receiving island's
		// migration sets
		migrationSets[islandTopology[island][receivingIslandIndex]].push_back(migrationSet);
		migrationSet.clear();
		if (options.verbosity >= 3) {
			cout << island << " sending migration set to island " << islandTopology[island][receivingIslandIndex] << endl;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x68(%rsp)
movq %rdx, 0x20(%rsp)
movl %esi, %ebp
movq %rdi, %r15
cmpl $0x2, 0x1614(%rdi)
movl %esi, %r14d
jbe 0x7eee3
movq 0xe70d4(%rip), %rdi     # 0x165f98
leaq 0x5f96b(%rip), %rsi     # 0xde836
callq 0x289f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
movq 0x20(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq 0x8(%rax,%rcx), %rdx
movq %rcx, 0x28(%rsp)
subq (%rax,%rcx), %rdx
shrq $0x2, %rdx
decl %edx
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
callq 0x8678c
andq $0x0, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
cmpq $0x0, 0x1608(%r15)
je 0x7ef98
movq 0x18f0(%r15), %rbx
movq %rsp, %r12
leaq 0x30(%rsp), %r13
cmpq 0x18f8(%r15), %rbx
je 0x7eff0
movl (%rbx), %edi
movq 0x15c8(%r15), %rsi
subq 0x15c0(%r15), %rsi
shrq $0x3, %rsi
callq 0x9ec1e
movslq %eax, %rcx
movq 0x1d20(%r15), %rdx
cmpl %ebp, (%rdx,%rcx,4)
jne 0x7ef92
movl %eax, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x8ed06
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movl 0x170c(%r15), %ecx
cmpq %rcx, %rax
je 0x7eff0
addq $0x8, %rbx
jmp 0x7ef35
movq 0x1ac8(%r15), %r13
leaq 0x1ab8(%r15), %rbx
movq %rsp, %r12
cmpq %rbx, %r13
je 0x7eff0
movl 0x28(%r13), %eax
movq 0x1d20(%r15), %rcx
cmpl %ebp, (%rcx,%rax,4)
jne 0x7efe3
leaq 0x28(%r13), %rsi
movq %r12, %rdi
callq 0x5a59e
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x2, %rax
movl 0x170c(%r15), %ecx
cmpq %rcx, %rax
je 0x7eff0
movq %r13, %rdi
callq 0x28700
movq %rax, %r13
jmp 0x7efa9
movq %r14, 0x58(%rsp)
leaq 0x58(%r15), %rsi
leaq 0x70(%rsp), %rdi
callq 0x867b6
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r13
movaps %xmm0, (%r13)
andq $0x0, 0x10(%r13)
leaq 0x1400(%r15), %rbp
movl %eax, %eax
xorl %r14d, %r14d
movq %rax, 0x50(%rsp)
leaq (,%rax,4), %rax
movq %rax, 0x60(%rsp)
movl 0x170c(%r15), %eax
cmpq %rax, %r14
jae 0x7f16a
cmpl $0x3, 0x1614(%r15)
jb 0x7f093
movq 0xe6f44(%rip), %rdi     # 0x165f98
leaq 0x5f7fe(%rip), %rsi     # 0xde859
callq 0x289f0
leaq 0x1(%r14), %rsi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
leaq 0x5f7f0(%rip), %rsi     # 0xde866
callq 0x289f0
movq (%rsp), %rcx
movl (%rcx,%r14,4), %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
leaq (,%r14,4), %rbx
movq (%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0x86502
movq (%rax), %r12
movq (%rsp), %rsi
addq %rbx, %rsi
movq %rbp, %rdi
callq 0x86502
cmpq 0x8(%rax), %r12
je 0x7f0d3
movq %r13, %rdi
movq %r12, %rsi
callq 0x5a080
addq $0x8, %r12
jmp 0x7f0ad
movq 0x20(%rsp), %rax
movq (%rax), %rax
movq 0x28(%rsp), %rcx
movq (%rax,%rcx), %rsi
addq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdi
callq 0x867c6
movq %rax, %rdi
movq %r13, %rsi
callq 0x86814
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x7f10f
movq %rax, 0x38(%rsp)
cmpl $0x3, 0x1614(%r15)
jb 0x7f162
movq 0xe6e78(%rip), %rdi     # 0x165f98
movq 0x58(%rsp), %rsi
callq 0x286e0
movq %rax, %rdi
leaq 0x5f742(%rip), %rsi     # 0xde876
callq 0x289f0
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq 0x28(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movq 0x50(%rsp), %rdx
movl (%rcx,%rdx,4), %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
incq %r14
jmp 0x7f033
leaq 0x30(%rsp), %rdi
callq 0x5c44e
movq %rsp, %rdi
callq 0x5b268
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7f191
jmp 0x7f191
jmp 0x7f191
movq %rax, %rbx
jmp 0x7f1a9
jmp 0x7f19c
jmp 0x7f19c
jmp 0x7f19c
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x5c44e
movq %rsp, %rdi
callq 0x5b268
movq %rbx, %rdi
callq 0x292f0
nop