// File: /workspace/github_cpp_makefile_star2_O2/thunlp[P]KB2E/TransR/Train_TransR.cpp
// Function: Train::bfgs()
void bfgs()
    {
        res=0;
        int nbatches=100;
        int nepoch = 1000;
        int batchsize = fb_h.size()/nbatches;
        relation_tmp=relation_vec;
        entity_tmp = entity_vec;
        A_tmp = A;
            for (int epoch=0; epoch<nepoch; epoch++)
            {

            	res=0;
             	for (int batch = 0; batch<nbatches; batch++)
             	{
             		for (int k=0; k<batchsize; k++)
             		{
             			int i=rand_max(fb_h.size());
						int j=rand_max(entity_num);
						double pr = 1000*right_mean[fb_r[i]]/(right_mean[fb_r[i]]+left_mean[fb_r[i]]);
						if (method ==0)
                            pr = 500;
						if (rand()%1000<pr)
						{

							 	while (ok[make_pair(fb_h[i],fb_r[i])].count(j)>0)
									j=rand_max(entity_num);
								train_kb(fb_h[i],fb_l[i],fb_r[i],fb_h[i],j,fb_r[i]);

						}
						else
						{
							while (ok[make_pair(j,fb_r[i])].count(fb_l[i])>0)
								j=rand_max(entity_num);
						 	train_kb(fb_h[i],fb_l[i],fb_r[i],j,fb_l[i],fb_r[i]);
                        }
                		norm(relation_tmp[fb_r[i]]);
                		norm(entity_tmp[fb_h[i]]);
						norm(entity_tmp[fb_l[i]]);
						norm(entity_tmp[j]);
						norm(entity_tmp[fb_h[i]],A_tmp[fb_r[i]]);
						norm(entity_tmp[fb_l[i]],A_tmp[fb_r[i]]);
						norm(entity_tmp[j],A_tmp[fb_r[i]]);
             		}
		            relation_vec = relation_tmp;
		            entity_vec = entity_tmp;
		            A = A_tmp;
             	}
                cout<<"epoch:"<<epoch<<' '<<res<<endl;
                FILE* f1 = fopen(("relation2vec."+version).c_str(),"w");
                FILE* f2 = fopen(("entity2vec."+version).c_str(),"w");
                FILE* f3 = fopen(("A."+version).c_str(),"w");
                for (int i=0; i<relation_num; i++)
                {
                    for (int ii=0; ii<m; ii++)
                        fprintf(f1,"%.6lf\t",relation_vec[i][ii]);
                    fprintf(f1,"\n");
                }
                for (int i=0; i<entity_num; i++)
                {
                    for (int ii=0; ii<n; ii++)
                        fprintf(f2,"%.6lf\t",entity_vec[i][ii]);
                    fprintf(f2,"\n");
                }
                for (int i=0; i<relation_num; i++)
                for (int jj=0; jj<n; jj++)
                {
                    for (int ii=0; ii<m; ii++)
                    {
                        fprintf(f3,"%.6lf\t",A[i][jj][ii]);
                    }
                    fprintf(f3,"\n");
                }
                fclose(f1);
                fclose(f2);
                fclose(f3);
            }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
andq $0x0, 0x40(%rdi)
movq 0x60(%rdi), %rax
subq 0x58(%rdi), %rax
sarq $0x2, %rax
pushq $0x64
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r15
leaq 0xa0(%rdi), %rsi
addq $0xd0, %rdi
movq %rdi, 0x70(%rsp)
movq %rsi, 0x38(%rsp)
callq 0x4dee
leaq 0xb8(%rbx), %rsi
leaq 0xe8(%rbx), %rdi
movq %rdi, 0x68(%rsp)
movq %rsi, 0x30(%rsp)
callq 0x4dee
leaq 0x100(%rbx), %r13
leaq 0x118(%rbx), %rdi
movq %rdi, 0x60(%rsp)
movq %r13, %rsi
callq 0x4ecc
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
movq %r15, 0x78(%rsp)
leaq 0x40(%rsp), %r15
cmpl $0x3e8, %eax            # imm = 0x3E8
je 0x47a0
movq %rax, 0x28(%rsp)
andq $0x0, 0x40(%rbx)
xorl %eax, %eax
cmpl $0x64, %eax
je 0x4576
movl %eax, 0x24(%rsp)
xorl %r14d, %r14d
cmpl 0x78(%rsp), %r14d
je 0x4540
movl %r14d, 0x18(%rsp)
movq 0x60(%rbx), %rsi
subq 0x58(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x4faa
movl %eax, %r12d
movl 0x37cd2(%rip), %esi     # 0x3bf44
movq %rbx, %rdi
callq 0x4faa
movl %eax, 0xc(%rsp)
movslq %r12d, %r12
leaq (,%r12,4), %rbp
movq 0x88(%rbx), %rsi
addq %rbp, %rsi
leaq 0x37e2e(%rip), %r14     # 0x3c0c8
movq %r14, %rdi
callq 0x35b6
movsd (%rax), %xmm0
mulsd 0x2d8a(%rip), %xmm0     # 0x7038
movsd %xmm0, 0x10(%rsp)
movq 0x88(%rbx), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x35b6
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x88(%rbx), %rsi
addq %rbp, %rsi
leaq 0x37db4(%rip), %rdi     # 0x3c098
callq 0x35b6
cmpl $0x0, 0x38(%rbx)
movsd 0x2d4b(%rip), %xmm0     # 0x7040
je 0x430e
movsd 0x80(%rsp), %xmm1
addsd (%rax), %xmm1
movsd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
callq 0x2070
cltd
movl $0x3e8, %ecx            # imm = 0x3E8
idivl %ecx
cvtsi2sd %edx, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
leaq 0xc(%rsp), %r14
jbe 0x4384
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movl (%rax,%r12,4), %eax
movl (%rcx,%r12,4), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, 0x40(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d02
movq %rax, %rdi
movq %r14, %rsi
callq 0x4fee
testq %rax, %rax
je 0x43d9
movl 0x37bce(%rip), %esi     # 0x3bf44
movq %rbx, %rdi
callq 0x4faa
movl %eax, 0xc(%rsp)
jmp 0x4336
movl 0xc(%rsp), %eax
movl 0x18(%rsp), %r14d
movq 0x88(%rbx), %rcx
movl (%rcx,%r12,4), %ecx
shlq $0x20, %rcx
movl %eax, %eax
orq %rcx, %rax
movq %rax, 0x40(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d02
movq 0x70(%rbx), %rsi
addq %rbp, %rsi
movq %rax, %rdi
callq 0x4fee
testq %rax, %rax
je 0x440e
movl 0x37b79(%rip), %esi     # 0x3bf44
movq %rbx, %rdi
callq 0x4faa
movl %eax, 0xc(%rsp)
jmp 0x438d
movq 0x58(%rbx), %rax
movq 0x70(%rbx), %rcx
movl (%rax,%r12,4), %esi
movl (%rcx,%r12,4), %edx
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %ecx
movl 0xc(%rsp), %r9d
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %esi, %r8d
callq 0x5008
movl 0x18(%rsp), %r14d
jmp 0x443c
movq 0x58(%rbx), %rax
movq 0x70(%rbx), %rcx
movl (%rax,%r12,4), %esi
movl (%rcx,%r12,4), %edx
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %ecx
movl 0xc(%rsp), %r8d
movl %ecx, (%rsp)
movq %rbx, %rdi
movl %edx, %r9d
callq 0x5008
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0xd0(%rbx), %rsi
movq %rbx, %rdi
callq 0x4146
movq 0x58(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4146
movq 0x70(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4146
movslq 0xc(%rsp), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x4146
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
imulq $0x18, %rax, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x50b4
movq 0x70(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
imulq $0x18, %rax, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x50b4
movslq 0xc(%rsp), %rax
imulq $0x18, %rax, %rsi
addq 0xe8(%rbx), %rsi
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
imulq $0x18, %rax, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x50b4
incl %r14d
jmp 0x4245
movq 0x38(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x4dee
movq 0x30(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x4dee
movq %r13, %rdi
movq 0x60(%rsp), %rsi
callq 0x4ecc
movl 0x24(%rsp), %eax
incl %eax
jmp 0x4235
movq 0x6a5b(%rip), %rdi      # 0xafd8
leaq 0x2c51(%rip), %rsi      # 0x71d5
callq 0x2190
movq %rax, %rdi
movq 0x28(%rsp), %rsi
callq 0x22c0
movq %rax, %rdi
pushq $0x20
popq %rsi
callq 0x21e0
movsd 0x40(%rbx), %xmm0
movq %rax, %rdi
callq 0x2290
movq %rax, %rdi
callq 0x20c0
movq %r15, %rdi
leaq 0x2c1c(%rip), %rsi      # 0x71dc
leaq 0x6c11(%rip), %r14      # 0xb1d8
movq %r14, %rdx
callq 0x51c4
movq 0x40(%rsp), %rdi
leaq 0x2c0f(%rip), %r12      # 0x71ea
movq %r12, %rsi
callq 0x21f0
movq %rax, 0x18(%rsp)
movq %r15, %rdi
callq 0x2338
movq %r15, %rdi
leaq 0x2bf2(%rip), %rsi      # 0x71ec
movq %r14, %rdx
callq 0x51c4
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x21f0
movq %rax, %rbp
movq %r15, %rdi
callq 0x2338
movq %r15, %rdi
leaq 0x2bd4(%rip), %rsi      # 0x71f8
movq %r14, %rdx
callq 0x51c4
movq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x21f0
movq %rax, %r12
movq %r15, %rdi
callq 0x2338
xorl %r14d, %r14d
movq %rbp, 0x10(%rsp)
movslq 0x378ed(%rip), %rax     # 0x3bf40
cmpq %rax, %r14
jge 0x46ac
imulq $0x18, %r14, %r15
xorl %ebp, %ebp
movslq 0x34(%rbx), %rax
cmpq %rax, %rbp
jge 0x4690
movq 0x38(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15), %rax
movsd (%rax,%rbp,8), %xmm0
movq 0x18(%rsp), %rdi
leaq 0x2b77(%rip), %rsi      # 0x71fb
movb $0x1, %al
callq 0x2240
incq %rbp
jmp 0x465e
pushq $0xa
popq %rdi
movq 0x18(%rsp), %rsi
callq 0x2210
incq %r14
leaq 0x40(%rsp), %r15
movq 0x10(%rsp), %rbp
jmp 0x464c
xorl %r14d, %r14d
movslq 0x3788e(%rip), %rax     # 0x3bf44
cmpq %rax, %r14
jge 0x4708
xorl %r15d, %r15d
movslq 0x30(%rbx), %rax
cmpq %rax, %r15
jge 0x46f3
movq 0x30(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq %rbp, %rdi
leaq 0x2b14(%rip), %rsi      # 0x71fb
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x46be
pushq $0xa
popq %rdi
movq %rbp, %rsi
callq 0x2210
incq %r14
leaq 0x40(%rsp), %r15
jmp 0x46af
xorl %ebp, %ebp
movslq 0x3782f(%rip), %rax     # 0x3bf40
cmpq %rax, %rbp
jge 0x4778
xorl %r14d, %r14d
movslq 0x30(%rbx), %rax
cmpq %rax, %r14
jge 0x4773
xorl %r15d, %r15d
movslq 0x34(%rbx), %rax
cmpq %rax, %r15
jge 0x475e
movq (%r13), %rax
imulq $0x18, %rbp, %rcx
movq (%rax,%rcx), %rax
imulq $0x18, %r14, %rcx
movq (%rax,%rcx), %rax
movsd (%rax,%r15,8), %xmm0
movq %r12, %rdi
leaq 0x2aa9(%rip), %rsi      # 0x71fb
movb $0x1, %al
callq 0x2240
incq %r15
jmp 0x4725
pushq $0xa
popq %rdi
movq %r12, %rsi
callq 0x2210
incq %r14
leaq 0x40(%rsp), %r15
jmp 0x4719
incq %rbp
jmp 0x470a
movq 0x18(%rsp), %rdi
callq 0x21a0
movq 0x10(%rsp), %rdi
callq 0x21a0
movq %r12, %rdi
callq 0x21a0
movq 0x28(%rsp), %rax
incl %eax
jmp 0x421e
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq