// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::processParamsDE(std::vector<double, std::allocator<double>>&, unsigned int, double)
void Swarm::processParamsDE(vector<double> &params, unsigned int subParID, double fit) {
	//unsigned int mid = fcalcMID(subParID,options.models.size());
	unsigned int pID = fcalcParID(subParID,options.models.size());

	if (options.verbosity >= 3) {
		cout << "Processing finished params for particle " << pID << " subParticle:" << subParID<<"  with fit of " << fit << endl;
	}

	unsigned int i = 0;

	//razi: TODO: modify, the params order is based on free parameters in the corresonding model, reorder based on the global list

	//outputError("Needs modifications: for models that have different numbers of free parameters, we need to check if the values are in the correct order.");

	// If this is the particle's first iteration, we need to store its params
	if (particleIterationCounter_[pID][0] == 1) {   //razi: later check consistency, models parameters
		//cout << "Storing " << params.size() << " params for particle " << pID << endl;
		for (auto param = params.begin(); param != params.end(); ++param) {
			if (options.verbosity >= 3) {
				cout << *param << endl;
			}
			particleCurrParamSets_[pID][i] = *param;

			//Raquel added: now parameters for subparticles are processed as well
			for(unsigned int mid=0; mid < options.models.size(); mid++){
				subparticleCurrParamSets_[pID][mid][i] = *param;
			}

			++i;
		}
	}

	// The the fit value of this param set is less than the particles best fit
	// we should update the particle's best fit, then store the best fit params
	if (particleBestFits_.at(pID) == 0 || fit < particleBestFits_.at(pID)) {
		if (options.verbosity >= 3) {
			cout << "Updating best fit and params for particle " << pID << endl;
		}

		// Insert into best fit lists, erasing in the case of the map with fits as keys
		particleBestFits_[pID] = fit;

		map<double, unsigned int>::iterator toDelIt = particleBestFitsByFit_.end();
		if (options.verbosity >= 4) {
			cout << "looping fits" << endl;
		}
		for (auto it = particleBestFitsByFit_.begin(); it != particleBestFitsByFit_.end(); ++it) {
			//cout << "loop: " << it->second << endl;
			if (it->second == pID) {
				//cout << "Erasing old best fit for particle " << pID << endl;
				toDelIt = it;
				//it = particleBestFitsByFit_.erase(it);
			}
		}
		if (options.verbosity >= 3) {
			cout << "setting new fit value" << endl;
		}
		if (toDelIt != particleBestFitsByFit_.end()) {
			particleBestFitsByFit_.erase(toDelIt);

		}
		if (options.verbosity >= 3) {
			cout << "done" << endl;
		}
		particleBestFitsByFit_.insert(pair<double, unsigned int>(fit, pID));

		//unsigned int i = 0;
		//for (auto param = params.begin(); param != params.end(); ++param) {
		//	cout << "Updating best param for particle " << pID << ": " << *param << endl;
		//	particleBestParamSets_[pID][i] = *param;
		//	++i;
		//}

		if (options.verbosity >= 3) {
			cout << "updating params" << endl;
		}
		for(unsigned int mid = 0; mid < options.models.size(); mid++){
			update_cur_particle_params(pID, mid, true);
		}
		if (options.verbosity >= 3) {
			cout << "done" << endl;
		}

	}



	if (subparticleBestFits_.at(subParID) == 0 || fit < subparticleBestFits_.at(subParID)) {
		if (options.verbosity >= 3) {
			cout << "Updating best fit and params for particle " << pID << endl;
		}

		// Insert into best fit lists, erasing in the case of the map with fits as keys
		subparticleBestFits_[subParID] = fit; //Raquel added support to subparticles
		subparticleBestFits_copy[subParID] = fit; //Raquel added support to subparticles

		map<double, unsigned int>::iterator toDelIt2 = subparticleBestFitsByFit_.end();
		if (options.verbosity >= 3) {
			cout << "looping fits" << endl;
		}
		for (auto it = subparticleBestFitsByFit_.begin(); it != subparticleBestFitsByFit_.end(); ++it) {
			//cout << "loop: " << it->second << endl;
			if (it->second == subParID) {
				//cout << "Erasing old best fit for particle " << pID << endl;
				toDelIt2 = it;
				//it = particleBestFitsByFit_.erase(it);
			}
		}
		if (options.verbosity >= 3) {
			cout << "seting new fit value" << endl;
		}
		if (toDelIt2 != subparticleBestFitsByFit_.end()) {
			subparticleBestFitsByFit_.erase(toDelIt2);

		}

		if (options.verbosity >= 3) {
			cout << "done" << endl;
		}
		subparticleBestFitsByFit_.insert(pair<double, unsigned int>(fit, subParID));//Raquel added support to subparticles
		subparticleBestFitsByFit_copy.insert(pair<double, unsigned int>(fit, subParID));//Raquel added support to subparticles
		currentsubparticleBestFitsByFit_.insert(pair<double, unsigned int>(fit, subParID));//Raquel added support to subparticles
		currentsubparticleBestFitsByFit_copy.insert(pair<double, unsigned int>(fit, subParID));//Raquel added support to subparticles

		//unsigned int i = 0;
		//for (auto param = params.begin(); param != params.end(); ++param) {
		//	cout << "Updating best param for particle " << pID << ": " << *param << endl;
		//	particleBestParamSets_[pID][i] = *param;
		//	++i;
		//}

		if (options.verbosity >= 3) {
			cout << "updating params" << endl;
		}
		for(unsigned int mid = 0; mid < options.models.size(); mid++){
			update_cur_particle_params(pID, mid, true);
		}
		if (options.verbosity >= 3) {
			cout << "done" << endl;
		}

	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x10(%rsp)
movl %edx, %ebp
movq %rsi, 0x38(%rsp)
movq %rdi, %rbx
movl %edx, 0xc(%rsp)
movq 0x15c8(%rdi), %rsi
subq 0x15c0(%rdi), %rsi
shrq $0x3, %rsi
movl %edx, %edi
callq 0x9ec1e
movl %eax, 0x8(%rsp)
cmpl $0x3, 0x1614(%rbx)
jb 0x75f5b
movl %eax, %r15d
movq 0xf0092(%rip), %rdi     # 0x165f98
leaq 0x674e8(%rip), %rsi     # 0xdd3f5
callq 0x289f0
movl %r15d, %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x8ae82(%rip), %rsi     # 0x100da6
movq %rax, %rdi
callq 0x289f0
movl %ebp, %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x674e1(%rip), %rsi     # 0xdd41e
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
movsd 0x10(%rsp), %xmm0
callq 0x290f0
movq %rax, %rdi
callq 0x284a0
leaq 0x1950(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0x865fc
leaq 0x18(%rsp), %rsi
andl $0x0, (%rsi)
movq %rax, %rdi
callq 0x86102
cmpl $0x1, (%rax)
jne 0x76075
movq 0x38(%rsp), %rax
movq (%rax), %r14
leaq 0x1400(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x1460(%rbx), %r12
xorl %r15d, %r15d
leaq 0x8(%rsp), %rbp
leaq 0x18(%rsp), %r13
movq 0x38(%rsp), %rax
cmpq 0x8(%rax), %r14
je 0x76075
cmpl $0x3, 0x1614(%rbx)
jb 0x75fde
movsd (%r14), %xmm0
movq 0xeffc7(%rip), %rdi     # 0x165f98
callq 0x290f0
movq %rax, %rdi
callq 0x284a0
movsd (%r14), %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x86502
movl %r15d, 0x34(%rsp)
movl %r15d, %r15d
movq (%rax), %rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax,%r15,8)
xorl %eax, %eax
movl %eax, 0x18(%rsp)
movl %eax, %eax
movq 0x15c8(%rbx), %rcx
subq 0x15c0(%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
jbe 0x76064
movsd (%r14), %xmm0
movsd %xmm0, 0x28(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0x86550
movq %rax, %rdi
movq %r13, %rsi
callq 0x86502
movq (%rax), %rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax,%r15,8)
movl 0x18(%rsp), %eax
incl %eax
jmp 0x7600f
movl 0x34(%rsp), %r15d
incl %r15d
addq $0x8, %r14
jmp 0x75fad
leaq 0x1a20(%rbx), %r14
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x8626a
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x76099
jnp 0x760b6
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x8626a
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x76245
cmpl $0x3, 0x1614(%rbx)
jb 0x760e6
movq 0xefed2(%rip), %rdi     # 0x165f98
leaq 0x67360(%rip), %rsi     # 0xdd42d
callq 0x289f0
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x862a2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
cmpl $0x4, 0x1614(%rbx)
jb 0x76121
movq 0xefe8b(%rip), %rdi     # 0x165f98
leaq 0x67344(%rip), %rsi     # 0xdd458
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x1ab0(%rbx), %r14
leaq 0x1ab8(%rbx), %r12
movq 0x1ac8(%rbx), %rax
movl 0x8(%rsp), %ebp
movq %r12, %r15
cmpq %r12, %rax
je 0x76153
cmpl %ebp, 0x28(%rax)
cmoveq %rax, %r15
movq %rax, %rdi
callq 0x28700
jmp 0x7613d
cmpl $0x2, 0x1614(%rbx)
jbe 0x76177
movq 0xefe35(%rip), %rdi     # 0x165f98
leaq 0x672fb(%rip), %rsi     # 0xdd465
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
cmpq %r12, %r15
je 0x76187
movq %r14, %rdi
movq %r15, %rsi
callq 0x8f7e2
cmpl $0x3, 0x1614(%rbx)
jb 0x761ab
movq 0xefe01(%rip), %rdi     # 0x165f98
leaq 0x635ce(%rip), %rsi     # 0xd976c
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x18(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsi)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq %r14, %rdi
callq 0x8edfc
cmpl $0x3, 0x1614(%rbx)
jb 0x761ed
movq 0xefdbf(%rip), %rdi     # 0x165f98
leaq 0x6729b(%rip), %rsi     # 0xdd47b
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
xorl %ebp, %ebp
pushq $0x1
popq %r14
movl %ebp, %eax
movq 0x15c8(%rbx), %rcx
subq 0x15c0(%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
jbe 0x76221
movl 0x8(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x75af6
incl %ebp
jmp 0x761f3
cmpl $0x2, 0x1614(%rbx)
jbe 0x76245
movq 0xefd67(%rip), %rdi     # 0x165f98
leaq 0x63534(%rip), %rsi     # 0xd976c
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x1a50(%rbx), %r14
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x8626a
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x76269
jnp 0x76286
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0x8626a
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x764a8
cmpl $0x3, 0x1614(%rbx)
jb 0x762b6
movq 0xefd02(%rip), %rdi     # 0x165f98
leaq 0x67190(%rip), %rsi     # 0xdd42d
callq 0x289f0
movl 0x8(%rsp), %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
leaq 0xc(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x862a2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x1a80(%rbx), %rdi
movq %r15, %rsi
callq 0x862a2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
cmpl $0x3, 0x1614(%rbx)
jb 0x7630d
movq 0xefc9f(%rip), %rdi     # 0x165f98
leaq 0x67158(%rip), %rsi     # 0xdd458
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x1b10(%rbx), %r14
leaq 0x1b18(%rbx), %r12
movq 0x1b28(%rbx), %rax
movl 0xc(%rsp), %ebp
movq %r12, %r15
cmpq %r12, %rax
je 0x7633f
cmpl %ebp, 0x28(%rax)
cmoveq %rax, %r15
movq %rax, %rdi
callq 0x28700
jmp 0x76329
cmpl $0x2, 0x1614(%rbx)
jbe 0x76363
movq 0xefc49(%rip), %rdi     # 0x165f98
leaq 0x67135(%rip), %rsi     # 0xdd48b
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
cmpq %r12, %r15
je 0x76373
movq %r14, %rdi
movq %r15, %rsi
callq 0x8f7e2
cmpl $0x3, 0x1614(%rbx)
jb 0x76397
movq 0xefc15(%rip), %rdi     # 0x165f98
leaq 0x633e2(%rip), %rsi     # 0xd976c
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x18(%rsp), %r15
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r15)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
callq 0x8edfc
leaq 0x1b40(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r15)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r15)
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x8edfc
leaq 0x1b70(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
leaq 0x18(%rsp), %r14
movq %r14, %rsi
callq 0x8edfc
leaq 0x1ba0(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
leaq 0x18(%rsp), %rsi
callq 0x8edfc
cmpl $0x3, 0x1614(%rbx)
jb 0x76450
movq 0xefb5c(%rip), %rdi     # 0x165f98
leaq 0x67038(%rip), %rsi     # 0xdd47b
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
xorl %ebp, %ebp
pushq $0x1
popq %r14
movl %ebp, %eax
movq 0x15c8(%rbx), %rcx
subq 0x15c0(%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
jbe 0x76484
movl 0x8(%rsp), %esi
movq %rbx, %rdi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x75af6
incl %ebp
jmp 0x76456
cmpl $0x2, 0x1614(%rbx)
jbe 0x764a8
movq 0xefb04(%rip), %rdi     # 0x165f98
leaq 0x632d1(%rip), %rsi     # 0xd976c
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop