// File: tyhsu[P]Ewn/uct.cpp
// Function: UCT::select_children_list_index(Tree_node*)
int UCT::select_children_list_index(Tree_node* cur_node)
{
	float child_value_list[18] = {0};
	float best_value = 0.0;

	// calculate every UCT value of the child nodes to find the max
	for (int i=0; i<18; i++) {
		if (cur_node->get_child_ptr(i) == NULL) break;

		Tree_node* node_ptr = cur_node->get_child_ptr(i);
		float exploitation = (float)node_ptr->get_win_count() / ((float)node_ptr->get_visit_count() + this->eps);
		float exploration = this->exploration_param * sqrt( log((float)cur_node->get_visit_count() + this->t_param) / ((float)node_ptr->get_visit_count() + this->eps));
		child_value_list[i] = exploitation + exploration;
		if (child_value_list[i] > best_value)
			best_value = child_value_list[i];
	}

	// gather the nodes whose UCT value approaches best_value
	int best_children_num = 0;
	float child_index_list[18];
	for (int i=0; i<18; i++) {
		if (cur_node->get_child_ptr(i) == NULL) break;
		if (best_value - child_value_list[i] < this->eps)
			child_index_list[ best_children_num++ ] = i;
	}
	
	// select one of best child node randomly and return its index
	return child_index_list[ rand()%best_children_num ];
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
movl $0x48, %edx
callq 0x2090
xorps %xmm0, %xmm0
movss %xmm0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
cmpl $0x12, -0x68(%rbp)
jge 0x7459
movq -0x10(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x7a80
cmpq $0x0, %rax
jne 0x734f
jmp 0x7459
movq -0x10(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x7a80
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x7a10
cvtsi2ss %eax, %xmm0
movss %xmm0, -0xec(%rbp)
movq -0x70(%rbp), %rdi
callq 0x7a20
movss -0xec(%rbp), %xmm0
movl %eax, %ecx
movq -0xe0(%rbp), %rax
cvtsi2ss %ecx, %xmm1
addss 0x8(%rax), %xmm1
divss %xmm1, %xmm0
movss %xmm0, -0x74(%rbp)
movss 0xc(%rax), %xmm0
movss %xmm0, -0xe4(%rbp)
movq -0x10(%rbp), %rdi
callq 0x7a20
movl %eax, %ecx
movq -0xe0(%rbp), %rax
cvtsi2ss %ecx, %xmm0
addss 0x10(%rax), %xmm0
callq 0x7560
movss %xmm0, -0xe8(%rbp)
movq -0x70(%rbp), %rdi
callq 0x7a20
movss -0xe8(%rbp), %xmm0
movl %eax, %ecx
movq -0xe0(%rbp), %rax
cvtsi2ss %ecx, %xmm1
addss 0x8(%rax), %xmm1
divss %xmm1, %xmm0
callq 0x7540
movaps %xmm0, %xmm1
movss -0xe4(%rbp), %xmm0
mulss %xmm1, %xmm0
movss %xmm0, -0x78(%rbp)
movss -0x74(%rbp), %xmm0
addss -0x78(%rbp), %xmm0
movslq -0x68(%rbp), %rax
movss %xmm0, -0x60(%rbp,%rax,4)
movslq -0x68(%rbp), %rax
movss -0x60(%rbp,%rax,4), %xmm0
ucomiss -0x64(%rbp), %xmm0
jbe 0x7449
movslq -0x68(%rbp), %rax
movss -0x60(%rbp,%rax,4), %xmm0
movss %xmm0, -0x64(%rbp)
jmp 0x744b
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x732d
movl $0x0, -0x7c(%rbp)
movl $0x0, -0xd4(%rbp)
cmpl $0x12, -0xd4(%rbp)
jge 0x74df
movq -0x10(%rbp), %rdi
leaq -0xd4(%rbp), %rsi
callq 0x7a80
cmpq $0x0, %rax
jne 0x748b
jmp 0x74df
movq -0xe0(%rbp), %rax
movss -0x64(%rbp), %xmm1
movslq -0xd4(%rbp), %rcx
subss -0x60(%rbp,%rcx,4), %xmm1
movss 0x8(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x74cc
cvtsi2ssl -0xd4(%rbp), %xmm0
movl -0x7c(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x7c(%rbp)
cltq
movss %xmm0, -0xd0(%rbp,%rax,4)
jmp 0x74ce
movl -0xd4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd4(%rbp)
jmp 0x746a
callq 0x2050
cltd
idivl -0x7c(%rbp)
movslq %edx, %rax
cvttss2si -0xd0(%rbp,%rax,4), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)