// File: /workspace/github_cpp_makefile_star2_O1/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 %r14d, %r14d
xorl %edi, %edi
callq 0x21b0
movl %eax, %edi
callq 0x21d0
movq 0x1508(%rbx), %rcx
cmpl $0x0, 0x34(%rcx)
jle 0x5196
movq %rbx, %r9
leaq 0x28(%rbx), %rdi
leaq 0x8(%rbx), %rsi
leaq 0x10(%rbx), %r8
movapd 0x1454(%rip), %xmm2     # 0x6070
movq %rbx, (%rsp)
movq %rdi, 0x20(%rsp)
movq %rsi, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq 0x14f0(%r9), %rax
movq (%rax), %rax
movsd (%rax), %xmm0
andpd %xmm2, %xmm0
movlpd %xmm0, 0x20(%r9)
movslq (%rcx), %rcx
cmpq $0x2, %rcx
jl 0x4c76
movl $0x1, %edx
movsd (%rax,%rdx,8), %xmm1
andpd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4c6e
movsd %xmm1, 0x20(%r9)
movapd %xmm1, %xmm0
incq %rdx
cmpq %rdx, %rcx
jne 0x4c55
movq %r14, 0x48(%rsp)
movq 0x1508(%r9), %rax
cmpl $0x0, (%rax)
jle 0x4ea5
xorl %ecx, %ecx
movq %rcx, 0x18(%rsp)
movq 0x1508(%r9), %rax
cmpl $0x0, 0x4(%rax)
jle 0x4e8a
movq 0x18(%rsp), %rax
leaq (,%rax,8), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x50(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq 0x1508(%r9), %rax
movl 0x3c(%rax), %ecx
testl %ecx, %ecx
jle 0x4d6b
xorl %ebp, %ebp
xorpd %xmm1, %xmm1
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
movq 0x18(%rsp), %rbx
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %r15
movq 0x20(%rsp), %r14
movq 0x30(%rsp), %r12
movsd %xmm0, 0x28(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl %r12d, %edx
movq %r13, %rcx
movq %r15, %r8
callq 0x3cb2
movsd 0x28(%rsp), %xmm0
movq (%rsp), %r9
movsd 0x8(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
movslq 0x8(%r9), %rcx
movq 0x14f0(%r9), %rdx
movq 0x1508(%r9), %rax
movq (%rdx), %rdx
movsd (%rdx,%rcx,8), %xmm3
addsd %xmm3, %xmm1
mulsd %xmm3, %xmm3
addsd 0x10(%r9), %xmm2
addsd %xmm3, %xmm0
incl %ebp
movl 0x3c(%rax), %ecx
cmpl %ecx, %ebp
jl 0x4cf9
jmp 0x4d77
xorpd %xmm2, %xmm2
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
divsd %xmm4, %xmm1
divsd %xmm4, %xmm0
movsd 0x58(%rax), %xmm3
addsd %xmm3, %xmm3
subsd %xmm1, %xmm0
mulsd %xmm3, %xmm0
ucomisd 0x12b1(%rip), %xmm0     # 0x6050
jb 0x4da7
sqrtsd %xmm0, %xmm0
jmp 0x4dd4
movsd %xmm1, 0x10(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm4, 0x28(%rsp)
callq 0x2150
movsd 0x28(%rsp), %xmm4
movq (%rsp), %r9
movsd 0x8(%rsp), %xmm2
movsd 0x10(%rsp), %xmm1
divsd %xmm4, %xmm2
movsd %xmm2, 0x8(%rsp)
subsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq 0x1508(%r9), %rax
movsd 0x58(%rax), %xmm0
movsd 0x123c(%rip), %xmm1     # 0x6038
callq 0x2350
movq (%rsp), %r9
movq 0x1508(%r9), %rax
movsd 0x58(%rax), %xmm1
mulsd 0x1237(%rip), %xmm1     # 0x6050
mulsd 0x121f(%rip), %xmm0     # 0x6040
addsd %xmm1, %xmm0
mulsd 0x20(%r9), %xmm0
movq 0x14e8(%r9), %rcx
movsd 0x10(%rsp), %xmm1
subsd %xmm0, %xmm1
movq (%rcx), %rcx
movq 0x50(%rsp), %rdx
movq (%rcx,%rdx), %rcx
movq 0x30(%rsp), %rsi
movsd %xmm1, (%rcx,%rsi,8)
movq %rsi, %rcx
mulsd 0x18(%rax), %xmm1
addsd 0x8(%rsp), %xmm1
movq 0x14e0(%r9), %rax
movq (%rax), %rax
movq (%rax,%rdx), %rax
movsd %xmm1, (%rax,%rsi,8)
incq %rcx
movq 0x1508(%r9), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %rcx
jl 0x4cbb
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x1508(%r9), %rax
movslq (%rax), %rax
cmpq %rax, %rcx
jl 0x4c8d
movq 0x1508(%r9), %rax
cmpl $0x0, 0x38(%rax)
jle 0x511c
xorl %ecx, %ecx
movl %ecx, 0x8(%rsp)
cmpl $0x0, (%rax)
jle 0x4fc4
movq (%rsp), %rsi
movq 0x14e0(%rsi), %rdx
movq 0x14f8(%rsi), %rcx
movq (%rcx), %rcx
movq (%rdx), %rdx
movq 0x1500(%rsi), %rsi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r9
movq (%rdx,%r9,8), %r8
movq 0x8(%rdx,%r9,8), %r9
cmpq %r9, %r8
sete %bpl
leaq 0x8(%r8), %r10
cmpq %r9, %r10
sete %r11b
orb %bpl, %r11b
movq %r8, %r15
jne 0x4f31
movq %r10, %r14
movq %r8, %rbp
movsd (%r14), %xmm0
ucomisd (%rbp), %xmm0
movq %r14, %r15
ja 0x4f25
movq %rbp, %r15
addq $0x8, %r14
movq %r15, %rbp
cmpq %r9, %r14
jne 0x4f13
movsd (%r15), %xmm0
ucomisd (%rcx,%rdi,8), %xmm0
jbe 0x4fb5
testb %r11b, %r11b
je 0x4f4e
movsd (%r8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
jmp 0x4f81
movq %r10, %r14
movq %r8, %rbp
movsd (%r14), %xmm0
ucomisd (%rbp), %xmm0
movq %r14, %r15
ja 0x4f66
movq %rbp, %r15
addq $0x8, %r14
movq %r15, %rbp
cmpq %r9, %r14
jne 0x4f54
movsd (%r15), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
testb %r11b, %r11b
je 0x4f86
movq %r8, %r11
jmp 0x4fa7
movq %r8, %r14
movsd (%r10), %xmm0
ucomisd (%r14), %xmm0
movq %r10, %r11
ja 0x4f9b
movq %r14, %r11
addq $0x8, %r10
movq %r11, %r14
cmpq %r9, %r10
jne 0x4f89
subq %r8, %r11
shrq $0x3, %r11
movq (%rsi), %r8
movl %r11d, (%r8,%rdi,4)
incq %rdi
movslq (%rax), %r8
cmpq %r8, %rdi
jl 0x4ee6
movq (%rsp), %r9
movq 0x1508(%r9), %rax
cmpl $0x0, (%rax)
jle 0x5106
xorl %ebx, %ebx
movq 0x1508(%r9), %rax
cmpl $0x0, 0x4(%rax)
jle 0x50f0
xorl %ebp, %ebp
movq 0x1508(%r9), %rax
movl 0x40(%rax), %ecx
xorpd %xmm2, %xmm2
testl %ecx, %ecx
jle 0x5076
xorl %r15d, %r15d
movq 0x20(%rsp), %r12
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %r14
movsd %xmm2, 0x10(%rsp)
movq %r12, %rdi
movl %ebx, %esi
movl %ebp, %edx
movq %r13, %rcx
movq %r14, %r8
callq 0x3cb2
movsd 0x10(%rsp), %xmm2
movq (%rsp), %r9
movq 0x1508(%r9), %rax
movq 0x14f0(%r9), %rcx
movq 0x14f8(%r9), %rdx
movslq 0x8(%r9), %rsi
movq (%rdx), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq (%rcx), %rcx
subsd (%rcx,%rsi,8), %xmm0
mulsd 0x18(%rax), %xmm0
addsd 0x10(%r9), %xmm0
addsd %xmm0, %xmm2
incl %r15d
movl 0x40(%rax), %ecx
cmpl %ecx, %r15d
jl 0x5011
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd %xmm0, %xmm2
movsd 0x18(%rax), %xmm0
movsd 0xf8a(%rip), %xmm1      # 0x6018
subsd %xmm0, %xmm1
mulsd 0x60(%rax), %xmm1
mulsd 0xfa9(%rip), %xmm1      # 0x6048
addsd %xmm2, %xmm1
movq 0x14e0(%r9), %rax
movq 0x14e8(%r9), %rcx
movq (%rcx), %rcx
leaq (,%rbx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx), %rcx
mulsd (%rcx,%rbp,8), %xmm0
addsd %xmm1, %xmm0
movq (%rax), %rax
movq (%rax,%rdx), %rax
movsd %xmm0, (%rax,%rbp,8)
incq %rbp
movq 0x1508(%r9), %rax
movslq 0x4(%rax), %rax
cmpq %rax, %rbp
jl 0x4fed
incq %rbx
movq 0x1508(%r9), %rax
movslq (%rax), %rax
cmpq %rax, %rbx
jl 0x4fda
movl 0x8(%rsp), %ecx
incl %ecx
movq 0x1508(%r9), %rax
cmpl 0x38(%rax), %ecx
jl 0x4eb8
movsd 0x60(%rax), %xmm0
mulsd 0xf07(%rip), %xmm0      # 0x6030
movsd %xmm0, 0x60(%rax)
shll 0x3c(%rax)
shll 0x40(%rax)
movq 0x14f0(%r9), %rdi
movq 0x14f8(%r9), %rsi
callq 0x53d0
movq (%rsp), %r9
movq 0x1508(%r9), %rcx
movq 0x48(%rsp), %r14
movl %r14d, %eax
cltd
idivl 0x6c(%rcx)
testl %edx, %edx
movq 0x20(%rsp), %rdi
jne 0x517a
movq 0x1500(%r9), %rsi
movq %rcx, %rdx
callq 0x2ebc
movq (%rsp), %r9
incl %r14d
movq 0x1508(%r9), %rcx
cmpl 0x34(%rcx), %r14d
movapd 0xee0(%rip), %xmm2      # 0x6070
jl 0x4c2f
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop