// File: richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::runSPSO()
void Swarm::runSPSO() {
	if (options.verbosity >= 3) {
		cout << "Running a synchronous PSO fit" << endl;
	}

	if (!checkIfFileExists(options.jobOutputDir + toString(currentGeneration))) {//Raquel: changed, subdir name was hardcoded to 1
		string createDirCmd = "mkdir " + options.jobOutputDir + toString(currentGeneration);//Raquel: changed, subdir name was hardcoded to 1
		runCommand(createDirCmd);
	}

	if (!resumingSavedSwarm) {
		// Generate all particles that will be present in the swarm
		populationTopology_ = generateTopology(options.swarmSize);
	}

	//saveSwarmState();
	string path; //Raquel added
	vector<unsigned int> finishedParticles;
	vector<unsigned int> particlestoProcess;

	bool stopCriteria = false;

	//int numFinishedParticles = 0;

	//unsigned int p = 1;
	//int mid; //Raquel added
	//int sp = 0; //Raquel added
	//int nModels = options.models.size(); //Raquel added


	while (!stopCriteria) {

		if (options.verbosity >= 0) {
			cout << "Generation (flock): " << currentGeneration << endl;
		}


/*
		while (finishedParticles.size() < options.swarmSize && p <= options.swarmSize) {
			if (runningParticles_.size() < options.parallelCount) {

				//Raquel Changed this to run the correct number of subparticles
				sp++; //Raquel added
				p = fcalcParID(sp, nModels);//Raquel added
				mid = fcalcMID(sp, nModels);//Raquel added
				launchSubParticle(p, mid, false);//Raquel added
				//launchParticle(p++);
			}
			cout << "RAQUEL: parallel count " << options.parallelCount << endl;
			cout << "RAQUEL: runningParticles_.size() " << runningParticles_.size() << endl;
			cout << "RAQUEL: options.swarmSize " << options.swarmSize << endl;
			cout << "RAQUEL : p " << p << endl;
			cout << "RAQUEL: finished " << finishedParticles.size() << endl;

			usleep(250000);

			vector<unsigned int> currFinishedParticles;
			cout << "Raquel before checkmastermessages" << endl;
			currFinishedParticles = checkMasterMessages();
			cout << "Raquel after checkmastermessages" << endl;


			if (currFinishedParticles.size()) {
				finishedParticles.insert(finishedParticles.end(), currFinishedParticles.begin(), currFinishedParticles.end());
			}




		}

	*/


		//sp=0;

	/*	finishedParticles_.clear();
		finishedSubParticles_.clear(); //Raquel added to solve problem of less and less result files as generations go
		particlestoProcess.clear();
		numFinishedParticles = 0;

		while (numFinishedParticles < options.swarmSize) { //razi: loop over particles, each particle includes nModels subPArticles

			if (runningSubParticles_.size()< options.parallelCount) {//razi: make sure the number of subparticles don't exceed parallel count limit
				sp++;
				p = fcalcParID(sp, nModels);
				mid = fcalcMID(sp, nModels);

				launchSubParticle(p, mid, false);
				//launchParticle(p, false);

			}
			// Check for any messages from particles
			usleep(10000);
			checkMasterMessages();

			numFinishedParticles = finishedParticles_.size();
		}

*/
			//numFinishedParticles = options.swarmSize;

				//cout << "RAQUEL: starting runGeneration" << endl;
				runGeneration();
				//usleep(250000);

				//cout << "RAQUEL: finished runGeneration" << endl;
				//cout << "RAQUEL: starting checkStopCriteria" << endl;
				stopCriteria = checkStopCriteria();
				//cout << "RAQUEL: STOP CRITERIA value" << stopCriteria << endl;
				//cout << "RAQUEL: starting saveSwarmState" << endl;

				saveSwarmState();
				//cout << "RAQUEL: finished saveSwarmState" << endl;

				string currentDirectory = options.jobOutputDir + toString(currentGeneration);
				if (options.deleteOldFiles) {
					//cout << "RAQUEL: starting cleanupFiles" << endl;

					cleanupFiles(currentDirectory.c_str());
					//cout << "RAQUEL: finished cleanupFiles" << endl;

				}



				// Check for stop criteria
				stopCriteria = checkStopCriteria();
				if (options.verbosity >= 3) {
					cout << "Stop Criteria value: " << stopCriteria << endl;
				}
				string outputPath = options.jobOutputDir + toString(currentGeneration - 1 ) + "_summary.txt";

				outputRunSummary(outputPath);


		if (!stopCriteria) {


			//for(int i=1; i <= options.swarmSize; i++){//
			//	for(int j=0; j<options.models.size(); j++){
			//		subparticleCurrParamSets_[i][j] = particleCurrParamSets_[i];
			//	}
			//}



			for(int i=1; i <= options.swarmSize; i++){//
							particlestoProcess.push_back(i);
			}

			// Process particles
			//cout << "Raquel before processParticlesPSO " << particlestoProcess.size() << " finished particles" << endl;
			processParticlesPSO(particlestoProcess, true);


			//cout << "Raquel After processParticlesPSO in the main loop" << endl;

			//cout << "RAQUEL FALSE STOP CRITERIA IN RUNSPSO" << endl;

		}




	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x230(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xf0875
movq 0x1e46f6(%rip), %rdi    # 0x2d4f50
leaq 0xf2b56(%rip), %rsi     # 0x1e33b7
callq 0x2ff50
movq %rax, %rdi
movq 0x1e4758(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x230(%rbp), %rax
movq %rax, %rcx
addq $0x1520, %rcx           # imm = 0x1520
movq %rcx, -0x240(%rbp)
movl 0x13e8(%rax), %esi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x238(%rbp)
callq 0x17e460
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0xb3380
jmp 0xf08bc
leaq -0x28(%rbp), %rdi
callq 0x17d410
movl %eax, -0x244(%rbp)
jmp 0xf08cd
movl -0x244(%rbp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x245(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x311e8
leaq -0x48(%rbp), %rdi
callq 0x311e8
movb -0x245(%rbp), %al
testb $0x1, %al
jne 0xf0902
jmp 0xf0a45
movq -0x230(%rbp), %rdx
addq $0x1520, %rdx           # imm = 0x1520
leaq 0xe9e80(%rip), %rsi     # 0x1da797
leaq -0x98(%rbp), %rdi
callq 0x44240
movq -0x230(%rbp), %rax
movl 0x13e8(%rax), %esi
leaq -0xb8(%rbp), %rdi
callq 0x17e460
jmp 0xf093e
leaq -0x78(%rbp), %rdi
leaq -0x98(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x9c7d0
jmp 0xf0957
leaq -0xb8(%rbp), %rdi
callq 0x311e8
leaq -0x98(%rbp), %rdi
callq 0x311e8
leaq -0xd8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x2f300
jmp 0xf0981
leaq -0xd8(%rbp), %rdi
callq 0x17e3f0
jmp 0xf098f
leaq -0xd8(%rbp), %rdi
callq 0x311e8
leaq -0x78(%rbp), %rdi
callq 0x311e8
jmp 0xf0a45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf09cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x311e8
leaq -0x48(%rbp), %rdi
callq 0x311e8
jmp 0xf0f13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0a00
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x311e8
leaq -0x98(%rbp), %rdi
callq 0x311e8
jmp 0xf0f13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0a37
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x311e8
leaq -0x78(%rbp), %rdi
callq 0x311e8
jmp 0xf0f13
movq -0x230(%rbp), %rax
testb $0x1, 0x13f0(%rax)
jne 0xf0a94
movq -0x230(%rbp), %rsi
movl 0x1620(%rsi), %edx
leaq -0xf0(%rbp), %rdi
callq 0xfa3e0
movq -0x230(%rbp), %rdi
addq $0x1a08, %rdi           # imm = 0x1A08
leaq -0xf0(%rbp), %rsi
callq 0x124320
leaq -0xf0(%rbp), %rdi
callq 0x121030
leaq -0x110(%rbp), %rdi
callq 0x2f880
leaq -0x128(%rbp), %rdi
callq 0xb2010
leaq -0x140(%rbp), %rdi
callq 0xb2010
movb $0x0, -0x141(%rbp)
movb -0x141(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xf0ad0
jmp 0xf0ec2
movq -0x230(%rbp), %rax
cmpl $0x0, 0x1614(%rax)
jb 0xf0b46
movq 0x1e4469(%rip), %rdi    # 0x2d4f50
leaq 0xf28e7(%rip), %rsi     # 0x1e33d5
callq 0x2ff50
movq %rax, -0x250(%rbp)
jmp 0xf0afc
movq -0x250(%rbp), %rdi
movq -0x230(%rbp), %rax
movl 0x13e8(%rax), %esi
callq 0x30640
movq %rax, -0x258(%rbp)
jmp 0xf0b1e
movq -0x258(%rbp), %rdi
movq 0x1e449c(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf0b33
jmp 0xf0b46
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0eef
movq -0x230(%rbp), %rdi
callq 0x100a10
jmp 0xf0b54
movq -0x230(%rbp), %rdi
callq 0xf99b0
movb %al, -0x259(%rbp)
jmp 0xf0b68
movq -0x230(%rbp), %rdi
movb -0x259(%rbp), %al
andb $0x1, %al
movb %al, -0x141(%rbp)
callq 0x10f2c0
jmp 0xf0b84
movq -0x230(%rbp), %rax
movq %rax, %rcx
addq $0x1520, %rcx           # imm = 0x1520
movq %rcx, -0x268(%rbp)
movl 0x13e8(%rax), %esi
leaq -0x188(%rbp), %rdi
callq 0x17e460
jmp 0xf0bb0
movq -0x268(%rbp), %rsi
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rdx
callq 0xb3380
jmp 0xf0bcc
leaq -0x188(%rbp), %rdi
callq 0x311e8
movq -0x230(%rbp), %rax
testb $0x1, 0x1646(%rax)
je 0xf0c35
leaq -0x168(%rbp), %rdi
callq 0x2f290
movq -0x230(%rbp), %rdi
movq %rax, %rsi
callq 0x105080
jmp 0xf0c05
jmp 0xf0c35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x311e8
jmp 0xf0eef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0eb4
movq -0x230(%rbp), %rdi
callq 0xf99b0
movb %al, -0x269(%rbp)
jmp 0xf0c49
movq -0x230(%rbp), %rax
movb -0x269(%rbp), %cl
andb $0x1, %cl
movb %cl, -0x141(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xf0cba
movq 0x1e42e1(%rip), %rdi    # 0x2d4f50
leaq 0xf2774(%rip), %rsi     # 0x1e33ea
callq 0x2ff50
movq %rax, -0x278(%rbp)
jmp 0xf0c84
movq -0x278(%rbp), %rdi
movzbl -0x141(%rbp), %esi
andl $0x1, %esi
callq 0x2f690
movq %rax, -0x280(%rbp)
jmp 0xf0ca3
movq -0x280(%rbp), %rdi
movq 0x1e4317(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf0cb8
jmp 0xf0cba
movq -0x230(%rbp), %rax
movq %rax, %rcx
addq $0x1520, %rcx           # imm = 0x1520
movq %rcx, -0x288(%rbp)
movl 0x13e8(%rax), %esi
decl %esi
leaq -0x1e8(%rbp), %rdi
callq 0x17e460
jmp 0xf0ce8
movq -0x288(%rbp), %rsi
leaq -0x1c8(%rbp), %rdi
leaq -0x1e8(%rbp), %rdx
callq 0xb3380
jmp 0xf0d04
leaq 0xf264a(%rip), %rdx     # 0x1e3355
leaq -0x1a8(%rbp), %rdi
leaq -0x1c8(%rbp), %rsi
callq 0x441f0
jmp 0xf0d20
leaq -0x1c8(%rbp), %rdi
callq 0x311e8
leaq -0x1e8(%rbp), %rdi
callq 0x311e8
leaq -0x208(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x2f300
jmp 0xf0d4d
movq -0x230(%rbp), %rdi
leaq -0x208(%rbp), %rsi
callq 0x105530
jmp 0xf0d62
leaq -0x208(%rbp), %rdi
callq 0x311e8
testb $0x1, -0x141(%rbp)
jne 0xf0e8b
movl $0x1, -0x20c(%rbp)
movq -0x230(%rbp), %rcx
movl -0x20c(%rbp), %eax
cmpl 0x1620(%rcx), %eax
jg 0xf0e34
movl -0x20c(%rbp), %eax
movl %eax, -0x210(%rbp)
leaq -0x140(%rbp), %rdi
leaq -0x210(%rbp), %rsi
callq 0x122310
jmp 0xf0dbf
jmp 0xf0dc1
movl -0x20c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20c(%rbp)
jmp 0xf0d85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0df8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1c8(%rbp), %rdi
callq 0x311e8
leaq -0x1e8(%rbp), %rdi
callq 0x311e8
jmp 0xf0eb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0xf0ea8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x208(%rbp), %rdi
callq 0x311e8
jmp 0xf0ea8
leaq -0x228(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x123be0
jmp 0xf0e49
movq -0x230(%rbp), %rdi
leaq -0x228(%rbp), %rsi
movl $0x1, %edx
callq 0xfbb20
jmp 0xf0e63
leaq -0x228(%rbp), %rdi
callq 0xb2400
jmp 0xf0e8b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x228(%rbp), %rdi
callq 0xb2400
jmp 0xf0ea8
leaq -0x1a8(%rbp), %rdi
callq 0x311e8
leaq -0x168(%rbp), %rdi
callq 0x311e8
jmp 0xf0abf
leaq -0x1a8(%rbp), %rdi
callq 0x311e8
leaq -0x168(%rbp), %rdi
callq 0x311e8
jmp 0xf0eef
leaq -0x140(%rbp), %rdi
callq 0xb2400
leaq -0x128(%rbp), %rdi
callq 0xb2400
leaq -0x110(%rbp), %rdi
callq 0x311e8
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
leaq -0x140(%rbp), %rdi
callq 0xb2400
leaq -0x128(%rbp), %rdi
callq 0xb2400
leaq -0x110(%rbp), %rdi
callq 0x311e8
movq -0x50(%rbp), %rdi
callq 0x30170
nopl (%rax)