// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::pickWeightedSA()
unsigned int Swarm::pickWeightedSA() {

	//Eq 6 Olensek et al

	// Calculate the reimann sum
	float sum = 0;
	for (int r = 1; r <= options.swarmSize; ++r) {
		sum += exp(0 - (signed)r);
		//cout << "0 - r: " << 0 - (signed)r << endl;
	}

	//cout << "sum: " << sum << endl;
	// Fill vector with probabilities of particles being selected
	map<double, unsigned int> probabilities;
	unsigned int r = 0;
	double S = 0;
	for (auto particle = particleBestFitsByFit_.begin(); particle != particleBestFitsByFit_.end(); ++particle) {
		S += exp(0 - (signed)++r) / sum;
		probabilities[S] = particle->second;
	}

	float rnd = ((double) rand() / (RAND_MAX));

	int count = 0;
	// Go through and choose the particle
	for (auto probability = probabilities.begin(); probability != probabilities.end(); ++probability) {
		cout << "rnd: " << rnd << " count: " << ++count << " prob: " << probability->first << endl;
		if (rnd < probability->first) {
			return probability->second;
		}

		//rnd -= probability->first;
	}

	// Fell off the end..return the worst particle.
	return probabilities.begin()->second;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
pushq $0x1
popq %r14
xorpd %xmm0, %xmm0
pushq $-0x1
popq %rbp
cmpl 0x1620(%rbx), %r14d
jg 0x8297f
movl %ebp, %edi
movss %xmm0, (%rsp)
callq 0x86a7b
cvtss2sd (%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm0
incl %r14d
decl %ebp
jmp 0x82956
leaq 0x20(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x8(%rsp), %r14
andq $0x0, (%r14)
movq 0x1ac8(%rbx), %r15
addq $0x1ab8, %rbx           # imm = 0x1AB8
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
pushq $-0x1
popq %rbp
leaq 0x18(%rsp), %r12
cmpq %rbx, %r15
je 0x829ff
movl %ebp, %edi
callq 0x86a7b
divsd (%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x28(%r15), %r13d
movq %r12, %rdi
movq %r14, %rsi
callq 0x5a4c8
movl %r13d, (%rax)
movq %r15, %rdi
callq 0x28700
movq %rax, %r15
decl %ebp
jmp 0x829c1
callq 0x28280
cvtsi2sd %eax, %xmm0
divsd 0x537f0(%rip), %xmm0    # 0xd6200
cvtsd2ss %xmm0, %xmm0
movq 0x30(%rsp), %rbx
movss %xmm0, (%rsp)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x1
popq %rbp
movq 0xe3566(%rip), %r14     # 0x165f98
leaq 0x5c4d0(%rip), %r15     # 0xdef09
leaq 0x5c4cf(%rip), %r12     # 0xdef0f
leaq 0x5c4d1(%rip), %r13     # 0xdef18
leaq 0x20(%rsp), %rax
cmpq %rax, %rbx
je 0x82aba
movq %r14, %rdi
movq %r15, %rsi
callq 0x289f0
movq %rax, %rdi
movss (%rsp), %xmm0
callq 0x28780
movq %rax, %rdi
movq %r12, %rsi
callq 0x289f0
movq %rax, %rdi
movl %ebp, %esi
callq 0x29290
movq %rax, %rdi
movq %r13, %rsi
callq 0x289f0
movsd 0x20(%rbx), %xmm0
movq %rax, %rdi
callq 0x290f0
movq %rax, %rdi
callq 0x284a0
movsd 0x20(%rbx), %xmm0
ucomisd 0x10(%rsp), %xmm0
ja 0x82abf
movq %rbx, %rdi
callq 0x28700
movq %rax, %rbx
incl %ebp
jmp 0x82a47
movq 0x30(%rsp), %rbx
movl 0x28(%rbx), %ebx
leaq 0x18(%rsp), %rdi
callq 0x45f7c
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x82adf
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x45f7c
movq %rbx, %rdi
callq 0x292f0