I'm new to stackoverflow and searched a lot, but couldn't find an answer to my question. I'm trying to minimise the problem bellow with the optimisation package Rsolnp. Although the solver gives me a solution, every time I run the code I get the following warning message:
Warning messages: 1: In cbind(temp, funv) : number of rows of result is not a multiple of vector length (arg 1)
Furthermore, the solution is completely different from the solutions I get with ipop and solve.QP. Their solutions are almost the same (0.2480, 0.0000, 0.0121, 0.7400). I tried many different formulations of the problem, but could not figure out what I did wrong. I am thankful for all help and information!
library(Rsolnp)
# Starting Values
x0 <- c(0.25,0,0.01,0.75)
fn <- function(x){
d <- c(0.0308, 0.0269, 0.0145, 0.0130)
d <- -d
D <- cbind(c(0.1486, 0.0778, -0.0240, -0.0154),
c(0.0778, 0.1170, 0.0066, 0.0029),
c(-0.0240, 0.0066, 0.0444, 0.0193),
c(-0.0154, 0.0029, 0.0193, 0.0148))
out <- t(d) %*% x + 0.5 * (t(x) %*% D %*% x)
out
}
# Inequality Constraint: 0 =< x 0 =< 1
lx <- rep(0,4)
ux <- rep(1,4)
sol <- solnp(pars = x0,
fun = fn,
eqfun = sum,
eqB = 1,
ineqLB = lx,
ineqUB = ux)
sol$pars
Welcome to SO.
You got two issues here a) problem w.r.t the result and b) a warning message you can't make any sense of.
ad a) This is simply because LaGrange optimization is often done by minimizing a negative value instead of maximizing a positive value. That's been the difference. When setting
d
to-d
(I did so in the edit) the solution is the same as the solution produced by other solvers.ad b) the warning message issue is somewhat tricky. The message itself means that
cbind
tries to bind two columns of different length. WhatR
does is it repeats the values in the shorter column as often as necessary to get two columns of equal length to bind. If the longer is not a multiple of the shorter you get this warning, but R continues to repeat values of the shorter if it means there's some unused remainder.However, it looks like there's a bug in the package. When you run
you realize that there is an object called
tempdf
being created that causes the problem. However, this object is never used again in the code of solnp. I'll ask the authors, maybe they can confirm this is a bug or show otherwise.Make the result of your objective function, fn, explicitly numeric:
and I get