// File: /workspace/github_c_makefile_star2_O2/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 $0x16e668, %rsp         # imm = 0x16E668
movq %r8, 0x110(%rsp)
movl %ecx, 0x9c(%rsp)
movq %rdx, 0x108(%rsp)
movl %esi, 0xa8(%rsp)
leaq 0x8c3a9(%rip), %rax     # 0xcd870
movq (%rax), %rax
xorl %ebx, %ebx
cmpl $0x0, 0x10(%rax)
sete %bl
leaq 0x120(%rsp), %r15
leaq 0x130(%rsp), %r14
pushq $0x5
popq %rbp
movq %r15, %rsi
movq %r14, %rdx
movl %ebp, %ecx
callq 0x2cfe5
leaq 0x118(%rsp), %r12
leaq 0x128(%rsp), %r13
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
movl %ebp, %ecx
callq 0x2cfe5
leaq 0xb0(%rsp), %rsi
leaq 0xb8(%rsp), %rdx
movq %r13, %rdi
movl %ebp, %ecx
callq 0x2cfe5
movq (%r12), %rdi
pushq $-0x1
popq %rbp
pushq $-0x2
popq %r14
movl %ebx, %esi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x2c130
movq %rax, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq (%r15), %rdi
movl %ebx, 0x98(%rsp)
movl %ebx, %esi
movl %ebp, %edx
movl %r14d, %ecx
callq 0x2c130
movq %rax, 0x100(%rsp)
movq %rdx, 0xf8(%rsp)
movsd 0x56726(%rip), %xmm1    # 0x97ca8
movsd 0x53f8e(%rip), %xmm0    # 0x95518
movsd %xmm0, 0xc8(%rsp)
pushq $0x1
popq %r13
leaq 0x138(%rsp), %r12
leaq 0xf4540(%rsp), %rbx
leaq 0x7a420(%rsp), %r14
movapd %xmm1, %xmm0
cmpl $0x12, %r13d
je 0x41818
cmpl $0x2, 0xa8(%rsp)
movsd %xmm0, 0xa0(%rsp)
je 0x41657
cmpl $0x1, 0xa8(%rsp)
je 0x41626
cmpl $0x0, 0xa8(%rsp)
jne 0x41696
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
leaq 0xb8(%rsp), %rsi
movsd 0xa0(%rsp), %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40fd7
jmp 0x41686
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40804
jmp 0x41686
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40bd4
movl $0x98, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x4310
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x3dd9a
movapd %xmm0, %xmm2
movsd 0xa0(%rsp), %xmm0
ucomisd 0x565dc(%rip), %xmm0    # 0x97ca8
movapd %xmm0, %xmm1
movsd %xmm2, 0xc0(%rsp)
jne 0x416dd
jnp 0x4170b
movsd 0xc8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x4170b
leaq 0x268(%rsp), %rdi
callq 0x3eeb2
movsd 0xc0(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
ucomisd 0x56595(%rip), %xmm1    # 0x97ca8
jne 0x41717
jnp 0x41726
movsd 0xc8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x4177b
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
callq 0x3d854
movl $0x98, %edx
leaq 0x268(%rsp), %rdi
movq %rbp, %rsi
callq 0x4310
movsd 0xc0(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd %xmm2, 0xc8(%rsp)
movq %rbx, %rdi
leaq 0x57dbc(%rip), %rsi     # 0x99541
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movl 0x188(%rsp), %edx
movq %r14, %rdi
leaq 0x5f3b9(%rip), %rsi     # 0xa0b59
xorl %eax, %eax
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f4fc
movq %rbx, %rdi
leaq 0x58bde(%rip), %rsi     # 0x9a39a
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x300(%rsp), %rbp
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
movq %r14, %rdi
movq %rbp, %rsi
movsd 0xc0(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f4fc
incl %r13d
movq %r12, %rdi
callq 0x3eeb2
movsd 0xa0(%rsp), %xmm0
addsd %xmm0, %xmm0
jmp 0x415b3
pushq $0x12
popq %r13
movsd 0x53cfc(%rip), %xmm2    # 0x95520
pushq $0xb
popq %r12
leaq 0x138(%rsp), %rbp
leaq 0xf4540(%rsp), %r14
subl $0x1, %r12d
jb 0x41abc
movsd 0xd0(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x544c9(%rip), %xmm0    # 0x95d20
mulsd %xmm2, %xmm1
mulsd 0x564e5(%rip), %xmm1    # 0x97d48
addsd %xmm0, %xmm1
cmpl $0x2, 0xa8(%rsp)
movsd %xmm2, 0xc0(%rsp)
movsd %xmm1, 0xf0(%rsp)
je 0x4190e
cmpl $0x1, 0xa8(%rsp)
je 0x418d9
cmpl $0x0, 0xa8(%rsp)
jne 0x4195e
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
leaq 0xb8(%rsp), %rsi
movsd 0xf0(%rsp), %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40fd7
jmp 0x41941
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40804
jmp 0x41941
movq 0xb8(%rsp), %rsi
movq 0xb0(%rsp), %rdx
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm1, %xmm0
movl 0x9c(%rsp), %ecx
movl 0x98(%rsp), %r8d
callq 0x40bd4
movl $0x98, %edx
leaq 0x138(%rsp), %rdi
movq %rbp, %rsi
leaq 0x138(%rsp), %rbp
callq 0x4310
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x3dd9a
movapd %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm0
movsd %xmm1, 0xa0(%rsp)
ucomisd %xmm1, %xmm0
jbe 0x41a13
movl %r13d, 0xec(%rsp)
leaq 0x268(%rsp), %r15
movq %r15, %rdi
callq 0x3eeb2
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1d0(%rsp), %rbp
movq %rbp, %rdi
callq 0x3d854
movl $0x98, %edx
movq %r15, %rdi
movl 0xec(%rsp), %r13d
movq %rbp, %rsi
leaq 0x138(%rsp), %rbp
callq 0x4310
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movq %r14, %rdi
leaq 0x57b24(%rip), %rsi     # 0x99541
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
movl 0x188(%rsp), %edx
leaq 0x7a420(%rsp), %rbx
movq %rbx, %rdi
leaq 0x5f119(%rip), %rsi     # 0xa0b59
xorl %eax, %eax
callq 0x41b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2f4fc
movq %r14, %rdi
leaq 0x5893e(%rip), %rsi     # 0x9a39a
movl %r13d, %edx
xorl %eax, %eax
callq 0x41b0
leaq 0x300(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x35305
movq %rbx, %rdi
movq %r15, %rsi
movsd 0xa0(%rsp), %xmm0
movb $0x1, %al
callq 0x41b0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2f4fc
incl %r13d
movq %rbp, %rdi
callq 0x3eeb2
movsd 0xc0(%rsp), %xmm2
addsd 0x53a69(%rip), %xmm2    # 0x95520
jmp 0x41838
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x3929f
leaq 0x268(%rsp), %rbx
movl 0x50(%rbx), %eax
movq 0x108(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x13
popq %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq 0x100(%rsp), %r14
movq %r14, %rdi
movq 0xf8(%rsp), %r15
movq %r15, %rsi
callq 0x3dd9a
movsd %xmm0, 0xa8(%rsp)
movq %rbx, %rdi
callq 0x3eeb2
movq %r14, %rdi
movq %r15, %rsi
callq 0x3929f
movq 0x120(%rsp), %rdi
callq 0x2c010
movq 0x118(%rsp), %rdi
callq 0x2c010
movq 0xb0(%rsp), %rdi
callq 0x2c010
movq 0xb8(%rsp), %rdi
callq 0x2c010
movq 0x110(%rsp), %rax
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0xa8(%rsp), %xmm0
addq $0x16e668, %rsp         # imm = 0x16E668
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq