// File: 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:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1e0(%rsp)
movq 0x1e0(%rsp), %rdi
movq %rdi, 0xf8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rdi)
movl $0x64, 0x1dc(%rsp)
movl $0x3e8, 0x1d8(%rsp) # imm = 0x3E8
addq $0x58, %rdi
callq 0x4610
movq 0xf8(%rsp), %rdi
movslq 0x1dc(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, 0x1d4(%rsp)
movq %rdi, %rsi
addq $0xa0, %rsi
addq $0xd0, %rdi
callq 0xb040
movq 0xf8(%rsp), %rdi
movq %rdi, %rsi
addq $0xb8, %rsi
addq $0xe8, %rdi
callq 0xb040
movq 0xf8(%rsp), %rdi
movq %rdi, %rsi
addq $0x100, %rsi            # imm = 0x100
addq $0x118, %rdi            # imm = 0x118
callq 0xb300
movl $0x0, 0x1d0(%rsp)
movl 0x1d0(%rsp), %eax
cmpl 0x1d8(%rsp), %eax
jge 0x9139
movq 0xf8(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rax)
movl $0x0, 0x1cc(%rsp)
movl 0x1cc(%rsp), %eax
cmpl 0x1dc(%rsp), %eax
jge 0x8c73
movl $0x0, 0x1c8(%rsp)
movl 0x1c8(%rsp), %eax
cmpl 0x1d4(%rsp), %eax
jge 0x8c03
movq 0xf8(%rsp), %rdi
addq $0x58, %rdi
callq 0x4610
movq 0xf8(%rsp), %rdi
movl %eax, %esi
callq 0xb5c0
movq 0xf8(%rsp), %rdi
movl %eax, 0x1c4(%rsp)
movl 0x48a8e(%rip), %esi     # 0x50fb4
callq 0xb5c0
movq 0xf8(%rsp), %rdi
movl %eax, 0x1c0(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq %rax, %rsi
leaq 0x48be0(%rip), %rdi     # 0x51138
callq 0x4670
movq 0xf8(%rsp), %rdi
movsd 0xdad3(%rip), %xmm0     # 0x16040
mulsd (%rax), %xmm0
movsd %xmm0, 0xf0(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq %rax, %rsi
leaq 0x48ba0(%rip), %rdi     # 0x51138
callq 0x4670
movq 0xf8(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0xe8(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq %rax, %rsi
leaq 0x48b38(%rip), %rdi     # 0x51108
callq 0x4670
movsd 0xe8(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
movq %rax, %rcx
movq 0xf8(%rsp), %rax
addsd (%rcx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x1b8(%rsp)
cmpl $0x0, 0x38(%rax)
jne 0x861a
movsd 0xda37(%rip), %xmm0     # 0x16048
movsd %xmm0, 0x1b8(%rsp)
callq 0x2090
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cvtsi2sd %edx, %xmm1
movsd 0x1b8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x87d0
jmp 0x8640
movq 0xf8(%rsp), %rdi
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movq %rax, 0xe0(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xe0(%rsp), %rdi
movq %rax, %rsi
callq 0x6ff0
movq 0xf8(%rsp), %rdi
movq %rax, 0x1b0(%rsp)
leaq 0x1b0(%rsp), %rsi
callq 0x6f00
movq %rax, %rdi
leaq 0x1c0(%rsp), %rsi
callq 0xb630
cmpq $0x0, %rax
jbe 0x86df
movq 0xf8(%rsp), %rdi
movl 0x488e6(%rip), %esi     # 0x50fb4
callq 0xb5c0
movl %eax, 0x1c0(%rsp)
jmp 0x8640
movq 0xf8(%rsp), %rdi
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xcc(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xd0(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xd4(%rsp)
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xd8(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, 0xdc(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl 0xcc(%rsp), %esi
movl 0xd0(%rsp), %edx
movl 0xd4(%rsp), %ecx
movl 0xd8(%rsp), %r8d
movl 0xdc(%rsp), %r9d
movl (%rax), %eax
movl %eax, (%rsp)
callq 0xb690
jmp 0x895d
jmp 0x87d2
movq 0xf8(%rsp), %rdi
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq %rax, %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x6ff0
movq 0xf8(%rsp), %rdi
movq %rax, 0x1a8(%rsp)
leaq 0x1a8(%rsp), %rsi
callq 0x6f00
movq 0xf8(%rsp), %rdi
movq %rax, 0xc0(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xc0(%rsp), %rdi
movq %rax, %rsi
callq 0xb630
cmpq $0x0, %rax
jbe 0x8871
movq 0xf8(%rsp), %rdi
movl 0x48754(%rip), %esi     # 0x50fb4
callq 0xb5c0
movl %eax, 0x1c0(%rsp)
jmp 0x87d2
movq 0xf8(%rsp), %rdi
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xac(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xb0(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xb4(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, 0xb8(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0xbc(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0xf8(%rsp), %rdi
movl 0xac(%rsp), %esi
movl 0xb0(%rsp), %edx
movl 0xb4(%rsp), %ecx
movl 0xb8(%rsp), %r8d
movl 0xbc(%rsp), %r9d
movl (%rax), %eax
movl %eax, (%rsp)
callq 0xb690
movq 0xf8(%rsp), %rdi
movq %rdi, %rax
addq $0xd0, %rax
movq %rax, 0x50(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x50(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x8330
movq 0xf8(%rsp), %rdi
movq %rdi, %rax
addq $0xe8, %rax
movq %rax, 0x58(%rsp)
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x58(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x8330
movq 0xf8(%rsp), %rdi
movq %rdi, %rax
addq $0xe8, %rax
movq %rax, 0x60(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x60(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x8330
movq 0xf8(%rsp), %rdi
addq $0xe8, %rdi
movslq 0x1c0(%rsp), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, %rsi
callq 0x8330
movq 0xf8(%rsp), %rdi
movq %rdi, %rax
addq $0xe8, %rax
movq %rax, 0x68(%rsp)
addq $0x58, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x68(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, 0x78(%rsp)
movq %rdi, %rax
addq $0x118, %rax            # imm = 0x118
movq %rax, 0x70(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x70(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8190
movq 0x78(%rsp), %rsi
movq 0xf8(%rsp), %rdi
movq %rax, %rdx
callq 0xb760
movq 0xf8(%rsp), %rdi
movq %rdi, %rax
addq $0xe8, %rax
movq %rax, 0x80(%rsp)
addq $0x70, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x80(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, 0x90(%rsp)
movq %rdi, %rax
addq $0x118, %rax            # imm = 0x118
movq %rax, 0x88(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x88(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8190
movq 0x90(%rsp), %rsi
movq 0xf8(%rsp), %rdi
movq %rax, %rdx
callq 0xb760
movq 0xf8(%rsp), %rdi
addq $0xe8, %rdi
movslq 0x1c0(%rsp), %rsi
callq 0x8250
movq 0xf8(%rsp), %rdi
movq %rax, 0xa0(%rsp)
movq %rdi, %rax
addq $0x118, %rax            # imm = 0x118
movq %rax, 0x98(%rsp)
addq $0x88, %rdi
movslq 0x1c4(%rsp), %rsi
callq 0xb610
movq 0x98(%rsp), %rdi
movslq (%rax), %rsi
callq 0x8190
movq 0xf8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %rax, %rdx
callq 0xb760
movl 0x1c8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c8(%rsp)
jmp 0x84dd
movq 0xf8(%rsp), %rdi
movq %rdi, %rsi
addq $0xd0, %rsi
addq $0xa0, %rdi
callq 0xb040
movq 0xf8(%rsp), %rdi
movq %rdi, %rsi
addq $0xe8, %rsi
addq $0xb8, %rdi
callq 0xb040
movq 0xf8(%rsp), %rdi
movq %rdi, %rsi
addq $0x118, %rsi            # imm = 0x118
addq $0x100, %rdi            # imm = 0x100
callq 0xb300
movl 0x1cc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1cc(%rsp)
jmp 0x84be
movq 0x1735e(%rip), %rdi     # 0x1ffd8
leaq 0xd524(%rip), %rsi      # 0x161a5
callq 0x2200
movq %rax, %rdi
movl 0x1d0(%rsp), %esi
callq 0x2390
movq %rax, %rdi
movl $0x20, %esi
callq 0x22a0
movq %rax, %rdi
movq 0xf8(%rsp), %rax
movsd 0x40(%rax), %xmm0
callq 0x23f0
movq %rax, %rdi
movq 0x172ff(%rip), %rsi     # 0x1ffc0
callq 0x2250
leaq 0xd4df(%rip), %rsi      # 0x161ac
leaq 0x17574(%rip), %rdx     # 0x20248
leaq 0x180(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xbaa0
movq 0x40(%rsp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0xd4c0(%rip), %rsi      # 0x161ba
callq 0x22b0
movq %rax, 0x48(%rsp)
jmp 0x8d06
leaq 0x180(%rsp), %rdi
callq 0x2428
movq 0x48(%rsp), %rax
movq %rax, 0x1a0(%rsp)
leaq 0xd495(%rip), %rsi      # 0x161bc
leaq 0x1751a(%rip), %rdx     # 0x20248
leaq 0x148(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xbaa0
movq 0x30(%rsp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0xd466(%rip), %rsi      # 0x161ba
callq 0x22b0
movq %rax, 0x38(%rsp)
jmp 0x8d60
leaq 0x148(%rsp), %rdi
callq 0x2428
movq 0x38(%rsp), %rax
movq %rax, 0x168(%rsp)
leaq 0xd447(%rip), %rsi      # 0x161c8
leaq 0x174c0(%rip), %rdx     # 0x20248
leaq 0x120(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xbaa0
movq 0x20(%rsp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0xd40c(%rip), %rsi      # 0x161ba
callq 0x22b0
movq %rax, 0x28(%rsp)
jmp 0x8dba
leaq 0x120(%rsp), %rdi
callq 0x2428
movq 0x28(%rsp), %rax
movq %rax, 0x140(%rsp)
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
cmpl 0x481c4(%rip), %eax     # 0x50fb0
jge 0x8f16
movl $0x0, 0x118(%rsp)
movq 0xf8(%rsp), %rcx
movl 0x118(%rsp), %eax
cmpl 0x34(%rcx), %eax
jge 0x8eea
movq 0xf8(%rsp), %rdi
movq 0x1a0(%rsp), %rax
movq %rax, 0x18(%rsp)
addq $0xa0, %rdi
movslq 0x11c(%rsp), %rsi
callq 0x8250
movq %rax, %rdi
movslq 0x118(%rsp), %rsi
callq 0x3c80
movq 0x18(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0xd36d(%rip), %rsi      # 0x161cb
movb $0x1, %al
callq 0x2310
movl 0x118(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x118(%rsp)
jmp 0x8dfd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x180(%rsp), %rdi
callq 0x2428
jmp 0x9141
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x2428
jmp 0x9141
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x178(%rsp)
movl %eax, 0x174(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x2428
jmp 0x9141
movq 0x1a0(%rsp), %rdi
leaq 0xd20f(%rip), %rsi      # 0x16108
movb $0x0, %al
callq 0x2310
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x8ddf
movl $0x0, 0x114(%rsp)
movl 0x114(%rsp), %eax
cmpl 0x48086(%rip), %eax     # 0x50fb4
jge 0x8fe2
movl $0x0, 0x110(%rsp)
movq 0xf8(%rsp), %rcx
movl 0x110(%rsp), %eax
cmpl 0x30(%rcx), %eax
jge 0x8fb6
movq 0xf8(%rsp), %rdi
movq 0x168(%rsp), %rax
movq %rax, 0x10(%rsp)
addq $0xb8, %rdi
movslq 0x114(%rsp), %rsi
callq 0x8250
movq %rax, %rdi
movslq 0x110(%rsp), %rsi
callq 0x3c80
movq 0x10(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0xd22f(%rip), %rsi      # 0x161cb
movb $0x1, %al
callq 0x2310
movl 0x110(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x110(%rsp)
jmp 0x8f3f
movq 0x168(%rsp), %rdi
leaq 0xd143(%rip), %rsi      # 0x16108
movb $0x0, %al
callq 0x2310
movl 0x114(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x114(%rsp)
jmp 0x8f21
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
cmpl 0x47fb6(%rip), %eax     # 0x50fb0
jge 0x90fc
movl $0x0, 0x108(%rsp)
movq 0xf8(%rsp), %rcx
movl 0x108(%rsp), %eax
cmpl 0x30(%rcx), %eax
jge 0x90e4
movl $0x0, 0x104(%rsp)
movq 0xf8(%rsp), %rcx
movl 0x104(%rsp), %eax
cmpl 0x34(%rcx), %eax
jge 0x90b8
movq 0xf8(%rsp), %rdi
movq 0x140(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0x100, %rdi            # imm = 0x100
movslq 0x10c(%rsp), %rsi
callq 0x8190
movq %rax, %rdi
movslq 0x108(%rsp), %rsi
callq 0x8250
movq %rax, %rdi
movslq 0x104(%rsp), %rsi
callq 0x3c80
movq 0x8(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0xd130(%rip), %rsi      # 0x161cb
movb $0x1, %al
callq 0x2310
movl 0x104(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x104(%rsp)
jmp 0x902e
movq 0x140(%rsp), %rdi
leaq 0xd041(%rip), %rsi      # 0x16108
movb $0x0, %al
callq 0x2310
movl 0x108(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x108(%rsp)
jmp 0x900b
jmp 0x90e6
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x8fed
movq 0x1a0(%rsp), %rdi
callq 0x2210
movq 0x168(%rsp), %rdi
callq 0x2210
movq 0x140(%rsp), %rdi
callq 0x2210
movl 0x1d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d0(%rsp)
jmp 0x848f
addq $0x1e8, %rsp            # imm = 0x1E8
retq
movq 0x178(%rsp), %rdi
callq 0x23a0
nop