// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::runNelderMead(unsigned int, unsigned int)
void Swarm::runNelderMead(unsigned int it, unsigned int cpu) {
	if (options.verbosity >= 3) {
		cout << "Running Nelder-Meade local search for particle " << it << " on cpu " << cpu << endl;
	}
	// On the master side, we just need to construct the simplex, serialize it,
	// then send it to the CPU..

	// calc -> params
	map<double, vector<double> > simplex {pair<double, vector<double> >(particleBestFits_.at(it), normalizeParams(particleCurrParamSets_.at(it)))};

	// Get our nearest neighbors in parameter space
	map<double, unsigned int> neighbors = getNearestNeighbors(it, this->getNumFreeParams());//map<double, unsigned int> neighbors = getNearestNeighbors(it, options.model->getNumFreeParams());

	// Fill our simplex
	for (auto particle = neighbors.begin(); particle != neighbors.end(); ++particle) {
		cout << "chose neighbor: " << particle->second << " with fit of " <<  particleBestFits_.at(particle->second) << endl;
		simplex[particleBestFits_.at(particle->second)] = normalizeParams(particleCurrParamSets_.at(particle->second));
		for (auto param = simplex[particleBestFits_.at(particle->second)].begin(); param != simplex[particleBestFits_.at(particle->second)].end(); ++ param) {
			cout << "p: " << *param << endl;
		}
	}

	std::stringstream oss;
	boost::archive::text_oarchive oa(oss);
	oa << simplex;
	std::string serializedSimplex(oss.str());

	vector<string> message;
	message.push_back(serializedSimplex);

	runningParticles_.insert(cpu);
	swarmComm->sendToSwarm(0, cpu, BEGIN_NELDER_MEAD, false, message);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4e0, %rsp            # imm = 0x4E0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3c0(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0x1175e0
movq 0x1bd9b0(%rip), %rdi    # 0x2d4f50
leaq 0xcd3b4(%rip), %rsi     # 0x1e495b
callq 0x2ff50
movq %rax, %rdi
movl -0xc(%rbp), %esi
callq 0x30640
movq %rax, %rdi
leaq 0xcc3a6(%rip), %rsi     # 0x1e3967
callq 0x2ff50
movq %rax, %rdi
movl -0x10(%rbp), %esi
callq 0x30640
movq %rax, %rdi
movq 0x1bd9ed(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x3c0(%rbp), %rdi
movb $0x1, -0xd9(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x78(%rbp)
addq $0x1a20, %rdi           # imm = 0x1A20
leaq -0xc(%rbp), %rsi
callq 0x1226a0
movq %rax, -0x3c8(%rbp)
jmp 0x11760f
movq -0x3c0(%rbp), %rdi
addq $0x1400, %rdi           # imm = 0x1400
leaq -0xc(%rbp), %rsi
callq 0x122470
movq %rax, -0x3d0(%rbp)
jmp 0x11762f
movq -0x3d0(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0xb2bc0
jmp 0x117644
movq -0x3c0(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
leaq -0xd8(%rbp), %rdx
callq 0x117e60
jmp 0x117660
movq -0x3c8(%rbp), %rsi
leaq -0x98(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x125e80
jmp 0x11767c
leaq -0x70(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xc1610
jmp 0x11768e
movb $0x0, -0xd9(%rbp)
leaq -0x70(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x1, -0x48(%rbp)
leaq -0xdb(%rbp), %rdi
movq %rdi, -0x3d8(%rbp)
callq 0x125ec0
movq -0x3d8(%rbp), %r8
movq -0x50(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x40(%rbp), %rdi
leaq -0xda(%rbp), %rcx
callq 0x125ee0
jmp 0x1176d9
leaq -0xdb(%rbp), %rdi
callq 0x125fb0
leaq -0x70(%rbp), %rax
movq %rax, -0x3e8(%rbp)
addq $0x20, %rax
movq %rax, -0x3e0(%rbp)
movq -0x3e0(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x3f0(%rbp)
callq 0xb8a10
movq -0x3e8(%rbp), %rcx
movq -0x3f0(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x3e0(%rbp)
jne 0x1176fb
leaq -0x98(%rbp), %rdi
callq 0xb3140
leaq -0xc0(%rbp), %rdi
callq 0xb2660
leaq -0xd8(%rbp), %rdi
callq 0xb2660
movq -0x3c0(%rbp), %rdi
movl -0xc(%rbp), %eax
movl %eax, -0x3f8(%rbp)
callq 0x121af0
movl %eax, -0x3f4(%rbp)
jmp 0x11776d
movl -0x3f4(%rbp), %ecx
movl -0x3f8(%rbp), %edx
movq -0x3c0(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x11a1b0
jmp 0x11778e
leaq -0x110(%rbp), %rdi
callq 0x124b90
movq %rax, -0x118(%rbp)
leaq -0x110(%rbp), %rdi
callq 0xcb1d0
movq %rax, -0x120(%rbp)
leaq -0x118(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0x1177d0
jmp 0x117ca2
movq 0x1bd779(%rip), %rdi    # 0x2d4f50
leaq 0xcd1ad(%rip), %rsi     # 0x1e498b
callq 0x2ff50
movq %rax, -0x400(%rbp)
jmp 0x1177ec
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x400(%rbp), %rdi
movl 0x8(%rax), %esi
callq 0x30640
movq %rax, -0x408(%rbp)
jmp 0x117810
movq -0x408(%rbp), %rdi
leaq 0xca571(%rip), %rsi     # 0x1e1d8f
callq 0x2ff50
movq %rax, -0x410(%rbp)
jmp 0x11782c
movq -0x3c0(%rbp), %rax
addq $0x1a20, %rax           # imm = 0x1A20
movq %rax, -0x420(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x420(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x1226a0
movq %rax, -0x418(%rbp)
jmp 0x117868
movq -0x410(%rbp), %rdi
movq -0x418(%rbp), %rax
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0x428(%rbp)
jmp 0x117888
movq -0x428(%rbp), %rdi
movq 0x1bd732(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x11789d
movq -0x3c0(%rbp), %rax
addq $0x1400, %rax           # imm = 0x1400
movq %rax, -0x438(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x438(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x122470
movq %rax, -0x430(%rbp)
jmp 0x1178d9
movq -0x430(%rbp), %rsi
leaq -0x150(%rbp), %rdi
callq 0xb2bc0
jmp 0x1178ee
movq -0x3c0(%rbp), %rsi
leaq -0x138(%rbp), %rdi
leaq -0x150(%rbp), %rdx
callq 0x117e60
jmp 0x11790a
movq -0x3c0(%rbp), %rax
addq $0x1a20, %rax           # imm = 0x1A20
movq %rax, -0x448(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x448(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x1226a0
movq %rax, -0x440(%rbp)
jmp 0x117946
movq -0x440(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x125fd0
movq %rax, -0x450(%rbp)
jmp 0x11795f
movq -0x450(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movq %rsi, -0x468(%rbp)
callq 0x122d70
movq -0x468(%rbp), %rdi
callq 0xb2660
leaq -0x150(%rbp), %rdi
callq 0xb2660
movq -0x3c0(%rbp), %rax
addq $0x1a20, %rax           # imm = 0x1A20
movq %rax, -0x460(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x460(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x1226a0
movq %rax, -0x458(%rbp)
jmp 0x1179cd
movq -0x458(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x125fd0
movq %rax, -0x470(%rbp)
jmp 0x1179e6
movq -0x470(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0x158(%rbp)
movq -0x3c0(%rbp), %rax
addq $0x1a20, %rax           # imm = 0x1A20
movq %rax, -0x480(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x122660
movq -0x480(%rbp), %rdi
movq %rax, %rsi
addq $0x8, %rsi
callq 0x1226a0
movq %rax, -0x478(%rbp)
jmp 0x117a35
movq -0x478(%rbp), %rsi
leaq -0x40(%rbp), %rdi
callq 0x125fd0
movq %rax, -0x488(%rbp)
jmp 0x117a4e
movq -0x488(%rbp), %rdi
callq 0xbdfd0
movq %rax, -0x160(%rbp)
leaq -0x158(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0x122510
testb $0x1, %al
jne 0x117a7d
jmp 0x117c8f
movq 0x1bd4cc(%rip), %rdi    # 0x2d4f50
leaq 0xca172(%rip), %rsi     # 0x1e1bfd
callq 0x2ff50
movq %rax, -0x490(%rbp)
jmp 0x117a99
leaq -0x158(%rbp), %rdi
callq 0xb2ed0
movq -0x490(%rbp), %rdi
movsd (%rax), %xmm0
callq 0x30710
movq %rax, -0x498(%rbp)
jmp 0x117abe
movq -0x498(%rbp), %rdi
movq 0x1bd4fc(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0x117ad3
jmp 0x117ad5
leaq -0x158(%rbp), %rdi
callq 0xb2ef0
jmp 0x1179f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117bca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117bbe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117bb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117ba6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0xdb(%rbp), %rdi
callq 0x125fb0
leaq -0x70(%rbp), %rax
movq %rax, -0x4a8(%rbp)
addq $0x20, %rax
movq %rax, -0x4a0(%rbp)
movq -0x4a0(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x4b0(%rbp)
callq 0xb8a10
movq -0x4a8(%rbp), %rcx
movq -0x4b0(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x4a0(%rbp)
jne 0x117b73
jmp 0x117ba6
leaq -0x98(%rbp), %rdi
callq 0xb3140
leaq -0xc0(%rbp), %rdi
callq 0xb2660
leaq -0xd8(%rbp), %rdi
callq 0xb2660
testb $0x1, -0xd9(%rbp)
jne 0x117bd5
jmp 0x117c19
movq -0x78(%rbp), %rax
leaq -0x70(%rbp), %rcx
cmpq %rax, %rcx
movq %rax, -0x4b8(%rbp)
je 0x117c17
movq -0x4b8(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x4c0(%rbp)
callq 0xb8a10
movq -0x4c0(%rbp), %rax
leaq -0x70(%rbp), %rcx
cmpq %rcx, %rax
movq %rax, -0x4b8(%rbp)
jne 0x117be9
jmp 0x117c19
jmp 0x117e52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117e49
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117e3d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117c7e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x138(%rbp), %rdi
callq 0xb2660
leaq -0x150(%rbp), %rdi
callq 0xb2660
jmp 0x117e3d
jmp 0x117c91
leaq -0x118(%rbp), %rdi
callq 0xcbd20
jmp 0x1177a1
leaq -0x2e8(%rbp), %rdi
callq 0x2f790
jmp 0x117cb0
leaq -0x2d8(%rbp), %rsi
leaq -0x370(%rbp), %rdi
xorl %edx, %edx
callq 0xd0ff0
jmp 0x117cc7
leaq -0x370(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x1260b0
jmp 0x117cd9
leaq -0x390(%rbp), %rdi
leaq -0x2e8(%rbp), %rsi
callq 0x302b0
jmp 0x117cee
leaq -0x3a8(%rbp), %rdi
movq %rdi, -0x4c8(%rbp)
callq 0x448b0
movq -0x4c8(%rbp), %rdi
leaq -0x390(%rbp), %rsi
callq 0x45280
jmp 0x117d16
movq -0x3c0(%rbp), %rdi
addq $0x1998, %rdi           # imm = 0x1998
leaq -0x10(%rbp), %rsi
callq 0x123270
movb %dl, -0x4d1(%rbp)
movq %rax, -0x4d0(%rbp)
jmp 0x117d3c
movq -0x3c0(%rbp), %rax
movb -0x4d1(%rbp), %cl
movq -0x4d0(%rbp), %rdx
movq %rdx, -0x3b8(%rbp)
movb %cl, -0x3b0(%rbp)
movq (%rax), %rdi
movl -0x10(%rbp), %edx
movq %rsp, %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movl $0x1a, %ecx
xorl %r8d, %r8d
leaq -0x3a8(%rbp), %r9
movl %r8d, %esi
callq 0xcd520
jmp 0x117d85
leaq -0x3a8(%rbp), %rdi
callq 0x448d0
leaq -0x390(%rbp), %rdi
callq 0x311e8
leaq -0x370(%rbp), %rdi
callq 0xd10b0
leaq -0x2e8(%rbp), %rdi
callq 0x2f940
leaq -0x110(%rbp), %rdi
callq 0x84630
leaq -0x40(%rbp), %rdi
callq 0xb39a0
addq $0x4e0, %rsp            # imm = 0x4E0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117e31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
jmp 0x117e25
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa0(%rbp)
movl %eax, -0xa4(%rbp)
leaq -0x3a8(%rbp), %rdi
callq 0x448d0
leaq -0x390(%rbp), %rdi
callq 0x311e8
leaq -0x370(%rbp), %rdi
callq 0xd10b0
leaq -0x2e8(%rbp), %rdi
callq 0x2f940
leaq -0x110(%rbp), %rdi
callq 0x84630
leaq -0x40(%rbp), %rdi
callq 0xb39a0
movq -0xa0(%rbp), %rdi
callq 0x30170
nop