// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x2170
movl %eax, %edi
callq 0x2190
xorl %esi, %esi
leaq 0x28(%rbx), %rax
movq %rax, 0x28(%rsp)
leaq 0x8(%rbx), %r12
leaq 0x10(%rbx), %r13
movq 0x1508(%rbx), %rax
cmpl 0x34(%rax), %esi
jge 0x45fe
movq %rsi, 0x30(%rsp)
movq 0x14f0(%rbx), %rcx
movq (%rcx), %rdx
movsd (%rdx), %xmm0
andpd 0x1eab(%rip), %xmm0     # 0x6090
movlpd %xmm0, 0x20(%rbx)
movl (%rax), %ecx
movslq %ecx, %rsi
pushq $0x1
popq %rdi
cmpq %rsi, %rdi
jge 0x4218
movsd (%rdx,%rdi,8), %xmm1
andpd 0x1e8c(%rip), %xmm1     # 0x6090
ucomisd %xmm0, %xmm1
jbe 0x4213
movsd %xmm1, 0x20(%rbx)
movapd %xmm1, %xmm0
incq %rdi
jmp 0x41f2
xorl %r15d, %r15d
movslq %ecx, %rdx
cmpq %rdx, %r15
jge 0x43ca
imulq $0x18, %r15, %rcx
movq %rcx, 0x38(%rsp)
xorl %r14d, %r14d
movslq 0x4(%rax), %rcx
cmpq %rcx, %r14
jge 0x43c0
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %ebp, %ebp
movl 0x3c(%rax), %ecx
cmpl %ecx, %ebp
jge 0x42c4
movq 0x28(%rsp), %rdi
movl %r15d, %esi
movl %r14d, %edx
movq %r12, %rcx
movq %r13, %r8
movapd %xmm1, 0x10(%rsp)
callq 0x36a0
movslq 0x8(%rbx), %rcx
movq 0x14f0(%rbx), %rdx
movq 0x1508(%rbx), %rax
movq (%rdx), %rdx
movsd (%rdx,%rcx,8), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0x10(%rsp), %xmm0
addpd %xmm1, %xmm0
movapd %xmm0, 0x10(%rsp)
movapd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm0
addsd 0x10(%rbx), %xmm0
movsd %xmm0, 0x8(%rsp)
incl %ebp
jmp 0x4250
cvtsi2sd %ecx, %xmm3
movapd %xmm3, %xmm0
unpcklpd %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0]
divpd %xmm0, %xmm1
movsd 0x58(%rax), %xmm0
addsd %xmm0, %xmm0
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
movapd %xmm2, 0x10(%rsp)
subsd %xmm2, %xmm1
mulsd %xmm0, %xmm1
ucomisd 0x1d5d(%rip), %xmm1     # 0x6058
jb 0x4303
sqrtsd %xmm1, %xmm0
jmp 0x4318
movapd %xmm1, %xmm0
movapd %xmm3, 0x40(%rsp)
callq 0x2120
movapd 0x40(%rsp), %xmm3
movsd 0x8(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp)
movq 0x1508(%rbx), %rax
movsd 0x58(%rax), %xmm0
movsd 0x1cfc(%rip), %xmm1     # 0x6048
callq 0x2310
movq 0x14e8(%rbx), %rcx
movq 0x1508(%rbx), %rax
movsd 0x58(%rax), %xmm1
mulsd 0x1cec(%rip), %xmm1     # 0x6058
mulsd 0x1cdc(%rip), %xmm0     # 0x6050
addsd %xmm1, %xmm0
mulsd 0x20(%rbx), %xmm0
movapd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movq (%rcx), %rcx
movq 0x38(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movsd %xmm1, (%rcx,%r14,8)
mulsd 0x18(%rax), %xmm1
addsd 0x8(%rsp), %xmm1
movq 0x14e0(%rbx), %rcx
movq (%rcx), %rcx
movq (%rcx,%rdx), %rcx
movsd %xmm1, (%rcx,%r14,8)
incq %r14
jmp 0x4233
incq %r15
movl (%rax), %ecx
jmp 0x421b
xorl %edx, %edx
cmpl 0x38(%rax), %edx
jge 0x4595
movl %edx, 0x8(%rsp)
pushq $0x2
popq %rbp
xorl %r14d, %r14d
movslq %ecx, %rdx
cmpq %rdx, %r14
jge 0x4496
movq 0x14e0(%rbx), %rax
movq 0x14f8(%rbx), %rcx
movq (%rcx), %rcx
movsd (%rcx,%r14,8), %xmm0
movsd %xmm0, 0x10(%rsp)
movq (%rax), %rax
movq -0x8(%rax,%rbp,4), %rdi
movq (%rax,%rbp,4), %rsi
callq 0x4ae5
movsd (%rax), %xmm0
ucomisd 0x10(%rsp), %xmm0
jbe 0x4481
movq 0x14e0(%rbx), %rax
movq (%rax), %rax
movq -0x8(%rax,%rbp,4), %rdi
movq (%rax,%rbp,4), %rsi
callq 0x4ae5
movsd (%rax), %xmm0
movq 0x14f8(%rbx), %rax
movq (%rax), %rax
movsd %xmm0, (%rax,%r14,8)
movq 0x14e0(%rbx), %rax
movq (%rax), %rax
movq -0x8(%rax,%rbp,4), %r15
movq (%rax,%rbp,4), %rsi
movq %r15, %rdi
callq 0x4ae5
subq %r15, %rax
shrq $0x3, %rax
movq 0x1500(%rbx), %rcx
movq (%rcx), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
movq 0x1508(%rbx), %rax
movl (%rax), %ecx
addq $0x6, %rbp
jmp 0x43df
xorl %r14d, %r14d
movslq %ecx, %rdx
cmpq %rdx, %r14
jge 0x458a
xorl %ebp, %ebp
movslq 0x4(%rax), %rcx
cmpq %rcx, %rbp
jge 0x4580
xorl %r15d, %r15d
xorpd %xmm2, %xmm2
movl 0x40(%rax), %ecx
cmpl %ecx, %r15d
jge 0x4520
movq 0x28(%rsp), %rdi
movl %r14d, %esi
movl %ebp, %edx
movq %r12, %rcx
movq %r13, %r8
movsd %xmm2, 0x10(%rsp)
callq 0x36a0
movsd 0x10(%rsp), %xmm2
movq 0x1508(%rbx), %rax
movq 0x14f0(%rbx), %rcx
movq 0x14f8(%rbx), %rdx
movslq 0x8(%rbx), %rsi
movq (%rdx), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq (%rcx), %rcx
subsd (%rcx,%rsi,8), %xmm0
mulsd 0x18(%rax), %xmm0
addsd 0x10(%rbx), %xmm0
addsd %xmm0, %xmm2
incl %r15d
jmp 0x44bb
cvtsi2sd %ecx, %xmm0
divsd %xmm0, %xmm2
movsd 0x18(%rax), %xmm0
movsd 0x1ae3(%rip), %xmm1     # 0x6018
subsd %xmm0, %xmm1
mulsd 0x60(%rax), %xmm1
mulsd 0x1afa(%rip), %xmm1     # 0x6040
addsd %xmm2, %xmm1
movq 0x14e0(%rbx), %rcx
movq 0x14e8(%rbx), %rdx
movq (%rdx), %rdx
imulq $0x18, %r14, %rsi
movq (%rdx,%rsi), %rdx
mulsd (%rdx,%rbp,8), %xmm0
addsd %xmm1, %xmm0
movq (%rcx), %rcx
movq (%rcx,%rsi), %rcx
movsd %xmm0, (%rcx,%rbp,8)
incq %rbp
jmp 0x44a7
incq %r14
movl (%rax), %ecx
jmp 0x4499
movl 0x8(%rsp), %edx
incl %edx
jmp 0x43cc
movsd 0x60(%rax), %xmm0
mulsd 0x1a8e(%rip), %xmm0     # 0x6030
movsd %xmm0, 0x60(%rax)
movq 0x3c(%rax), %xmm0
paddd %xmm0, %xmm0
movq %xmm0, 0x3c(%rax)
movq 0x14f0(%rbx), %rdi
movq 0x14f8(%rbx), %rsi
callq 0x4998
movq 0x1508(%rbx), %rcx
movq 0x30(%rsp), %rsi
movl %esi, %eax
cltd
idivl 0x6c(%rcx)
testl %edx, %edx
jne 0x45f7
movq 0x1500(%rbx), %rsi
movq 0x28(%rsp), %rdi
movq %rcx, %rdx
callq 0x2ba5
movq 0x30(%rsp), %rsi
incl %esi
jmp 0x41ba
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop