// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/algorithm.c
// Function: tune_hyperparameters_of_the_supervised_algorithm
void* tune_hyperparameters_of_the_supervised_algorithm(int algorithm, Instanceptr* traindata, Instanceptr* cvdata, Instanceptr* tunedata, void* parameters)
{
 /*!Last Changed 12.03.2009*/
 Xmldocumentptr doc;
 Xmlelementptr element, child;
 void* bestmodel = NULL;
 void* model;
 int valuecount, i, classno, bestnear;
 Knn_parameterptr pknn;
 C45_parameterptr pc45;
 Multildt_parameterptr pmultildt;
 Logistic_parameterptr plogistic;
 Rbf_parameterptr prbf;
 Mlpparametersptr pmlp;
 Svm_parameterptr psvm;
 Svm_simplex_parameterptr ssvm;
 Randomforest_parameterptr prf;
 Kforest_parameterptr pkf;
 double* posteriors = NULL;
 Instanceptr test, next;
 Prediction predicted;
 double performance, bestperformance, real;
 if (in_list(algorithm, 18, KNN, KNNREG, LOGISTIC, RBF, RBFREG, MLPGENERIC, MLPGENERICREG, DNC, DNCREG, SVM, SIMPLEXSVM, SVMREG, SIMPLEXSVMREG, SVMTREE, MULTIRIPPER, HYBRIDRIPPER, RANDOMFOREST, KFOREST) || (in_list(algorithm, 4, C45, LDT, MULTILDT, OMNILDT) && (get_parameter_l("prunetype") == PREPRUNING)))
  {
   if (get_algorithm_type(algorithm) == CLASSIFICATION)
     posteriors = safemalloc(current_dataset->classno * sizeof(double), "tune_hyperparameters_of_the_supervised_algorithm", 20);   
   doc = xml_document("hyperparameters.xml");
   if (!doc)
    {
     printf(m1428);
     return NULL;
    }
   xml_parse(doc);
   if (in_list(algorithm, 2, KNN, KNNREG))
     element = xml_get_child_with_specific_attribute_value(doc->root, "name", "k");
   else
     if (in_list(algorithm, 4, C45, LDT, MULTILDT, OMNILDT))
       element = xml_get_child_with_specific_attribute_value(doc->root, "name", "pruningthreshold");
     else
       if (in_list(algorithm, 7, LOGISTIC, RBF, RBFREG, MLPGENERIC, MLPGENERICREG, DNC, DNCREG))
         element = xml_get_child_with_specific_attribute_value(doc->root, "name", "learningrate");
       else
         if (in_list(algorithm, 7, SVM, SIMPLEXSVM, SVMREG, SIMPLEXSVMREG, SVMTREE, MULTIRIPPER, HYBRIDRIPPER))
           element = xml_get_child_with_specific_attribute_value(doc->root, "name", "C");
         else
           if (in_list(algorithm, 2, RANDOMFOREST, KFOREST))
             element = xml_get_child_with_specific_attribute_value(doc->root, "name", "featuresize");
           else
            {
             printf(m1451);
             if (get_algorithm_type(algorithm) == CLASSIFICATION)
               return classification_algorithms[algorithm - SELECTMAX].train_algorithm(traindata, cvdata, parameters);
             else
               return regression_algorithms[algorithm - ONEFEATURE].train_algorithm(traindata, cvdata, parameters);
            }
   valuecount = xml_number_of_children(element);
   child = element->child;
   for (i = 0; i < valuecount; i++)
    {
     switch (algorithm){
       case KNN          :
       case KNNREG       :pknn = (Knn_parameterptr) parameters;
                          pknn->nearcount = atoi(child->pcdata);
                          break;
       case C45          :
       case LDT          :
       case SVMTREE      :pc45 = (C45_parameterptr) parameters;
                          pc45->pruningthreshold = atof(child->pcdata);
                          break;
       case MULTILDT     :
       case OMNILDT      :pmultildt = (Multildt_parameterptr) parameters;
                          pmultildt->c45param.pruningthreshold = atof(child->pcdata);
                          break;
       case LOGISTIC     :plogistic = (Logistic_parameterptr) parameters;
                          plogistic->learning_rate = atof(child->pcdata);
                          break;
       case RBF          :
       case RBFREG       :prbf = (Rbf_parameterptr) parameters;
                          prbf->eta = atof(child->pcdata);
                          break;
       case MLPGENERIC   :
       case MLPGENERICREG:
       case DNC          :
       case DNCREG       :pmlp = (Mlpparametersptr) parameters;
                          pmlp->eta = atof(child->pcdata);
                          break;
       case SVM          :
       case SVMREG       :psvm = (Svm_parameterptr) parameters;
                          psvm->C = atof(child->pcdata);
                          break;
       case SIMPLEXSVM   :
       case SIMPLEXSVMREG:ssvm = (Svm_simplex_parameterptr) parameters;
                          ssvm->C = atof(child->pcdata);
                          break;
						 case MULTIRIPPER  :
						 case HYBRIDRIPPER :set_parameter_f("svmC", child->pcdata, -INT_MAX, +INT_MAX);
							                   break;
       case RANDOMFOREST :prf = (Randomforest_parameterptr) parameters;
                          prf->featuresize = atoi(child->pcdata);
                          if (prf->featuresize >= current_dataset->featurecount - 1)
                            continue;
                          break;
       case KFOREST      :pkf = (Kforest_parameterptr) parameters;
                          pkf->featuresize = atoi(child->pcdata);
                          break;
     }
     if (get_algorithm_type(algorithm) == CLASSIFICATION)
       model = classification_algorithms[algorithm - SELECTMAX].train_algorithm(traindata, cvdata, parameters);
     else
       model = regression_algorithms[algorithm - ONEFEATURE].train_algorithm(traindata, cvdata, parameters);
     test = *tunedata;
     performance = 0.0;
     while (test)
      {
       next = test->next;
       test->next = NULL;
       if (get_algorithm_type(algorithm) == CLASSIFICATION)
         predicted = classification_algorithms[algorithm - SELECTMAX].test_algorithm(test, model, parameters, posteriors);
       else
         predicted = regression_algorithms[algorithm - ONEFEATURE].test_algorithm(test, model, parameters, NULL);
       test->next = next;
       if (get_algorithm_type(algorithm) == CLASSIFICATION)
        {
         classno = give_classno(test);
         if (classno != predicted.classno)
           performance++;
        }
       else
        {
         real = give_regressionvalue(test);
         performance += (real - predicted.regvalue) * (real - predicted.regvalue);
        }
       test = test->next;
      }
     if (i == 0 || performance < bestperformance)
      {
       bestperformance = performance;
       write_array_variables("aout", totalrun, 15, "f", atof(child->pcdata));
       if (algorithm == KNN || algorithm == KNNREG)
         bestnear = atoi(child->pcdata);
       if (i != 0)
         free_model_of_supervised_algorithm(algorithm, bestmodel, current_dataset);
       bestmodel = model;
      }
     else
       free_model_of_supervised_algorithm(algorithm, model, current_dataset);
     child = child->sibling;
    }
   if (algorithm == KNN || algorithm == KNNREG)
    {    
     pknn = (Knn_parameterptr) parameters;
     pknn->nearcount = bestnear;    
    }
   xml_free_document(doc);
   if (get_algorithm_type(algorithm) == CLASSIFICATION)
     safe_free(posteriors);
   return bestmodel;
  }
 else
  {
   if (get_algorithm_type(algorithm) == CLASSIFICATION)
     return classification_algorithms[algorithm - SELECTMAX].train_algorithm(traindata, cvdata, parameters);
   else
     return regression_algorithms[algorithm - ONEFEATURE].train_algorithm(traindata, cvdata, parameters);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r8, 0x8(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movl %edi, %ebx
pushq $0x12
popq %rsi
movl $0x3e9, %edx            # imm = 0x3E9
movl $0x7d1, %ecx            # imm = 0x7D1
movl $0x3f8, %r8d            # imm = 0x3F8
movl $0x402, %r9d            # imm = 0x402
xorl %eax, %eax
pushq $0x40c                  # imm = 0x40C
pushq $0x403                  # imm = 0x403
pushq $0x3f3                  # imm = 0x3F3
pushq $0x3f2                  # imm = 0x3F2
pushq $0x3fb                  # imm = 0x3FB
pushq $0x7d9                  # imm = 0x7D9
pushq $0x7d8                  # imm = 0x7D8
pushq $0x3fa                  # imm = 0x3FA
pushq $0x3f9                  # imm = 0x3F9
pushq $0x7da                  # imm = 0x7DA
pushq $0x3ff                  # imm = 0x3FF
pushq $0x7d3                  # imm = 0x7D3
pushq $0x3f4                  # imm = 0x3F4
pushq $0x7db                  # imm = 0x7DB
callq 0x35812
addq $0x70, %rsp
testl %eax, %eax
je 0x52e6
movl %ebx, %edi
callq 0x445c
movl %eax, 0x1c(%rsp)
testl %eax, %eax
je 0x5344
movq $0x0, 0x38(%rsp)
jmp 0x536a
pushq $0x4
popq %rsi
movl %ebx, %edi
movl $0x3ea, %edx            # imm = 0x3EA
movl $0x3ed, %ecx            # imm = 0x3ED
movl $0x3f6, %r8d            # imm = 0x3F6
movl $0x3f7, %r9d            # imm = 0x3F7
xorl %eax, %eax
callq 0x35812
testl %eax, %eax
je 0x531c
leaq 0x9041f(%rip), %rdi     # 0x95732
callq 0x4cf11
testl %eax, %eax
je 0x52c9
movl %ebx, %edi
callq 0x445c
testl %eax, %eax
je 0x58e0
addl $0xfffff830, %ebx       # imm = 0xFFFFF830
movslq %ebx, %rax
imulq $0x28, %rax, %rax
leaq 0xc74f1(%rip), %rcx     # 0xcc830
jmp 0x58f4
leaq 0xc8525(%rip), %rax     # 0xcd870
movq (%rax), %rax
movslq 0x10(%rax), %rdi
shlq $0x3, %rdi
leaq 0x903df(%rip), %rsi     # 0x9573c
pushq $0x14
popq %rdx
callq 0x34f59
movq %rax, 0x38(%rsp)
leaq 0x903fc(%rip), %rdi     # 0x9576d
callq 0x93f7c
testq %rax, %rax
je 0x53af
movq %rax, %r14
movq %rax, %rdi
callq 0x94657
xorl %r12d, %r12d
pushq $0x2
popq %rsi
movl %ebx, %edi
movl $0x3e9, %edx            # imm = 0x3E9
movl $0x7d1, %ecx            # imm = 0x7D1
xorl %eax, %eax
callq 0x35812
testl %eax, %eax
je 0x53c3
leaq 0x9b93c(%rip), %rdx     # 0xa0ce6
jmp 0x54a6
leaq 0x907eb(%rip), %rdi     # 0x95ba1
callq 0x4090
xorl %r15d, %r15d
jmp 0x58b4
pushq $0x4
popq %rsi
movl %ebx, %edi
movl $0x3ea, %edx            # imm = 0x3EA
movl $0x3ed, %ecx            # imm = 0x3ED
movl $0x3f6, %r8d            # imm = 0x3F6
movl $0x3f7, %r9d            # imm = 0x3F7
xorl %eax, %eax
callq 0x35812
testl %eax, %eax
je 0x53f5
leaq 0x90391(%rip), %rdx     # 0x95781
jmp 0x54a6
subq $0x8, %rsp
pushq $0x7
popq %rsi
movl %ebx, %edi
movl $0x3f8, %edx            # imm = 0x3F8
movl $0x402, %ecx            # imm = 0x402
movl $0x7db, %r8d            # imm = 0x7DB
movl $0x3f4, %r9d            # imm = 0x3F4
xorl %eax, %eax
pushq $0x7da                  # imm = 0x7DA
pushq $0x3ff                  # imm = 0x3FF
pushq $0x7d3                  # imm = 0x7D3
callq 0x35812
addq $0x20, %rsp
testl %eax, %eax
je 0x543b
leaq 0x90359(%rip), %rdx     # 0x95792
jmp 0x54a6
subq $0x8, %rsp
pushq $0x7
popq %rsi
movl %ebx, %edi
movl $0x3f9, %edx            # imm = 0x3F9
movl $0x3fa, %ecx            # imm = 0x3FA
movl $0x7d8, %r8d            # imm = 0x7D8
movl $0x7d9, %r9d            # imm = 0x7D9
xorl %eax, %eax
pushq $0x3f3                  # imm = 0x3F3
pushq $0x3f2                  # imm = 0x3F2
pushq $0x3fb                  # imm = 0x3FB
callq 0x35812
addq $0x20, %rsp
testl %eax, %eax
je 0x5481
leaq 0x90106(%rip), %rdx     # 0x95585
jmp 0x54a6
pushq $0x2
popq %rsi
movl %ebx, %edi
movl $0x403, %edx            # imm = 0x403
movl $0x40c, %ecx            # imm = 0x40C
xorl %eax, %eax
callq 0x35812
testl %eax, %eax
je 0x58c9
leaq 0x902f9(%rip), %rdx     # 0x9579f
movq %r14, 0x70(%rsp)
movq 0x8(%r14), %rdi
leaq 0x995ee(%rip), %rsi     # 0x9eaa4
callq 0x9454e
movq %rax, %r15
movq %rax, %rdi
callq 0x944ea
leal -0x7d0(%rbx), %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %r8
leaq 0xc7356(%rip), %rcx     # 0xcc830
leaq (%rcx,%r8), %rdx
leal -0x3e8(%rbx), %esi
movslq %esi, %rsi
imulq $0x28, %rsi, %rsi
leaq 0xc6cce(%rip), %rdi     # 0xcc1c0
leaq (%rdi,%rsi), %r9
testl %eax, %eax
cmovlel %r12d, %eax
movl %eax, 0x44(%rsp)
cmpl $0x0, 0x1c(%rsp)
movq 0x20(%r15), %rax
movq %rax, 0x10(%rsp)
leaq 0x8(%r8,%rcx), %rax
movq %rax, 0x78(%rsp)
cmovneq %rdx, %r9
movq %r9, 0x88(%rsp)
leaq 0x8(%rsi,%rdi), %r13
leal -0x3e9(%rbx), %r14d
leal -0x7d1(%rbx), %eax
movq %rax, 0x48(%rsp)
xorl %r15d, %r15d
movq %rbx, 0x50(%rsp)
movq %r14, 0x80(%rsp)
cmpl 0x44(%rsp), %r12d
je 0x587e
cmpl $0x23, %r14d
ja 0x5589
leaq 0x8fd15(%rip), %rcx     # 0x95278
movslq (%rcx,%r14,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
movq 0x8(%rsp), %rax
movsd %xmm0, 0x8(%rax)
jmp 0x569f
cmpl $0xa, 0x48(%rsp)
ja 0x569f
movq 0x48(%rsp), %rax
leaq 0x8fd68(%rip), %rcx     # 0x95308
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
movq 0x8(%rsp), %rax
movsd %xmm0, 0x38(%rax)
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rsi
leaq 0x901d5(%rip), %rdi     # 0x957ab
movsd 0x8ff32(%rip), %xmm0    # 0x95510
movsd 0x8ff32(%rip), %xmm1    # 0x95518
callq 0x4cdfb
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
movq 0x8(%rsp), %rax
movsd %xmm0, 0x28(%rax)
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4200
movq 0x8(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
movq 0x8(%rsp), %rax
movsd %xmm0, 0x30(%rax)
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
movq 0x8(%rsp), %rax
movsd %xmm0, 0x20(%rax)
jmp 0x569f
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4200
movq 0x8(%rsp), %rcx
movl %eax, 0x4(%rcx)
leaq 0xc81fb(%rip), %rcx     # 0xcd870
movq (%rcx), %rcx
movl 0x1bc(%rcx), %ecx
decl %ecx
cmpl %ecx, %eax
jl 0x569f
jmp 0x5876
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4200
movq 0x8(%rsp), %rcx
movl %eax, 0x4(%rcx)
movq %r15, 0x58(%rsp)
movq %r12, 0x68(%rsp)
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x88(%rsp), %rax
callq *(%rax)
movq %rax, %r15
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x90(%rsp), %rbx
movq (%rbx), %r12
testq %r12, %r12
je 0x57a4
leaq 0x10(%r12), %rbx
movq 0x10(%r12), %rbp
andq $0x0, 0x10(%r12)
cmpl $0x0, 0x1c(%rsp)
jne 0x5746
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0x38(%rsp), %r8
callq *(%r13)
movl 0xa8(%rsp), %r14d
movq %rbp, 0x10(%r12)
movq %r12, %rdi
callq 0x2b47e
cmpl %r14d, %eax
je 0x56d7
movsd 0x20(%rsp), %xmm0
addsd 0x8fde2(%rip), %xmm0    # 0x95520
movsd %xmm0, 0x20(%rsp)
jmp 0x56d7
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
xorl %r8d, %r8d
movq 0x78(%rsp), %rax
callq *(%rax)
movsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movq %rbp, 0x10(%r12)
movq %r12, %rdi
callq 0x2b4a0
subsd 0x98(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
jmp 0x56d7
movq 0x68(%rsp), %r12
testl %r12d, %r12d
je 0x57d7
movsd 0x60(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
ja 0x57d7
movq %r15, %rsi
movq 0x58(%rsp), %r15
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x50(%rsp), %rbx
jmp 0x5843
leaq 0x3a4842(%rip), %rax    # 0x3aa020
movl (%rax), %r12d
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4130
leaq 0x8ffba(%rip), %rdi     # 0x957b0
movl %r12d, %esi
pushq $0xf
popq %rdx
leaq 0x92df6(%rip), %rcx     # 0x985f9
movb $0x1, %al
callq 0x2f310
movq 0x50(%rsp), %rbx
cmpl $0x7d1, %ebx            # imm = 0x7D1
movq 0x58(%rsp), %r14
je 0x5824
cmpl $0x3e9, %ebx            # imm = 0x3E9
jne 0x5836
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x4200
movl %eax, 0x40(%rsp)
movq %r14, %rsi
movq 0x68(%rsp), %r12
testl %r12d, %r12d
je 0x5854
leaq 0xc8026(%rip), %rax     # 0xcd870
movq (%rax), %rdx
movl %ebx, %edi
callq 0x4d2f
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq %rax, 0x10(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x80(%rsp), %r14
incl %r12d
jmp 0x554b
cmpl $0x7d1, %ebx            # imm = 0x7D1
movq 0x70(%rsp), %rdi
je 0x5893
cmpl $0x3e9, %ebx            # imm = 0x3E9
jne 0x589e
movq 0x8(%rsp), %rax
movl 0x40(%rsp), %ecx
movl %ecx, (%rax)
callq 0x94639
cmpl $0x0, 0x1c(%rsp)
jne 0x58b4
movq 0x38(%rsp), %rdi
callq 0x34ff7
movq %r15, %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x9030a(%rip), %rdi     # 0x95bda
callq 0x4090
cmpl $0x0, 0x1c(%rsp)
jne 0x532b
addl $0xfffffc18, %ebx       # imm = 0xFFFFFC18
movslq %ebx, %rax
imulq $0x28, %rax, %rax
leaq 0xc68cc(%rip), %rcx     # 0xcc1c0
movq (%rax,%rcx), %rax
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmpq *%rax