// File: /workspace/github_cpp_makefile_star2_O3/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, 0xd0(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
leaq 0x80(%rsi), %rbp
movq %rbp, %rdi
callq 0x71a3
ucomisd 0x8(%rbx), %xmm0
jbe 0x5d8f
movq %r15, %rdi
movq %r14, %rsi
callq 0x3762
testb %al, %al
je 0x6262
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f74
movl 0x18(%r14), %eax
jmp 0x6276
movq %r14, 0x20(%rsp)
movq (%rbx), %rax
movq 0x30(%rax), %rsi
leaq 0x30(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x60(%rsp), %rdi
movq %rbx, %r14
leaq 0xa8(%rsp), %rbx
movq %rbx, %rcx
callq 0x29c6
movq (%r14), %rax
movq 0x30(%rax), %rsi
movb $0x0, (%rbx)
leaq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
leaq 0x80(%rsp), %rcx
callq 0x4468
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
movq %r14, 0x28(%rsp)
movq (%r14), %rax
cmpq $0x0, 0x30(%rax)
movq %r15, 0x10(%rsp)
je 0x5e98
xorl %ebx, %ebx
leaq 0x3f(%r13), %r14
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
movl %eax, %r12d
movq 0x20(%rsp), %rdi
movl %ebx, %esi
callq 0x373c
cmpl %eax, %r12d
movq 0x10(%rsp), %r15
jne 0x5e83
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
movq 0x60(%rsp), %rcx
movl %eax, (%rcx,%rbx,4)
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
movslq %eax, %rcx
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x6, %edx
movslq %edx, %rdx
shlq $0x3, %rdx
addq 0x30(%rsp), %rdx
andq %r14, %rcx
xorl %esi, %esi
cmpq %r13, %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 0x5e0a
leaq 0xd8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x2f74
leaq 0xa8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6622
movq 0x28(%rsp), %rbx
movq (%rbx), %rax
movq 0x30(%rax), %rax
movq %rax, %xmm0
punpckldq 0x21eb(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x21f3(%rip), %xmm0     # 0x80d0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x23d7(%rip), %xmm1     # 0x82c8
cvttsd2si %xmm1, %ecx
decl %eax
movl $0x0, 0xa0(%rsp)
movl %eax, 0xa4(%rsp)
movl %ecx, 0x5c(%rsp)
testl %ecx, %ecx
jle 0x627e
movq $0x0, 0x78(%rsp)
leaq 0x3f(%r13), %r14
leaq 0xa0(%rsp), %r12
movq $0x0, 0x8(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x2e1e
leaq 0x30(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x66b4
movq (%rbx), %rax
cmpq $0x0, 0x30(%rax)
je 0x6156
movq 0x60(%rsp), %rdx
xorl %ebx, %ebx
cmpl $-0x1, (%rdx,%rbx,4)
jne 0x6149
movq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x373c
cltq
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r14, %rdx
xorl %esi, %esi
cmpq %r13, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jb 0x5ff8
movq 0x20(%rsp), %rdi
movl %ebx, %esi
callq 0x373c
cltq
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r14, %rdx
xorl %esi, %esi
cmpq %r13, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x60de
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
cltq
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r14, %rdx
xorl %esi, %esi
cmpq %r13, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x60d2
movq 0x20(%rsp), %r15
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
cltq
leal 0x3f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
sarl $0x6, %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq 0x30(%rsp), %rcx
movq %rax, %rdx
andq %r14, %rdx
xorl %esi, %esi
cmpq %r13, %rdx
setbe %sil
movq -0x8(%rcx,%rsi,8), %rcx
btq %rax, %rcx
jae 0x60d2
movq %r12, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x4638
movslq %eax, %rcx
leal 0x3f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x6, %edx
movslq %edx, %rdx
shlq $0x3, %rdx
addq 0x30(%rsp), %rdx
movq %rcx, %rsi
andq %r14, %rsi
xorl %edi, %edi
cmpq %r13, %rsi
setbe %dil
movq -0x8(%rdx,%rdi,8), %rdx
btq %rcx, %rdx
jae 0x6104
movq %r12, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x4638
jmp 0x608e
movq %r15, %rdi
movl %ebx, %esi
callq 0x373c
jmp 0x6104
movq %rbp, %rdi
callq 0x71a3
movsd 0x21d2(%rip), %xmm1     # 0x82c0
ucomisd %xmm0, %xmm1
movq 0x20(%rsp), %rdi
cmovaq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x373c
movq 0x60(%rsp), %rdx
movl %eax, (%rdx,%rbx,4)
movslq %eax, %rcx
leal 0x3f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
sarl $0x6, %eax
cltq
shlq $0x3, %rax
addq 0x30(%rsp), %rax
movq %rcx, %rsi
andq %r14, %rsi
xorl %edi, %edi
cmpq %r13, %rsi
setbe %dil
movl $0x1, %esi
shlq %cl, %rsi
orq %rsi, -0x8(%rax,%rdi,8)
movq 0x28(%rsp), %rax
movq (%rax), %rax
incq %rbx
cmpq 0x30(%rax), %rbx
jb 0x5f6c
leaq 0xf0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x2f74
leaq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0x36e4
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x6198
movq 0x100(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x3b34
movq 0x28(%rsp), %rbx
leaq 0x80(%rsp), %rdi
movl %eax, %esi
callq 0x374a
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x61f0
leaq 0x80(%rsp), %rsi
callq 0x3762
testb %al, %al
je 0x622d
movq 0x8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x2e1e
jmp 0x621e
movl $0x20, %edi
callq 0x2190
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x3698
movq %r15, %rdi
leaq 0x80(%rsp), %rsi
callq 0x2e1e
movl 0x98(%rsp), %eax
movq 0x8(%rsp), %rcx
movl %eax, 0x18(%rcx)
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x624a
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x78(%rsp), %rax
incl %eax
movq %rax, 0x78(%rsp)
cmpl 0x5c(%rsp), %eax
jne 0x5f33
jmp 0x6287
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f74
movl 0x18(%r15), %eax
movl %eax, 0x18(%rbx)
jmp 0x6321
movq $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x2f74
movl 0x18(%r14), %eax
movl %eax, 0x18(%rbx)
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x62c0
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x62dd
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6305
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq $0x0, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x631c
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x18(%rsp), %rbx
movq %rbx, %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x63b4
movq %rax, %rbx
jmp 0x63f1
jmp 0x637e
movq %rax, %rbx
jmp 0x6439
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x21a0
jmp 0x638d
jmp 0x637e
jmp 0x63b4
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
testq %rdi, %rdi
je 0x63b7
movq 0x100(%rsp), %rsi
jmp 0x63a2
jmp 0x63b4
jmp 0x63b4
movq %rax, %rbx
jmp 0x640e
jmp 0x63b4
jmp 0x63b4
movq %rax, %rbx
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x63b7
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
jmp 0x63b7
jmp 0x63b4
jmp 0x63b4
jmp 0x63b4
jmp 0x63b4
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x63f1
movq 0xc8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0xa8(%rsp)
movl $0x0, 0xb0(%rsp)
movq %rax, 0xb8(%rsp)
movq 0xd8(%rsp), %rdi
testq %rdi, %rdi
je 0x640e
movq 0xe8(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x6439
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
xorl %eax, %eax
movq %rax, 0x30(%rsp)
movl $0x0, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x6450
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x21a0
movq %rbx, %rdi
callq 0x2280