// File: /workspace/github_cpp_makefile_star2_O1/qmmlib[P]QmmLibrary/linear.cpp
// Function: solve_l2r_l1l2_svr(problem const*, double*, parameter const*, int)
static void solve_l2r_l1l2_svr(
	const problem *prob, double *w, const parameter *param,
	int solver_type)
{
	int l = prob->l;
	double C = param->C;
	double p = param->p;
	int w_size = prob->n;
	double eps = param->eps;
	int i, s, iter = 0;
	int max_iter = 1000;
	int active_size = l;
	int *index = new int[l];

	double d, G, H;
	double Gmax_old = INF;
	double Gmax_new, Gnorm1_new;
	double Gnorm1_init;
	double *beta = new double[l];
	double *QD = new double[l];
	double *y = prob->y;

	// L2R_L2LOSS_SVR_DUAL
	double lambda[1], upper_bound[1];
	lambda[0] = 0.5/C;
	upper_bound[0] = INF;

	if(solver_type == L2R_L1LOSS_SVR_DUAL)
	{
		lambda[0] = 0;
		upper_bound[0] = C;
	}

	// Initial beta can be set here. Note that
	// -upper_bound <= beta[i] <= upper_bound
	for(i=0; i<l; i++)
		beta[i] = 0;

	for(i=0; i<w_size; i++)
		w[i] = 0;
	for(i=0; i<l; i++)
	{
		QD[i] = 0;
		feature_node *xi = prob->x[i];
		while(xi->index != -1)
		{
			double val = xi->value;
			QD[i] += val*val;
			w[xi->index-1] += beta[i]*val;
			xi++;
		}

		index[i] = i;
	}


	while(iter < max_iter)
	{
		Gmax_new = 0;
		Gnorm1_new = 0;

		for(i=0; i<active_size; i++)
		{
			int j = i+rand()%(active_size-i);
			swap(index[i], index[j]);
		}

		for(s=0; s<active_size; s++)
		{
			i = index[s];
			G = -y[i] + lambda[GETI(i)]*beta[i];
			H = QD[i] + lambda[GETI(i)];

			feature_node *xi = prob->x[i];
			while(xi->index != -1)
			{
				int ind = xi->index-1;
				double val = xi->value;
				G += val*w[ind];
				xi++;
			}

			double Gp = G+p;
			double Gn = G-p;
			double violation = 0;
			if(beta[i] == 0)
			{
				if(Gp < 0)
					violation = -Gp;
				else if(Gn > 0)
					violation = Gn;
				else if(Gp>Gmax_old && Gn<-Gmax_old)
				{
					active_size--;
					swap(index[s], index[active_size]);
					s--;
					continue;
				}
			}
			else if(beta[i] >= upper_bound[GETI(i)])
			{
				if(Gp > 0)
					violation = Gp;
				else if(Gp < -Gmax_old)
				{
					active_size--;
					swap(index[s], index[active_size]);
					s--;
					continue;
				}
			}
			else if(beta[i] <= -upper_bound[GETI(i)])
			{
				if(Gn < 0)
					violation = -Gn;
				else if(Gn > Gmax_old)
				{
					active_size--;
					swap(index[s], index[active_size]);
					s--;
					continue;
				}
			}
			else if(beta[i] > 0)
				violation = fabs(Gp);
			else
				violation = fabs(Gn);

			Gmax_new = max(Gmax_new, violation);
			Gnorm1_new += violation;

			// obtain Newton direction d
			if(Gp < H*beta[i])
				d = -Gp/H;
			else if(Gn > H*beta[i])
				d = -Gn/H;
			else
				d = -beta[i];

			if(fabs(d) < 1.0e-12)
				continue;

			double beta_old = beta[i];
			beta[i] = min(max(beta[i]+d, -upper_bound[GETI(i)]), upper_bound[GETI(i)]);
			d = beta[i]-beta_old;

			if(d != 0)
			{
				xi = prob->x[i];
				while(xi->index != -1)
				{
					w[xi->index-1] += d*xi->value;
					xi++;
				}
			}
		}

		if(iter == 0)
			Gnorm1_init = Gnorm1_new;
		iter++;
		if(iter % 10 == 0)
			info(".");

		if(Gnorm1_new <= eps*Gnorm1_init)
		{
			if(active_size == l)
				break;
			else
			{
				active_size = l;
				info("*");
				Gmax_old = INF;
				continue;
			}
		}

		Gmax_old = Gmax_new;
	}

	info("\noptimization finished, #iter = %d\n", iter);
	if(iter >= max_iter)
		info("\nWARNING: reaching max number of iterations\nUsing -s 11 may be faster\n\n");

	// calculate objective value
	double v = 0;
	int nSV = 0;
	for(i=0; i<w_size; i++)
		v += w[i]*w[i];
	v = 0.5*v;
	for(i=0; i<l; i++)
	{
		v += p*fabs(beta[i]) - y[i]*beta[i] + 0.5*lambda[GETI(i)]*beta[i]*beta[i];
		if(beta[i] != 0)
			nSV++;
	}

	info("Objective value = %lf\n", v);
	info("nSV = %d\n",nSV);

	delete [] beta;
	delete [] QD;
	delete [] index;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %ecx, 0x8(%rsp)
movq %rsi, %r12
movslq (%rdi), %r13
movsd 0x80(%rdx), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x88(%rdx), %xmm0
movaps %xmm0, 0x30(%rsp)
movsd 0xa8(%rdx), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rdi, 0x28(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x58(%rsp)
leaq (,%r13,4), %rdi
testq %r13, %r13
movq $-0x1, %r15
cmovsq %r15, %rdi
leaq (,%r13,8), %rbp
cmovnsq %rbp, %r15
callq 0x3030
movq %rax, %rbx
movq %r15, %rdi
callq 0x3030
movq %rax, %r14
movq %r15, %rdi
callq 0x3030
movapd 0x30(%rsp), %xmm4
movq %rax, %r15
xorpd %xmm0, %xmm0
cmpl $0xd, 0x8(%rsp)
je 0x13bde
movsd 0x34f6(%rip), %xmm0     # 0x170d0
divsd %xmm4, %xmm0
je 0x13be8
movsd 0x2420(%rip), %xmm4     # 0x16008
movq 0x28(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
testq %r13, %r13
movsd %xmm0, 0x18(%rsp)
movapd %xmm4, 0x30(%rsp)
jle 0x13c1a
movq %r14, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x3110
movapd 0x30(%rsp), %xmm4
movq 0x58(%rsp), %rax
testl %eax, %eax
jle 0x13c3b
leaq (,%rax,8), %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0x3110
movapd 0x30(%rsp), %xmm4
testl %r13d, %r13d
jle 0x13cb0
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movq $0x0, (%r15,%rcx,8)
movq (%rax,%rcx,8), %rdx
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0x13ca5
movsd (%r14,%rcx,8), %xmm0
movsd (%r15,%rcx,8), %xmm1
addq $0x10, %rdx
movsd -0x8(%rdx), %xmm2
movapd %xmm2, %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm1
movsd %xmm1, (%r15,%rcx,8)
movslq %esi, %rsi
mulsd %xmm0, %xmm2
addsd -0x8(%r12,%rsi,8), %xmm2
movsd %xmm2, -0x8(%r12,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0x13c6e
movl %ecx, (%rbx,%rcx,4)
incq %rcx
cmpq %r13, %rcx
jne 0x13c4b
movapd 0x3447(%rip), %xmm11    # 0x17100
xorpd %xmm4, %xmm11
movsd 0x2342(%rip), %xmm1     # 0x16008
xorl %eax, %eax
movq %r13, 0x60(%rsp)
movq %r14, 0x70(%rsp)
movapd %xmm11, 0x90(%rsp)
movapd %xmm1, 0x40(%rsp)
movq %rax, 0x78(%rsp)
testl %r13d, %r13d
jle 0x13d16
movl %r13d, %ebp
xorl %r14d, %r14d
callq 0x30a0
cltd
idivl %ebp
movslq %edx, %rax
addq %r14, %rax
movl (%rbx,%r14,4), %ecx
movl (%rbx,%rax,4), %edx
movl %edx, (%rbx,%r14,4)
movl %ecx, (%rbx,%rax,4)
incq %r14
decq %rbp
jne 0x13cf2
testl %r13d, %r13d
jle 0x13f73
movq 0x28(%rsp), %rax
movq 0x10(%rax), %rax
movapd 0x40(%rsp), %xmm15
movapd %xmm15, %xmm0
movapd 0x33c3(%rip), %xmm12    # 0x17100
xorpd %xmm12, %xmm0
xorpd %xmm1, %xmm1
movsd %xmm1, 0x10(%rsp)
xorl %ecx, %ecx
movsd %xmm1, 0x8(%rsp)
movq 0x70(%rsp), %r14
movsd 0x20(%rsp), %xmm7
movsd 0x18(%rsp), %xmm8
movq 0x68(%rsp), %r11
movapd 0x30(%rsp), %xmm9
movapd 0x3375(%rip), %xmm10    # 0x170f0
movapd 0x90(%rsp), %xmm11
xorpd %xmm13, %xmm13
movsd 0x334d(%rip), %xmm14    # 0x170e0
movslq %ecx, %rdi
movl (%rbx,%rdi,4), %r8d
movslq %r8d, %rsi
movsd (%r14,%rsi,8), %xmm1
movapd %xmm8, %xmm3
mulsd %xmm1, %xmm3
subsd (%r11,%rsi,8), %xmm3
movq (%rax,%rsi,8), %rdx
movl (%rdx), %r10d
cmpl $-0x1, %r10d
je 0x13de4
leaq 0x10(%rdx), %r9
movsd -0x8(%r9), %xmm2
movslq %r10d, %r10
mulsd -0x8(%r12,%r10,8), %xmm2
addsd %xmm2, %xmm3
movl (%r9), %r10d
addq $0x10, %r9
cmpl $-0x1, %r10d
jne 0x13dc3
movsd (%r15,%rsi,8), %xmm4
addsd %xmm8, %xmm4
movapd %xmm7, %xmm2
addsd %xmm3, %xmm2
subsd %xmm7, %xmm3
ucomisd %xmm13, %xmm1
jne 0x13e16
jp 0x13e16
ucomisd %xmm2, %xmm13
jbe 0x13e31
movapd %xmm2, %xmm5
xorpd %xmm12, %xmm5
jmp 0x13e87
ucomisd %xmm9, %xmm1
jae 0x13e53
ucomisd %xmm1, %xmm11
jae 0x13e6d
ucomisd %xmm13, %xmm1
jbe 0x13e7e
movapd %xmm2, %xmm5
jmp 0x13e82
ucomisd %xmm13, %xmm3
movapd %xmm3, %xmm5
ja 0x13e87
ucomisd %xmm15, %xmm2
xorpd %xmm5, %xmm5
jbe 0x13e87
ucomisd %xmm3, %xmm0
ja 0x13f50
jmp 0x13e87
ucomisd %xmm13, %xmm2
movapd %xmm2, %xmm5
ja 0x13e87
xorpd %xmm5, %xmm5
ucomisd %xmm2, %xmm0
jbe 0x13e87
jmp 0x13f50
ucomisd %xmm3, %xmm13
jbe 0x13f41
movapd %xmm3, %xmm5
jmp 0x13e0f
movapd %xmm3, %xmm5
andpd %xmm10, %xmm5
movapd %xmm1, %xmm6
mulsd %xmm4, %xmm6
ucomisd %xmm2, %xmm6
jbe 0x13ea0
xorpd %xmm12, %xmm2
divsd %xmm4, %xmm2
jmp 0x13ebe
ucomisd %xmm6, %xmm3
jbe 0x13eb5
xorpd %xmm12, %xmm3
divsd %xmm4, %xmm3
movapd %xmm3, %xmm2
jmp 0x13ebe
movapd %xmm1, %xmm2
xorpd %xmm12, %xmm2
movsd 0x10(%rsp), %xmm3
maxsd %xmm5, %xmm3
movsd %xmm3, 0x10(%rsp)
movsd 0x8(%rsp), %xmm3
addsd %xmm5, %xmm3
movsd %xmm3, 0x8(%rsp)
movapd %xmm2, %xmm3
andpd %xmm10, %xmm3
ucomisd %xmm3, %xmm14
ja 0x13f66
addsd %xmm1, %xmm2
maxsd %xmm11, %xmm2
minsd %xmm9, %xmm2
movsd %xmm2, (%r14,%rsi,8)
subsd %xmm1, %xmm2
ucomisd %xmm13, %xmm2
jne 0x13f0f
jnp 0x13f66
movl (%rdx), %esi
cmpl $-0x1, %esi
je 0x13f66
addq $0x10, %rdx
movslq %esi, %rsi
movsd -0x8(%rdx), %xmm1
mulsd %xmm2, %xmm1
addsd -0x8(%r12,%rsi,8), %xmm1
movsd %xmm1, -0x8(%r12,%rsi,8)
movl (%rdx), %esi
addq $0x10, %rdx
cmpl $-0x1, %esi
jne 0x13f1a
jmp 0x13f66
xorpd %xmm5, %xmm5
ucomisd %xmm15, %xmm3
jbe 0x13e87
movslq %r13d, %rcx
decl %r13d
movl -0x4(%rbx,%rcx,4), %edx
movl %edx, (%rbx,%rdi,4)
movl %r8d, -0x4(%rbx,%rcx,4)
decl %edi
movl %edi, %ecx
incl %ecx
cmpl %r13d, %ecx
jl 0x13d93
jmp 0x13f88
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %r14
movsd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm2
movq 0x78(%rsp), %rcx
testl %ecx, %ecx
je 0x13fa4
movsd 0x88(%rsp), %xmm2
leal 0x1(%rcx), %ebp
imull $0xcccccccd, %ebp, %eax # imm = 0xCCCCCCCD
rorl %eax
cmpl $0x19999999, %eax       # imm = 0x19999999
ja 0x13fdb
leaq 0x3991(%rip), %rdi      # 0x1794e
xorl %eax, %eax
movsd %xmm2, 0x40(%rsp)
callq 0x7152
movsd 0x40(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movq 0x78(%rsp), %rcx
movsd 0x80(%rsp), %xmm0
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jae 0x13ff6
movsd 0x10(%rsp), %xmm1
jmp 0x14025
movq 0x60(%rsp), %rax
cmpl %eax, %r13d
je 0x14049
movq %rax, %r13
leaq 0x346e(%rip), %rdi      # 0x17478
xorl %eax, %eax
movsd %xmm2, 0x40(%rsp)
callq 0x7152
movsd 0x40(%rsp), %xmm2
movsd 0x1fe3(%rip), %xmm1     # 0x16008
movl %ebp, %eax
movsd %xmm2, 0x88(%rsp)
cmpl $0x3e8, %ebp            # imm = 0x3E8
jne 0x13cdc
movl $0x3e7, %r13d           # imm = 0x3E7
movl $0x3e8, %ebp            # imm = 0x3E8
jmp 0x1404c
movq %rcx, %r13
leaq 0x3427(%rip), %rdi      # 0x1747a
movl %ebp, %esi
xorl %eax, %eax
callq 0x7152
cmpl $0x3e7, %r13d           # imm = 0x3E7
jb 0x14073
leaq 0x36ea(%rip), %rdi      # 0x17756
xorl %eax, %eax
callq 0x7152
movq 0x58(%rsp), %rcx
testl %ecx, %ecx
movq 0x60(%rsp), %rdx
movapd 0x3069(%rip), %xmm7     # 0x170f0
xorpd %xmm0, %xmm0
jle 0x140bb
xorl %eax, %eax
movsd 0x20(%rsp), %xmm5
movsd 0x18(%rsp), %xmm6
movsd (%r12,%rax,8), %xmm1
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm0
incq %rax
cmpq %rax, %rcx
jne 0x1409b
mulsd 0x3017(%rip), %xmm0     # 0x170d0
jmp 0x140c7
movsd 0x20(%rsp), %xmm5
movsd 0x18(%rsp), %xmm6
testl %edx, %edx
jle 0x14129
mulsd 0x2ffd(%rip), %xmm6     # 0x170d0
xorl %eax, %eax
xorpd %xmm1, %xmm1
xorl %ebp, %ebp
movq 0x68(%rsp), %rsi
movsd (%r14,%rax,8), %xmm2
movapd %xmm2, %xmm3
andpd %xmm7, %xmm3
movsd (%rsi,%rax,8), %xmm4
mulsd %xmm2, %xmm4
mulsd %xmm5, %xmm3
subsd %xmm4, %xmm3
movapd %xmm6, %xmm4
mulsd %xmm2, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
addsd %xmm4, %xmm0
cmpneqsd %xmm1, %xmm2
movq %xmm2, %rcx
subl %ecx, %ebp
incq %rax
cmpq %rax, %rdx
jne 0x140e0
jmp 0x1412b
xorl %ebp, %ebp
leaq 0x3399(%rip), %rdi      # 0x174cb
movb $0x1, %al
callq 0x7152
leaq 0x33a2(%rip), %rdi      # 0x174e2
movl %ebp, %esi
xorl %eax, %eax
callq 0x7152
movq %r14, %rdi
callq 0x32c0
movq %r15, %rdi
callq 0x32c0
movq %rbx, %rdi
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x32c0