// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/regressionrule.c
// Function: optimize_regression_ruleset
void optimize_regression_ruleset(Regressionruleset* r, Instanceptr* traindata, Instanceptr* cvdata, Regrule_parameterptr param)
{
	/*!Last Changed 03.04.2005*/
	/*!Last Changed 05.03.2005*/
	Regressionruleptr rule, before = NULL, replacementrule, revisionrule, next, newrule;
	Instanceptr removedtrain = NULL, removedcv = NULL, newsample, grow, prune;
	matrix w;
	int exitnow;
	double error, revisionerror, replacementerror, regvalue;
	rule = r->start;
	while (rule && can_be_divided_rule(*traindata, param->sigma, &w, &regvalue, r->leaftype))
	 {
			if (*cvdata)
				{
		   error = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
     divide_instancelist(traindata, &prune, &grow, 3);
     replacementrule = grow_regression_rule(&grow, param);
  			if (replacementrule->conditioncount > 0)
						 prune_regression_rule(replacementrule, prune, param->modelselectionmethod);
				}
			else
			 {
		   error = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
				 newsample = bootstrap_sample_without_stratification(*traindata);
					replacementrule = grow_regression_rule(&newsample, param);
  			if (replacementrule->conditioncount > 0)
						 prune_regression_rule(replacementrule, *traindata, param->modelselectionmethod);
			 }
			remove_regression_rule_after(r, before, rule);
			add_regression_rule_after(r, before, replacementrule);
			if (*cvdata)
			 {
  		 replacementerror = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
				 revisionrule = grow_regression_revision_rule(&grow, rule, param);
					if (revisionrule->conditioncount > 0)
						 prune_regression_rule(revisionrule, prune, param->modelselectionmethod);
			 }
			else
			 {
				 replacementerror = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
  			deallocate(newsample);
				 revisionrule = grow_regression_revision_rule(traindata, rule, param);
					if (revisionrule->conditioncount > 0)
       prune_regression_rule(revisionrule, *traindata, param->modelselectionmethod);
			 }
			remove_regression_rule_after(r, before, replacementrule);
			add_regression_rule_after(r, before, revisionrule);
			if (*cvdata)
			 {
  		 revisionerror = generalization_error_of_regression_ruleset(*r, *cvdata, param->modelselectionmethod);
  			*traindata = restore_instancelist(*traindata, grow);
  			merge_instancelist(traindata, prune);
			 }
			else
				 revisionerror = generalization_error_of_regression_ruleset(*r, *traindata, param->modelselectionmethod);
   if ((revisionerror < error) || (replacementerror < error))
				 if (replacementerror <= revisionerror)
					 {
						 remove_regression_rule_after(r, before, revisionrule);
							add_regression_rule_after(r, before, replacementrule);
							free_regression_rule(*revisionrule);
							safe_free(revisionrule);
							free_regression_rule(*rule);
							safe_free(rule);
  				 remove_regression_covered(traindata, &removedtrain, replacementrule);
    			if (*cvdata)
    				 remove_regression_covered(cvdata, &removedcv, replacementrule);
							rule = replacementrule;
					 }
					else
					 {
							free_regression_rule(*replacementrule);
							safe_free(replacementrule);
							free_regression_rule(*rule);
							safe_free(rule);						 
  				 remove_regression_covered(traindata, &removedtrain, revisionrule);
    			if (*cvdata)
    				 remove_regression_covered(cvdata, &removedcv, revisionrule);
							rule = revisionrule;
					 }
			else
			 {
					remove_regression_rule_after(r, before, revisionrule);
					add_regression_rule_after(r, before, rule);
					free_regression_rule(*revisionrule);
					safe_free(revisionrule);
					free_regression_rule(*replacementrule);
					safe_free(replacementrule);
  			remove_regression_covered(traindata, &removedtrain, rule);
  			if (*cvdata)
    		 remove_regression_covered(cvdata, &removedcv, rule);
			 }
			before = rule;
			rule = rule->next;
	 }
	if (!rule)
	 {
  	while (can_be_divided_rule(*traindata, param->sigma, &w, &regvalue, r->leaftype))
			 {
	  	 exitnow = 0;
  			if (*cvdata)
					 {
       divide_instancelist(traindata, &prune, &grow, 3);
       newrule = grow_regression_rule(&grow, param);
					 }
			  else
				  	newrule = grow_regression_rule(traindata, param);
  	  if (newrule && newrule->start)
					 {
    			if (*cvdata)
				  		 prune_regression_rule(newrule, prune, param->modelselectionmethod);
				  	else
					  	 prune_regression_rule(newrule, *traindata, param->modelselectionmethod);
					  if (newrule->start)
							 {
				  		 add_regression_rule(r, newrule);
      			if (*cvdata)
									 {
  			  	   *traindata = restore_instancelist(*traindata, grow);
  				  	  merge_instancelist(traindata, prune);
									 }
							 }
				  	else
					  	 exitnow = 1;
					 }
			  else
				   exitnow = 1;
			  if (!exitnow)
					 {
			  	 remove_regression_covered(traindata, &removedtrain, newrule);
  	  	 if (r->leaftype == LINEARLEAF)
      		 matrix_free(w);				 
					 }
			  else
					 {
    			if (*cvdata)
								{
  			  	 *traindata = restore_instancelist(*traindata, grow);
  				  	merge_instancelist(traindata, prune);
								}
				   break;
					 }
			 }
	  if (r->leaftype == CONSTANTLEAF)
	  	 r->regressionvalue = regvalue;
	  else
    	r->w = w;
	 }
	else
	 {
  	while (rule)
			 {
	  	 next = rule->next;
					if (before)
		  		 before->next = next;
					else
							r->start = next;
     free_regression_rule(*rule);
		  	safe_free(rule);
     r->rulecount--;
     if (next == NULL)
			  		r->end = before;
			  rule = next;
			 }
	  if (before)
	    before->next = NULL;
	 }
	merge_instancelist(traindata, removedtrain);
	if (*cvdata)
		 merge_instancelist(cvdata, removedcv);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rcx, %r12
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r15
leaq 0x70(%rsp), %rax
andq $0x0, (%rax)
leaq 0xa8(%rsp), %rax
andq $0x0, (%rax)
xorl %r14d, %r14d
movq %rdi, %rax
movq %rdx, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rcx, 0x78(%rsp)
movq (%rax), %rbx
testq %rbx, %rbx
je 0x6ae5c
movq (%r13), %rdi
movsd 0x10(%r12), %xmm0
movl 0x20(%r15), %ecx
leaq 0xb0(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x6b081
testl %eax, %eax
je 0x6af85
movq (%rbp), %rdi
testq %rdi, %rdi
movq %r14, 0x88(%rsp)
je 0x6aa6b
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x6a520
movsd %xmm0, 0x80(%rsp)
movq %r13, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x68(%rsp), %r14
movq %r14, %rdx
pushq $0x3
popq %rcx
callq 0x2cfe5
movq %r14, %rdi
movq %r12, %rsi
callq 0x6b153
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x6aa61
movq 0x60(%rsp), %rsi
movl 0x4(%r12), %edx
movq %r13, %rdi
callq 0x6b276
movq 0x88(%rsp), %r14
jmp 0x6aae4
movq (%r13), %rdi
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x6a520
movsd %xmm0, 0x80(%rsp)
movq (%r13), %rdi
callq 0x2c859
movq %rax, 0xa0(%rsp)
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
callq 0x6b153
movq %r13, %rcx
movq %rax, %r13
cmpl $0x0, 0x30(%rax)
jle 0x6aae4
movq (%rcx), %rsi
movl 0x4(%r12), %edx
movq %r13, %rdi
callq 0x6b276
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6a5f0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x6a617
movq (%rbp), %rdi
testq %rdi, %rdi
je 0x6ab72
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x6a520
movsd %xmm0, 0x90(%rsp)
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x6b33c
movq %rax, %rbp
cmpl $0x0, 0x30(%rax)
jle 0x6abee
movq 0x60(%rsp), %rsi
movl 0x4(%r12), %edx
movq %rbp, %rdi
callq 0x6b276
jmp 0x6abee
movq 0x50(%rsp), %r14
movq (%r14), %rdi
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x6a520
movsd %xmm0, 0x90(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x2c010
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x6b33c
movq %rax, %rbp
cmpl $0x0, 0x30(%rax)
jle 0x6abe6
movq (%r14), %rsi
movl 0x4(%r12), %edx
movq %rbp, %rdi
callq 0x6b276
movq 0x88(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x6a5f0
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x6a617
movq 0x58(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x6ac7e
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x6a520
movsd %xmm0, 0xc0(%rsp)
movq 0x50(%rsp), %r12
movq (%r12), %rdi
movq 0x68(%rsp), %rsi
callq 0x2d2da
movq %rax, (%r12)
movq 0x60(%rsp), %rsi
movq %r12, %rdi
callq 0x2c6e4
movsd 0xc0(%rsp), %xmm0
jmp 0x6acb7
movq 0x50(%rsp), %rax
movq (%rax), %rdi
movl 0x4(%r12), %esi
movq 0x30(%r15), %rax
movq %rax, 0x30(%rsp)
movupd (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups 0x20(%r15), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movupd %xmm0, (%rsp)
callq 0x6a520
movsd 0x80(%rsp), %xmm2
ucomisd %xmm0, %xmm2
movsd 0x90(%rsp), %xmm1
ja 0x6ad79
ucomisd %xmm1, %xmm2
ja 0x6ad79
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x6a5f0
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6a617
pushq $0x9
popq %r14
movq %r14, %rcx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %rbp, %rdi
callq 0x34ff7
movq %r14, %rcx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %r13, %rdi
callq 0x34ff7
movq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbx, %rdx
callq 0x6b46d
movq 0x58(%rsp), %rbp
cmpq $0x0, (%rbp)
movq %rbx, %r13
movq 0x78(%rsp), %r12
je 0x6ad68
movq %rbp, %rdi
leaq 0xa8(%rsp), %rsi
movq %r13, %rdx
callq 0x6b46d
leaq 0x38(%r13), %rax
movq %r13, %r14
movq 0x50(%rsp), %r13
jmp 0x6a99e
ucomisd %xmm1, %xmm0
movq 0x78(%rsp), %r12
jae 0x6ade3
pushq $0x9
popq %r14
movq %r14, %rcx
movq %rsp, %rdi
movq %r13, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %r13, %rdi
callq 0x34ff7
movq %r14, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %rbx, %rdi
callq 0x34ff7
movq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %rbp, %rdx
callq 0x6b46d
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
movq %rbp, %r13
movq %rax, %rbp
jne 0x6ad55
jmp 0x6ad68
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x6a5f0
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x6a617
pushq $0x9
popq %r14
movq %r14, %rcx
movq %rsp, %rdi
movq %rbp, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %rbp, %rdi
callq 0x34ff7
movq %r14, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %rbx, %rdi
callq 0x34ff7
movq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq %r13, %rdx
callq 0x6b46d
movq 0x58(%rsp), %rbp
cmpq $0x0, (%rbp)
jne 0x6ad55
jmp 0x6ad68
leaq 0xb0(%rsp), %rbx
leaq 0x98(%rsp), %r14
leaq 0x70(%rsp), %r13
movq 0x50(%rsp), %r12
movq (%r12), %rdi
movq 0x78(%rsp), %rax
movsd 0x10(%rax), %xmm0
movl 0x20(%r15), %ecx
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6b081
testl %eax, %eax
je 0x6b022
cmpq $0x0, (%rbp)
movq %r12, %rdi
je 0x6aec0
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
leaq 0x68(%rsp), %r12
movq %r12, %rdx
pushq $0x3
popq %rcx
callq 0x2cfe5
movq %r12, %rdi
movq 0x78(%rsp), %r12
movq %r12, %rsi
callq 0x6b153
testq %rax, %rax
je 0x6aff4
movq %rax, %rbp
cmpq $0x0, 0x20(%rax)
je 0x6aff4
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
movl 0x4(%r12), %edx
leaq 0x60(%rsp), %rax
cmoveq 0x50(%rsp), %rax
movq (%rax), %rsi
movq %rbp, %rdi
callq 0x6b276
cmpq $0x0, 0x20(%rbp)
je 0x6aff4
movq %r15, %rdi
movq %rbp, %rsi
callq 0x6b511
movq 0x58(%rsp), %rax
cmpq $0x0, (%rax)
movq 0x50(%rsp), %r12
je 0x6af4d
movq (%r12), %rdi
movq 0x68(%rsp), %rsi
callq 0x2d2da
movq %rax, (%r12)
movq 0x60(%rsp), %rsi
movq %r12, %rdi
callq 0x2c6e4
movq %r12, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x6b46d
cmpl $0x1, 0x20(%r15)
movq 0x58(%rsp), %rbp
jne 0x6ae71
movq 0xb0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x3929f
jmp 0x6ae71
leaq 0x38(%r14), %rbp
testq %r14, %r14
movq %rbp, 0x80(%rsp)
cmoveq %r15, %rbp
pushq $0x9
popq %r12
testq %rbx, %rbx
je 0x6afd7
movq 0x38(%rbx), %r13
movq %r13, (%rbp)
movq %r12, %rcx
movq %rsp, %rdi
movq %rbx, %rsi
rep  movsq (%rsi), %es:(%rdi)
callq 0x6a646
movq %rbx, %rdi
callq 0x34ff7
decl 0x30(%r15)
movq %r13, %rbx
testq %r13, %r13
jne 0x6af9c
movq %r14, 0x8(%r15)
movq %r13, %rbx
jmp 0x6af9c
testq %r14, %r14
movq 0x58(%rsp), %rbp
movq 0x50(%rsp), %rdi
je 0x6b04e
movq 0x80(%rsp), %rax
andq $0x0, (%rax)
jmp 0x6b04e
movq 0x58(%rsp), %rbp
cmpq $0x0, (%rbp)
movq 0x50(%rsp), %rbx
je 0x6b022
movq (%rbx), %rdi
movq 0x68(%rsp), %rsi
callq 0x2d2da
movq %rax, (%rbx)
movq 0x60(%rsp), %rsi
movq %rbx, %rdi
callq 0x2c6e4
cmpl $0x0, 0x20(%r15)
je 0x6b03a
movupd 0xb0(%rsp), %xmm0
movupd %xmm0, 0x10(%r15)
jmp 0x6b049
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x28(%r15)
movq 0x50(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x2c6e4
cmpq $0x0, (%rbp)
je 0x6b06f
movq 0xa8(%rsp), %rsi
movq %rbp, %rdi
callq 0x2c6e4
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq