// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x70(%rbp)
movq %rdi, %rax
movq %rax, -0x68(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xfe06f
movq 0x1d6f07(%rip), %rdi    # 0x2d4f50
leaq 0xe3cc6(%rip), %rsi     # 0x1e1d16
callq 0x2ff50
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x30640
movq %rax, %rdi
movq 0x1d6f5e(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x60(%rbp), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x120, %rdi            # imm = 0x120
callq 0x123030
cmpq $0x1, %rax
jb 0xfe268
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x74(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movl -0x74(%rbp), %edi
movl %eax, %edx
xorl %esi, %esi
callq 0x17d3a0
movq -0x60(%rbp), %rdi
movl %eax, -0x24(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movq -0x60(%rbp), %rdi
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
cmpq $0x1, %rax
jbe 0xfe1cf
movl $0x1, -0x28(%rbp)
movq -0x60(%rbp), %rdi
movl -0x28(%rbp), %eax
movq %rax, -0x80(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq %rax, %rcx
movq -0x80(%rbp), %rax
cmpq %rcx, %rax
jae 0xfe1cd
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x84(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movl -0x88(%rbp), %edi
movl -0x84(%rbp), %esi
movl %eax, %edx
callq 0x17d3a0
movq -0x60(%rbp), %rdi
movl %eax, -0x24(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe1bd
movq -0x60(%rbp), %rdi
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0xfe1bf
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xfe106
jmp 0xfe1cf
movq -0x60(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xfe266
movq 0x1d6d69(%rip), %rdi    # 0x2d4f50
leaq 0xe3b50(%rip), %rsi     # 0x1e1d3e
callq 0x2ff50
movq -0x60(%rbp), %rdi
movq %rax, -0x98(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movq -0x98(%rbp), %rdi
movss 0x4(%rax), %xmm0
callq 0x30ce0
movq %rax, %rdi
leaq 0xe3b34(%rip), %rsi     # 0x1e1d5d
callq 0x2ff50
movq -0x60(%rbp), %rdi
movq %rax, -0x90(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movq -0x90(%rbp), %rdi
movl (%rax), %esi
callq 0x2f800
movq %rax, %rdi
movq 0x1d6d67(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xfe285
movq -0x60(%rbp), %rdi
addq $0x1a20, %rdi           # imm = 0x1A20
leaq -0x14(%rbp), %rsi
callq 0x1226a0
movsd (%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x60(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x2c(%rbp)
addq $0x1a08, %rdi           # imm = 0x1A08
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x1228d0
movq %rax, %rdi
callq 0xcc840
movq %rax, -0x38(%rbp)
movq -0x60(%rbp), %rdi
addq $0x1a08, %rdi           # imm = 0x1A08
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x1228d0
movq %rax, %rdi
callq 0xcc6d0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xd0cd0
testb $0x1, %al
jne 0xfe2e3
jmp 0xfe520
movq -0x60(%rbp), %rax
addq $0x1a20, %rax           # imm = 0x1A20
movq %rax, -0xa0(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movq -0xa0(%rbp), %rdi
movq %rax, %rsi
callq 0x123070
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x1230a0
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xfe32e
jp 0xfe32e
jmp 0xfe512
movq -0x60(%rbp), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x120, %rdi            # imm = 0x120
callq 0x123030
cmpq $0x1, %rax
jb 0xfe4d4
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movq -0x60(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movl -0xa4(%rbp), %edi
movl %eax, %edx
xorl %esi, %esi
callq 0x17d3a0
movq -0x60(%rbp), %rdi
movl %eax, -0x24(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe3d3
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
movq -0x60(%rbp), %rdi
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
cmpq $0x1, %rax
jbe 0xfe4d2
movl $0x1, -0x54(%rbp)
movq -0x60(%rbp), %rdi
movl -0x54(%rbp), %eax
movq %rax, -0xb0(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq %rax, %rcx
movq -0xb0(%rbp), %rax
cmpq %rcx, %rax
jae 0xfe4d0
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movq -0x60(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xb8(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0xb4(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movl -0xb8(%rbp), %edi
movl -0xb4(%rbp), %esi
movl %eax, %edx
callq 0x17d3a0
movq -0x60(%rbp), %rdi
movl %eax, -0x24(%rbp)
addq $0x18f0, %rdi           # imm = 0x18F0
movslq -0x24(%rbp), %rsi
callq 0x123050
movss 0x4(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe4c0
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0xfe4c2
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xfe3fb
jmp 0xfe4d2
jmp 0xfe510
leaq -0x48(%rbp), %rdi
callq 0x1230a0
movsd 0x8(%rax), %xmm1
movsd -0x20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfe50e
leaq -0x38(%rbp), %rdi
callq 0xd0d10
movl (%rax), %eax
movl %eax, -0x2c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x1230a0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
jmp 0xfe510
jmp 0xfe512
leaq -0x38(%rbp), %rdi
callq 0xd0f50
jmp 0xfe2ac
movq -0x60(%rbp), %rdi
addq $0x1cc0, %rdi           # imm = 0x1CC0
movl -0x2c(%rbp), %eax
movl %eax, -0x58(%rbp)
leaq -0x58(%rbp), %rsi
callq 0x122470
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0xb2bc0
movq -0x68(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)