Maximize quadratic objective with linear constraints in R: Rsolnp or Auglag
<p>I am trying to find a solution to the following optimization problem using either auglag or Rsolnp. </p>

<pre><code>Max t(w1 - w2) * Kf * Sf * t(Kf) * (w1 - w2)
subject to Kc * w1 = Kc * w2
and sum(w1) = 1 and sum(w2) = 1 and w1,w2 &gt;= 0
Sc and Sf are variance covariance matrices at the coarse and fine level respectively.
Kc and Kf are exposure matrices as the coarse and fine level respectively.
Nc and Nf are nodes at which exposure nodes at the coarse and fine level.
</code></pre>

<p>This is effectively trying to find the wts of two portfolios w1 and w2 that would maximize the TEV at finer exposure level, subject to sum of wts = 1 and all wts > 0. There is another equality constraint too (This effectively means exposure at the coarse level are identical for the two portfolios). Rsolnp fails to maximize and gives back a solution where the objective function is 0 and auglag completely blows up and does not meet constraints with a bunch of warnings as well. </p>

<p>Can anyone please help me understand where am I going wrong?</p>

<pre><code>    seqFineNodes &lt;- c(1, 2, 3, 4, 5, 6)

Nc &lt;- c(2, 3, 5)

Kc &lt;- matrix(c(0.2481316799436,0.495478766935844,0,0,0,0,0,0,0.743360061619584,0.497321712603124,0,0,0,0,0,0.497321712603124,1.23913608908603,1.48240730986596), nrow=length(seqFineNodes), ncol=length(Nc))
dimnames(Kc) &lt;- list(as.character(seqFineNodes), as.character(Nc))

Sc &lt;- matrix(c(619.806079280659,627.832850585004,549.805085990891,627.832850585004,668.726833059322,624.524848194842,549.805085990891,624.524848194842,696.498483673357), nrow=length(Nc), ncol=length(Nc))
dimnames(Sc) &lt;- list(as.character(Nc), as.character(Nc))

Nf &lt;- c(2, 3, 4, 5)

Kf &lt;- matrix(c(0.2481316799436,0.495478766935844,0,0,0,0,0,0,0.743360061619584,0,0,0,0,0,0,0.994643425206249,0,0,0,0,0,0,1.23913608908603,1.48240730986596), nrow=length(seqFineNodes), ncol=length(Nf))
dimnames(Kf) &lt;- list(as.character(seqFineNodes), as.character(Nf))

Sf &lt;- matrix(c(619.806079280659,627.832850585004,602.504944834256,549.805085990891,627.832850585004,668.726833059322,666.196728425214,624.524848194842,602.504944834256,666.196728425214,696.688027074344,681.064062606848,549.805085990891,624.524848194842,681.064062606848,696.498483673357), nrow=length(Nf), ncol=length(Nf))
dimnames(Sf) &lt;- list(as.character(Nf), as.character(Nf))

KRD_fine &lt;- Kf
KRD_coarse &lt;- Kc
VC_fine &lt;- Sf
VC_coarse &lt;- Sc
countw &lt;- length(seqFineNodes)


t1 &lt;- diag(x = 1, nrow = countw, ncol = countw)
t2 &lt;- diag(x = -1, nrow = countw, ncol = countw)
tr &lt;- cbind(t1,t2)

D_fine &lt;- t(tr) %*% KRD_fine %*% VC_fine %*% t(KRD_fine) %*% tr
#round(eigen(Dmat)$values, 4)
D_fine &lt;- as.matrix(nearPD(D_fine)$mat)
#round(eigen(Dmat)$values, 4)

eq_coarse_krd_A &lt;- t(KRD_coarse) %*% tr
eq_coarse_krd_b &lt;- rep(0, nrow(VC_coarse))

# Equality constraints
eq_A1 &lt;- c(rep(1, countw), rep(0,countw))
eq_A2 &lt;- c(rep(0, countw), rep(1,countw))
eq_b &lt;- c(1 , 1)

# Constraint wts greater than zero
ineq_A &lt;- diag(x = 1, nrow = 2 * countw, ncol = 2 * countw)
ineq_b &lt;- rep(0, 2 * countw)

# Combine constraints
heq &lt;- rbind(eq_coarse_krd_A, eq_A1, eq_A2)
beq &lt;- c(eq_coarse_krd_b, eq_b)

hin &lt;- ineq_A

theta &lt;- c(1, rep(0, countw - 1), 1, rep(0, countw - 1))

krdsol &lt;- solnp(par = theta, 
                fun = function(x) -c(t(x) %*% D_fine %*% x), 
                ineqfun = function(x) c(hin %*% x),
                ineqLB = rep(0, 2 * countw),
                ineqUB = rep(1, 2 * countw),
                eqfun = function(x) c(heq %*% x),
                eqB = beq)


krdFine &lt;- auglag(par = theta, 
                  fn = function(x) c(t(x) %*% D_fine %*% x), 
                  hin = function(x) c(hin %*% x),
                  heq = function(x) c(heq %*% x) - beq,
                  control.outer = list(method = "nlminb"),
                  control.optim=list(fnscale=-1))
</code></pre>

<p>I solved your problem about <code>solnp</code>. <code>?solnp</code> says <code>fun</code>, <code>ineqfun</code> and <code>eqfun</code> return <code>vector</code> but yours return <code>matrix</code>. So I added <code>c(...)</code> to them.</p>

<pre><code>library(Rsolnp)

krdsol &lt;- solnp(par = theta, 
                fun = function(x) c(-t(x) %*% D_fine %*% x), 
                ineqfun = function(x) c(hin %*% x),
                ineqLB = rep(0, 2 * countw),
                ineqUB = rep(1, 2 * countw),
                eqfun = function(x) c(heq %*% x),
                eqB = beq)
</code></pre>

[Edited]

<p>The elements <code>auglag(control.optim=list(...))</code> takes as arguments are listed in <code>?nlminb()</code> (and see <code>?auglag()</code>)</p>

python matlab mathematical-optimization