// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::calcParticlePosQPSO(unsigned int, std::vector<double, std::allocator<double>>)
vector<double> Swarm::calcParticlePosQPSO(unsigned int particle, vector<double> mBests) {

	vector<double> nextPositions;
	vector<double> neighborhoodBests = getNeighborhoodBestPositions(particle);
	for (unsigned int d = 0; d < mBests.size(); ++d) {
		//cout << particle << " before: " << particleCurrParamSets_[particle][d] << endl;
		//cout << particle << " mbest: " << mBests[d] << endl;
		//cout << particle << " best: " << particleBestParamSets_[particle][d] << endl;
		//cout << particle << " swarmbest: " << getNeighborhoodBestPositions(particle)[d] << endl;
		double fi1 = ((double) rand() / (RAND_MAX));
		//cout << "f1: " << fi1 << endl;
		//double fi2 = ((double) rand() / (RAND_MAX));
		//double p = ((fi1 * particleBestFits_.at(particle)) + (fi2 * swarmBestFits_.begin()->first)) / (fi1 + fi2);
		// TODO: Work out whether or not we should be using abs() for this and below
		double p = fi1 * abs(particleBestParamSets_[particle][d]) + (1 - fi1) * neighborhoodBests[d];
		double u = ((double) rand() / (RAND_MAX));

		// TODO: Linearly decrease beta from 1.0 to 0.5? See Liu et al.

		// Liu et all, 2005, eq 2a
		if (u > 0.5) {
			nextPositions.push_back(p - beta_ * abs(mBests[d] - abs(particleCurrParamSets_.at(particle)[d])) * log(1/u));
			//cout << particle << " p: " << p << endl;
			//cout << "mbest - curr: " << mBests[d] - particleCurrParamSets_.at(particle)[d] << endl;
			//cout << "log: " << log(1/u) << endl;
			//cout << particle << " after: " << p - beta_ * abs(mBests[d] - particleCurrParamSets_.at(particle)[d]) * log(1/u) << endl;
		}
		else {
			nextPositions.push_back(p + beta_ * abs(mBests[d] - abs(particleCurrParamSets_.at(particle)[d])) * log(1/u));
			//cout << particle << " after: " << p + beta_ * abs(mBests[d] - particleCurrParamSets_.at(particle)[d]) * log(1/u) << endl;
		}
	}

	return nextPositions;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rcx, -0x98(%rbp)
movq %rdi, -0x90(%rbp)
movq %rdi, %rax
movq %rax, -0x88(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x80(%rbp)
movb $0x0, -0x21(%rbp)
callq 0xb2580
movq -0x80(%rbp), %rsi
movl -0x14(%rbp), %edx
leaq -0x40(%rbp), %rdi
callq 0xfe010
jmp 0xfdc65
movl $0x0, -0x50(%rbp)
movq -0x98(%rbp), %rdi
movl -0x50(%rbp), %eax
movq %rax, -0xa0(%rbp)
callq 0xb25c0
movq %rax, %rcx
movq -0xa0(%rbp), %rax
cmpq %rcx, %rax
jae 0xfdfc8
callq 0x30b00
movq -0x80(%rbp), %rdi
cvtsi2sd %eax, %xmm0
movsd 0xdce76(%rip), %xmm1    # 0x1dab20
divsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
addq $0x1cc0, %rdi           # imm = 0x1CC0
leaq -0x14(%rbp), %rsi
callq 0x122da0
movq %rax, -0xa8(%rbp)
jmp 0xfdcd9
movq -0xa8(%rbp), %rdi
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd (%rax), %xmm0
callq 0x9c8a0
movsd %xmm0, -0xb8(%rbp)
jmp 0xfdcfd
movsd 0xdce23(%rip), %xmm0    # 0x1dab28
subsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xb2610
movsd -0xc0(%rbp), %xmm1
movsd -0xb0(%rbp), %xmm0
movsd -0xb8(%rbp), %xmm2
mulsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
callq 0x30b00
cvtsi2sd %eax, %xmm0
movsd 0xdcdc6(%rip), %xmm1    # 0x1dab20
divsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movsd -0x68(%rbp), %xmm0
movsd 0xdb480(%rip), %xmm1    # 0x1d91f0
ucomisd %xmm1, %xmm0
jbe 0xfdeb0
movq -0x80(%rbp), %rax
movq -0x98(%rbp), %rdi
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd 0x1da0(%rax), %xmm0
movsd %xmm0, -0xd8(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movq -0x80(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0xd0(%rbp)
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x14(%rbp), %rsi
callq 0x122470
movq %rax, -0xc8(%rbp)
jmp 0xfddd5
movq -0xc8(%rbp), %rdi
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd (%rax), %xmm0
callq 0x9c8a0
movsd %xmm0, -0xe8(%rbp)
jmp 0xfddf9
movsd -0xe8(%rbp), %xmm1
movsd -0xd0(%rbp), %xmm0
subsd %xmm1, %xmm0
callq 0x9c8a0
movsd %xmm0, -0xf0(%rbp)
jmp 0xfde1c
movsd -0xf0(%rbp), %xmm1
movsd -0xd8(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0xf8(%rbp)
movsd -0x68(%rbp), %xmm1
movsd 0xdcce3(%rip), %xmm0    # 0x1dab28
divsd %xmm1, %xmm0
callq 0x2f410
movsd -0xf8(%rbp), %xmm1
movq -0x90(%rbp), %rdi
movaps %xmm0, %xmm2
movsd -0xe0(%rbp), %xmm0
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
leaq -0x70(%rbp), %rsi
callq 0xb2630
jmp 0xfde80
jmp 0xfdfb8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xfdff7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xb2660
jmp 0xfdff7
movq -0x80(%rbp), %rax
movq -0x98(%rbp), %rdi
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0x118(%rbp)
movsd 0x1da0(%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movq -0x80(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x108(%rbp)
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x14(%rbp), %rsi
callq 0x122470
movq %rax, -0x100(%rbp)
jmp 0xfdf0b
movq -0x100(%rbp), %rdi
movl -0x50(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd (%rax), %xmm0
callq 0x9c8a0
movsd %xmm0, -0x120(%rbp)
jmp 0xfdf2f
movsd -0x120(%rbp), %xmm1
movsd -0x108(%rbp), %xmm0
subsd %xmm1, %xmm0
callq 0x9c8a0
movsd %xmm0, -0x128(%rbp)
jmp 0xfdf52
movsd -0x128(%rbp), %xmm1
movsd -0x110(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0x130(%rbp)
movsd -0x68(%rbp), %xmm1
movsd 0xdcbad(%rip), %xmm0    # 0x1dab28
divsd %xmm1, %xmm0
callq 0x2f410
movsd -0x118(%rbp), %xmm1
movq -0x90(%rbp), %rdi
movaps %xmm0, %xmm2
movsd -0x130(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x78(%rbp)
leaq -0x78(%rbp), %rsi
callq 0xb2630
jmp 0xfdfb6
jmp 0xfdfb8
jmp 0xfdfba
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xfdc6c
movb $0x1, -0x21(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xb2660
testb $0x1, -0x21(%rbp)
jne 0xfdfe7
movq -0x90(%rbp), %rdi
callq 0xb2660
movq -0x88(%rbp), %rax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x90(%rbp), %rdi
callq 0xb2660
movq -0x48(%rbp), %rdi
callq 0x30170
nopl (%rax)