// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::generateTrialPointSA(unsigned int, unsigned int, std::vector<double, std::allocator<double>>, std::vector<float, std::allocator<float>>, std::vector<float, std::allocator<float>>, std::vector<std::vector<float, std::allocator<float>>, std::allocator<std::vector<float, std::allocator<float>>>>&)
vector<double> Swarm::generateTrialPointSA(unsigned int controller, unsigned int receiver, vector<double> particleRadii, vector<float>particleCRs, vector<float>particleFs, vector<vector<float> > &trialParams) {
	vector<double> currParams = normalizeParams(particleCurrParamSets_.at(controller));
	vector<double> testParams = deNormalizeParams(currParams);

	float cr;
	float f;

	// Choose either random F and CR or take from receiver
	if ( ((float)rand() / (float)RAND_MAX) < options.randParamsProbability) {
		cout << "choosing random F and CR" << endl;
		float min = 0.5;
		float max = 1.5;

		float r = (float)rand() / (float)RAND_MAX;
		float diff = max - min;
		f = (r * diff) + min;

		min = 0.1;
		max = 0.9;
		r = (float)rand() / (float)RAND_MAX;
		diff = max - min;
		cr = (r * diff) + min;

		//cout << "f is " << f << " and cr is " << cr << endl;
	}
	else {
		f = particleFs[receiver];
		cr = particleCRs[receiver];
		//cout << "using receiver f and cr of " << f << " and " << cr << endl;
	}

	trialParams[receiver][0] = f;
	trialParams[receiver][1] = cr;

	cout << "before mutate: " << currParams[0] << endl;
	// Mutation functions pulls parameters from the global list
	currParams = mutateParticleSA(controller, f);
	cout << "before crossover: " << currParams[0] << endl;
	// Crossover function uses params returned from mutation function
	currParams = crossoverParticleDE(controller, currParams, cr, true);

	//cout << "mutated and crossed over params" << endl;

	vector<double> newParams;
	for (auto param = currParams.begin(); param != currParams.end(); ++param) {
		float r = (float)rand() / (float)RAND_MAX;
		cout << "before jiggle: " << *param << endl;
		float newParam = *param + particleRadii[controller] * tan(3.141592654 * (r - 0.5));
		cout << "after jiggle: " << newParam << endl;

		if (newParam <= 0 || newParam > 1) {
			newParam = (double)rand() / (double)RAND_MAX;
			//cout << "new: " << newParam << endl;
		}

		newParams.push_back(newParam);
	}

	return deNormalizeParams(newParams);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %r9, -0x180(%rbp)
movq %r8, -0x178(%rbp)
movq %rdi, -0x170(%rbp)
movq %rdi, %rax
movq %rax, -0x168(%rbp)
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rax, -0x160(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x158(%rbp)
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x14(%rbp), %rsi
callq 0x122470
movq %rax, %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0x150(%rbp)
callq 0xb2bc0
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rdx
leaq -0x40(%rbp), %rdi
callq 0x117e60
jmp 0x116e86
leaq -0x58(%rbp), %rdi
callq 0xb2660
leaq -0x98(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xb2bc0
jmp 0x116ea1
movq -0x158(%rbp), %rsi
leaq -0x80(%rbp), %rdi
leaq -0x98(%rbp), %rdx
callq 0x117f40
jmp 0x116eba
leaq -0x98(%rbp), %rdi
callq 0xb2660
callq 0x30b00
movl %eax, %ecx
movq -0x158(%rbp), %rax
cvtsi2ss %ecx, %xmm1
movss 0xc9c78(%rip), %xmm0    # 0x1e0b58
divss %xmm0, %xmm1
movss 0x1724(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x11707e
movq 0x1be054(%rip), %rdi    # 0x2d4f50
leaq 0xcca84(%rip), %rsi     # 0x1e3987
callq 0x2ff50
movq %rax, -0x188(%rbp)
jmp 0x116f11
movq -0x188(%rbp), %rdi
movq 0x1be0a9(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x116f26
movss 0xc3c12(%rip), %xmm0    # 0x1dab40
movss %xmm0, -0xa4(%rbp)
movss 0xc9c2e(%rip), %xmm0    # 0x1e0b6c
movss %xmm0, -0xa8(%rbp)
callq 0x30b00
cvtsi2ss %eax, %xmm0
movss 0xc9c01(%rip), %xmm1    # 0x1e0b58
divss %xmm1, %xmm0
movss %xmm0, -0xac(%rbp)
movss -0xa8(%rbp), %xmm0
subss -0xa4(%rbp), %xmm0
movss %xmm0, -0xb0(%rbp)
movss -0xac(%rbp), %xmm0
movss -0xb0(%rbp), %xmm2
movss -0xa4(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0xa0(%rbp)
movss 0xc9bbd(%rip), %xmm0    # 0x1e0b68
movss %xmm0, -0xa4(%rbp)
movss 0xc9ba9(%rip), %xmm0    # 0x1e0b64
movss %xmm0, -0xa8(%rbp)
callq 0x30b00
cvtsi2ss %eax, %xmm0
movss 0xc9b84(%rip), %xmm1    # 0x1e0b58
divss %xmm1, %xmm0
movss %xmm0, -0xac(%rbp)
movss -0xa8(%rbp), %xmm0
subss -0xa4(%rbp), %xmm0
movss %xmm0, -0xb0(%rbp)
movss -0xac(%rbp), %xmm0
movss -0xb0(%rbp), %xmm2
movss -0xa4(%rbp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x9c(%rbp)
jmp 0x1170b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0xb2660
jmp 0x117566
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x11755d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x98(%rbp), %rdi
callq 0xb2660
jmp 0x11755d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x117554
movq -0x160(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0x124910
movq -0x180(%rbp), %rdi
movss (%rax), %xmm0
movss %xmm0, -0xa0(%rbp)
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0x124910
movss (%rax), %xmm0
movss %xmm0, -0x9c(%rbp)
movss -0xa0(%rbp), %xmm0
movss %xmm0, -0x198(%rbp)
movq 0x18(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0x1249a0
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x124910
movss -0x198(%rbp), %xmm0
movss %xmm0, (%rax)
movss -0x9c(%rbp), %xmm0
movss %xmm0, -0x194(%rbp)
movq 0x18(%rbp), %rdi
movl -0x18(%rbp), %eax
movl %eax, %esi
callq 0x1249a0
movq %rax, %rdi
movl $0x1, %esi
callq 0x124910
movss -0x194(%rbp), %xmm0
movss %xmm0, (%rax)
movq 0x1bde24(%rip), %rdi    # 0x2d4f50
leaq 0xcc86d(%rip), %rsi     # 0x1e39a0
callq 0x2ff50
movq %rax, -0x190(%rbp)
jmp 0x117141
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xb2610
movq -0x190(%rbp), %rdi
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0x1a0(%rbp)
jmp 0x117167
movq -0x1a0(%rbp), %rdi
movq 0x1bde53(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x11717c
movq -0x158(%rbp), %rsi
movl -0x14(%rbp), %edx
movss -0xa0(%rbp), %xmm0
leaq -0xc8(%rbp), %rdi
callq 0x117f90
jmp 0x11719c
leaq -0x40(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
movq %rsi, -0x1b0(%rbp)
callq 0x122d70
movq -0x1b0(%rbp), %rdi
callq 0xb2660
movq 0x1bdd8a(%rip), %rdi    # 0x2d4f50
leaq 0xcc7e3(%rip), %rsi     # 0x1e39b0
callq 0x2ff50
movq %rax, -0x1a8(%rbp)
jmp 0x1171db
xorl %eax, %eax
movl %eax, %esi
leaq -0x40(%rbp), %rdi
callq 0xb2610
movq -0x1a8(%rbp), %rdi
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0x1b8(%rbp)
jmp 0x117201
movq -0x1b8(%rbp), %rdi
movq 0x1bddb9(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x117216
movl -0x14(%rbp), %eax
movl %eax, -0x1bc(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xb2bc0
jmp 0x117231
movl -0x1bc(%rbp), %edx
movq -0x158(%rbp), %rsi
movss -0x9c(%rbp), %xmm0
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rcx
movl $0x1, %r8d
callq 0x115e50
jmp 0x117261
leaq -0x40(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x122d70
leaq -0xe0(%rbp), %rdi
callq 0xb2660
leaq -0xf8(%rbp), %rdi
callq 0xb2660
leaq -0x110(%rbp), %rdi
callq 0xb2580
leaq -0x40(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0x118(%rbp)
leaq -0x40(%rbp), %rdi
callq 0xbdfd0
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x122510
testb $0x1, %al
jne 0x1172d1
jmp 0x1174c5
callq 0x30b00
cvtsi2ss %eax, %xmm0
movss 0xc988e(%rip), %xmm1    # 0x1e0b70
mulss %xmm1, %xmm0
movss %xmm0, -0x124(%rbp)
movq 0x1bdc5b(%rip), %rdi    # 0x2d4f50
leaq 0xcc6c7(%rip), %rsi     # 0x1e39c3
callq 0x2ff50
movq %rax, -0x1c8(%rbp)
jmp 0x11730a
leaq -0x118(%rbp), %rdi
callq 0xb2ed0
movq -0x1c8(%rbp), %rdi
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0x1d0(%rbp)
jmp 0x11732f
movq -0x1d0(%rbp), %rdi
movq 0x1bdc8b(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x117344
leaq -0x118(%rbp), %rdi
callq 0xb2ed0
movq -0x178(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1e8(%rbp)
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd (%rax), %xmm0
movsd %xmm0, -0x1e0(%rbp)
movss -0x124(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0xc97a3(%rip), %xmm1    # 0x1e0b30
addsd %xmm1, %xmm0
movsd 0xc979f(%rip), %xmm1    # 0x1e0b38
mulsd %xmm1, %xmm0
callq 0x2fe30
movsd -0x1e8(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd -0x1e0(%rbp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x128(%rbp)
movq 0x1bdb80(%rip), %rdi    # 0x2d4f50
leaq 0xcc5fc(%rip), %rsi     # 0x1e39d3
callq 0x2ff50
movq %rax, -0x1d8(%rbp)
jmp 0x1173e5
movq -0x1d8(%rbp), %rdi
movss -0x128(%rbp), %xmm0
callq 0x30ce0
movq %rax, -0x1f0(%rbp)
jmp 0x117402
movq -0x1f0(%rbp), %rdi
movq 0x1bdbb8(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x117417
xorps %xmm0, %xmm0
ucomiss -0x128(%rbp), %xmm0
jae 0x117438
movss -0x128(%rbp), %xmm0
movss 0xbbbd1(%rip), %xmm1    # 0x1d3004
ucomiss %xmm1, %xmm0
jbe 0x117489
callq 0x30b00
cvtsi2sd %eax, %xmm0
movsd 0xc36d7(%rip), %xmm1    # 0x1dab20
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x128(%rbp)
jmp 0x117489
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0xb2660
jmp 0x117554
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x117548
movss -0x128(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x130(%rbp)
leaq -0x110(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0xb2630
jmp 0x1174b2
jmp 0x1174b4
leaq -0x118(%rbp), %rdi
callq 0xb2ef0
jmp 0x1172a5
leaq -0x148(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0xb2bc0
jmp 0x1174da
movq -0x158(%rbp), %rsi
movq -0x170(%rbp), %rdi
leaq -0x148(%rbp), %rdx
callq 0x117f40
jmp 0x1174f6
leaq -0x148(%rbp), %rdi
callq 0xb2660
leaq -0x110(%rbp), %rdi
callq 0xb2660
leaq -0x80(%rbp), %rdi
callq 0xb2660
leaq -0x40(%rbp), %rdi
callq 0xb2660
movq -0x168(%rbp), %rax
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x148(%rbp), %rdi
callq 0xb2660
leaq -0x110(%rbp), %rdi
callq 0xb2660
leaq -0x80(%rbp), %rdi
callq 0xb2660
leaq -0x40(%rbp), %rdi
callq 0xb2660
movq -0x60(%rbp), %rdi
callq 0x30170
nop