// File: /workspace/github_cpp_makefile_star2_O2/aleperno[P]Datos/HashingPerceptron.cpp
// Function: HashingPerceptron::Initialize()
void HashingPerceptron::Initialize()
{

	cout << "Began to balance perceptron's weights" << endl;
	printf("\nIterations \t Errors \t Average \t Time \n");
	int iter_count = 1;
	int rev_counter = 0;
	clock_t inicio = clock();
	int min_error = -1;
	int min_count = 0;
	vector<float> aux_weights;
	while (true)
	{
		int error_count = 0;
		rev_counter = 0;
		pair<int,vector<unsigned int>> elem;
		BOOST_FOREACH(elem,this->test_vec)
		{
			rev_counter++;
			float aux = dot_product(elem.second);
			bool result = aux > this->threshold;
			//int error = elem.second.first - result;
			int error = elem.first - result;
			if (error != 0)
			{
				int pos;
				error_count++;
				BOOST_FOREACH(pos, elem.second)
				{
					this->weights[pos] += learning_rate * error;
				}
			}
		}
		//Imprimo los resultados parciales del balanceo
		double final = clock();
		double secs = double(final - inicio) / CLOCKS_PER_SEC;
		float min = floor(secs/60);
		float sec = secs - min*60;
		printf("%d\t\t%d\t\t%0.5f\t\t%.0f:%.3f\n",iter_count,error_count, 1.0 - (double)error_count/rev_counter,min,sec);
		fflush(stdout);
		//Veo si el error es el minimo, guardo el hiperplano
		if (error_count <= min_error || min_error == -1)
		{
			if (error_count == min_error)
				min_count++;
			else
				min_count = 0;
			min_error = error_count;
			aux_weights = this->weights;
		}
		//Controlo el corte
		if (iter_count == ITERATION_LIMIT)
		{
			cout << "Reached Iteration Limit" << endl;
			break;
		}
		if (error_count == 0) break;
		if (min_count == 5)
		{
			cout << "Reached Iteration Limit with minimum error" << endl;
			break;
		}
		iter_count++;
	}
	cout << "Finished perceptron balance" << endl;
	//Guardo el hiperplano de minimo error
	cout << "Minimum error was " << min_error << endl;
	this->weights = aux_weights;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r14
movq 0x771c(%rip), %rdi      # 0xdfd8
leaq 0x28ff(%rip), %rsi      # 0x91c2
callq 0x3220
movq %rax, %rdi
callq 0x3140
leaq 0x2a08(%rip), %rdi      # 0x92df
callq 0x3370
callq 0x3190
leaq -0x80(%rbp), %rcx
andq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq -0x98(%rbp), %r13
leaq 0x38(%r14), %rcx
movq %rcx, -0x58(%rbp)
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x60(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x2c(%rbp)
pushq $-0x1
popq %r12
leaq -0xb8(%rbp), %rbx
movl $0x0, -0x30(%rbp)
movl %r12d, -0x4c(%rbp)
andl $0x0, -0xa0(%rbp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r13)
andq $0x0, 0x10(%r13)
movq 0x88(%r14), %rsi
movq 0x90(%r14), %rax
movq %rax, -0x68(%rbp)
movb $0x1, %al
movl $0x0, -0x34(%rbp)
xorl %r12d, %r12d
testb $0x1, %al
je 0x6a31
cmpq -0x68(%rbp), %rsi
je 0x6a31
leaq -0xa0(%rbp), %rdi
movq %rsi, -0x40(%rbp)
callq 0x71be
leal 0x1(%r12), %eax
movl %eax, -0x50(%rbp)
leal 0x2(%r12), %r15d
xorl %eax, %eax
incl %r12d
cmpl %r12d, %r15d
je 0x6a16
movq %rbx, %rdi
movq %r13, %rsi
callq 0x72b4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6bfa
movss %xmm0, -0x48(%rbp)
movq %rbx, %rdi
callq 0x75b8
xorl %ecx, %ecx
movss -0x48(%rbp), %xmm0
ucomiss 0x54(%r14), %xmm0
seta %cl
movl -0xa0(%rbp), %edx
movb $0x1, %al
subl %ecx, %edx
je 0x698a
incl -0x34(%rbp)
movq -0x98(%rbp), %rax
movq -0x90(%rbp), %rcx
cvtsi2ss %edx, %xmm0
cmpq %rcx, %rax
je 0x6a0f
movslq (%rax), %rdx
movq 0x38(%r14), %rsi
movss 0x50(%r14), %xmm1
mulss %xmm0, %xmm1
addss (%rsi,%rdx,4), %xmm1
movss %xmm1, (%rsi,%rdx,4)
addq $0x4, %rax
jmp 0x69e9
movb $0x1, %al
jmp 0x698a
movzbl %al, %ecx
andl $0x1, %ecx
shll $0x5, %ecx
movq -0x40(%rbp), %rsi
addq %rcx, %rsi
movl -0x50(%rbp), %ecx
movl %ecx, %r12d
jmp 0x6959
cvtsi2sd %r12d, %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x3190
cvtsi2sd %rax, %xmm0
subsd -0x60(%rbp), %xmm0
divsd 0x26fe(%rip), %xmm0     # 0x9150
movsd %xmm0, -0x40(%rbp)
divsd 0x26f9(%rip), %xmm0     # 0x9158
callq 0x33b0
cvtsd2ss %xmm0, %xmm0
cvtss2sd %xmm0, %xmm1
mulss 0x26fc(%rip), %xmm0     # 0x9170
cvtss2sd %xmm0, %xmm0
addsd -0x40(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm2
movl -0x34(%rbp), %r15d
cvtsi2sd %r15d, %xmm3
divsd -0x48(%rbp), %xmm3
movsd 0x26c9(%rip), %xmm0     # 0x9160
subsd %xmm3, %xmm0
cvtss2sd %xmm2, %xmm2
leaq 0x2742(%rip), %rdi      # 0x91e8
movl -0x2c(%rbp), %esi
movl %r15d, %edx
movb $0x3, %al
callq 0x3050
movq 0x7506(%rip), %rax      # 0xdfc0
movq (%rax), %rdi
callq 0x32a0
movl -0x4c(%rbp), %r12d
cmpl %r12d, %r15d
setg %al
cmpl $-0x1, %r12d
setne %cl
testb %al, %cl
jne 0x6afa
movl -0x30(%rbp), %ecx
incl %ecx
cmpl %r12d, %r15d
movl $0x0, %eax
cmovnel %eax, %ecx
movl %ecx, -0x30(%rbp)
leaq -0x80(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x730c
movl %r15d, %r12d
cmpl $0x96, -0x2c(%rbp)
jne 0x6b27
movq 0x74ce(%rip), %rdi      # 0xdfd8
leaq 0x26f1(%rip), %rsi      # 0x9202
callq 0x3220
movq %rax, %rdi
callq 0x3140
movl $0x96, -0x2c(%rbp)
jmp 0x6b4d
testl %r15d, %r15d
je 0x6b4d
cmpl $0x5, -0x30(%rbp)
jne 0x6b63
movq 0x749f(%rip), %rdi      # 0xdfd8
leaq 0x26da(%rip), %rsi      # 0x921a
callq 0x3220
movq %rax, %rdi
callq 0x3140
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x75b8
testb %r15b, %r15b
jne 0x6921
jmp 0x6b6b
incl -0x2c(%rbp)
movb $0x1, %r15b
jmp 0x6b50
movq 0x7466(%rip), %rdi      # 0xdfd8
leaq 0x26cc(%rip), %rsi      # 0x9245
callq 0x3220
movq %rax, %rdi
callq 0x3140
movq 0x744b(%rip), %rdi      # 0xdfd8
leaq 0x26cd(%rip), %rsi      # 0x9261
callq 0x3220
movq %rax, %rdi
movl %r12d, %esi
callq 0x33d0
movq %rax, %rdi
callq 0x3140
leaq -0x80(%rbp), %rsi
movq -0x58(%rbp), %rdi
callq 0x730c
leaq -0x80(%rbp), %rdi
callq 0x756a
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x6be8
jmp 0x6bdd
jmp 0x6bdd
movq %rax, %rbx
movq %r13, %rdi
callq 0x75b8
leaq -0x80(%rbp), %rdi
callq 0x756a
movq %rbx, %rdi
callq 0x33e0
nop