// File: /workspace/github_cpp_makefile_star2_O3/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
cmpl $0x0, 0x1620(%rdi)
jle 0xa1b53
xorps %xmm0, %xmm0
xorl %ebp, %ebp
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
movss %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
callq 0x23270
xorps %xmm1, %xmm1
cvtss2sd 0x8(%rsp), %xmm1
addsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
incl %ebp
decl %r14d
cmpl 0x1620(%rbx), %ebp
jl 0xa1b19
cvtss2sd %xmm0, %xmm0
jmp 0xa1b56
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x20(%rsp), %rcx
movl $0x0, (%rcx)
xorl %eax, %eax
movq %rax, 0x8(%rcx)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
movq %rax, 0x20(%rcx)
movq %rax, 0x10(%rsp)
movq 0x1ac8(%rbx), %r14
addq $0x1ab8, %rbx           # imm = 0x1AB8
cmpq %rbx, %r14
je 0xa1be2
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x18(%rsp), %r15
leaq 0x10(%rsp), %r12
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
callq 0x23270
divsd 0x8(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x28(%r14), %r13d
movq %r15, %rdi
movq %r12, %rsi
callq 0x662fc
movl %r13d, (%rax)
movq %r14, %rdi
callq 0x22670
movq %rax, %r14
decl %ebp
cmpq %rbx, %rax
jne 0xa1ba0
callq 0x22230
movq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r13
cmpq %r13, %rbx
je 0xa1cdf
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x566d7(%rip), %xmm0    # 0xf82e0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x1, %ebp
movq 0xd4385(%rip), %r14     # 0x175fa8
leaq 0x5f105(%rip), %r12     # 0x100d2f
movl $0x5, %edx
movq %r14, %rdi
leaq 0x5f0e7(%rip), %rsi     # 0x100d20
callq 0x22ab0
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x23060
movq %rax, %r15
movl $0x8, %edx
movq %rax, %rdi
leaq 0x5f0c8(%rip), %rsi     # 0x100d26
callq 0x22ab0
movq %r15, %rdi
movl %ebp, %esi
callq 0x231e0
movq %rax, %r15
movl $0x7, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x22ab0
movsd 0x20(%rbx), %xmm0
movq %r15, %rdi
callq 0x23060
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 0x20(%rbx), %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0xa1cdf
movq %rbx, %rdi
callq 0x22670
movq %rax, %rbx
incl %ebp
cmpq %r13, %rax
jne 0xa1c2a
movq 0x30(%rsp), %rbx
movl 0x28(%rbx), %ebx
leaq 0x18(%rsp), %rdi
callq 0x462de
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa1cff
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x462de
movq %rbx, %rdi
callq 0x23240