// File: /workspace/github_cpp_makefile_star2_O3/uclaopt[P]AsyncQVI/include/algo.h
// Function: VRQVI::solve()
void solve(){
			srand (time(NULL));
			for(int t = 0; t < params->max_outer_iter; t++){
				
				// max element of v_fix
				v_outer_max = fabs((*v_outer)[0]);
				for(int index = 1; index < params->len_state; index++){
					if(fabs((*v_outer)[index]) > v_outer_max)
						v_outer_max = fabs((*v_outer)[index]);
				}
				
				// compute a coarse estimate of Q
				for(int i = 0; i < params->len_state; i++){
					for(int a = 0; a < params->len_action; a++){
						double v_sum = 0;
						double v_square_sum = 0;
						double r_sum = 0;
						for(int n = 0; n < params->sample_num_1; n++){
							s.SO(i, a, next_state, r);
							v_sum += (*v_outer)[next_state];
							v_square_sum += pow((*v_outer)[next_state],2);
							r_sum += r;
						}
						double v_ave = v_sum / params->sample_num_1;
						double v_square_ave = v_square_sum / params->sample_num_1;
						double r_ave = r_sum / params->sample_num_1;
						(*w)[i][a] = v_ave - sqrt(2*params->alpha1*(v_square_ave-v_ave))
						            - (4*pow(params->alpha1,0.75) + 2/3*params->alpha1)*v_outer_max;
						(*Q)[i][a] = r_ave + params->gamma * (*w)[i][a];
						
					}
				}
				
			    // improve Q 
				for(int k = 0; k < params->max_inner_iter; k++){				
					// compute the estimate of P(v_inner - v_outer)
					for(int i = 0; i < params->len_state; i++){
						// update v and pi
						if((*v_inner)[i] < *max_element(((*Q)[i]).begin(), ((*Q)[i]).end())){
							(*v_inner)[i] = *max_element(((*Q)[i]).begin(), ((*Q)[i]).end());
							(*pi)[i] =  distance(((*Q)[i]).begin(), max_element(((*Q)[i]).begin(), ((*Q)[i]).end()));
						}
					}
				
					for(int i = 0; i < params->len_state; i++){
						for(int a = 0; a < params->len_action; a++){
							double g = 0.;
							for(int n = 0; n < params->sample_num_2; n++){
								s.SO(i, a, next_state, r);
								g += r + params->gamma * ((*v_inner)[next_state]-(*v_outer)[next_state]);
							}
							(*Q)[i][a] = g/params->sample_num_2 -(1-params->gamma)*params->epsilon/8.
							             + params->gamma * (*w)[i][a];					
						}
					}
				}
				// reset parameters. The resetting fashion is tunable
				params->epsilon /= 2.;
				params->sample_num_1 *= 2; 
				params->sample_num_2 *= 2; 
				
				*v_outer = *v_inner;
				if(t % params->check_step==0){
					test_sailing(s, pi, params);
				}
			}
			return;
		}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r15
movq $0x0, 0x28(%rsp)
xorl %edi, %edi
callq 0x21b0
movl %eax, %edi
callq 0x21d0
movq 0x1508(%r15), %rcx
cmpl $0x0, 0x34(%rcx)
jle 0x5216
leaq 0x28(%r15), %r14
leaq 0x8(%r15), %rax
movq %rax, 0x50(%rsp)
leaq 0x10(%r15), %rax
movq %rax, 0x48(%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x38(%rsp)
movq 0x14f0(%r15), %rax
movq (%rax), %rdx
movsd (%rdx), %xmm0
andpd 0x13c5(%rip), %xmm0     # 0x60c0
movlpd %xmm0, 0x20(%r15)
movslq (%rcx), %rax
cmpq $0x2, %rax
jl 0x4d34
movl $0x1, %esi
movsd (%rdx,%rsi,8), %xmm1
andpd 0x13a4(%rip), %xmm1     # 0x60c0
ucomisd %xmm0, %xmm1
jbe 0x4d2c
movsd %xmm1, 0x20(%r15)
movapd %xmm1, %xmm0
incq %rsi
cmpq %rsi, %rax
jne 0x4d0f
testl %eax, %eax
jle 0x4f33
xorl %esi, %esi
cmpl $0x0, 0x4(%rcx)
jle 0x4f24
movq %rsi, 0x40(%rsp)
leaq (,%rsi,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x58(%rsp)
xorl %ebx, %ebx
movq %rbx, 0x30(%rsp)
movl 0x3c(%rcx), %eax
xorpd %xmm0, %xmm0
testl %eax, %eax
jle 0x4e0b
xorl %r15d, %r15d
xorps %xmm2, %xmm2
movq 0x50(%rsp), %r13
movq 0x48(%rsp), %rbp
movq 0x40(%rsp), %r14
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r12
movapd %xmm0, 0x10(%rsp)
movsd %xmm2, (%rsp)
movq 0x38(%rsp), %rdi
movl %r14d, %esi
movl %ebx, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x3c72
movsd (%rsp), %xmm2
movslq 0x8(%r12), %rax
movq 0x14f0(%r12), %rdx
movq 0x1508(%r12), %rcx
movq (%rdx), %rdx
movsd (%rdx,%rax,8), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
addsd 0x10(%r12), %xmm2
movapd 0x10(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd 0x10(%rsp), %xmm0
incl %r15d
movl 0x3c(%rcx), %eax
cmpl %eax, %r15d
jl 0x4d93
jmp 0x4e0f
xorpd %xmm2, %xmm2
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
movapd %xmm4, %xmm1
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
divpd %xmm1, %xmm0
movsd 0x58(%rcx), %xmm1
addsd %xmm1, %xmm1
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
subsd %xmm3, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x1215(%rip), %xmm0     # 0x6058
jb 0x4e4b
sqrtsd %xmm0, %xmm0
jmp 0x4e72
movsd %xmm2, (%rsp)
movapd %xmm3, 0x10(%rsp)
movapd %xmm4, 0x60(%rsp)
callq 0x2150
movapd 0x60(%rsp), %xmm4
movapd 0x10(%rsp), %xmm3
movsd (%rsp), %xmm2
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r15
divsd %xmm4, %xmm2
movsd %xmm2, (%rsp)
subsd %xmm0, %xmm3
movapd %xmm3, 0x10(%rsp)
movq 0x1508(%r15), %rax
movsd 0x58(%rax), %xmm0
movsd 0x119d(%rip), %xmm1     # 0x6040
callq 0x2350
movq 0x14e8(%r15), %rax
movq 0x1508(%r15), %rcx
movsd 0x58(%rcx), %xmm1
mulsd 0x1195(%rip), %xmm1     # 0x6058
mulsd 0x117d(%rip), %xmm0     # 0x6048
addsd %xmm1, %xmm0
mulsd 0x20(%r15), %xmm0
movapd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movq (%rax), %rax
movq 0x58(%rsp), %rdx
movq (%rax,%rdx), %rax
movsd %xmm1, (%rax,%rbx,8)
mulsd 0x18(%rcx), %xmm1
addsd (%rsp), %xmm1
movq 0x14e0(%r15), %rax
movq (%rax), %rax
movq (%rax,%rdx), %rax
movsd %xmm1, (%rax,%rbx,8)
incq %rbx
movslq 0x4(%rcx), %rax
cmpq %rax, %rbx
jl 0x4d60
movl (%rcx), %eax
movq 0x40(%rsp), %rsi
incq %rsi
movslq %eax, %rdx
cmpq %rdx, %rsi
jl 0x4d3e
cmpl $0x0, 0x38(%rcx)
jle 0x519d
xorl %eax, %eax
movl %eax, (%rsp)
movl (%rcx), %r8d
testl %r8d, %r8d
jle 0x518a
movq 0x14e0(%r15), %rdx
movq 0x14f8(%r15), %rax
movq (%rax), %rax
movq (%rdx), %rdx
movq 0x1500(%r15), %rsi
xorl %edi, %edi
movsd (%rax,%rdi,8), %xmm1
leaq (%rdi,%rdi,2), %r10
movq (%rdx,%r10,8), %r9
movq 0x8(%rdx,%r10,8), %r10
cmpq %r10, %r9
sete %bpl
leaq 0x8(%r9), %r11
cmpq %r10, %r11
sete %r14b
orb %bpl, %r14b
movsd (%r9), %xmm0
je 0x4fb0
ucomisd %xmm1, %xmm0
jbe 0x5048
movsd %xmm0, (%rax,%rdi,8)
movq %r9, %r8
jmp 0x5037
movapd %xmm0, %xmm2
movq %r11, %r15
movq %r9, %r14
movsd (%r15), %xmm3
ucomisd %xmm2, %xmm3
cmovaq %r15, %r14
maxsd %xmm2, %xmm3
addq $0x8, %r15
movapd %xmm3, %xmm2
cmpq %r10, %r15
jne 0x4fba
movsd (%r14), %xmm2
ucomisd %xmm1, %xmm2
movq %r11, %r15
movq %r9, %r14
jbe 0x5048
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
cmovaq %r15, %r14
maxsd %xmm0, %xmm1
addq $0x8, %r15
movapd %xmm1, %xmm0
cmpq %r10, %r15
jne 0x4fe9
movsd (%r14), %xmm0
movsd %xmm0, (%rax,%rdi,8)
movsd (%r9), %xmm0
movq %r9, %r8
movsd (%r11), %xmm1
ucomisd %xmm0, %xmm1
cmovaq %r11, %r8
maxsd %xmm0, %xmm1
addq $0x8, %r11
movapd %xmm1, %xmm0
cmpq %r10, %r11
jne 0x5019
subq %r9, %r8
shrq $0x3, %r8
movq (%rsi), %r9
movl %r8d, (%r9,%rdi,4)
movl (%rcx), %r8d
incq %rdi
movslq %r8d, %r9
cmpq %r9, %rdi
jl 0x4f6b
testl %r8d, %r8d
jle 0x518a
xorl %ebx, %ebx
cmpl $0x0, 0x4(%rcx)
jle 0x517b
xorl %r12d, %r12d
movl 0x40(%rcx), %eax
testl %eax, %eax
jle 0x50fa
xorpd %xmm2, %xmm2
xorl %r15d, %r15d
movq 0x50(%rsp), %r13
movq 0x48(%rsp), %rbp
movq 0x8(%rsp), %r14
movsd %xmm2, 0x10(%rsp)
movq 0x38(%rsp), %rdi
movl %ebx, %esi
movl %r12d, %edx
movq %r13, %rcx
movq %rbp, %r8
callq 0x3c72
movsd 0x10(%rsp), %xmm2
movq 0x1508(%r14), %rcx
movq 0x14f0(%r14), %rax
movq 0x14f8(%r14), %rdi
movslq 0x8(%r14), %rsi
movq (%rdi), %rdi
movsd (%rdi,%rsi,8), %xmm1
movq (%rax), %rax
subsd (%rax,%rsi,8), %xmm1
movsd 0x18(%rcx), %xmm0
mulsd %xmm0, %xmm1
addsd 0x10(%r14), %xmm1
addsd %xmm1, %xmm2
incl %r15d
movl 0x40(%rcx), %eax
cmpl %eax, %r15d
jl 0x5090
jmp 0x5108
movsd 0x18(%rcx), %xmm0
xorpd %xmm2, %xmm2
movq 0x8(%rsp), %r14
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm2
movsd 0xefd(%rip), %xmm1      # 0x6018
subsd %xmm0, %xmm1
mulsd 0x60(%rcx), %xmm1
mulsd 0xf24(%rip), %xmm1      # 0x6050
addsd %xmm2, %xmm1
movq 0x14e8(%r14), %rax
movq (%rax), %rax
leaq (,%rbx,8), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rax,%rsi), %rax
mulsd (%rax,%r12,8), %xmm0
movq 0x14e0(%r14), %rax
addsd %xmm1, %xmm0
movq (%rax), %rax
movq (%rax,%rsi), %rax
movsd %xmm0, (%rax,%r12,8)
incq %r12
movslq 0x4(%rcx), %rax
cmpq %rax, %r12
jl 0x506f
movl (%rcx), %r8d
incq %rbx
movslq %r8d, %rax
cmpq %rax, %rbx
jl 0x5062
movl (%rsp), %eax
incl %eax
cmpl 0x38(%rcx), %eax
movq 0x8(%rsp), %r15
jl 0x4f3f
movsd 0x60(%rcx), %xmm0
mulsd 0xe8e(%rip), %xmm0      # 0x6038
movsd %xmm0, 0x60(%rcx)
movq 0x3c(%rcx), %xmm0
paddd %xmm0, %xmm0
movq %xmm0, 0x3c(%rcx)
movq 0x14f0(%r15), %rdi
movq 0x14f8(%r15), %rsi
callq 0x5468
movq 0x1508(%r15), %rcx
movq 0x28(%rsp), %rsi
movl %esi, %eax
cltd
idivl 0x6c(%rcx)
testl %edx, %edx
jne 0x5206
movq 0x1500(%r15), %rsi
movq 0x38(%rsp), %rdi
movq %rcx, %rdx
callq 0x2ebc
movq 0x28(%rsp), %rsi
movq 0x1508(%r15), %rcx
incl %esi
movq %rsi, 0x28(%rsp)
cmpl 0x34(%rcx), %esi
jl 0x4ce5
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop