// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::consolidate_model_params()
void Swarm::consolidate_model_params(){

	if(options.verbosity>=1){
		cout<<"consolidating " << this->getNumModels()<<" models."<<endl;
	}

	//Raquel: commented, unnecessary
	/*if (options.models.size() <1){
		if(options.verbosity >= 3) cout<<"No need to consolidate models. Less than two models exist"<<endl;
		return;
	}
 *
 * Bug Note by Raquel
 * There was a bug in the consolidation function bellow
 * When you have more then 2 models that share equal paramaters, the command:
 * delete options.models.at(j)->freeParams_[fj->first];
 * will try to delete the free parameter from the list more then once
 * this will cause an error of memory access violation
 * I solved this problem by creating a new map containing only the unique parameters
 */



	map<string,FreeParam*> uniqueList;
	int uniqueIndex = 0;
	//remove dulicate free parameters
	unsigned int i,j, k, cnt=0;


	if(options.models.size() > 1 ){

	for (i=0; i <options.models.size(); i++){  //k-1 first models
		//cout << "in the first loop" << endl;
		for (j=i+1; j <options.models.size(); j++){  //the subsequent model to the end
			//cout << "in the second loop" << endl;
			for (auto fi=options.models.at(i)->freeParams_.begin(); fi!=options.models.at(i)->freeParams_.end(); ++fi){
				//cout << "in the thrid loop" << endl;
				for (auto fj=options.models.at(j)->freeParams_.begin(); fj!=options.models.at(j)->freeParams_.end(); ++fj){
					//cout << "in the fourth loop" << endl;
					if(fi->first.compare(fj->first)==0){  //common free parameters
						//cout << "in the if if the fourth loop" << endl;
						cnt++;
						if(options.verbosity >= 4)
							cout<<"Common free parameters found.  Param:"<< fi->first <<"  model1:" <<options.models.at(i)->getName() <<"   model2:" << options.models.at(j)->getName() <<endl;
						//cout << "before deleting" << endl;

						if ( uniqueList.find(fj->first) == uniqueList.end() ) {
							cout << fj->first << endl;
							//delete options.models.at(j)->freeParams_[fj->first]; //delete free param objecyt in j
							uniqueList[fj->first] = fj->second;
							uniqueIndex++;
							//cout << "Found parameter " << fj->first << endl;
						  // found
						} else {
							  // not found
								//cout << "parameter already found: " << fj->first << endl;

						}
						//cout << "after delete" << endl;
						//options.models.at(j)->freeParams_[fj->first] = options.models.at(i)->freeParams_[fi->first];  //map to i
						fj->second = fi->second;
					}
				}
			}
		}
	}
	//cout << "Found " << uniqueIndex << " unique parameters" << endl;
	//Raquel changed the way that the function find unique parameters

	if(options.verbosity >= 3) cout<<"Removing duplicate free parameters is finished. "<<cnt<<" duplicate free parameters are deleted."<<endl;

	}else{

		uniqueList = options.models.at(0)->freeParams_;
	}



		for (i=0; i <options.models.size(); i++){
			options.models.at(i)->freeParams_ = uniqueList;
			if(options.verbosity >= 1){
				cout << "model " << i << " free parameters " << options.models.at(i)->freeParams_.size() << endl;
			}
		}


		options.freeParams_.clear(); cnt=0;
		options.freeParams_ = uniqueList;
		cnt = options.freeParams_.size();

	 //razi make full list of union of free parameters
	options.freeParams_.clear(); cnt=0;
	for (i=0; i <options.models.size(); i++){  //razi: all models, was k-1 first models, later test
		for (auto fi=options.models.at(i)->freeParams_.begin(); fi!=options.models.at(i)->freeParams_.end(); ++fi){
			if (options.freeParams_.count(fi->first) < 1){  //a new free parameters
				options.freeParams_.insert(make_pair(fi->first, fi->second)); cnt++;
				if(options.verbosity >= 3) cout<<"Free parameter:"<<fi->first <<" is added to the list"<<endl;
			}
		}
	}


	if(options.verbosity >= 3) cout<<"Free parameters found:"<< cnt << endl;


	//razi make mapping from models free parameters to the full list of free parameters


	std::map<unsigned int, unsigned int> dummay_map;
	for (i=0; i <options.models.size(); i++){  //razi: all models, was k-1 first models, later test
		j = 0;

		freeParamMapping.push_back(dummay_map);
		for (auto fj=options.models.at(i)->freeParams_.begin(); fj!=options.models.at(i)->freeParams_.end(); ++fj){
			k=0;
			for (auto fk=options.freeParams_.begin(); fk!=options.freeParams_.end(); ++fk){
//cout<<"Full List   FreeParam["<<k<<"] :" << fk->first<<endl; //mypause();
				if (fj->first == fk->first){
					freeParamMapping.at(i).insert(make_pair(j,k));
				}
				k++;
			}
			j++;
		}
	}


	if(options.verbosity >= 3)cout<<"Model consolidation is completed. "<<cnt<<" free parameters are found."<<endl;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
cmpl $0x0, 0x1614(%rdi)
je 0x6a667
movq 0xfb970(%rip), %rdi     # 0x165f98
leaq 0x71f61(%rip), %rsi     # 0xdc590
callq 0x289f0
movq 0x15c8(%rbx), %rcx
subq 0x15c0(%rbx), %rcx
shrq $0x3, %rcx
movl %ecx, %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x72216(%rip), %rsi     # 0xdc86d
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x70(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
leaq 0x15c0(%rbx), %r14
movq 0x15c0(%rbx), %rax
movq %rbx, 0x8(%rsp)
movq 0x15c8(%rbx), %rcx
movq %rcx, %rdx
subq %rax, %rdx
cmpq $0x9, %rdx
movq %r14, 0x10(%rsp)
jb 0x6a8e7
movl $0x0, 0x1c(%rsp)
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %ebx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
jbe 0x6ac9a
leal 0x1(%rbx), %edx
movl %edx, 0x28(%rsp)
movl %edx, %r15d
movq %rbx, 0x30(%rsp)
movl %r15d, %r15d
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %r15, %rdx
jbe 0x6a6c0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3374c
movq (%rax), %rax
movq 0x48(%rax), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3374c
movq (%rax), %rax
addq $0x38, %rax
cmpq %rax, 0x20(%rsp)
je 0x6a8cc
movq %r14, %rdi
movq %r15, %rsi
callq 0x3374c
movq (%rax), %rax
movq 0x48(%rax), %rbp
movq 0x20(%rsp), %rax
leaq 0x20(%rax), %r12
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x3374c
movq (%rax), %rax
addq $0x38, %rax
cmpq %rax, %rbp
je 0x6a8ae
leaq 0x20(%rbp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x28060
testl %eax, %eax
jne 0x6a89e
movq 0x8(%rsp), %rax
cmpl $0x4, 0x1614(%rax)
jb 0x6a84b
movq 0xfb804(%rip), %rdi     # 0x165f98
leaq 0x71e04(%rip), %rsi     # 0xdc59f
callq 0x289f0
movq %rax, %rdi
movq %r12, %rsi
callq 0x28910
movq %rax, %rdi
leaq 0x71e10(%rip), %rsi     # 0xdc5c5
callq 0x289f0
movq %rax, %r14
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x3374c
leaq 0x38(%rsp), %rbx
movq (%rax), %rsi
addq $0x68, %rsi
movq %rbx, %rdi
callq 0x28dd0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x28910
movq %rax, %rdi
leaq 0x71dda(%rip), %rsi     # 0xdc5cf
callq 0x289f0
movq %rax, %r14
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x3374c
leaq 0x98(%rsp), %rbx
movq (%rax), %rsi
addq $0x68, %rsi
movq %rbx, %rdi
callq 0x28dd0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x28910
movq %rax, %rdi
callq 0x284a0
leaq 0x98(%rsp), %rdi
callq 0x295a0
leaq 0x38(%rsp), %rdi
callq 0x295a0
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x8d740
leaq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jne 0x6a88d
movq 0xfb72f(%rip), %rdi     # 0x165f98
movq %r13, %rsi
callq 0x28910
movq %rax, %rdi
callq 0x284a0
movq 0x40(%rbp), %r14
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x85d68
movq %r14, (%rax)
incl 0x1c(%rsp)
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rax
movq %rax, 0x40(%rbp)
movq %rbp, %rdi
callq 0x28700
movq %rax, %rbp
jmp 0x6a747
movq 0x20(%rsp), %rdi
callq 0x28700
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %r14
movq 0x30(%rsp), %rbx
jmp 0x6a70f
incl %r15d
movq 0x8(%rsp), %rcx
movq 0x15c0(%rcx), %rax
movq 0x15c8(%rcx), %rcx
jmp 0x6a6e6
movq %r14, %rdi
xorl %esi, %esi
callq 0x3374c
movq (%rax), %rsi
addq $0x30, %rsi
leaq 0x68(%rsp), %rdi
callq 0x8a834
xorl %ebx, %ebx
leaq 0x68(%rsp), %r15
movq 0xfb688(%rip), %r12     # 0x165f98
leaq 0x6c783(%rip), %r13     # 0xd709a
leaq 0x71d15(%rip), %rbp     # 0xdc633
movl %ebx, %ebx
movq 0x8(%rsp), %rcx
movq 0x15c8(%rcx), %rax
subq 0x15c0(%rcx), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0x6a9b5
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3374c
movq (%rax), %rdi
addq $0x30, %rdi
movq %r15, %rsi
callq 0x8a834
movq 0x8(%rsp), %rax
cmpl $0x0, 0x1614(%rax)
je 0x6a9ae
movq %r12, %rdi
movq %r13, %rsi
callq 0x289f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x286e0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x289f0
movq %rax, %r14
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x3374c
movq (%rax), %rax
movq 0x58(%rax), %rsi
movq %r14, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
incl %ebx
jmp 0x6a91e
movq 0x8(%rsp), %rax
leaq 0x1590(%rax), %rbx
movq %rbx, %rdi
callq 0x8d9b6
leaq 0x68(%rsp), %rsi
movq %rbx, %rdi
callq 0x8a834
movq %rbx, %rdi
callq 0x8d9b6
xorl %r14d, %r14d
leaq 0x38(%rsp), %r15
xorl %r12d, %r12d
movl %r12d, %r12d
movq 0x8(%rsp), %rcx
movq 0x15c8(%rcx), %rax
subq 0x15c0(%rcx), %rax
sarq $0x3, %rax
cmpq %r12, %rax
jbe 0x6aad1
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3374c
movq (%rax), %rax
movq 0x48(%rax), %r13
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3374c
movq (%rax), %rax
addq $0x38, %rax
cmpq %rax, %r13
je 0x6aac9
leaq 0x20(%r13), %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x33588
testq %rax, %rax
jne 0x6aab9
leaq 0x40(%r13), %rdx
movq %r15, %rdi
movq %rbp, %rsi
callq 0x8ab4c
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4edba
movq %r15, %rdi
callq 0x295a0
incl %r14d
movq 0x8(%rsp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x6aab9
movq 0xfb50d(%rip), %rdi     # 0x165f98
leaq 0x71bb3(%rip), %rsi     # 0xdc645
callq 0x289f0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x28910
movq %rax, %rdi
leaq 0x71ba9(%rip), %rsi     # 0xdc655
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq %r13, %rdi
callq 0x28700
movq %rax, %r13
jmp 0x6aa20
incl %r12d
jmp 0x6a9e9
movq 0x8(%rsp), %rax
cmpl $0x3, 0x1614(%rax)
movl %r14d, %ebx
jb 0x6ab08
movq 0xfb4af(%rip), %rdi     # 0x165f98
leaq 0x71b7b(%rip), %rsi     # 0xdc66b
callq 0x289f0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
movq %rbx, 0x28(%rsp)
leaq 0x40(%rsp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
movq 0x8(%rsp), %rbx
leaq 0x1980(%rbx), %rax
movq %rax, 0x20(%rsp)
leaq 0x1598(%rbx), %r14
xorl %r12d, %r12d
movl %r12d, %r12d
movq 0x15c8(%rbx), %rax
subq 0x15c0(%rbx), %rax
sarq $0x3, %rax
cmpq %r12, %rax
jbe 0x6ac34
movq 0x20(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x85dce
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3374c
movq (%rax), %rax
movq 0x48(%rax), %r15
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3374c
movq (%rax), %rax
addq $0x38, %rax
cmpq %rax, %r15
je 0x6ac2c
movq 0x15a8(%rbx), %rbx
movq %r15, 0x30(%rsp)
leaq 0x20(%r15), %r13
movl %ebp, 0x1c(%rsp)
movl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r14, %rbx
je 0x6ac0f
leaq 0x20(%rbx), %rsi
movq %r13, %rdi
callq 0x3d600
testb %al, %al
je 0x6abff
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x85dfa
movq %r15, %rcx
shlq $0x20, %rcx
orq %rbp, %rcx
movq %rcx, 0x98(%rsp)
movq %rax, %rdi
leaq 0x98(%rsp), %rsi
callq 0x8ddea
incl %r15d
movq %rbx, %rdi
callq 0x28700
movq %rax, %rbx
jmp 0x6abbb
movl 0x1c(%rsp), %ebp
incl %ebp
movq 0x30(%rsp), %rdi
callq 0x28700
movq %rax, %r15
movq 0x8(%rsp), %rbx
jmp 0x6ab85
incl %r12d
jmp 0x6ab42
cmpl $0x3, 0x1614(%rbx)
jb 0x6ac74
movq 0xfb354(%rip), %rdi     # 0x165f98
leaq 0x71a37(%rip), %rsi     # 0xdc682
callq 0x289f0
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x286e0
leaq 0x71a41(%rip), %rsi     # 0xdc6a5
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x38(%rsp), %rdi
callq 0x8ab6a
leaq 0x68(%rsp), %rdi
callq 0x4e856
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rsp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0x6a902
movq 0xfb2e5(%rip), %rdi     # 0x165f98
leaq 0x71920(%rip), %rsi     # 0xdc5da
callq 0x289f0
movl 0x1c(%rsp), %esi
movq %rax, %rdi
callq 0x286e0
leaq 0x71939(%rip), %rsi     # 0xdc60b
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
jmp 0x6a902
jmp 0x6ad05
jmp 0x6ad3d
jmp 0x6ad05
jmp 0x6ad05
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad2c
jmp 0x6ad05
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad3d
jmp 0x6ad05
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x8ab6a
jmp 0x6ad40
jmp 0x6ad3d
jmp 0x6ad3d
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x295a0
jmp 0x6ad2f
jmp 0x6ad2c
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x295a0
jmp 0x6ad40
jmp 0x6ad3d
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x4e856
movq %rbx, %rdi
callq 0x292f0