// File: /workspace/github_cpp_makefile_star2_O3/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 $0x18, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movl %edx, 0x10(%rsp)
cmpl $0x3, 0x1614(%rsi)
jb 0x8a772
movq 0xeb87e(%rip), %r15     # 0x175fa8
leaq 0x74b15(%rip), %rsi     # 0xff246
movl $0x27, %edx
movq %r15, %rdi
callq 0x22ab0
movl %ebp, %esi
movq %r15, %rdi
callq 0x22650
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r15, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
cmpq $0x0, 0x1608(%r14)
je 0x8a8c3
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
movl %ebp, %edi
xorl %esi, %esi
callq 0xbe989
movl %eax, %r15d
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm1
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
cmpq $0x9, %rdx
setae %al
sarq $0x3, %rdx
cmpq $0x2, %rdx
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8a81f
movl $0x1, %r12d
movsd %xmm1, 0x8(%rsp)
movl %ebp, %edi
movl %r12d, %esi
callq 0xbe989
movl %eax, %r15d
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx,%rax,8), %xmm1
minsd 0x8(%rsp), %xmm1
incl %r12d
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq %r12, %rdx
ja 0x8a7da
cmpl $0x3, 0x1614(%r14)
jb 0x8a8de
movq 0xeb774(%rip), %r12     # 0x175fa8
leaq 0x74a33(%rip), %rsi     # 0xff26e
movl $0x1e, %edx
movq %r12, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x22ab0
movslq %r15d, %r13
movq 0x18f0(%r14), %rax
cvtss2sd 0x4(%rax,%r13,8), %xmm0
movq %r12, %rdi
callq 0x23060
movq %rax, %r15
leaq 0x74a1c(%rip), %rsi     # 0xff28d
movl $0x7, %edx
movq %rax, %rdi
callq 0x22ab0
movq 0x18f0(%r14), %rax
movl (%rax,%r13,8), %esi
movq %r15, %rdi
callq 0x231e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r15, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movsd 0x8(%rsp), %xmm1
jmp 0x8a8de
leaq 0x1a20(%r14), %rdi
leaq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xa7a8c
movsd (%rax), %xmm1
movl (%r15), %ebp
movl %ebp, %eax
movq 0x1a08(%r14), %rcx
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %r12
cmpq 0x8(%rcx,%rax,8), %r12
je 0x8aa66
leaq 0x1a28(%r14), %r13
movq 0x1a30(%r14), %rdx
movq %r13, %rsi
testq %rdx, %rdx
je 0x8a940
movl (%r12), %ecx
movq %r13, %rax
xorl %esi, %esi
cmpl %ecx, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rax
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x8a917
movq %r13, %rsi
cmpq %r13, %rax
je 0x8a940
cmpl 0x20(%rax), %ecx
cmovbq %r13, %rax
movq %rax, %rsi
movsd 0x28(%rsi), %xmm0
ucomisd 0x6c19b(%rip), %xmm0    # 0xf6ae8
jne 0x8a970
jp 0x8a970
addq $0x4, %r12
movl 0x10(%rsp), %eax
movq 0x1a08(%r14), %rcx
leaq (%rax,%rax,2), %rax
cmpq 0x8(%rcx,%rax,8), %r12
jne 0x8a901
jmp 0x8aa66
cmpq $0x0, 0x1608(%r14)
je 0x8aa4f
movl (%r12), %edi
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
xorl %esi, %esi
movsd %xmm1, 0x8(%rsp)
callq 0xbe989
movsd 0x8(%rsp), %xmm1
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x8a9c7
movl (%r12), %ebp
movapd %xmm0, %xmm1
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
cmpq $0x9, %rdx
setae %al
sarq $0x3, %rdx
cmpq $0x2, %rdx
setae %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8a951
movl $0x1, %r15d
movsd %xmm1, 0x8(%rsp)
movl (%r12), %edi
movl %r15d, %esi
callq 0xbe989
movsd 0x8(%rsp), %xmm1
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x8aa30
movl (%r12), %ebp
movapd %xmm0, %xmm1
incl %r15d
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
ja 0x8a9f8
jmp 0x8a951
ucomisd %xmm0, %xmm1
jbe 0x8a951
movl (%r12), %ebp
movapd %xmm0, %xmm1
jmp 0x8a951
addq $0x1cc0, %r14           # imm = 0x1CC0
leaq 0x14(%rsp), %rsi
movl %ebp, (%rsi)
movq %r14, %rdi
callq 0xa7a46
movq %rbx, %rdi
movq %rax, %rsi
callq 0x65d5c
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop