// File: /workspace/github_cpp_makefile_star2_O1/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
movq $0x0, 0x40(%rdi)
movq 0x60(%rdi), %rax
subq 0x58(%rdi), %rax
sarq $0x2, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rcx # imm = 0x28F5C28F5C28F5C3
mulq %rcx
shrq $0x2, %rdx
movq %rdx, 0x48(%rsp)
leaq 0xa0(%rdi), %rsi
addq $0xd0, %rdi
movq %rdi, 0x70(%rsp)
movq %rsi, 0x78(%rsp)
callq 0x52ee
leaq 0xb8(%rbx), %rsi
leaq 0xe8(%rbx), %rdi
movq %rdi, 0x60(%rsp)
movq %rsi, 0x68(%rsp)
callq 0x52ee
leaq 0x100(%rbx), %rsi
leaq 0x118(%rbx), %rdi
movq %rdi, 0x50(%rsp)
movq %rsi, 0x58(%rsp)
callq 0x5466
movq $0x0, 0x18(%rsp)
leaq 0x20(%rsp), %r15
movq $0x0, 0x40(%rbx)
xorl %eax, %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x48(%rsp)
jle 0x4a9b
xorl %r13d, %r13d
movl %r13d, 0x10(%rsp)
movq 0x60(%rbx), %rsi
subq 0x58(%rbx), %rsi
shrq $0x2, %rsi
movq %rbx, %rdi
callq 0x55c4
movl %eax, %r14d
movl 0x387b9(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x55c4
movl %eax, %ebp
movslq %r14d, %r12
leaq (,%r12,4), %r14
movq 0x88(%rbx), %rsi
addq %r14, %rsi
leaq 0x38917(%rip), %r13     # 0x3d0b8
movq %r13, %rdi
callq 0x3a8a
movsd (%rax), %xmm0
mulsd 0x3883(%rip), %xmm0     # 0x8038
movsd %xmm0, 0x8(%rsp)
movq 0x88(%rbx), %rsi
addq %r14, %rsi
movq %r13, %rdi
callq 0x3a8a
movsd (%rax), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x88(%rbx), %rsi
addq %r14, %rsi
leaq 0x3889d(%rip), %rdi     # 0x3d088
callq 0x3a8a
cmpl $0x0, 0x38(%rbx)
movsd 0x3844(%rip), %xmm0     # 0x8040
je 0x4815
movsd 0x80(%rsp), %xmm1
addsd (%rax), %xmm1
movsd 0x8(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x2080
cltq
imulq $0x10624dd3, %rax, %rcx # imm = 0x10624DD3
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x26, %rcx
addl %edx, %ecx
imull $0x3e8, %ecx, %ecx      # imm = 0x3E8
subl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x48ce
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, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x41ca
movq 0x10(%rax), %rdx
addq $0x8, %rax
movq %rax, %rcx
testq %rdx, %rdx
je 0x48a5
movq %rax, %rcx
xorl %esi, %esi
cmpl %ebp, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x488e
movq %rax, %rdx
cmpq %rax, %rcx
je 0x48b7
cmpl 0x20(%rcx), %ebp
cmovlq %rax, %rcx
movq %rcx, %rdx
cmpq %rax, %rdx
je 0x4918
movl 0x38672(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x55c4
movl %eax, %ebp
jmp 0x4851
movq 0x88(%rbx), %rax
movl (%rax,%r12,4), %eax
shlq $0x20, %rax
movl %ebp, %ecx
orq %rax, %rcx
movq %rcx, 0x20(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x41ca
movq 0x70(%rbx), %rsi
addq %r14, %rsi
movq %rax, %rdi
callq 0x5608
testq %rax, %rax
je 0x4941
movl 0x38628(%rip), %esi     # 0x3cf34
movq %rbx, %rdi
callq 0x55c4
movl %eax, %ebp
jmp 0x48ce
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 %ecx, (%rsp)
movq %rbx, %rdi
movl %esi, %r8d
movl %ebp, %r9d
jmp 0x4968
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 %ecx, (%rsp)
movq %rbx, %rdi
movl %ebp, %r8d
movl %edx, %r9d
callq 0x5652
movl 0x10(%rsp), %r13d
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xd0(%rbx), %rsi
movq %rbx, %rdi
callq 0x460e
movq 0x58(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x460e
movq 0x70(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movq %rbx, %rdi
callq 0x460e
movslq %ebp, %rax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r14
movq 0xe8(%rbx), %rsi
addq %r14, %rsi
movq %rbx, %rdi
callq 0x460e
movq 0x58(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x5722
movq 0x70(%rbx), %rax
movq 0x88(%rbx), %rcx
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rsi
shlq $0x3, %rsi
addq 0xe8(%rbx), %rsi
movslq (%rcx,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
callq 0x5722
addq 0xe8(%rbx), %r14
movq 0x88(%rbx), %rax
movslq (%rax,%r12,4), %rax
leaq (%rax,%rax,2), %rdx
shlq $0x3, %rdx
addq 0x118(%rbx), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5722
incl %r13d
cmpl 0x48(%rsp), %r13d
jne 0x4759
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x52ee
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x52ee
movq 0x58(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x5466
movl 0x44(%rsp), %eax
incl %eax
cmpl $0x64, %eax
jne 0x4747
movl $0x6, %edx
movq 0x74f5(%rip), %r14      # 0xbfd8
movq %r14, %rdi
leaq 0x36e8(%rip), %rsi      # 0x81d5
callq 0x21f0
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x22d0
movb $0x20, 0x20(%rsp)
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x21f0
movsd 0x40(%rbx), %xmm0
movq %rax, %rdi
callq 0x22a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2110
movq %r15, %rdi
leaq 0x3687(%rip), %rsi      # 0x81dc
leaq 0x766c(%rip), %r14      # 0xc1c8
movq %r14, %rdx
callq 0x584a
movq 0x20(%rsp), %rdi
leaq 0x367a(%rip), %r13      # 0x81ea
movq %r13, %rsi
callq 0x2200
movq %rax, 0x10(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %r12
cmpq %r12, %rdi
je 0x4b99
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r15, %rdi
leaq 0x3649(%rip), %rsi      # 0x81ec
movq %r14, %rdx
callq 0x584a
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2200
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
leaq 0x362f(%rip), %rbp      # 0x81fb
je 0x4bdb
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
movq %r15, %rdi
leaq 0x3613(%rip), %rsi      # 0x81f8
movq %r14, %rdx
callq 0x584a
movq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x2200
movq %rax, %r14
movq 0x20(%rsp), %rdi
cmpq %r12, %rdi
je 0x4c14
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x21d0
cmpl $0x0, 0x38315(%rip)     # 0x3cf30
movq 0x10(%rsp), %r15
jle 0x4c78
xorl %r12d, %r12d
cmpl $0x0, 0x34(%rbx)
jle 0x4c5c
xorl %r13d, %r13d
movq 0xa0(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %r15, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x2240
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x4c2e
movl $0xa, %edi
movq %r15, %rsi
callq 0x2220
incq %r12
movslq 0x382bd(%rip), %rax     # 0x3cf30
cmpq %rax, %r12
jl 0x4c25
cmpl $0x0, 0x382b5(%rip)     # 0x3cf34
movq 0x8(%rsp), %r15
jle 0x4cdc
xorl %r12d, %r12d
cmpl $0x0, 0x30(%rbx)
jle 0x4cc0
xorl %r13d, %r13d
movq 0xb8(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %r15, %rdi
movq %rbp, %rsi
movb $0x1, %al
callq 0x2240
incq %r13
movslq 0x30(%rbx), %rax
cmpq %rax, %r13
jl 0x4c92
movl $0xa, %edi
movq %r15, %rsi
callq 0x2220
incq %r12
movslq 0x3825d(%rip), %rax     # 0x3cf34
cmpq %rax, %r12
jl 0x4c89
cmpl $0x0, 0x3824d(%rip)     # 0x3cf30
jle 0x4d69
xorl %r12d, %r12d
cmpl $0x0, 0x30(%rbx)
jle 0x4d5a
xorl %ebp, %ebp
cmpl $0x0, 0x34(%rbx)
leaq 0x34fc(%rip), %r15      # 0x81fb
jle 0x4d41
xorl %r13d, %r13d
movq 0x100(%rbx), %rax
leaq (%r12,%r12,2), %rcx
movq (%rax,%rcx,8), %rax
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movq (%rax,%rcx,8), %rax
movsd (%rax,%r13,8), %xmm0
movq %r14, %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x2240
incq %r13
movslq 0x34(%rbx), %rax
cmpq %rax, %r13
jl 0x4d04
movl $0xa, %edi
movq %r14, %rsi
callq 0x2220
incq %rbp
movslq 0x30(%rbx), %rax
cmpq %rax, %rbp
jl 0x4cf4
incq %r12
movslq 0x381cc(%rip), %rax     # 0x3cf30
cmpq %rax, %r12
jl 0x4cec
movq 0x10(%rsp), %rdi
callq 0x21b0
movq 0x8(%rsp), %rdi
callq 0x21b0
movq %r14, %rdi
callq 0x21b0
movq 0x18(%rsp), %rax
incl %eax
movq %rax, 0x18(%rsp)
cmpl $0x3e8, %eax            # imm = 0x3E8
leaq 0x20(%rsp), %r15
jne 0x473d
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop