// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::getNeighborhoodBestPositions(unsigned int)
vector<double> Swarm::getNeighborhoodBestPositions(unsigned int particle) {

	if (options.verbosity >= 3) {
		cout << "Getting neighborhood best for particle " << particle << endl;
	}

	double currBestFit;

	int subPar;

	//Raquel, if we are using constraints, select fit and constraints combined
	if(options.constraints_.size()>=1){

		subPar = fcalcsubParID(particle, 0, options.models.size());
		currBestFit = subParRankFinal[subPar].second;

		//Raquel: also look at other models inside the same particle
		if(options.models.size()>1){

			for(unsigned int mid=1; mid < options.models.size(); mid++){

				subPar = fcalcsubParID(particle, mid, options.models.size());
				if(subParRankFinal[subPar].second < currBestFit){
					currBestFit = subParRankFinal[subPar].second;
				}

			}

		}

		if (options.verbosity >= 3) {
			cout << "############### currBestFit = " << subParRankFinal[subPar].second << " subpar" << subParRankFinal[subPar].first << endl;
		}
	}else{

		// Set the current best fit to our own best fit
		currBestFit = particleBestFits_.at(particle);
		//cout << "cbf: " << currBestFit << endl;
		//cout << "RAQUEL: defining currentBestNeighbor" << endl;


	}


	int currentBestNeighbor = particle;
	//cout << "set initial nb to " << currentBestNeighbor << endl;

	//cout << "allParticles size: " << populationTopology_.size() << endl;
	//cout << "allParticles neighbor size: " << populationTopology_[particle].size() << endl;

	//cout << "RAQUEL: entering for loop populationTopology_" << endl;

	// For every neighbor in this particle's neighborhood
	for (auto neighbor = populationTopology_[particle].begin(); neighbor != populationTopology_[particle].end(); ++neighbor) {


		auto it = particleBestFits_.find(*neighbor);




		// Skip this neighbor if it doesn't contain a fit value
		if (it->second == 0) {
			continue;
		}


		//Raquel adding support to constraints with the if below
		//cout << "checking if neighbor " << *neighbor << " has a better fit of " << it->second << " than " << currBestFit << endl;
		if(options.constraints_.size()>=1){
			//Raquel: support to constraints
			subPar = fcalcsubParID(*neighbor, 0, options.models.size());
			double it2 = subParRankFinal[subPar].second;

			if (it2 < currBestFit) {
				//cout << "it does! setting current best fit of particle " << *neighbor << " of " << it->second << endl;
				currentBestNeighbor = *neighbor;
				currBestFit = it2;
			}

			//Raquel: also look into other models from the same particle
			if(options.models.size()>1){

				for(unsigned int mid=1; mid < options.models.size(); mid++){

					subPar = fcalcsubParID(*neighbor, mid, options.models.size());
					it2 = subParRankFinal[subPar].second;

					if (it2 < currBestFit) {
						//cout << "it does! setting current best fit of particle " << *neighbor << " of " << it->second << endl;
						currentBestNeighbor = *neighbor;
						currBestFit = it2;
					}

				}

			}
		}else{
			// If Neighbor's best fit is better than ours, update the best
			// neighbor. Also, update the current best fit value
			if (it->second < currBestFit) {
				//cout << "it does! setting current best fit of particle " << *neighbor << " of " << it->second << endl;
				currentBestNeighbor = *neighbor;
				currBestFit = it->second;
			}

		}


	}

	//cout << "RAQUEL: exiting for loop populationTopology_" << endl;

	//cout << "cbn: " << currentBestNeighbor << endl;
	return particleBestParamSets_.at(currentBestNeighbor);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, 0x18(%rsp)
movl %edx, 0x14(%rsp)
cmpl $0x3, 0x1614(%rsi)
jb 0x7533a
movq 0xf0c7c(%rip), %rdi     # 0x165f98
leaq 0x68083(%rip), %rsi     # 0xdd3a6
callq 0x289f0
movl %ebp, %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
cmpq $0x0, 0x1608(%r14)
je 0x75443
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
movl %ebp, %edi
xorl %esi, %esi
callq 0x9ec09
movl %eax, %r15d
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x15c0(%r14), %rax
movq 0x15c8(%r14), %rdx
movq %rdx, %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x753e3
pushq $0x1
popq %r12
subq %rax, %rdx
movl %r12d, %eax
sarq $0x3, %rdx
cmpq %rax, %rdx
jbe 0x753e3
movl %ebp, %edi
movl %r12d, %esi
callq 0x9ec09
movl %eax, %r15d
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
minsd 0x8(%rsp), %xmm0
incl %r12d
movq 0x15c0(%r14), %rax
movq 0x15c8(%r14), %rdx
movsd %xmm0, 0x8(%rsp)
jmp 0x75399
cmpl $0x3, 0x1614(%r14)
jb 0x75464
movq 0xf0ba4(%rip), %rdi     # 0x165f98
leaq 0x67fd3(%rip), %rsi     # 0xdd3ce
callq 0x289f0
movslq %r15d, %rbx
movq 0x18f0(%r14), %rcx
movss 0x4(%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x28780
leaq 0x67fce(%rip), %rsi     # 0xdd3ed
movq %rax, %rdi
callq 0x289f0
movq 0x18f0(%r14), %rcx
movl (%rcx,%rbx,8), %esi
movq %rax, %rdi
callq 0x29290
movq %rax, %rdi
callq 0x284a0
jmp 0x75464
leaq 0x1a20(%r14), %rdi
leaq 0x14(%rsp), %r15
movq %r15, %rsi
callq 0x8626a
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movl (%r15), %ebp
movl %ebp, %ecx
movq 0x1a08(%r14), %rax
imulq $0x18, %rcx, %rcx
movq (%rax,%rcx), %r15
leaq 0x1a20(%r14), %r12
pushq $0x1
popq %r13
movl %ebp, %ebx
movl %ebp, %ecx
imulq $0x18, %rcx, %rcx
cmpq 0x8(%rax,%rcx), %r15
je 0x7559b
movq %r12, %rdi
movq %r15, %rsi
callq 0x8f6f4
movsd 0x28(%rax), %xmm0
ucomisd 0x5f5d5(%rip), %xmm0    # 0xd4a80
jne 0x754c0
jp 0x754c0
addq $0x4, %r15
movl 0x14(%rsp), %ebp
movq 0x1a08(%r14), %rax
jmp 0x75482
cmpq $0x0, 0x1608(%r14)
je 0x7557d
movl (%r15), %edi
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
xorl %esi, %esi
callq 0x9ec09
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7550e
movl (%r15), %ebx
movsd %xmm0, 0x8(%rsp)
movq 0x15c0(%r14), %rax
movq 0x15c8(%r14), %rdx
movq %rdx, %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x754af
movl %r13d, %ebp
subq %rax, %rdx
movl %ebp, %eax
sarq $0x3, %rdx
cmpq %rax, %rdx
jbe 0x754af
movl (%r15), %edi
movl %ebp, %esi
callq 0x9ec09
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7556b
movl (%r15), %ebx
movsd %xmm0, 0x8(%rsp)
incl %ebp
movq 0x15c0(%r14), %rax
movq 0x15c8(%r14), %rdx
jmp 0x7552b
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x754af
movl (%r15), %ebx
movsd %xmm0, 0x8(%rsp)
jmp 0x754af
addq $0x1cc0, %r14           # imm = 0x1CC0
leaq 0x24(%rsp), %rsi
movl %ebx, (%rsi)
movq %r14, %rdi
callq 0x861e4
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x59fa2
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop