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

	if (!checkIfFileExists(options.jobOutputDir + "1")) {
		string createDirCmd = "mkdir " + options.jobOutputDir + "1";
		runCommand(createDirCmd);
	}

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

		// Run first flight
		initPSOswarm();
	}


	if (options.verbosity >= 0) {
		cout << "Generation (flock): " << toString(currentGeneration - 1 ) << endl;
	}
		vector<unsigned int> particlestoProcess;


	bool stopCriteria = false;
	while (!stopCriteria){
		if (options.verbosity >= 3) {
			cout << "RAQUEL: Started runasyncGeneration();" << endl;
		}
		runAsyncGeneration();
		if (options.verbosity >= 3) {
			cout << "RAQUEL: Finished runasyncGeneration();" << endl;

			cout << "RAQUEL: Started checkStopCriteria();" << endl;
		}

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

		stopCriteria = checkStopCriteria();
		if (options.verbosity >= 3) {
			cout << "RAQUEL: Stop criteria value is " << stopCriteria << endl;

			cout << "RAQUEL: Finished checkStopCriteria();" << endl;
			cout << "RAQUEL: Started saveSwarmState();" << endl;
		}
		saveSwarmState();
		if (options.verbosity >= 3) {

			cout << "RAQUEL: Finished saveSwarmState();" << endl;
		}

		string currentDirectory = options.jobOutputDir + toString(currentGeneration);
		if (options.deleteOldFiles) {
			if (options.verbosity >= 3) {
				cout << "RAQUEL: Started cleanupFiles();" << endl;
			}
			cleanupFiles(currentDirectory.c_str());
			if (options.verbosity >= 3) {
				cout << "RAQUEL: Finished cleanupFiles();" << endl;
			}
		}

		string outputPath = options.jobOutputDir + toString(currentGeneration - 1 ) + "_summary.txt";
		if (options.verbosity >= 3) {
			cout << "RAQUEL: Started outputRunSummary();" << endl;
		}
		outputRunSummary(outputPath);
		if (options.verbosity >= 3) {
			cout << "RAQUEL: Finished outputRunSummary();" << endl;
		}

		if (!stopCriteria) {
			if (options.verbosity >= 3) {
				cout << "RAQUEL: started processParticlesPSO();" << endl;
			}
			particlestoProcess.clear();

			for(auto i = finishedParticles_.begin(); i!=finishedParticles_.end(); ++i){
				if (options.verbosity >= 3) {
					cout << "RAQUEL: processing particle number: " << *i << endl;
				}
				particlestoProcess.push_back(*i);
			}
			processParticlesPSO(particlestoProcess, true);

			if (options.verbosity >= 3) {
				cout << "RAQUEL: finished processParticlesPSO();" << endl;
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x200(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xf3955
movq 0x1e1616(%rip), %rdi    # 0x2d4f50
leaq 0xefd0b(%rip), %rsi     # 0x1e364c
callq 0x2ff50
movq %rax, %rdi
movq 0x1e1678(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
movq -0x200(%rbp), %rsi
addq $0x1520, %rsi           # imm = 0x1520
leaq 0xf1514(%rip), %rdx     # 0x1e4e7e
leaq -0x28(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x447b0
movq -0x210(%rbp), %rdi
callq 0x17d410
movl %eax, -0x204(%rbp)
jmp 0xf398e
movl -0x204(%rbp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movb %al, -0x211(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x311e8
movb -0x211(%rbp), %al
testb $0x1, %al
jne 0xf39ba
jmp 0xf3aa0
movq -0x200(%rbp), %rdx
addq $0x1520, %rdx           # imm = 0x1520
leaq 0xe6dc8(%rip), %rsi     # 0x1da797
leaq -0x78(%rbp), %rdi
movq %rdi, -0x220(%rbp)
callq 0x44240
movq -0x220(%rbp), %rsi
leaq 0xf1491(%rip), %rdx     # 0x1e4e7e
leaq -0x58(%rbp), %rdi
callq 0x441f0
jmp 0xf39f8
leaq -0x78(%rbp), %rdi
callq 0x311e8
leaq -0x98(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x2f300
jmp 0xf3a13
leaq -0x98(%rbp), %rdi
callq 0x17e3f0
jmp 0xf3a21
leaq -0x98(%rbp), %rdi
callq 0x311e8
leaq -0x58(%rbp), %rdi
callq 0x311e8
jmp 0xf3aa0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x311e8
jmp 0xf4399
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x311e8
jmp 0xf4399
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xf3a92
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x311e8
leaq -0x58(%rbp), %rdi
callq 0x311e8
jmp 0xf4399
movq -0x200(%rbp), %rax
testb $0x1, 0x13f0(%rax)
jne 0xf3afd
movq -0x200(%rbp), %rsi
movl 0x1620(%rsi), %edx
leaq -0xb0(%rbp), %rdi
callq 0xfa3e0
movq -0x200(%rbp), %rdi
addq $0x1a08, %rdi           # imm = 0x1A08
leaq -0xb0(%rbp), %rsi
callq 0x124320
leaq -0xb0(%rbp), %rdi
callq 0x121030
movq -0x200(%rbp), %rdi
xorl %esi, %esi
callq 0x10f460
movq -0x200(%rbp), %rax
cmpl $0x0, 0x1614(%rax)
jb 0xf3bac
movq 0x1e1438(%rip), %rdi    # 0x2d4f50
leaq 0xef8b6(%rip), %rsi     # 0x1e33d5
callq 0x2ff50
movq %rax, %rcx
movq -0x200(%rbp), %rax
movq %rcx, -0x238(%rbp)
movl 0x13e8(%rax), %esi
decl %esi
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x230(%rbp)
callq 0x17e460
movq -0x238(%rbp), %rdi
movq -0x230(%rbp), %rsi
callq 0x2f780
movq %rax, -0x228(%rbp)
jmp 0xf3b6c
movq -0x228(%rbp), %rdi
movq 0x1e144e(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3b81
leaq -0xd0(%rbp), %rdi
callq 0x311e8
jmp 0xf3bac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x311e8
jmp 0xf4399
leaq -0xe8(%rbp), %rdi
callq 0xb2010
movb $0x0, -0xe9(%rbp)
movb -0xe9(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0xf3bd0
jmp 0xf4378
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf3c24
movq 0x1e1369(%rip), %rdi    # 0x2d4f50
leaq 0xefa7e(%rip), %rsi     # 0x1e366c
callq 0x2ff50
movq %rax, -0x240(%rbp)
jmp 0xf3bfc
movq -0x240(%rbp), %rdi
movq 0x1e13be(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3c11
jmp 0xf3c24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xf438d
movq -0x200(%rbp), %rdi
callq 0x103840
jmp 0xf3c32
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf3ca6
movq 0x1e1307(%rip), %rdi    # 0x2d4f50
leaq 0xefa42(%rip), %rsi     # 0x1e3692
callq 0x2ff50
movq %rax, -0x248(%rbp)
jmp 0xf3c5e
movq -0x248(%rbp), %rdi
movq 0x1e135c(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3c73
movq 0x1e12d6(%rip), %rdi    # 0x2d4f50
leaq 0xef88f(%rip), %rsi     # 0x1e3510
callq 0x2ff50
movq %rax, -0x250(%rbp)
jmp 0xf3c8f
movq -0x250(%rbp), %rdi
movq 0x1e132b(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3ca4
jmp 0xf3ca6
movq -0x200(%rbp), %rax
cmpl $0x0, 0x1614(%rax)
jb 0xf3d4f
movq 0x1e128f(%rip), %rdi    # 0x2d4f50
leaq 0xef70d(%rip), %rsi     # 0x1e33d5
callq 0x2ff50
movq %rax, -0x258(%rbp)
jmp 0xf3cd6
movq -0x200(%rbp), %rax
movl 0x13e8(%rax), %esi
decl %esi
leaq -0x110(%rbp), %rdi
callq 0x17e460
jmp 0xf3cf3
movq -0x258(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x2f780
movq %rax, -0x260(%rbp)
jmp 0xf3d0f
movq -0x260(%rbp), %rdi
movq 0x1e12ab(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3d24
leaq -0x110(%rbp), %rdi
callq 0x311e8
jmp 0xf3d4f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x110(%rbp), %rdi
callq 0x311e8
jmp 0xf438d
movq -0x200(%rbp), %rdi
callq 0xf99b0
movb %al, -0x261(%rbp)
jmp 0xf3d63
movq -0x200(%rbp), %rax
movb -0x261(%rbp), %cl
andb $0x1, %cl
movb %cl, -0xe9(%rbp)
cmpl $0x3, 0x1614(%rax)
jb 0xf3e3a
movq 0x1e11c3(%rip), %rdi    # 0x2d4f50
leaq 0xef805(%rip), %rsi     # 0x1e3599
callq 0x2ff50
movq %rax, -0x270(%rbp)
jmp 0xf3da2
movq -0x270(%rbp), %rdi
movzbl -0xe9(%rbp), %esi
andl $0x1, %esi
callq 0x2f690
movq %rax, -0x278(%rbp)
jmp 0xf3dc1
movq -0x278(%rbp), %rdi
movq 0x1e11f9(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3dd6
movq 0x1e1173(%rip), %rdi    # 0x2d4f50
leaq 0xef7d5(%rip), %rsi     # 0x1e35b9
callq 0x2ff50
movq %rax, -0x280(%rbp)
jmp 0xf3df2
movq -0x280(%rbp), %rdi
movq 0x1e11c8(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3e07
movq 0x1e1142(%rip), %rdi    # 0x2d4f50
leaq 0xef7ca(%rip), %rsi     # 0x1e35df
callq 0x2ff50
movq %rax, -0x288(%rbp)
jmp 0xf3e23
movq -0x288(%rbp), %rdi
movq 0x1e1197(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3e38
jmp 0xf3e3a
movq -0x200(%rbp), %rdi
callq 0x10f2c0
jmp 0xf3e48
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf3e8b
movq 0x1e10f1(%rip), %rdi    # 0x2d4f50
leaq 0xef6cf(%rip), %rsi     # 0x1e3535
callq 0x2ff50
movq %rax, -0x290(%rbp)
jmp 0xf3e74
movq -0x290(%rbp), %rdi
movq 0x1e1146(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3e89
jmp 0xf3e8b
movq -0x200(%rbp), %rax
movq %rax, %rcx
addq $0x1520, %rcx           # imm = 0x1520
movq %rcx, -0x298(%rbp)
movl 0x13e8(%rax), %esi
leaq -0x150(%rbp), %rdi
callq 0x17e460
jmp 0xf3eb7
movq -0x298(%rbp), %rsi
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rdx
callq 0xb3380
jmp 0xf3ed3
leaq -0x150(%rbp), %rdi
callq 0x311e8
movq -0x200(%rbp), %rax
testb $0x1, 0x1646(%rax)
je 0xf3fc6
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf3f64
movq 0x1e1046(%rip), %rdi    # 0x2d4f50
leaq 0xef647(%rip), %rsi     # 0x1e3558
callq 0x2ff50
movq %rax, -0x2a0(%rbp)
jmp 0xf3f1f
movq -0x2a0(%rbp), %rdi
movq 0x1e109b(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3f34
jmp 0xf3f64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x311e8
jmp 0xf438d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xf436a
leaq -0x130(%rbp), %rdi
callq 0x2f290
movq -0x200(%rbp), %rdi
movq %rax, %rsi
callq 0x105080
jmp 0xf3f81
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf3fc4
movq 0x1e0fb8(%rip), %rdi    # 0x2d4f50
leaq 0xef5d9(%rip), %rsi     # 0x1e3578
callq 0x2ff50
movq %rax, -0x2a8(%rbp)
jmp 0xf3fad
movq -0x2a8(%rbp), %rdi
movq 0x1e100d(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf3fc2
jmp 0xf3fc4
jmp 0xf3fc6
movq -0x200(%rbp), %rax
movq %rax, %rcx
addq $0x1520, %rcx           # imm = 0x1520
movq %rcx, -0x2b0(%rbp)
movl 0x13e8(%rax), %esi
decl %esi
leaq -0x1b0(%rbp), %rdi
callq 0x17e460
jmp 0xf3ff4
movq -0x2b0(%rbp), %rsi
leaq -0x190(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
callq 0xb3380
jmp 0xf4010
leaq 0xef33e(%rip), %rdx     # 0x1e3355
leaq -0x170(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x441f0
jmp 0xf402c
leaq -0x190(%rbp), %rdi
callq 0x311e8
leaq -0x1b0(%rbp), %rdi
callq 0x311e8
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf40cf
movq 0x1e0ef5(%rip), %rdi    # 0x2d4f50
leaq 0xef657(%rip), %rsi     # 0x1e36b9
callq 0x2ff50
movq %rax, -0x2b8(%rbp)
jmp 0xf4070
movq -0x2b8(%rbp), %rdi
movq 0x1e0f4a(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf4085
jmp 0xf40cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xf40ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x311e8
leaq -0x1b0(%rbp), %rdi
callq 0x311e8
jmp 0xf436a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0xf435e
leaq -0x1d0(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x2f300
jmp 0xf40e4
movq -0x200(%rbp), %rdi
leaq -0x1d0(%rbp), %rsi
callq 0x105530
jmp 0xf40f9
leaq -0x1d0(%rbp), %rdi
callq 0x311e8
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf4165
movq 0x1e0e34(%rip), %rdi    # 0x2d4f50
leaq 0xef5ba(%rip), %rsi     # 0x1e36dd
callq 0x2ff50
movq %rax, -0x2c0(%rbp)
jmp 0xf4131
movq -0x2c0(%rbp), %rdi
movq 0x1e0e89(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf4146
jmp 0xf4165
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x311e8
jmp 0xf435e
testb $0x1, -0xe9(%rbp)
jne 0xf4341
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf41b5
movq 0x1e0dc7(%rip), %rdi    # 0x2d4f50
leaq 0xef572(%rip), %rsi     # 0x1e3702
callq 0x2ff50
movq %rax, -0x2c8(%rbp)
jmp 0xf419e
movq -0x2c8(%rbp), %rdi
movq 0x1e0e1c(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf41b3
jmp 0xf41b5
leaq -0xe8(%rbp), %rdi
callq 0x122bf0
movq -0x200(%rbp), %rdi
addq $0x18c0, %rdi           # imm = 0x18C0
callq 0x123360
movq %rax, -0x1d8(%rbp)
movq -0x200(%rbp), %rdi
addq $0x18c0, %rdi           # imm = 0x18C0
callq 0x1233c0
movq %rax, -0x1e0(%rbp)
leaq -0x1d8(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x123390
testb $0x1, %al
jne 0xf4211
jmp 0xf42a7
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf4277
movq 0x1e0d28(%rip), %rdi    # 0x2d4f50
leaq 0xef4fa(%rip), %rsi     # 0x1e3729
callq 0x2ff50
movq %rax, -0x2d0(%rbp)
jmp 0xf423d
leaq -0x1d8(%rbp), %rdi
callq 0x1233f0
movq -0x2d0(%rbp), %rdi
movl (%rax), %esi
callq 0x30640
movq %rax, -0x2d8(%rbp)
jmp 0xf4260
movq -0x2d8(%rbp), %rdi
movq 0x1e0d5a(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf4275
jmp 0xf4277
leaq -0x1d8(%rbp), %rdi
callq 0x1233f0
movq %rax, %rsi
leaq -0xe8(%rbp), %rdi
callq 0xb4640
jmp 0xf4294
jmp 0xf4296
leaq -0x1d8(%rbp), %rdi
callq 0x123410
jmp 0xf41db
leaq -0x1f8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x123be0
jmp 0xf42bc
movq -0x200(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
movl $0x1, %edx
callq 0xfbb20
jmp 0xf42d6
leaq -0x1f8(%rbp), %rdi
callq 0xb2400
movq -0x200(%rbp), %rax
cmpl $0x3, 0x1614(%rax)
jb 0xf433f
movq 0x1e0c57(%rip), %rdi    # 0x2d4f50
leaq 0xef44e(%rip), %rsi     # 0x1e374e
callq 0x2ff50
movq %rax, -0x2e0(%rbp)
jmp 0xf430e
movq -0x2e0(%rbp), %rdi
movq 0x1e0cac(%rip), %rsi    # 0x2d4fc8
callq 0x30c10
jmp 0xf4323
jmp 0xf433f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0xb2400
jmp 0xf435e
jmp 0xf4341
leaq -0x170(%rbp), %rdi
callq 0x311e8
leaq -0x130(%rbp), %rdi
callq 0x311e8
jmp 0xf3bbf
leaq -0x170(%rbp), %rdi
callq 0x311e8
leaq -0x130(%rbp), %rdi
callq 0x311e8
jmp 0xf438d
leaq -0xe8(%rbp), %rdi
callq 0xb2400
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
leaq -0xe8(%rbp), %rdi
callq 0xb2400
movq -0x30(%rbp), %rdi
callq 0x30170
nopw %cs:(%rax,%rax)