// File: thunlp[P]KB2E/TransE/Train_TransE.cpp
// Function: Train::bfgs()
void bfgs()
    {
        res=0;
        int nbatches=100;
        int nepoch = 1000;
        int batchsize = fb_h.size()/nbatches;
            for (int epoch=0; epoch<nepoch; epoch++)
            {

            	res=0;
             	for (int batch = 0; batch<nbatches; batch++)
             	{
             		relation_tmp=relation_vec;
            		entity_tmp = entity_vec;
             		for (int k=0; k<batchsize; k++)
             		{
						int i=rand_max(fb_h.size());
						int j=rand_max(entity_num);
						double pr = 1000*right_num[fb_r[i]]/(right_num[fb_r[i]]+left_num[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]);
             		}
		            relation_vec = relation_tmp;
		            entity_vec = entity_tmp;
             	}
                cout<<"epoch:"<<epoch<<' '<<res<<endl;
                FILE* f2 = fopen(("relation2vec."+version).c_str(),"w");
                FILE* f3 = fopen(("entity2vec."+version).c_str(),"w");
                for (int i=0; i<relation_num; i++)
                {
                    for (int ii=0; ii<n; ii++)
                        fprintf(f2,"%.6lf\t",relation_vec[i][ii]);
                    fprintf(f2,"\n");
                }
                for (int i=0; i<entity_num; i++)
                {
                    for (int ii=0; ii<n; ii++)
                        fprintf(f3,"%.6lf\t",entity_vec[i][ii]);
                    fprintf(f3,"\n");
                }
                fclose(f2);
                fclose(f3);
            }
    }

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq 0x150(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rdi)
movl $0x64, 0x14c(%rsp)
movl $0x3e8, 0x148(%rsp) # imm = 0x3E8
addq $0x68, %rdi
callq 0x6a90
movslq 0x14c(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movl %eax, 0x144(%rsp)
movl $0x0, 0x140(%rsp)
movl 0x140(%rsp), %eax
cmpl 0x148(%rsp), %eax
jge 0x8afa
movq 0xa0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movl $0x0, 0x13c(%rsp)
movl 0x13c(%rsp), %eax
cmpl 0x14c(%rsp), %eax
jge 0x87db
movq 0xa0(%rsp), %rdi
movq %rdi, %rsi
addq $0xc8, %rsi
addq $0xf8, %rdi
callq 0x9f10
movq 0xa0(%rsp), %rdi
movq %rdi, %rsi
addq $0xe0, %rsi
addq $0x110, %rdi            # imm = 0x110
callq 0x9f10
movl $0x0, 0x138(%rsp)
movl 0x138(%rsp), %eax
cmpl 0x144(%rsp), %eax
jge 0x8789
movq 0xa0(%rsp), %rdi
addq $0x68, %rdi
callq 0x6a90
movq 0xa0(%rsp), %rdi
movl %eax, %esi
callq 0xa1d0
movq 0xa0(%rsp), %rdi
movl %eax, 0x134(%rsp)
movl 0x42d4c(%rip), %esi     # 0x4afb4
callq 0xa1d0
movq 0xa0(%rsp), %rdi
movl %eax, 0x130(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq %rax, %rsi
leaq 0x42e6e(%rip), %rdi     # 0x4b108
callq 0x4020
movq 0xa0(%rsp), %rdi
movsd 0x9d99(%rip), %xmm0     # 0x12048
mulsd (%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq %rax, %rsi
leaq 0x42e2e(%rip), %rdi     # 0x4b108
callq 0x4020
movq 0xa0(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x90(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq %rax, %rsi
leaq 0x42dc6(%rip), %rdi     # 0x4b0d8
callq 0x4020
movsd 0x90(%rsp), %xmm1
movsd 0x98(%rsp), %xmm0
movq %rax, %rcx
movq 0xa0(%rsp), %rax
addsd (%rcx), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x128(%rsp)
cmpl $0x0, 0x34(%rax)
jne 0x835c
movsd 0x9cfd(%rip), %xmm0     # 0x12050
movsd %xmm0, 0x128(%rsp)
callq 0x2090
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
cvtsi2sd %edx, %xmm1
movsd 0x128(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8503
jmp 0x8382
movq 0xa0(%rsp), %rdi
addq $0x68, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movq %rax, 0x88(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0x88(%rsp), %rdi
movq %rax, %rsi
callq 0x6600
movq 0xa0(%rsp), %rdi
movq %rax, 0x120(%rsp)
leaq 0x120(%rsp), %rsi
callq 0x6510
movq %rax, %rdi
leaq 0x130(%rsp), %rsi
callq 0xa240
cmpq $0x0, %rax
jbe 0x8421
movq 0xa0(%rsp), %rdi
movl 0x42ba4(%rip), %esi     # 0x4afb4
callq 0xa1d0
movl %eax, 0x130(%rsp)
jmp 0x8382
movq 0xa0(%rsp), %rdi
addq $0x68, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x74(%rsp)
addq $0x80, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x78(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x7c(%rsp)
addq $0x68, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x80(%rsp)
movl 0x130(%rsp), %eax
movl %eax, 0x84(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl 0x74(%rsp), %esi
movl 0x78(%rsp), %edx
movl 0x7c(%rsp), %ecx
movl 0x80(%rsp), %r8d
movl 0x84(%rsp), %r9d
movl (%rax), %eax
movl %eax, (%rsp)
callq 0xa2a0
jmp 0x8675
jmp 0x8505
movq 0xa0(%rsp), %rdi
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
callq 0x6600
movq 0xa0(%rsp), %rdi
movq %rax, 0x118(%rsp)
leaq 0x118(%rsp), %rsi
callq 0x6510
movq 0xa0(%rsp), %rdi
movq %rax, 0x68(%rsp)
addq $0x80, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0x68(%rsp), %rdi
movq %rax, %rsi
callq 0xa240
cmpq $0x0, %rax
jbe 0x85a1
movq 0xa0(%rsp), %rdi
movl 0x42a24(%rip), %esi     # 0x4afb4
callq 0xa1d0
movl %eax, 0x130(%rsp)
jmp 0x8505
movq 0xa0(%rsp), %rdi
addq $0x68, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x54(%rsp)
addq $0x80, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x58(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x5c(%rsp)
movl 0x130(%rsp), %eax
movl %eax, 0x60(%rsp)
addq $0x80, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x64(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0xa0(%rsp), %rdi
movl 0x54(%rsp), %esi
movl 0x58(%rsp), %edx
movl 0x5c(%rsp), %ecx
movl 0x60(%rsp), %r8d
movl 0x64(%rsp), %r9d
movl (%rax), %eax
movl %eax, (%rsp)
callq 0xa2a0
movq 0xa0(%rsp), %rdi
movq %rdi, %rax
addq $0xf8, %rax
movq %rax, 0x38(%rsp)
addq $0x98, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0x38(%rsp), %rdi
movslq (%rax), %rsi
callq 0x7fb0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x8090
movq 0xa0(%rsp), %rdi
movq %rdi, %rax
addq $0x110, %rax            # imm = 0x110
movq %rax, 0x40(%rsp)
addq $0x68, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0x40(%rsp), %rdi
movslq (%rax), %rsi
callq 0x7fb0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x8090
movq 0xa0(%rsp), %rdi
movq %rdi, %rax
addq $0x110, %rax            # imm = 0x110
movq %rax, 0x48(%rsp)
addq $0x80, %rdi
movslq 0x134(%rsp), %rsi
callq 0xa220
movq 0x48(%rsp), %rdi
movslq (%rax), %rsi
callq 0x7fb0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x8090
movq 0xa0(%rsp), %rdi
addq $0x110, %rdi            # imm = 0x110
movslq 0x130(%rsp), %rsi
callq 0x7fb0
movq 0xa0(%rsp), %rdi
movq %rax, %rsi
callq 0x8090
movl 0x138(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x138(%rsp)
jmp 0x821f
movq 0xa0(%rsp), %rdi
movq %rdi, %rsi
addq $0xf8, %rsi
addq $0xc8, %rdi
callq 0x9f10
movq 0xa0(%rsp), %rdi
movq %rdi, %rsi
addq $0x110, %rsi            # imm = 0x110
addq $0xe0, %rdi
callq 0x9f10
movl 0x13c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x13c(%rsp)
jmp 0x81c4
movq 0x117f6(%rip), %rdi     # 0x19fd8
leaq 0x9991(%rip), %rsi      # 0x1217a
callq 0x2200
movq %rax, %rdi
movl 0x140(%rsp), %esi
callq 0x2390
movq %rax, %rdi
movl $0x20, %esi
callq 0x22a0
movq %rax, %rdi
movq 0xa0(%rsp), %rax
movsd 0x38(%rax), %xmm0
callq 0x23f0
movq %rax, %rdi
movq 0x11797(%rip), %rsi     # 0x19fc0
callq 0x2250
leaq 0x994c(%rip), %rsi      # 0x12181
leaq 0x11a0c(%rip), %rdx     # 0x1a248
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xa360
movq 0x28(%rsp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0x992d(%rip), %rsi      # 0x1218f
callq 0x22b0
movq %rax, 0x30(%rsp)
jmp 0x886e
leaq 0xf0(%rsp), %rdi
callq 0x2428
movq 0x30(%rsp), %rax
movq %rax, 0x110(%rsp)
leaq 0x9902(%rip), %rsi      # 0x12191
leaq 0x119b2(%rip), %rdx     # 0x1a248
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0xa360
movq 0x18(%rsp), %rdi
callq 0x20e0
movq %rax, %rdi
leaq 0x98d3(%rip), %rsi      # 0x1218f
callq 0x22b0
movq %rax, 0x20(%rsp)
jmp 0x88c8
leaq 0xb8(%rsp), %rdi
callq 0x2428
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
cmpl 0x426b6(%rip), %eax     # 0x4afb0
jge 0x89fe
movl $0x0, 0xb0(%rsp)
movq 0xa0(%rsp), %rcx
movl 0xb0(%rsp), %eax
cmpl 0x30(%rcx), %eax
jge 0x89d2
movq 0xa0(%rsp), %rdi
movq 0x110(%rsp), %rax
movq %rax, 0x10(%rsp)
addq $0xc8, %rdi
movslq 0xb4(%rsp), %rsi
callq 0x7fb0
movq %rax, %rdi
movslq 0xb0(%rsp), %rsi
callq 0x37d0
movq 0x10(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0x9831(%rip), %rsi      # 0x1219d
movb $0x1, %al
callq 0x2310
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x890b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x2428
jmp 0x8b02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe8(%rsp)
movl %eax, 0xe4(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2428
jmp 0x8b02
movq 0x110(%rsp), %rdi
leaq 0x9717(%rip), %rsi      # 0x120f8
movb $0x0, %al
callq 0x2310
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x88ed
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0x4259e(%rip), %eax     # 0x4afb4
jge 0x8aca
movl $0x0, 0xa8(%rsp)
movq 0xa0(%rsp), %rcx
movl 0xa8(%rsp), %eax
cmpl 0x30(%rcx), %eax
jge 0x8a9e
movq 0xa0(%rsp), %rdi
movq 0xd8(%rsp), %rax
movq %rax, 0x8(%rsp)
addq $0xe0, %rdi
movslq 0xac(%rsp), %rsi
callq 0x7fb0
movq %rax, %rdi
movslq 0xa8(%rsp), %rsi
callq 0x37d0
movq 0x8(%rsp), %rdi
movsd (%rax), %xmm0
leaq 0x9719(%rip), %rsi      # 0x1219d
movb $0x1, %al
callq 0x2310
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x8a27
movq 0xd8(%rsp), %rdi
leaq 0x964b(%rip), %rsi      # 0x120f8
movb $0x0, %al
callq 0x2310
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x8a09
movq 0x110(%rsp), %rdi
callq 0x2210
movq 0xd8(%rsp), %rdi
callq 0x2210
movl 0x140(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x140(%rsp)
jmp 0x8195
addq $0x158, %rsp            # imm = 0x158
retq
movq 0xe8(%rsp), %rdi
callq 0x23a0
nop