// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::processParamsPSO(std::vector<double, std::allocator<double>>&, unsigned int, double)
void Swarm::processParamsPSO(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;
		subparticleBestFits_[subParID] = fit; //Raquel added support to subparticles
		subparticleBestFits_copy[subParID] = fit;

		map<double, unsigned int>::iterator toDelIt = particleBestFitsByFit_.end();

		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;
			}
		}

		if (toDelIt != particleBestFitsByFit_.end()) {
			particleBestFitsByFit_.erase(toDelIt);
		}

		particleBestFitsByFit_.insert(pair<double, unsigned int>(fit, pID));
		particleBestFitsByFit_copy.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;
		}

		for(unsigned int mid = 0; mid < options.models.size(); mid++){
			update_cur_particle_params(pID, mid, true);
		}



	}


	//Raquel added to support subparticles
	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;

			map<double, unsigned int>::iterator toDelIt2 = subparticleBestFitsByFit_.end();

			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;
				}
			}

			if (toDelIt2 != subparticleBestFitsByFit_.end()) {
				subparticleBestFitsByFit_.erase(toDelIt2); //Raquel added support to subparticles
			}


			//cout << "Adding " << fit << " to the fit values of subpar " << subParID << endl;
			subparticleBestFitsByFit_.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));
			for(unsigned int mid = 0; mid < options.models.size(); mid++){
				update_cur_particle_params(pID, mid, true);
			}
	}



}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x150(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x154(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movl -0x154(%rbp), %edi
movl %eax, %esi
callq 0x17d3f0
movl %eax, %ecx
movq -0x150(%rbp), %rax
movl %ecx, -0x24(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xfe627
movq 0x1d6985(%rip), %rdi    # 0x2d4f50
leaq 0xe3793(%rip), %rsi     # 0x1e1d65
callq 0x2ff50
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x30640
movq %rax, %rdi
leaq 0x107726(%rip), %rsi    # 0x205d12
callq 0x2ff50
movq %rax, %rdi
movl -0x14(%rbp), %esi
callq 0x30640
movq %rax, %rdi
leaq 0xe3788(%rip), %rsi     # 0x1e1d8e
callq 0x2ff50
movq %rax, %rdi
movsd -0x20(%rbp), %xmm0
callq 0x30710
movq %rax, %rdi
movq 0x1d69a6(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x150(%rbp), %rdi
movl $0x0, -0x28(%rbp)
addq $0x1950, %rdi           # imm = 0x1950
leaq -0x24(%rbp), %rsi
callq 0x1230c0
movq %rax, %rdi
movl $0x0, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x122180
cmpl $0x1, (%rax)
jne 0xfe7c0
movq -0x10(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0xbdfd0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x122510
testb $0x1, %al
jne 0xfe691
jmp 0xfe7be
movq -0x150(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xfe6c9
leaq -0x38(%rbp), %rdi
callq 0xb2ed0
movsd (%rax), %xmm0
movq 0x1d689b(%rip), %rdi    # 0x2d4f50
callq 0x30710
movq %rax, %rdi
movq 0x1d6904(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
leaq -0x38(%rbp), %rdi
callq 0xb2ed0
movq -0x150(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x160(%rbp)
addq $0x1400, %rdi           # imm = 0x1400
leaq -0x24(%rbp), %rsi
callq 0x122da0
movq %rax, %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd -0x160(%rbp), %xmm0
movsd %xmm0, (%rax)
movl $0x0, -0x44(%rbp)
movq -0x150(%rbp), %rdi
movl -0x44(%rbp), %eax
movq %rax, -0x168(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq %rax, %rcx
movq -0x168(%rbp), %rax
cmpq %rcx, %rax
jae 0xfe7a7
leaq -0x38(%rbp), %rdi
callq 0xb2ed0
movq -0x150(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x170(%rbp)
addq $0x1460, %rdi           # imm = 0x1460
leaq -0x24(%rbp), %rsi
callq 0x122e80
movq %rax, %rdi
leaq -0x44(%rbp), %rsi
callq 0x122da0
movq %rax, %rdi
movl -0x28(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd -0x170(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xfe715
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
leaq -0x38(%rbp), %rdi
callq 0xb2ef0
jmp 0xfe66e
jmp 0xfe7c0
movq -0x150(%rbp), %rdi
addq $0x1a20, %rdi           # imm = 0x1A20
leaq -0x24(%rbp), %rsi
callq 0x1226a0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xfe7e8
jp 0xfe7e8
jmp 0xfe822
movq -0x150(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x178(%rbp)
addq $0x1a20, %rdi           # imm = 0x1A20
leaq -0x24(%rbp), %rsi
callq 0x1226a0
movsd -0x178(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfeb5c
movq -0x150(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xfe85f
movq 0x1d6717(%rip), %rdi    # 0x2d4f50
leaq 0xe355d(%rip), %rsi     # 0x1e1d9d
callq 0x2ff50
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x30640
movq %rax, %rdi
movq 0x1d676e(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x150(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x190(%rbp)
addq $0x1a20, %rdi           # imm = 0x1A20
leaq -0x24(%rbp), %rsi
callq 0x122740
movsd -0x190(%rbp), %xmm0
movq -0x150(%rbp), %rdi
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x188(%rbp)
addq $0x1a50, %rdi           # imm = 0x1A50
leaq -0x14(%rbp), %rsi
callq 0x122740
movsd -0x188(%rbp), %xmm0
movq -0x150(%rbp), %rdi
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x180(%rbp)
addq $0x1a80, %rdi           # imm = 0x1A80
leaq -0x14(%rbp), %rsi
callq 0x122740
movsd -0x180(%rbp), %xmm0
movq -0x150(%rbp), %rdi
movsd %xmm0, (%rax)
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x1231a0
movq -0x150(%rbp), %rdi
movq %rax, -0x50(%rbp)
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x122630
movq %rax, -0x58(%rbp)
movq -0x150(%rbp), %rdi
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x1231a0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0xfe947
jmp 0xfe96d
leaq -0x58(%rbp), %rdi
callq 0x122660
movl 0x8(%rax), %eax
cmpl -0x24(%rbp), %eax
jne 0xfe960
movq -0x58(%rbp), %rax
movq %rax, -0x50(%rbp)
jmp 0xfe962
leaq -0x58(%rbp), %rdi
callq 0xcbd20
jmp 0xfe91d
movq -0x150(%rbp), %rdi
addq $0x1ab0, %rdi           # imm = 0x1AB0
callq 0x1231a0
movq %rax, -0x68(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0xfe997
jmp 0xfe9ba
movq -0x150(%rbp), %rdi
addq $0x1ab0, %rdi           # imm = 0x1AB0
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
callq 0x123200
movq %rax, -0x78(%rbp)
movq -0x150(%rbp), %rax
addq $0x1ab0, %rax           # imm = 0x1AB0
movq %rax, -0x1a0(%rbp)
leaq -0x88(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
callq 0x123240
movq -0x1a0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x122340
movq %rax, %rcx
movq -0x150(%rbp), %rax
movq %rcx, -0x90(%rbp)
addq $0x1ae0, %rax           # imm = 0x1AE0
movq %rax, -0x198(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x24(%rbp), %rdx
callq 0x123240
movq -0x198(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x122340
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movq -0x10(%rbp), %rdi
callq 0xb2ea0
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
callq 0xbdfd0
movq %rax, -0xc0(%rbp)
leaq -0xb8(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x122510
testb $0x1, %al
jne 0xfea84
jmp 0xfeaef
leaq -0xb8(%rbp), %rdi
callq 0xb2ed0
movq -0x150(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1a8(%rbp)
addq $0x1cc0, %rdi           # imm = 0x1CC0
leaq -0x24(%rbp), %rsi
callq 0x122da0
movq %rax, %rdi
movl -0xac(%rbp), %eax
movl %eax, %esi
callq 0xb2610
movsd -0x1a8(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0xb2ef0
jmp 0xfea5b
movl $0x0, -0xc4(%rbp)
movq -0x150(%rbp), %rdi
movl -0xc4(%rbp), %eax
movq %rax, -0x1b0(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
cmpq %rcx, %rax
jae 0xfeb5a
movq -0x150(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0xc4(%rbp), %edx
movl $0x1, %ecx
callq 0xfeea0
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0xfeaf9
jmp 0xfeb5c
movq -0x150(%rbp), %rdi
addq $0x1a50, %rdi           # imm = 0x1A50
leaq -0x14(%rbp), %rsi
callq 0x1226a0
movsd (%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xfeb84
jp 0xfeb84
jmp 0xfebbe
movq -0x150(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x1b8(%rbp)
addq $0x1a50, %rdi           # imm = 0x1A50
leaq -0x14(%rbp), %rsi
callq 0x1226a0
movsd -0x1b8(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xfee8f
movq -0x150(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xfebfb
movq 0x1d637b(%rip), %rdi    # 0x2d4f50
leaq 0xe31c1(%rip), %rsi     # 0x1e1d9d
callq 0x2ff50
movq %rax, %rdi
movl -0x24(%rbp), %esi
callq 0x30640
movq %rax, %rdi
movq 0x1d63d2(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x150(%rbp), %rdi
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x1c8(%rbp)
addq $0x1a50, %rdi           # imm = 0x1A50
leaq -0x14(%rbp), %rsi
callq 0x122740
movsd -0x1c8(%rbp), %xmm0
movq -0x150(%rbp), %rdi
movsd %xmm0, (%rax)
movsd -0x20(%rbp), %xmm0
movsd %xmm0, -0x1c0(%rbp)
addq $0x1a80, %rdi           # imm = 0x1A80
leaq -0x14(%rbp), %rsi
callq 0x122740
movsd -0x1c0(%rbp), %xmm0
movq -0x150(%rbp), %rdi
movsd %xmm0, (%rax)
addq $0x1b10, %rdi           # imm = 0x1B10
callq 0x1231a0
movq -0x150(%rbp), %rdi
movq %rax, -0xd0(%rbp)
addq $0x1b10, %rdi           # imm = 0x1B10
callq 0x122630
movq %rax, -0xd8(%rbp)
movq -0x150(%rbp), %rdi
addq $0x1b10, %rdi           # imm = 0x1B10
callq 0x1231a0
movq %rax, -0xe0(%rbp)
leaq -0xd8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0xfecc2
jmp 0xfecf4
leaq -0xd8(%rbp), %rdi
callq 0x122660
movl 0x8(%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0xfece4
movq -0xd8(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0xfece6
leaq -0xd8(%rbp), %rdi
callq 0xcbd20
jmp 0xfec8f
movq -0x150(%rbp), %rdi
addq $0x1b10, %rdi           # imm = 0x1B10
callq 0x1231a0
movq %rax, -0xe8(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x1231d0
testb $0x1, %al
jne 0xfed27
jmp 0xfed56
movq -0x150(%rbp), %rdi
addq $0x1b10, %rdi           # imm = 0x1B10
movq -0xd0(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rsi
callq 0x123200
movq %rax, -0xf8(%rbp)
movq -0x150(%rbp), %rax
addq $0x1b10, %rax           # imm = 0x1B10
movq %rax, -0x1e0(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x14(%rbp), %rdx
callq 0x123240
movq -0x1e0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x122340
movq %rax, %rcx
movq -0x150(%rbp), %rax
movq %rcx, -0x110(%rbp)
addq $0x1b70, %rax           # imm = 0x1B70
movq %rax, -0x1d8(%rbp)
leaq -0x120(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x14(%rbp), %rdx
callq 0x123240
movq -0x1d8(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x122340
movq %rax, %rcx
movq -0x150(%rbp), %rax
movq %rcx, -0x128(%rbp)
addq $0x1ba0, %rax           # imm = 0x1BA0
movq %rax, -0x1d0(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x14(%rbp), %rdx
callq 0x123240
movq -0x1d0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x122340
movq %rax, -0x140(%rbp)
movl $0x0, -0x144(%rbp)
movq -0x150(%rbp), %rdi
movl -0x144(%rbp), %eax
movq %rax, -0x1e8(%rbp)
addq $0x14c0, %rdi           # imm = 0x14C0
addq $0x100, %rdi            # imm = 0x100
callq 0x45170
movq %rax, %rcx
movq -0x1e8(%rbp), %rax
cmpq %rcx, %rax
jae 0xfee8d
movq -0x150(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x144(%rbp), %edx
movl $0x1, %ecx
callq 0xfeea0
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0xfee2c
jmp 0xfee8f
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
nopl (%rax,%rax)