// File: /workspace/github_c_makefile_star2_O3/olcaytaner[P]Isell/model.c
// Function: mlp_model_selection
double mlp_model_selection(Instanceptr* data, MODEL_SELECTION_TYPE modelselectionmethod, int* hiddennodes, int maxhidden, double* bestalpha)
{
	/*!Last Changed 25.06.2005*/
 char varname[MAXLENGTH];
 char varvalue[MAXLENGTH], pst[READLINELENGTH];
	Mlpnetwork best, neuralnetwork;
	double a, alphaerror, besterror = +INT_MAX, result, besta;
	matrix alpha, test;
	int i, problemtype, totalrun = 1;
	Instanceptr testdata, data1, data2, data3, alphavalidation, epochvalidation;
	if (current_dataset->classno == 0)
		 problemtype = REGRESSION;
	else
		 problemtype = CLASSIFICATION;
	divide_instancelist(data, &testdata, &data1, 5);
	divide_instancelist(&data1, &alphavalidation, &data2, 5);
	divide_instancelist(&data2, &epochvalidation, &data3, 5);
	alpha = instancelist_to_matrix(alphavalidation, problemtype, -1, MULTIVARIATE_LINEAR);
	test = instancelist_to_matrix(testdata, problemtype, -1, MULTIVARIATE_LINEAR);
	/*Exponential search*/
	for (a = 0.001; a < 100; a *= 2)
	 {
  	switch (modelselectionmethod)
			 {
			  case MODEL_SELECTION_CV :neuralnetwork = mlp_model_selection_crossvalidation(&data3, epochvalidation, a, maxhidden, problemtype);
				                          break;
	    case MODEL_SELECTION_AIC:neuralnetwork = mlp_model_selection_aic(data3, epochvalidation, a, maxhidden, problemtype);
			  	                        break;
			  case MODEL_SELECTION_BIC:neuralnetwork = mlp_model_selection_bic(data3, epochvalidation, a, maxhidden, problemtype);
				                          break;
     default                 :break;
			 }
			alphaerror = test_mlp_network(neuralnetwork, alpha);
			if (a != 0.001 && alphaerror < besterror)
				 free_mlpnnetwork(&best);
			if (a == 0.001 || alphaerror < besterror)
			 {
					besta = a;
					best = copy_of_Mlpnetwork(neuralnetwork);
					besterror = alphaerror;
			 }
	  sprintf(varname, "aout1[%d]", totalrun);
	  sprintf(varvalue, "%d", neuralnetwork.P.hidden[0]);
  	set_variable(varname, varvalue);
	  sprintf(varname, "aout2[%d]", totalrun);
			set_precision(pst, NULL, NULL);
	  sprintf(varvalue, pst, alphaerror);
  	set_variable(varname, varvalue);
   totalrun++;
			free_mlpnnetwork(&neuralnetwork);
	 }
	/*Linear search*/
	for (i = 1; i < 12; i++)
	 {
		 a = besta / 2 + (i * besta) / 8;
  	switch (modelselectionmethod)
			 {
			  case MODEL_SELECTION_CV :neuralnetwork = mlp_model_selection_crossvalidation(&data3, epochvalidation, a, maxhidden, problemtype);
				                          break;
	    case MODEL_SELECTION_AIC:neuralnetwork = mlp_model_selection_aic(data3, epochvalidation, a, maxhidden, problemtype);
			  	                        break;
			  case MODEL_SELECTION_BIC:neuralnetwork = mlp_model_selection_bic(data3, epochvalidation, a, maxhidden, problemtype);
				                          break;
     default                 :break;
			 }
			alphaerror = test_mlp_network(neuralnetwork, alpha);
			if (alphaerror < besterror)
			 {
	  		free_mlpnnetwork(&best);
					best = copy_of_Mlpnetwork(neuralnetwork);
					besta = a;
					besterror = alphaerror;
			 }
	  sprintf(varname, "aout1[%d]", totalrun);
	  sprintf(varvalue, "%d", neuralnetwork.P.hidden[0]);
   set_variable(varname, varvalue);
	  sprintf(varname, "aout2[%d]", totalrun);
			set_precision(pst, NULL, NULL);
	  sprintf(varvalue, pst, alphaerror);
  	set_variable(varname, varvalue);
   totalrun++;
			free_mlpnnetwork(&neuralnetwork);
	 }
	matrix_free(alpha);
	*hiddennodes = best.P.hidden[0];
	result = test_mlp_network(best, test);
	free_mlpnnetwork(&best);
	matrix_free(test);
	deallocate(testdata);
	deallocate(alphavalidation);
	deallocate(epochvalidation);
	deallocate(data3);
	*bestalpha = besta;
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x16e658, %rsp         # imm = 0x16E658
movq %r8, 0x100(%rsp)
movl %ecx, 0x9c(%rsp)
movq %rdx, 0xf8(%rsp)
movl %esi, %r15d
leaq 0x8d652(%rip), %rax     # 0xd5870
movq (%rax), %rax
xorl %ebp, %ebp
cmpl $0x0, 0x10(%rax)
sete %bpl
leaq 0x110(%rsp), %rbx
leaq 0x120(%rsp), %r14
movq %rbx, %rsi
movq %r14, %rdx
movl $0x5, %ecx
callq 0x31019
leaq 0x108(%rsp), %r12
leaq 0x118(%rsp), %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl $0x5, %ecx
callq 0x31019
leaq 0xa8(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
movq %r13, %rdi
movl $0x5, %ecx
callq 0x31019
movq (%r12), %rdi
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x3008b
movq %rax, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq (%rbx), %rdi
movl %ebp, 0x98(%rsp)
movl %ebp, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
callq 0x3008b
movq %rax, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movl $0x1, %r13d
movsd 0x5d22f(%rip), %xmm0    # 0xa5518
movsd %xmm0, 0xc0(%rsp)
movsd 0x5fa2e(%rip), %xmm1    # 0xa7d28
leaq 0x258(%rsp), %r12
leaq 0xf4530(%rsp), %rbx
leaq 0x7a410(%rsp), %r14
cmpl $0x2, %r15d
movsd %xmm1, 0xa0(%rsp)
je 0x4839f
cmpl $0x1, %r15d
je 0x4836a
testl %r15d, %r15d
jne 0x483e2
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
leaq 0xb0(%rsp), %rsi
movsd 0xa0(%rsp), %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x47d0c
jmp 0x483d2
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x47469
jmp 0x483d2
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x4787a
movl $0x98, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4310
movl $0x13, %ecx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x44466
movapd %xmm0, %xmm2
movsd 0xa0(%rsp), %xmm0
ucomisd 0x5f90e(%rip), %xmm0    # 0xa7d28
movapd %xmm0, %xmm1
movsd %xmm2, 0xb8(%rsp)
jne 0x4842b
jnp 0x48459
movsd 0xc0(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x48459
leaq 0x1c0(%rsp), %rdi
callq 0x45646
movsd 0xb8(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
ucomisd 0x5f8c7(%rip), %xmm1    # 0xa7d28
jne 0x48465
jnp 0x48474
movsd 0xc0(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x484cb
movl $0x13, %ecx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x128(%rsp), %rbp
movq %rbp, %rdi
callq 0x43d0c
movl $0x98, %edx
leaq 0x1c0(%rsp), %rdi
movq %rbp, %rsi
callq 0x4310
movsd 0xb8(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd %xmm2, 0xc0(%rsp)
movq %rbx, %rdi
leaq 0x610fc(%rip), %rsi     # 0xa95d1
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movl 0x2a8(%rsp), %edx
movq %r14, %rdi
leaq 0x68705(%rip), %rsi     # 0xb0bf5
xorl %eax, %eax
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33827
movq %rbx, %rdi
leaq 0x61f5e(%rip), %rsi     # 0xaa46a
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x2f0(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a535
movq %r14, %rdi
movq %rbp, %rsi
movsd 0xb8(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33827
incl %r13d
movq %r12, %rdi
callq 0x45646
movsd 0xa0(%rsp), %xmm1
addsd %xmm1, %xmm1
cmpl $0x12, %r13d
jne 0x48312
movsd 0x5cfab(%rip), %xmm2    # 0xa5520
movl $0x12, %ebp
leaq 0x258(%rsp), %r13
leaq 0xf4530(%rsp), %r14
movsd 0xc8(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x5d779(%rip), %xmm0    # 0xa5d18
mulsd %xmm2, %xmm1
mulsd 0x5f81d(%rip), %xmm1    # 0xa7dc8
addsd %xmm0, %xmm1
cmpl $0x2, %r15d
movsd %xmm2, 0xb8(%rsp)
movsd %xmm1, 0xe0(%rsp)
je 0x48645
cmpl $0x1, %r15d
je 0x48610
testl %r15d, %r15d
jne 0x48688
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %r12
movq %r12, %rdi
leaq 0xb0(%rsp), %rsi
movsd 0xe0(%rsp), %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x47d0c
jmp 0x48678
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %r12
movq %r12, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x47469
jmp 0x48678
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
leaq 0x128(%rsp), %r12
movq %r12, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x4787a
movl $0x98, %edx
movq %r13, %rdi
movq %r12, %rsi
callq 0x4310
movl $0x13, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x44466
movapd %xmm0, %xmm1
movsd 0xc0(%rsp), %xmm0
movsd %xmm1, 0xa0(%rsp)
ucomisd %xmm1, %xmm0
jbe 0x48737
leaq 0x1c0(%rsp), %rdi
callq 0x45646
movl $0x13, %ecx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %ebx
movq %r13, %r15
leaq 0x128(%rsp), %r13
movq %r13, %rdi
callq 0x43d0c
movl $0x98, %edx
leaq 0x1c0(%rsp), %rdi
movq %r13, %rsi
movq %r15, %r13
movl %ebx, %r15d
callq 0x4310
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movq %r14, %rdi
leaq 0x60e90(%rip), %rsi     # 0xa95d1
movl %ebp, %edx
xorl %eax, %eax
callq 0x41b0
movl 0x2a8(%rsp), %edx
leaq 0x7a410(%rsp), %r12
movq %r12, %rdi
leaq 0x68492(%rip), %rsi     # 0xb0bf5
xorl %eax, %eax
callq 0x41b0
movq %r14, %rdi
movq %r12, %rsi
callq 0x33827
movq %r14, %rdi
leaq 0x61ceb(%rip), %rsi     # 0xaa46a
movl %ebp, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x2f0(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3a535
movq %r12, %rdi
movq %rbx, %rsi
movsd 0xa0(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %r14, %rdi
movq %r12, %rsi
callq 0x33827
incl %ebp
movq %r13, %rdi
callq 0x45646
movsd 0xb8(%rsp), %xmm2
addsd 0x5cd48(%rip), %xmm2    # 0xa5520
cmpl $0x1d, %ebp
jne 0x4858a
movq 0xd8(%rsp), %rdi
movq 0xd0(%rsp), %rsi
callq 0x3ed51
leaq 0x1c0(%rsp), %rbx
movl 0x50(%rbx), %eax
movq 0xf8(%rsp), %rcx
movl %eax, (%rcx)
movl $0x13, %ecx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xf0(%rsp), %r14
movq %r14, %rdi
movq 0xe8(%rsp), %r15
movq %r15, %rsi
callq 0x44466
movsd %xmm0, 0xa0(%rsp)
movq %rbx, %rdi
callq 0x45646
movq %r14, %rdi
movq %r15, %rsi
callq 0x3ed51
movq 0x110(%rsp), %rdi
callq 0x2ff55
movq 0x108(%rsp), %rdi
callq 0x2ff55
movq 0xa8(%rsp), %rdi
callq 0x2ff55
movq 0xb0(%rsp), %rdi
callq 0x2ff55
movq 0x100(%rsp), %rax
movsd 0xc8(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0xa0(%rsp), %xmm0
addq $0x16e658, %rsp         # imm = 0x16E658
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq