// File: 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
movq %rsp, %rbp
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x1d8(%rbp)
movq %rdi, %rax
movq %rax, -0x1d0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq -0x38(%rbp), %rdi
xorps %xmm0, %xmm0
movsd 0x4756(%rip), %xmm1     # 0xf270
callq 0xb900
movq -0x1c8(%rbp), %rsi
addq $0x80, %rsi
leaq -0x38(%rbp), %rdi
callq 0xb930
movq -0x1c8(%rbp), %rax
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
ucomisd 0x8(%rax), %xmm0
jbe 0xab8b
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x6080
testb $0x1, %al
jne 0xab61
jmp 0xab76
movq -0x1d8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x8c00
jmp 0xb4cf
movq -0x1d8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x8c00
jmp 0xb4cf
movq -0x1c8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movq %rax, -0x1e8(%rbp)
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
leaq -0x5d(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x2bb0
movq -0x1e8(%rbp), %rsi
movq -0x1e0(%rbp), %rcx
leaq -0x58(%rbp), %rdi
leaq -0x5c(%rbp), %rdx
callq 0x2bd0
jmp 0xabd4
leaq -0x5d(%rbp), %rdi
callq 0x2cb0
movq -0x1c8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
movq %rax, -0x1f8(%rbp)
movb $0x0, -0x99(%rbp)
leaq -0x9a(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x73e0
movq -0x1f8(%rbp), %rsi
movq -0x1f0(%rbp), %rcx
leaq -0x98(%rbp), %rdi
leaq -0x99(%rbp), %rdx
callq 0x7400
jmp 0xac2f
leaq -0x9a(%rbp), %rdi
callq 0x7480
movq $0x0, -0xa8(%rbp)
movq -0x1c8(%rbp), %rcx
movq -0xa8(%rbp), %rax
movq (%rcx), %rcx
cmpq 0x30(%rcx), %rax
jae 0xad9e
movq -0x18(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0x5f90
movl %eax, -0x1fc(%rbp)
jmp 0xac78
movq -0x20(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0x5f90
movl %eax, -0x200(%rbp)
jmp 0xac8f
movl -0x1fc(%rbp), %eax
movl -0x200(%rbp), %ecx
cmpl %ecx, %eax
jne 0xad85
movq -0x18(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0x5f90
movl %eax, -0x204(%rbp)
jmp 0xacba
movq -0xa8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x204(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
movl -0xa8(%rbp), %esi
callq 0x5f90
movl %eax, -0x208(%rbp)
jmp 0xace9
movl -0x208(%rbp), %eax
movslq %eax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x218(%rbp)
movq %rax, -0x210(%rbp)
jmp 0xad0e
movq -0x218(%rbp), %rax
movq -0x210(%rbp), %rcx
movq %rcx, -0xb8(%rbp)
movq %rax, -0xb0(%rbp)
leaq -0xb8(%rbp), %rdi
movl $0x1, %esi
callq 0x75d0
jmp 0xad85
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x5d(%rbp), %rdi
callq 0x2cb0
jmp 0xb4df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x9a(%rbp), %rdi
callq 0x7480
jmp 0xb4c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xb4b8
jmp 0xad87
movq -0xa8(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
jmp 0xac46
leaq -0xd0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x4cc0
jmp 0xadb0
leaq -0xf8(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xb960
jmp 0xadc5
movq -0x1c8(%rbp), %rax
movq $0x0, -0x100(%rbp)
movq (%rax), %rcx
movq 0x30(%rcx), %xmm0
movaps 0x429a(%rip), %xmm1     # 0xf080
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x429e(%rip), %xmm1     # 0xf090
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x445f(%rip), %xmm1     # 0xf268
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movl %ecx, -0x104(%rbp)
movq (%rax), %rax
movl 0x30(%rax), %edx
decl %edx
leaq -0x10c(%rbp), %rdi
xorl %esi, %esi
callq 0x7390
jmp 0xae2f
movl $0x0, -0x110(%rbp)
movl -0x110(%rbp), %eax
cmpl -0x104(%rbp), %eax
jge 0xb45c
leaq -0x58(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x4070
jmp 0xae5d
leaq -0x98(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xbae0
jmp 0xae72
movq $0x0, -0x118(%rbp)
movq -0x1c8(%rbp), %rcx
movq -0x118(%rbp), %rax
movq (%rcx), %rcx
cmpq 0x30(%rcx), %rax
jae 0xb2ec
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
cmpl $-0x1, (%rax)
jne 0xb2d3
movq -0x18(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x21c(%rbp)
jmp 0xaec8
movl -0x21c(%rbp), %eax
movslq %eax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x230(%rbp)
movq %rax, -0x228(%rbp)
jmp 0xaeed
movq -0x230(%rbp), %rax
movq -0x228(%rbp), %rcx
movq %rcx, -0x128(%rbp)
movq %rax, -0x120(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x7530
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x231(%rbp)
jne 0xaf90
movq -0x20(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x238(%rbp)
jmp 0xaf3b
movl -0x238(%rbp), %eax
movslq %eax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x248(%rbp)
movq %rax, -0x240(%rbp)
jmp 0xaf60
movq -0x248(%rbp), %rax
movq -0x240(%rbp), %rcx
movq %rcx, -0x138(%rbp)
movq %rax, -0x130(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x7530
xorb $-0x1, %al
movb %al, -0x231(%rbp)
movb -0x231(%rbp), %al
testb $0x1, %al
jne 0xaf9f
jmp 0xb066
movq -0x1c8(%rbp), %rsi
subq $-0x80, %rsi
leaq -0x38(%rbp), %rdi
callq 0xb930
movsd %xmm0, -0x250(%rbp)
jmp 0xafbd
movsd -0x250(%rbp), %xmm0
movsd %xmm0, -0x140(%rbp)
movsd 0x428b(%rip), %xmm0     # 0xf260
ucomisd -0x140(%rbp), %xmm0
jbe 0xb032
movq -0x18(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x254(%rbp)
jmp 0xaff6
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x254(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xb061
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xb4ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xb4a0
movq -0x20(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x258(%rbp)
jmp 0xb049
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x258(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xb277
movq -0x18(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x25c(%rbp)
jmp 0xb07d
movl -0x25c(%rbp), %eax
movslq %eax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x270(%rbp)
movq %rax, -0x268(%rbp)
jmp 0xb0a2
movq -0x270(%rbp), %rax
movq -0x268(%rbp), %rcx
movq %rcx, -0x150(%rbp)
movq %rax, -0x148(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x7530
xorb $-0x1, %al
testb $0x1, %al
jne 0xb0d2
jmp 0xb106
movq -0x18(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x274(%rbp)
jmp 0xb0e9
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x274(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xb275
movq -0x20(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x278(%rbp)
jmp 0xb11d
movl -0x278(%rbp), %eax
movslq %eax, %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x288(%rbp)
movq %rax, -0x280(%rbp)
jmp 0xb142
movq -0x288(%rbp), %rax
movq -0x280(%rbp), %rcx
movq %rcx, -0x160(%rbp)
movq %rax, -0x158(%rbp)
leaq -0x160(%rbp), %rdi
callq 0x7530
xorb $-0x1, %al
testb $0x1, %al
jne 0xb172
jmp 0xb1a6
movq -0x20(%rbp), %rdi
movl -0x118(%rbp), %esi
callq 0x5f90
movl %eax, -0x28c(%rbp)
jmp 0xb189
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x28c(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xb273
movq -0x1c8(%rbp), %rsi
subq $-0x80, %rsi
leaq -0x10c(%rbp), %rdi
callq 0x74a0
movl %eax, -0x290(%rbp)
jmp 0xb1c5
movl -0x290(%rbp), %eax
movl %eax, -0x164(%rbp)
movslq -0x164(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x2a0(%rbp)
movq %rax, -0x298(%rbp)
jmp 0xb1f4
movq -0x2a0(%rbp), %rax
movq -0x298(%rbp), %rcx
movq %rcx, -0x178(%rbp)
movq %rax, -0x170(%rbp)
leaq -0x178(%rbp), %rdi
callq 0x7530
testb $0x1, %al
jne 0xb222
jmp 0xb24f
movq -0x1c8(%rbp), %rsi
subq $-0x80, %rsi
leaq -0x10c(%rbp), %rdi
callq 0x74a0
movl %eax, -0x2a4(%rbp)
jmp 0xb241
movl -0x2a4(%rbp), %eax
movl %eax, -0x164(%rbp)
jmp 0xb1d1
movl -0x164(%rbp), %eax
movl %eax, -0x2a8(%rbp)
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movl -0x2a8(%rbp), %ecx
movl %ecx, (%rax)
jmp 0xb275
jmp 0xb277
movq -0x118(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x2cf0
movslq (%rax), %rsi
leaq -0x98(%rbp), %rdi
callq 0x74d0
movq %rdx, -0x2b8(%rbp)
movq %rax, -0x2b0(%rbp)
jmp 0xb2a6
movq -0x2b8(%rbp), %rax
movq -0x2b0(%rbp), %rcx
movq %rcx, -0x188(%rbp)
movq %rax, -0x180(%rbp)
leaq -0x188(%rbp), %rdi
movl $0x1, %esi
callq 0x75d0
jmp 0xb2d5
movq -0x118(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
jmp 0xae7d
leaq -0x1c0(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x4cc0
jmp 0xb2fe
leaq -0x1a8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x5ed0
jmp 0xb313
leaq -0x1c0(%rbp), %rdi
callq 0x2c50
movq -0x28(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x6530
movl %eax, -0x2bc(%rbp)
jmp 0xb337
movl -0x2bc(%rbp), %esi
leaq -0x1a8(%rbp), %rdi
callq 0x5fe0
jmp 0xb34b
cmpq $0x0, -0x100(%rbp)
jne 0xb3ee
movl $0x20, %edi
callq 0x21b0
movq %rax, -0x2c8(%rbp)
jmp 0xb36c
movq -0x2c8(%rbp), %rdi
xorl %esi, %esi
callq 0x5e60
jmp 0xb37c
movq -0x2c8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x9880
jmp 0xb39f
jmp 0xb42e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x2c50
jmp 0xb4a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xb44e
movq -0x2c8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
movl $0x20, %esi
callq 0x21c0
jmp 0xb44e
movq -0x100(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x6080
movb %al, -0x2c9(%rbp)
jmp 0xb409
movb -0x2c9(%rbp), %al
testb $0x1, %al
jne 0xb415
jmp 0xb42c
movq -0x100(%rbp), %rdi
leaq -0x1a8(%rbp), %rsi
callq 0x9880
jmp 0xb42a
jmp 0xb42c
jmp 0xb42e
leaq -0x1a8(%rbp), %rdi
callq 0x5840
movl -0x110(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x110(%rbp)
jmp 0xae39
leaq -0x1a8(%rbp), %rdi
callq 0x5840
jmp 0xb4a0
movq -0x1d8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x8c00
jmp 0xb471
leaq -0xf8(%rbp), %rdi
callq 0x7690
leaq -0xd0(%rbp), %rdi
callq 0x2c50
leaq -0x98(%rbp), %rdi
callq 0x7690
leaq -0x58(%rbp), %rdi
callq 0x2c50
jmp 0xb4cf
leaq -0xf8(%rbp), %rdi
callq 0x7690
leaq -0xd0(%rbp), %rdi
callq 0x2c50
leaq -0x98(%rbp), %rdi
callq 0x7690
leaq -0x58(%rbp), %rdi
callq 0x2c50
jmp 0xb4df
movq -0x1d0(%rbp), %rax
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x22d0
nopl (%rax,%rax)