// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/mlp.c
// Function: dnc_cls
Mlpnetwork dnc_cls(matrix Train, Mlpparametersptr P)
{
 /*!Last Changed 16.04.2007*/
 Mlpnetwork nN;
 matrixptr prevWeights, H;
 double *errorinwidth, sqrdError, lastError, control;
 int tr, trial, *I, iindex, k, lastNum = 0;
 nN = allocate_mlpnnetwork(*P);
 prevWeights = allocate_weights(*P, INITIAL_VALUE, 0);
 H = allocate_hiddens(*P);
 errorinwidth = safemalloc(P->windowsize * sizeof(double), "dnc_cls", 8);
 P->eta = P->initialeta;
 for (tr = 0; tr < P->epochs; tr++)
  {
   trial = tr + 1;
   sqrdError = 0.0;  
   I = random_array(P->inputnum);
   for (k = 0; k < P->inputnum; k++)
    {
     iindex = I[k];
     sqrdError = sqrdError + backPropMlpn_cls(Train.values[iindex], nN.weights, H, prevWeights, *P);
    }
   safe_free(I);
   sqrdError = sqrdError / P->inputnum; 
   P->sqrderror = sqrdError;
   if (sqrdError <= P->errorthreshold)
     break;
   if (tr == 0)
     lastError = sqrdError;  
   if ((trial > P->windowsize) && ((trial - P->windowsize) >= lastNum))
    {
				 if (P->maxhidden != 0 && P->hidden[0] == P->maxhidden)
						 break;
     control = fabs(sqrdError - errorinwidth[(trial - 1) % P->windowsize]) / lastError;
     if (control < P->errordropratio)
      {
       lastError = sqrdError;
       lastNum = trial;
       free_weights(prevWeights, P->layernum);
							reallocate_weights(*P, nN.weights, 0, 1, RANDOM, 0);
							reallocate_hiddens(H, *P, 0, 1);
							H[0].values[0][P->hidden[0]] = 0.0;
       P->hidden[0]++;
       P->eta = P->initialeta;
       prevWeights = allocate_weights(*P, INITIAL_VALUE, 0);
      }
    }
   errorinwidth[(trial - 1) % P->windowsize] = sqrdError;    
   P->eta = P->eta * P->etadecrease;
  }    
 nN.P = *P;
 safe_free(errorinwidth);
 free_weights(prevWeights, P->layernum);
 free_hiddens(H, P->layernum);    
 P->epochstrained = trial;
 return nN;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %rcx, %rbx
movq %rdx, %r15
movq %rdi, %r14
leaq 0xc8(%rsp), %r12
movl $0x90, %edx
movq %r12, %rdi
movq %rcx, %rsi
callq 0x4310
pushq $0x12
popq %r13
movq %r13, %rcx
movq %rsp, %rdi
movq %r12, %rsi
rep  movsq (%rsi), %es:(%rdi)
xorl %edi, %edi
xorl %esi, %esi
callq 0x3eb62
movq %rax, (%r14)
movq %r14, 0xa8(%rsp)
leaq 0x8(%r14), %rdi
xorl %r14d, %r14d
movl $0x90, %edx
movq %rdi, 0xb8(%rsp)
movq %r12, %rsi
callq 0x4310
movq %r13, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
pushq $0x1
popq %rdi
xorl %esi, %esi
callq 0x3eb62
movq %rax, %r12
movq %r13, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x3ee10
movq %rax, %rbp
movslq 0x68(%rbx), %rdi
shlq $0x3, %rdi
leaq 0x5a198(%rip), %rsi     # 0x9a2e6
pushq $0x8
popq %rdx
callq 0x34f59
movq %rax, 0xa0(%rsp)
movsd 0x20(%rbx), %xmm1
movl $0x0, 0x90(%rsp)
movsd %xmm1, 0x38(%rbx)
cmpl 0x14(%rbx), %r14d
jge 0x4039c
movq %r14, 0xb0(%rsp)
movl 0x8(%rbx), %edi
callq 0x35cf7
movq %rax, %r13
xorpd %xmm1, %xmm1
xorl %r14d, %r14d
movsd %xmm1, 0x98(%rsp)
movslq 0x8(%rbx), %rax
cmpq %rax, %r14
jge 0x401ec
movslq (%r13,%r14,4), %rax
movq (%r15,%rax,8), %rax
movq 0xa8(%rsp), %rcx
movq (%rcx), %rdx
pushq $0x12
popq %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
movq %rdx, %rsi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x3f3eb
movsd 0x98(%rsp), %xmm1
addsd %xmm0, %xmm1
incq %r14
jmp 0x40197
movq 0xb0(%rsp), %r14
leal 0x1(%r14), %eax
movl %eax, 0x94(%rsp)
movq %r13, %rdi
callq 0x34ff7
cvtsi2sdl 0x8(%rbx), %xmm0
movsd 0x98(%rsp), %xmm3
divsd %xmm0, %xmm3
movsd %xmm3, 0x88(%rbx)
movsd 0x70(%rbx), %xmm0
ucomisd %xmm3, %xmm0
jae 0x40394
movapd %xmm3, %xmm0
testl %r14d, %r14d
je 0x40242
movsd 0xc0(%rsp), %xmm0
movl 0x68(%rbx), %ecx
cmpl %ecx, %r14d
setl %al
movl 0x94(%rsp), %r13d
movl %r13d, %edx
subl %ecx, %edx
cmpl 0x90(%rsp), %edx
setl %dl
orb %al, %dl
jne 0x40366
movl 0x84(%rbx), %eax
testl %eax, %eax
je 0x4027d
cmpl %eax, 0x48(%rbx)
je 0x4039c
movl %r14d, %eax
cltd
idivl %ecx
movapd %xmm3, %xmm1
movq 0xa0(%rsp), %rax
subsd (%rax,%rdx,8), %xmm1
andpd 0x55f44(%rip), %xmm1    # 0x961e0
divsd %xmm0, %xmm1
movsd 0x78(%rbx), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x40366
movl 0x54(%rbx), %esi
movq %r12, %rdi
movsd %xmm3, 0x98(%rsp)
callq 0x3eec0
movq 0xa8(%rsp), %rax
movq (%rax), %rax
pushq $0x12
popq %r14
movq %r14, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
xorl %esi, %esi
pushq $0x1
popq %r12
movl %r12d, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3ecae
movl 0x48(%rbx), %ecx
incl %ecx
movq (%rbp), %rdi
movq 0x8(%rbp), %rsi
movl %r12d, %edx
callq 0x3993b
movq %rax, (%rbp)
movq %rdx, 0x8(%rbp)
movq (%rdx), %rax
movslq 0x48(%rbx), %rcx
andq $0x0, (%rax,%rcx,8)
leal 0x1(%rcx), %eax
movl %eax, 0x48(%rbx)
movsd 0x20(%rbx), %xmm0
movsd %xmm0, 0x38(%rbx)
movq %r14, %rcx
movq 0xb0(%rsp), %r14
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
movl %r12d, %edi
xorl %esi, %esi
callq 0x3eb62
movsd 0x98(%rsp), %xmm3
movq %rax, %r12
movl 0x68(%rbx), %ecx
movapd %xmm3, %xmm0
movl %r13d, 0x90(%rsp)
movl %r14d, %eax
cltd
idivl %ecx
movq 0xa0(%rsp), %rax
movsd %xmm3, (%rax,%rdx,8)
movsd 0x38(%rbx), %xmm1
mulsd 0x40(%rbx), %xmm1
movsd %xmm0, 0xc0(%rsp)
movl %r13d, %r14d
jmp 0x4016e
movl 0x94(%rsp), %r13d
movl $0x90, %edx
movq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x4310
movq 0xa0(%rsp), %rdi
callq 0x34ff7
movl 0x54(%rbx), %esi
movq %r12, %rdi
callq 0x3eec0
movl 0x54(%rbx), %esi
movq %rbp, %rdi
callq 0x3ef02
movl %r13d, 0x80(%rbx)
movq 0xa8(%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq