// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rsi, %r14
movq %rdi, %rbx
movl %edx, 0x4(%rsp)
cmpl $0x3, 0x1614(%rsi)
jb 0x8b088
movq 0xeaf52(%rip), %r15     # 0x175f90
leaq 0x74191(%rip), %rsi     # 0xff1d6
movl $0x27, %edx
movq %r15, %rdi
callq 0x22ab0
movl 0x4(%rsp), %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 0x8b1e0
movl 0x4(%rsp), %edi
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
xorl %esi, %esi
callq 0xbf2a3
movl %eax, %ebp
cltq
movq 0x18f0(%r14), %rcx
cvtss2sd 0x4(%rcx,%rax,8), %xmm1
movq 0x15c8(%r14), %rax
subq 0x15c0(%r14), %rax
cmpq $0x9, %rax
jb 0x8b13c
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq $0x2, %rdx
jb 0x8b13c
movl $0x1, %r15d
movsd %xmm1, 0x8(%rsp)
movl 0x4(%rsp), %edi
movl %r15d, %esi
callq 0xbf2a3
movl %eax, %ebp
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm1, %xmm1
cvtss2sd 0x4(%rcx,%rax,8), %xmm1
minsd 0x8(%rsp), %xmm1
incl %r15d
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
ja 0x8b0f6
cmpl $0x3, 0x1614(%r14)
jb 0x8b1f5
movq 0xeae3f(%rip), %r15     # 0x175f90
leaq 0x740a6(%rip), %rsi     # 0xff1fe
movl $0x1e, %edx
movq %r15, %rdi
movsd %xmm1, 0x8(%rsp)
callq 0x22ab0
movslq %ebp, %r12
movq 0x18f0(%r14), %rax
cvtss2sd 0x4(%rax,%r12,8), %xmm0
movq %r15, %rdi
callq 0x23060
movq %rax, %r15
leaq 0x7408f(%rip), %rsi     # 0xff21d
movl $0x7, %edx
movq %rax, %rdi
callq 0x22ab0
movq 0x18f0(%r14), %rax
movl (%rax,%r12,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 0x8b1f5
leaq 0x1a20(%r14), %rdi
leaq 0x4(%rsp), %rsi
callq 0xa83c8
movsd (%rax), %xmm1
movl 0x4(%rsp), %r12d
movq 0x1a08(%r14), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %r13
cmpq 0x8(%rax,%rcx,8), %r13
je 0x8b38d
leaq 0x1a28(%r14), %rbp
movq 0x1a30(%r14), %rcx
movq %rbp, %rax
testq %rcx, %rcx
je 0x8b248
movl (%r13), %edx
movq %rbp, %rax
xorl %esi, %esi
cmpl %edx, 0x20(%rcx)
setb %sil
cmovaeq %rcx, %rax
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x8b231
movq %rbp, %rcx
cmpq %rbp, %rax
je 0x8b25e
movl (%r13), %ecx
cmpl 0x20(%rax), %ecx
cmovbq %rbp, %rax
movq %rax, %rcx
movsd 0x28(%rcx), %xmm0
ucomisd 0x6b8cd(%rip), %xmm0    # 0xf6b38
jne 0x8b28e
jp 0x8b28e
addq $0x4, %r13
movl 0x4(%rsp), %eax
movq 0x1a08(%r14), %rcx
leaq (%rax,%rax,2), %rax
cmpq 0x8(%rcx,%rax,8), %r13
jne 0x8b21b
jmp 0x8b38d
cmpq $0x0, 0x1608(%r14)
je 0x8b376
movl (%r13), %edi
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
shrq $0x3, %rdx
xorl %esi, %esi
movsd %xmm1, 0x8(%rsp)
callq 0xbf2a3
movsd 0x8(%rsp), %xmm1
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x8b2e5
movl (%r13), %r12d
movapd %xmm0, %xmm1
movq 0x15c8(%r14), %rax
subq 0x15c0(%r14), %rax
cmpq $0x9, %rax
jb 0x8b26f
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq $0x2, %rdx
jb 0x8b26f
movl $0x1, %r15d
movsd %xmm1, 0x8(%rsp)
movl (%r13), %edi
movl %r15d, %esi
callq 0xbf2a3
movsd 0x8(%rsp), %xmm1
cltq
movq 0x18f0(%r14), %rcx
xorps %xmm0, %xmm0
cvtss2sd 0x4(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x8b357
movl (%r13), %r12d
movapd %xmm0, %xmm1
incl %r15d
movq 0x15c8(%r14), %rdx
subq 0x15c0(%r14), %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
ja 0x8b31f
jmp 0x8b26f
ucomisd %xmm0, %xmm1
jbe 0x8b26f
movl (%r13), %r12d
movapd %xmm0, %xmm1
jmp 0x8b26f
addq $0x1cc0, %r14           # imm = 0x1CC0
leaq 0x14(%rsp), %rsi
movl %r12d, (%rsi)
movq %r14, %rdi
callq 0xa837c
movq %rbx, %rdi
movq %rax, %rsi
callq 0x66228
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop