// File: /workspace/github_cpp_makefile_star2_O1/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 0xa25f5
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 0xa25bb
cvtss2sd %xmm0, %xmm0
jmp 0xa25f8
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 0xa2684
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 0x667c6
movl %r13d, (%rax)
movq %r14, %rdi
callq 0x22670
movq %rax, %r14
decl %ebp
cmpq %rbx, %rax
jne 0xa2642
callq 0x22230
movq 0x30(%rsp), %rbx
leaq 0x20(%rsp), %r13
cmpq %r13, %rbx
sete %r12b
je 0xa2783
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x55c51(%rip), %xmm0    # 0xf8300
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0x1, %ebp
movq 0xd38c7(%rip), %r14     # 0x175f90
movl $0x5, %edx
movq %r14, %rdi
leaq 0x5e661(%rip), %rsi     # 0x100d39
callq 0x22ab0
movq %r14, %rdi
movsd 0x8(%rsp), %xmm0
callq 0x23060
movq %rax, %r15
movl $0x8, %edx
movq %rax, %rdi
leaq 0x5e642(%rip), %rsi     # 0x100d3f
callq 0x22ab0
movq %r15, %rdi
movl %ebp, %esi
callq 0x231e0
movq %rax, %r15
movl $0x7, %edx
movq %rax, %rdi
leaq 0x5e62a(%rip), %rsi     # 0x100d48
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 0xa2785
movq %rbx, %rdi
callq 0x22670
movq %rax, %rbx
incl %ebp
cmpq %r13, %rax
sete %r12b
jne 0xa26c9
jmp 0xa2789
jmp 0xa2789
movl 0x28(%rbx), %r15d
testb $0x1, %r12b
je 0xa2798
movq 0x30(%rsp), %rax
movl 0x28(%rax), %r15d
leaq 0x18(%rsp), %rdi
callq 0x464c2
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xa27b6
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x464c2
movq %rbx, %rdi
callq 0x23240
nop