// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x14(%rbp)
movl $0x1, -0x18(%rbp)
movq -0xa8(%rbp), %rcx
movl -0x18(%rbp), %eax
cmpl 0x1620(%rcx), %eax
jg 0x116b19
xorl %edi, %edi
subl -0x18(%rbp), %edi
callq 0x124b70
movaps %xmm0, %xmm1
movss -0x14(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x14(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x116ad9
leaq -0x48(%rbp), %rdi
callq 0xcae50
movq -0xa8(%rbp), %rdi
movl $0x0, -0x4c(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x122630
movq %rax, -0x60(%rbp)
movq -0xa8(%rbp), %rdi
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x1231a0
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0x116b75
jmp 0x116c12
movl -0x4c(%rbp), %edi
movl %edi, %eax
incl %eax
movl %eax, -0x4c(%rbp)
notl %edi
callq 0x124b70
movsd %xmm0, -0xb0(%rbp)
jmp 0x116b90
movsd -0xb0(%rbp), %xmm1
movss -0x14(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
divsd %xmm0, %xmm1
movsd -0x58(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x122660
movl 0x8(%rax), %eax
movl %eax, -0xbc(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0xb4170
movq %rax, -0xb8(%rbp)
jmp 0x116bdb
movq -0xb8(%rbp), %rax
movl -0xbc(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x60(%rbp), %rdi
callq 0xcbd20
jmp 0x116b48
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x84630
jmp 0x116dd9
callq 0x30b00
cvtsi2sd %eax, %xmm0
movsd 0xc3efd(%rip), %xmm1    # 0x1dab20
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x124b90
movq %rax, -0x88(%rbp)
leaq -0x48(%rbp), %rdi
callq 0xcb1d0
movq %rax, -0x90(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0x116c73
jmp 0x116d98
movq 0x1be2d6(%rip), %rdi    # 0x2d4f50
leaq 0xcccef(%rip), %rsi     # 0x1e3970
callq 0x2ff50
movq %rax, -0xc8(%rbp)
jmp 0x116c8f
movq -0xc8(%rbp), %rdi
movss -0x78(%rbp), %xmm0
callq 0x30ce0
movq %rax, -0xd0(%rbp)
jmp 0x116ca9
movq -0xd0(%rbp), %rdi
leaq 0xcccbf(%rip), %rsi     # 0x1e3976
callq 0x2ff50
movq %rax, -0xd8(%rbp)
jmp 0x116cc5
movq -0xd8(%rbp), %rdi
movl -0x7c(%rbp), %esi
incl %esi
movl %esi, -0x7c(%rbp)
callq 0x2f800
movq %rax, -0xe0(%rbp)
jmp 0x116ce2
movq -0xe0(%rbp), %rdi
leaq 0xccc8f(%rip), %rsi     # 0x1e397f
callq 0x2ff50
movq %rax, -0xe8(%rbp)
jmp 0x116cfe
leaq -0x88(%rbp), %rdi
callq 0x122660
movq -0xe8(%rbp), %rdi
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0xf0(%rbp)
jmp 0x116d23
movq -0xf0(%rbp), %rdi
movq 0x1be297(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x116d38
movss -0x78(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0xf8(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x122660
movsd -0xf8(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x116d85
leaq -0x88(%rbp), %rdi
callq 0x122660
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
jmp 0x116dc4
jmp 0x116d87
leaq -0x88(%rbp), %rdi
callq 0xcbd20
jmp 0x116c47
leaq -0x48(%rbp), %rdi
callq 0x124b90
movq %rax, -0xa0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x122660
movl 0x8(%rax), %eax
movl %eax, -0x4(%rbp)
movl $0x1, -0x94(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x84630
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0x70(%rbp), %rdi
callq 0x30170
nopw %cs:(%rax,%rax)