// File: /workspace/github_cpp_makefile_star2_O1/cssartori[P]ga-qap/src/geneticalgorithm.cpp
// Function: GeneticAlgorithm::crossover_opt(Solution const&, Solution const&, ObjectiveFunction const&)
Solution GeneticAlgorithm::crossover_opt(const Solution& p1, const Solution& p2, const ObjectiveFunction& of){
	/*probability of crossover occurring*/
	std::uniform_real_distribution<double> prob(0,1);
	double pcs = prob(generator);
	if(pcs > pc){ /*if crossover does not occur, return the best parent*/
		if(p1 > p2) return p2;
		else return p1;
	}

	/*The new child*/
	std::vector<int> child(data.n, -1);
	/*Vector indicating wheter a given facility was already placed or not*/
	std::vector<bool> placed(data.n, false);	

	/*Copy equal positions from parents into child*/
	for(size_t i=0;i<data.n;i++){
		if(p1[i] == p2[i]){
			child[i] = p1[i];
			placed[p1[i]] = true;
		}
	}
	
	/*Save the equal positions stored*/
	std::vector<int> childm = child;
	std::vector<bool> placedm = placed;
	Solution *best = NULL;
	/*Tries N*0.2 times*/
    int lim = data.n*0.2;
	std::uniform_int_distribution<int> rand_place (0,data.n-1);
	
	for(int m=0;m<lim;m++){
		child = childm;
		placed = placedm;

		/*Assign the rest of the positions according to parents or randomly*/
		for(size_t i=0;i<data.n;i++){
			if(child[i] == -1){
				if(not placed[p1[i]] && not placed[p2[i]]){
					/*If none of the parents position was already placed, choose probabilistically*/
					double p = prob(generator);
					if( p < 0.5  ) child[i] = p1[i];
					else child[i] = p2[i];
				}else if(not placed[p1[i]]){
					child[i] = p1[i];
				}else if(not placed[p2[i]]){
					child[i] = p2[i];
				}else{
					/*If both positions were placed, choose another randomly*/
					int r = rand_place(generator);
					while(placed[r]) r = rand_place(generator);
					child[i] = r;
				}
			
				/*Mark this place as used*/
				placed[child[i]] = true;
			}
		}
		Solution s(child);
		s.set_fitness(of.eval(s));

		if(best == NULL){
			best = new Solution();
			*best = s;
		}else if(*best > s){
			*best = s;
		}
		
	}

	return *best;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %r8, 0xc8(%rsp)
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, 0x20(%rsp)
leaq 0x80(%rsi), %rbp
movq %rbp, %rdi
callq 0x6d1f
ucomisd 0x8(%r15), %xmm0
jbe 0x59be
movq %r12, %rdi
movq %r14, %rsi
callq 0x362e
testb %al, %al
je 0x5e9d
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2e46
movl 0x18(%r14), %eax
jmp 0x5eb2
movq %r14, 0x18(%rsp)
movq (%r15), %rax
movq 0x30(%rax), %rsi
leaq 0x30(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x60(%rsp), %rdi
leaq 0x78(%rsp), %rbx
movq %rbx, %rcx
callq 0x29aa
movq (%r15), %rax
movq 0x30(%rax), %rsi
movb $0x0, (%rbx)
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
callq 0x4078
movabsq $-0x8000000000000000, %r14 # imm = 0x8000000000000000
movq %r15, 0x28(%rsp)
movq (%r15), %rax
cmpq $0x0, 0x30(%rax)
movq %r12, 0x10(%rsp)
je 0x5abf
xorl %ebx, %ebx
leaq 0x3f(%r14), %r15
movq %r12, %rdi
movl %ebx, %esi
callq 0x3608
movl %eax, %r12d
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
cmpl %eax, %r12d
movq 0x10(%rsp), %r12
jne 0x5aaa
movq %r12, %rdi
movl %ebx, %esi
callq 0x3608
movq 0x60(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
movq 0x10(%rsp), %r12
movq %r12, %rdi
movl %ebx, %esi
callq 0x3608
movslq %eax, %rcx
leaq 0x3f(%rcx), %rdx
testq %rcx, %rcx
cmovnsq %rcx, %rdx
sarq $0x6, %rdx
shlq $0x3, %rdx
addq 0x30(%rsp), %rdx
andq %r15, %rcx
xorl %esi, %esi
cmpq %r14, %rcx
setbe %sil
movl $0x1, %edi
movl %eax, %ecx
shlq %cl, %rdi
orq %rdi, -0x8(%rdx,%rsi,8)
incq %rbx
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpq 0x30(%rax), %rbx
jb 0x5a30
leaq 0xd8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2e46
leaq 0x78(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6296
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movq %rax, %xmm0
punpckldq 0x1587(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x158f(%rip), %xmm0     # 0x7090
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1773(%rip), %xmm1     # 0x7288
cvttsd2si %xmm1, %ecx
decl %eax
movl $0x0, 0xc0(%rsp)
movl %eax, 0xc4(%rsp)
xorl %ebx, %ebx
movl %ecx, 0x5c(%rsp)
testl %ecx, %ecx
jle 0x5eba
leaq 0x3f(%r14), %r15
leaq 0xc0(%rsp), %r12
movq $0x0, 0x8(%rsp)
movq %rbx, 0xd0(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x2cfe
leaq 0x30(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x6328
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpq $0x0, 0x30(%rax)
je 0x5d98
xorl %ebx, %ebx
movq 0x60(%rsp), %rax
cmpl $-0x1, (%rax,%rbx,4)
jne 0x5d83
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
cltq
leaq 0x3f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x6, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r15, %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jb 0x5c21
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
cltq
leaq 0x3f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x6, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r15, %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x5d14
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
cltq
leaq 0x3f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x6, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r15, %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x5cf8
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
cltq
leaq 0x3f(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x6, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r15, %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x5d06
movq %r12, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x4242
movslq %eax, %rcx
leaq 0x3f(%rcx), %rdx
testq %rcx, %rcx
cmovnsq %rcx, %rdx
sarq $0x6, %rdx
shlq $0x3, %rdx
addq 0x30(%rsp), %rdx
movq %rcx, %rsi
andq %r15, %rsi
xorl %edi, %edi
cmpq %r14, %rsi
setbe %dil
movq -0x8(%rdx,%rdi,8), %rdx
btq %rcx, %rdx
jae 0x5d44
movq %r12, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x4242
jmp 0x5cb3
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
jmp 0x5d44
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
jmp 0x5d44
movq %rbp, %rdi
callq 0x6d1f
movsd 0x155c(%rip), %xmm1     # 0x7280
ucomisd %xmm0, %xmm1
jbe 0x5d38
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
jmp 0x5d44
movq 0x18(%rsp), %rdi
movl %ebx, %esi
callq 0x3608
movq 0x60(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
movslq %eax, %rcx
leaq 0x3f(%rcx), %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
sarq $0x6, %rax
shlq $0x3, %rax
addq 0x30(%rsp), %rax
movq %rcx, %rdx
andq %r15, %rdx
xorl %esi, %esi
cmpq %r14, %rdx
setbe %sil
movl $0x1, %edx
shlq %cl, %rdx
orq %rdx, -0x8(%rax,%rsi,8)
incq %rbx
movq 0x28(%rsp), %rax
movq (%rax), %rax
cmpq 0x30(%rax), %rbx
jb 0x5b8e
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2e46
leaq 0xa0(%rsp), %rdi
movq %rbx, %rsi
callq 0x35b0
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
movq 0xd0(%rsp), %rbx
je 0x5de2
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0xc8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3738
leaq 0xa0(%rsp), %rdi
movl %eax, %esi
callq 0x3616
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x5e35
leaq 0xa0(%rsp), %rsi
callq 0x362e
testb %al, %al
je 0x5e72
movq 0x8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2cfe
jmp 0x5e63
movl $0x20, %edi
callq 0x2190
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x3564
movq %r13, %rdi
leaq 0xa0(%rsp), %rsi
callq 0x2cfe
movl 0xb8(%rsp), %eax
movq 0x8(%rsp), %rcx
movl %eax, 0x18(%rcx)
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x5e8f
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
incl %ebx
cmpl 0x5c(%rsp), %ebx
jne 0x5b50
jmp 0x5ec3
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2e46
movl 0x18(%r12), %eax
movl %eax, 0x18(%rbx)
jmp 0x5f87
movq $0x0, 0x8(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2e46
movl 0x18(%r14), %eax
movl %eax, 0x18(%rbx)
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x5f1e
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0x78(%rsp)
movl %eax, 0x80(%rsp)
movq %rax, 0x88(%rsp)
movl %eax, 0x90(%rsp)
movq %rax, 0x98(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x5f3b
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5f6b
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x5f82
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x20(%rsp), %rbx
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x601a
movq %rax, %rbx
jmp 0x605c
jmp 0x5fe4
movq %rax, %rbx
jmp 0x60a9
movq %rax, %rbx
movl $0x20, %esi
movq %r13, %rdi
callq 0x21a0
jmp 0x5ff3
jmp 0x5fe4
jmp 0x601a
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x601d
movq 0x100(%rsp), %rsi
jmp 0x6008
jmp 0x601a
jmp 0x601a
movq %rax, %rbx
jmp 0x6079
jmp 0x601a
jmp 0x601a
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x601d
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x601d
jmp 0x601a
jmp 0x601a
jmp 0x601a
jmp 0x601a
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x605c
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0x78(%rsp)
movl %eax, 0x80(%rsp)
movq %rax, 0x88(%rsp)
movl %eax, 0x90(%rsp)
movq %rax, 0x98(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x6079
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x60a9
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl %eax, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movl %eax, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x60c0
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x2280