// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, %rbx
cmpl $0x3, 0x1614(%rdi)
jb 0x6e238
movq 0xf7d74(%rip), %rdi     # 0x165f98
leaq 0x7077d(%rip), %rsi     # 0xde9a8
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x1520(%rbx), %r15
movl 0x13e8(%rbx), %esi
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x9f56b
leaq 0xe8(%rsp), %rdi
movq %r15, 0x8(%rsp)
movq %r15, %rsi
movq %r14, %rdx
callq 0x5a115
leaq 0xe8(%rsp), %rdi
callq 0x9ec28
movl %eax, %ebp
leaq 0xe8(%rsp), %rdi
callq 0x295a0
leaq 0x10(%rsp), %rdi
callq 0x295a0
testl %ebp, %ebp
jne 0x6e31c
leaq 0x67bf8(%rip), %rsi     # 0xd5e97
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rdx
callq 0x3338b
movl 0x13e8(%rbx), %esi
leaq 0x30(%rsp), %rdi
callq 0x9f56b
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x4e6ed
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0x50(%rsp), %rdi
callq 0x295a0
leaq 0xc8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x28dd0
leaq 0xc8(%rsp), %rdi
callq 0x9f543
leaq 0xc8(%rsp), %rdi
callq 0x295a0
leaq 0x10(%rsp), %rdi
callq 0x295a0
cmpb $0x0, 0x13f0(%rbx)
jne 0x6e352
movl 0x1620(%rbx), %edx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x734e6
leaq 0x1a08(%rbx), %rdi
movq %r14, %rsi
callq 0x9ca60
movq %r14, %rdi
callq 0x85c60
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
leaq 0x90(%rsp), %r15
andq $0x0, 0x10(%r15)
movaps %xmm0, (%r15)
leaq 0x148(%rsp), %rbp
leaq 0x108(%rsp), %r13
movq 0xf7bfc(%rip), %rdi     # 0x165f98
leaq 0x70623(%rip), %rsi     # 0xde9c6
callq 0x289f0
movl 0x13e8(%rbx), %esi
movq %rax, %rdi
callq 0x286e0
movq %rax, %rdi
callq 0x284a0
movq %rbx, %rdi
callq 0x76bce
movq %rbx, %rdi
callq 0x72f2a
movq %rbx, %rdi
callq 0x7ec70
movl 0x13e8(%rbx), %esi
leaq 0x30(%rsp), %r14
movq %r14, %rdi
callq 0x9f56b
leaq 0x50(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x5a115
movq %r14, %rdi
callq 0x295a0
cmpb $0x1, 0x1646(%rbx)
jne 0x6e416
movq 0x50(%rsp), %rsi
callq 0x791e0
movq %rbx, %rdi
callq 0x72f2a
movl %eax, %r12d
cmpl $0x3, 0x1614(%rbx)
jb 0x6e451
movq 0xf7b67(%rip), %rdi     # 0x165f98
leaq 0x705a3(%rip), %rsi     # 0xde9db
callq 0x289f0
movzbl %r12b, %esi
movq %rax, %rdi
callq 0x29180
movq %rax, %rdi
callq 0x284a0
movl 0x13e8(%rbx), %esi
decl %esi
movq %rbp, %r14
movq %rbp, %rdi
callq 0x9f56b
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x5a115
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x704c0(%rip), %rdx     # 0xde946
callq 0x3336c
movq %r13, %rdi
callq 0x295a0
movq %rbp, %rdi
callq 0x295a0
leaq 0x128(%rsp), %rdi
movq %r14, %rsi
movq %rdi, %r14
callq 0x28dd0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7942a
movq %r14, %rdi
callq 0x295a0
testb %r12b, %r12b
jne 0x6e531
pushq $0x1
popq %r12
cmpl 0x1620(%rbx), %r12d
jg 0x6e4eb
movl %r12d, 0x108(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x8ed06
incl %r12d
jmp 0x6e4ca
leaq 0xb0(%rsp), %rdi
movq %r15, %rsi
callq 0x8673e
movq %rbx, %rdi
leaq 0xb0(%rsp), %r14
movq %r14, %rsi
xorl %edx, %edx
callq 0x73f2e
movq %r14, %rdi
callq 0x5b268
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0x50(%rsp), %rdi
callq 0x295a0
jmp 0x6e395
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0x50(%rsp), %rdi
callq 0x295a0
leaq 0x90(%rsp), %rdi
callq 0x5b268
leaq 0x70(%rsp), %rdi
callq 0x5b268
leaq 0x10(%rsp), %rdi
callq 0x295a0
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x6e5ae
jmp 0x6e5b8
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x295a0
jmp 0x6e599
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
jmp 0x6e650
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x295a0
jmp 0x6e64b
movq %rax, %rbx
jmp 0x6e64b
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x5b268
jmp 0x6e620
jmp 0x6e618
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
jmp 0x6e62f
jmp 0x6e613
movq %rax, %rbx
jmp 0x6e5f5
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x295a0
leaq 0x148(%rsp), %rdi
jmp 0x6e625
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x295a0
jmp 0x6e620
jmp 0x6e61d
movq %rax, %rbx
jmp 0x6e62a
movq %rax, %rbx
jmp 0x6e634
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x295a0
leaq 0x50(%rsp), %rdi
callq 0x295a0
leaq 0x90(%rsp), %rdi
callq 0x5b268
leaq 0x70(%rsp), %rdi
callq 0x5b268
leaq 0x10(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x292f0
nop