// File: /workspace/github_cpp_makefile_star2_O3/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 0xbe99e
movl %eax, 0x8(%rsp)
cmpl $0x3, 0x1614(%rbx)
jb 0x8b761
movl %eax, %r15d
movq 0xea8d6(%rip), %r12     # 0x175fa8
leaq 0x73bbc(%rip), %rsi     # 0xff295
movl $0x28, %edx
movq %r12, %rdi
callq 0x22ab0
movl %r15d, %esi
movq %r12, %rdi
callq 0x22650
movq %rax, %r15
leaq 0x971ad(%rip), %rsi     # 0x1228a8
movl $0xd, %edx
movq %rax, %rdi
callq 0x22ab0
movl %ebp, %esi
movq %r15, %rdi
callq 0x22650
movq %rax, %r15
leaq 0x73ba2(%rip), %rsi     # 0xff2be
movl $0xe, %edx
movq %rax, %rdi
callq 0x22ab0
movq %r15, %rdi
movsd 0x10(%rsp), %xmm0
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
leaq 0x1950(%rbx), %rdi
leaq 0x8(%rsp), %rsi
callq 0xa7ecc
leaq 0x20(%rsp), %rsi
movl $0x0, (%rsi)
movq %rax, %rdi
callq 0xa7800
cmpl $0x1, (%rax)
jne 0x8b8bb
movq 0x38(%rsp), %rax
movq (%rax), %r15
cmpq 0x8(%rax), %r15
je 0x8b8bb
leaq 0x1400(%rbx), %rax
movq %rax, 0x40(%rsp)
leaq 0x1460(%rbx), %r12
xorl %r14d, %r14d
leaq 0x8(%rsp), %rbp
leaq 0x20(%rsp), %r13
cmpl $0x3, 0x1614(%rbx)
jb 0x8b808
movsd (%r15), %xmm0
movq 0xea7d3(%rip), %rdi     # 0x175fa8
callq 0x23060
movq %rax, 0x18(%rsp)
movq (%rax), %rcx
movq -0x18(%rcx), %rdi
addq %rax, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq 0x18(%rsp), %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movsd (%r15), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0xa7e00
movl %r14d, 0x34(%rsp)
movl %r14d, %r14d
movq (%rax), %rax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax,%r14,8)
movl $0x0, 0x20(%rsp)
movq 0x15c8(%rbx), %rax
cmpq 0x15c0(%rbx), %rax
je 0x8b8a0
movsd (%r15), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r12, %rdi
movq %rbp, %rsi
callq 0xa7e66
movq %rax, %rdi
movq %r13, %rsi
callq 0xa7e00
movq (%rax), %rax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax,%r14,8)
movl 0x20(%rsp), %eax
incl %eax
movl %eax, 0x20(%rsp)
movq 0x15c8(%rbx), %rcx
subq 0x15c0(%rbx), %rcx
sarq $0x3, %rcx
cmpq %rax, %rcx
ja 0x8b84f
movl 0x34(%rsp), %r14d
incl %r14d
addq $0x8, %r15
movq 0x38(%rsp), %rax
cmpq 0x8(%rax), %r15
jne 0x8b7c0
leaq 0x1a20(%rbx), %r14
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xa7a8c
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8b8df
jnp 0x8b8fc
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xa7a8c
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x8bb8b
cmpl $0x3, 0x1614(%rbx)
jb 0x8b956
movq 0xea69c(%rip), %r15     # 0x175fa8
leaq 0x739ba(%rip), %rsi     # 0xff2cd
movl $0x2a, %edx
movq %r15, %rdi
callq 0x22ab0
movl 0x8(%rsp), %esi
movq %r15, %rdi
callq 0x22650
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
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0xa7ad2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x1ab8(%rbx), %r12
cmpl $0x4, 0x1614(%rbx)
jb 0x8b9bf
movq 0xea624(%rip), %r14     # 0x175fa8
leaq 0x7396d(%rip), %rsi     # 0xff2f8
movl $0xc, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x1ac8(%rbx), %rax
movq %r12, %r14
cmpq %r12, %rax
je 0x8b9e9
movl 0x8(%rsp), %ebp
movq %r12, %r14
cmpl %ebp, 0x28(%rax)
cmoveq %rax, %r14
movq %rax, %rdi
callq 0x22670
cmpq %r12, %rax
jne 0x8b9d5
cmpl $0x2, 0x1614(%rbx)
jbe 0x8ba34
movq 0xea5af(%rip), %r15     # 0x175fa8
leaq 0x73905(%rip), %rsi     # 0xff305
movl $0x15, %edx
movq %r15, %rdi
callq 0x22ab0
movq (%r15), %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
leaq 0x1ab0(%rbx), %r15
cmpq %r12, %r14
je 0x8ba4b
movq %r15, %rdi
movq %r14, %rsi
callq 0xaf960
cmpl $0x3, 0x1614(%rbx)
jb 0x8ba96
movq 0xea54d(%rip), %r14     # 0x175fa8
leaq 0x6fd12(%rip), %rsi     # 0xfb774
movl $0x4, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
leaq 0x20(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsi)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsi)
movq %r15, %rdi
callq 0xaefd4
cmpl $0x3, 0x1614(%rbx)
jb 0x8baff
movq 0xea4e4(%rip), %r14     # 0x175fa8
leaq 0x73850(%rip), %rsi     # 0xff31b
movl $0xf, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x15c8(%rbx), %rax
cmpq 0x15c0(%rbx), %rax
je 0x8bb40
xorl %r14d, %r14d
movl 0x8(%rsp), %esi
movq %rbx, %rdi
movl %r14d, %edx
movl $0x1, %ecx
callq 0x8b0b0
incl %r14d
movq 0x15c8(%rbx), %rax
subq 0x15c0(%rbx), %rax
sarq $0x3, %rax
cmpq %r14, %rax
ja 0x8bb12
cmpl $0x2, 0x1614(%rbx)
jbe 0x8bb8b
movq 0xea458(%rip), %r14     # 0x175fa8
leaq 0x6fc1d(%rip), %rsi     # 0xfb774
movl $0x4, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
leaq 0x1a50(%rbx), %r14
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0xa7a8c
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x8bbaf
jnp 0x8bbcc
leaq 0xc(%rsp), %rsi
movq %r14, %rdi
callq 0xa7a8c
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x8beee
cmpl $0x3, 0x1614(%rbx)
jb 0x8bc26
movq 0xea3cc(%rip), %r15     # 0x175fa8
leaq 0x736ea(%rip), %rsi     # 0xff2cd
movl $0x2a, %edx
movq %r15, %rdi
callq 0x22ab0
movl 0x8(%rsp), %esi
movq %r15, %rdi
callq 0x22650
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
leaq 0xc(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xa7ad2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x1a80(%rbx), %rdi
movq %r15, %rsi
callq 0xa7ad2
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
leaq 0x1b18(%rbx), %r12
cmpl $0x3, 0x1614(%rbx)
jb 0x8bcab
movq 0xea338(%rip), %r14     # 0x175fa8
leaq 0x73681(%rip), %rsi     # 0xff2f8
movl $0xc, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x1b28(%rbx), %rax
movq %r12, %r14
cmpq %r12, %rax
je 0x8bcd5
movl 0xc(%rsp), %ebp
movq %r12, %r14
cmpl %ebp, 0x28(%rax)
cmoveq %rax, %r14
movq %rax, %rdi
callq 0x22670
cmpq %r12, %rax
jne 0x8bcc1
cmpl $0x2, 0x1614(%rbx)
jbe 0x8bd20
movq 0xea2c3(%rip), %r15     # 0x175fa8
leaq 0x7363f(%rip), %rsi     # 0xff32b
movl $0x14, %edx
movq %r15, %rdi
callq 0x22ab0
movq (%r15), %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
leaq 0x1b10(%rbx), %r15
cmpq %r12, %r14
je 0x8bd37
movq %r15, %rdi
movq %r14, %rsi
callq 0xaf960
cmpl $0x3, 0x1614(%rbx)
jb 0x8bd82
movq 0xea261(%rip), %r14     # 0x175fa8
leaq 0x6fa26(%rip), %rsi     # 0xfb774
movl $0x4, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
leaq 0x20(%rsp), %r14
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
movq %r15, %rdi
movq %r14, %rsi
callq 0xaefd4
leaq 0x1b40(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0xaefd4
leaq 0x1b70(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
leaq 0x20(%rsp), %r14
movq %r14, %rsi
callq 0xaefd4
leaq 0x1ba0(%rbx), %rdi
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x8(%r14)
leaq 0x20(%rsp), %rsi
callq 0xaefd4
cmpl $0x3, 0x1614(%rbx)
jb 0x8be62
movq 0xea181(%rip), %r14     # 0x175fa8
leaq 0x734ed(%rip), %rsi     # 0xff31b
movl $0xf, %edx
movq %r14, %rdi
callq 0x22ab0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %r14, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
movq 0x15c8(%rbx), %rax
cmpq 0x15c0(%rbx), %rax
je 0x8bea3
xorl %r14d, %r14d
movl 0x8(%rsp), %esi
movq %rbx, %rdi
movl %r14d, %edx
movl $0x1, %ecx
callq 0x8b0b0
incl %r14d
movq 0x15c8(%rbx), %rax
subq 0x15c0(%rbx), %rax
sarq $0x3, %rax
cmpq %r14, %rax
ja 0x8be75
cmpl $0x2, 0x1614(%rbx)
jbe 0x8beee
movq 0xea0f5(%rip), %rbx     # 0x175fa8
leaq 0x6f8ba(%rip), %rsi     # 0xfb774
movl $0x4, %edx
movq %rbx, %rdi
callq 0x22ab0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x22940
movsbl %al, %esi
movq %rbx, %rdi
callq 0x22050
movq %rax, %rdi
callq 0x22660
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop