Multi-objective optimization with both equality and inequality constraints in R - r

I have a multi-objective optimization problem with both equality and inequality constraints. I tried GA package in R but I didn't figure out how to set equality constraints like the GA in Matlab. Could anybody give some suggestions about which algorithm could I use in R and how to solve that. Thanks a lot. The problem is like below:
Objective functions(I would like to find a set of w1,w2,w3,w4,w5 to minimize f1, and at the same time I want to make f2 as small as possible):
f1<-(w1)^2+4*(w2)^4+3*(w3)^2+5*w4+w5
f2<-((w1)-(w2)^2+3(w3)^2-4*(w4)^3-w5)^2-8
Constraints:
w1+w2+w3+w4+w5=1
0<w1,w2,w3,w4,w5<1

Use normalization so you can drop the equality constraint:
Objectives:
w <- w / sum(w)
f1<-(w1)^2+4*(w2)^4+3*(w3)^2+5*w4+w5
f2<-((w1)-(w2)^2+3(w3)^2-4*(w4)^3-w5)^2-8
Constraints:
0 <= w1,w2,w3,w4,w5 <= 1

Related

is there an R package that allows to maximize a linear objective with a non-linear constraint?

I've been able to minimize a non-linear objective with a linear constraint using quadprog, however, I haven't been able to do it the other way around...
require(quadprog)
min_var <- function(Obj,Rentabilidades,var_covar){
b <- c(Obj,1)
Betha <- var_covar
A <- t(matrix(rbind(Rentabilidades,c(1,1)),nrow=2))
Gamma <- matrix(0,nrow=2)
solve.QP(Betha,Gamma,A,b,2)
}
Now I want to maximize what previously was the constraint taking as a new constraint the former objective. Regrettably, solve.QP() only supports linear constraints. Does anyone know a package similar to quadprog that might help me?
A standard portfolio optimization model looks like:
min sum((i,j), x(i)*Q(i,j)*x(j))
sum(i,x(i)) = 1
sum(i,r(i)*x(i)) >= R
x(i) >= 0
This is a Quadratic Programming model, and can be solved with standard QP solvers.
If you turn this around (maximize return subject to a risk constraint), you can write:
max sum(i,r(i)*x(i))
sum((i,j), x(i)*Q(i,j)*x(j)) <= V
sum(i,x(i)) = 1
x(i) >= 0
This is now a Quadratically Constrained problem. Luckily this is convex so you can use solvers like Cplex, Gurobi, or Mosek to solve them (they have R interfaces). An open source candidate could be a solver like ECOSolveR, or even better a framework like cxvr.

Solve quadratic optimization with nonlinear constraints [duplicate]

I am trying to solve the following inequality constraint:
Given time-series data for N stocks, I am trying to construct a portfolio weight vector to minimize the variance of the returns.
the objective function:
min w^{T}\sum w
s.t. e_{n}^{T}w=1
\left \| w \right \|\leq C
where w is the vector of weights, \sum is the covariance matrix, e_{n}^{T} is a vector of ones, C is a constant. Where the second constraint (\left \| w \right \|) is an inequality constraint (2-norm of the weights).
I tried using the nloptr() function but it gives me an error: Incorrect algorithm supplied. I'm not sure how to select the correct algorithm and I'm also not sure if this is the right method of solving this inequality constraint.
I am also open to using other functions as long as they solve this constraint.
Here is my attempted solution:
data <- replicate(4,rnorm(100))
N <- 4
fn<-function(x) {cov.Rt<-cov(data); return(as.numeric(t(x) %*%cov.Rt%*%x))}
eqn<-function(x){one.vec<-matrix(1,ncol=N,nrow=1); return(-1+as.numeric(one.vec%*%x))}
C <- 1.5
ineq<-function(x){
z1<- t(x) %*% x
return(as.numeric(z1-C))
}
uh <-rep(C^2,N)
lb<- rep(0,N)
x0 <- rep(1,N)
local_opts <- list("algorithm"="NLOPT_LN_AUGLAG,",xtol_rel=1.0e-7)
opts <- list("algorithm"="NLOPT_LN_AUGLAG,",
"xtol_rel"=1.0e-8,local_opts=local_opts)
sol1<-nloptr(x0,eval_f=fn,eval_g_eq=eqn, eval_g_ineq=ineq,ub=uh,lb=lb,opts=opts)
This looks like a simple QP (Quadratic Programming) problem. It may be easier to use a QP solver instead of a general purpose NLP (NonLinear Programming) solver (no need for derivatives, functions etc.). R has a QP solver called quadprog. It is not totally trivial to setup a problem for quadprog, but here is a very similar portfolio example with complete R code to show how to solve this. It has the same objective (minimize risk), the same budget constraint and the lower and upper-bounds. The example just has an extra constraint that specifies a minimum required portfolio return.
Actually I misread the question: the second constraint is ||x|| <= C. I think we can express the whole model as:
This actually looks like a convex model. I could solve it with "big" solvers like Cplex,Gurobi and Mosek. These solvers support convex Quadratically Constrained problems. I also believe this can be formulated as a cone programming problem, opening up more possibilities.
Here is an example where I use package cccp in R. cccp stands for
Cone Constrained Convex Problems and is a port of CVXOPT.
The 2-norm of weights doesn't make sense. It has to be the 1-norm. This is essentially a constraint on the leverage of the portfolio. 1-norm(w) <= 1.6 implies that the portfolio is at most 130/30 (Sorry for using finance language here). You want to read about quadratic cones though. w'COV w = w'L'Lw (Cholesky decomp) and hence w'Cov w = 2-Norm (Lw)^2. Hence you can introduce the linear constraint y - Lw = 0 and t >= 2-Norm(Lw) [This defines a quadratic cone). Now you minimize t. The 1-norm can also be replaced by cones as abs(x_i) = sqrt(x_i^2) = 2-norm(x_i). So introduce a quadratic cone for each element of the vector x.

R quadratic programming

i have a problem that i'd like to solve in R.
I see that i can use the function lsei in the package limSolve to minimise a system of linear equations written Ax=b in the matrix form, subject to equality constraints Ex=f and the inequality constraints Gx>=h.
However, rather than a system of linear equations, i now have a system of quadratic equations that can be written t(x)Ax=b.
I see there's the package quadprog for the quadratic case, but it doesn't seem to allow for a set of quadratic equations, just the one equation.
Does anyone know what i could use to minimise a system of quadratic equations under both an equality and an inequality constraint?
Here's my example. I'm trying to combine 3 probabilities P(A), P(B), P(C) - this creates 7 segments v1 to v7, where v1 is P(A solus) etc... v4 is P((A AND B) NOT C) etc.. and v7 is P(A AND B AND C).
The function i'm trying to minimise is:
obj.fc<-function(x){
f<-rep(NA,4)
v1<-x[1]
v2<-x[2]
v3<-x[3]
v4<-x[4]
v5<-x[5]
v6<-x[6]
v7<-x[7]
f[1]<-(v4+v7)*(1-(v1+v2+v4+v5+v6+v7))-2*(v1+v6)*(v2+v5)
f[2]<-(v5+v7)*(1-(v2+v3+v4+v5+v6+v7))-13*(v2+v4)*(v3+v6)
f[3]<-(v6+v7)*(1-(v1+v3+v4+v5+v6+v7))-11*(v1+v4)*(v3+v5)
f[4]<-(v4+v5+v6)*(1-(v1+v2+v3+v4+v5+v6+v7))-4*(v1+v2+v3)*v7
return(f)
}
My equality constraints are:
v1+v4+v6+v7=0.14
v2+v4+v5+v7=0.01
v3+v5+v6+v7=0.08
And my inequality constraints are that the Vi have to be between 0 and 1 and their sum can't exceed 1.
You can represent each of your equality constraints as two inequality constraints, e.g.
Ax = b <=> Ax <= b, and
Ax >= b
Note however that quadprog only allows solving "quadratic programs" in the sense of a quadratic objective function with linear constraints. From quadprog documentation:
This routine implements the dual method of Goldfarb and Idnani (1982,
1983) for solving quadratic programming problems of the form min(−dT b
+ 1/2bT Db) with the constraints AT b >= b0.
So in your case, you should probably look at another package. I would suggest e.g. [NlcOptim][2], or, find the most appropriate solver for you from here:
https://cran.r-project.org/web/views/Optimization.html

r optimization function with formula for constrains

I need to optimize a function, say g(x), where x is vector and g is an analytic function.
The problem is that I need to optimize with a constraint (analytic) function c(x) which gives a scalar as an output, i.e the constraint is c(x) > k for some k > 0.
constrOptim only allows to give a constrain for every field separately.
Advices?
Found the right tool - nloptr package. A very robust package where I can define functions for the optimization (g) and constraint (c). Also I can define upper and lower bounds for each of the variables separately, and use different king of optimization methods.

Minimizing quadratic function subject to norm inequality constraint

I am trying to solve the following inequality constraint:
Given time-series data for N stocks, I am trying to construct a portfolio weight vector to minimize the variance of the returns.
the objective function:
min w^{T}\sum w
s.t. e_{n}^{T}w=1
\left \| w \right \|\leq C
where w is the vector of weights, \sum is the covariance matrix, e_{n}^{T} is a vector of ones, C is a constant. Where the second constraint (\left \| w \right \|) is an inequality constraint (2-norm of the weights).
I tried using the nloptr() function but it gives me an error: Incorrect algorithm supplied. I'm not sure how to select the correct algorithm and I'm also not sure if this is the right method of solving this inequality constraint.
I am also open to using other functions as long as they solve this constraint.
Here is my attempted solution:
data <- replicate(4,rnorm(100))
N <- 4
fn<-function(x) {cov.Rt<-cov(data); return(as.numeric(t(x) %*%cov.Rt%*%x))}
eqn<-function(x){one.vec<-matrix(1,ncol=N,nrow=1); return(-1+as.numeric(one.vec%*%x))}
C <- 1.5
ineq<-function(x){
z1<- t(x) %*% x
return(as.numeric(z1-C))
}
uh <-rep(C^2,N)
lb<- rep(0,N)
x0 <- rep(1,N)
local_opts <- list("algorithm"="NLOPT_LN_AUGLAG,",xtol_rel=1.0e-7)
opts <- list("algorithm"="NLOPT_LN_AUGLAG,",
"xtol_rel"=1.0e-8,local_opts=local_opts)
sol1<-nloptr(x0,eval_f=fn,eval_g_eq=eqn, eval_g_ineq=ineq,ub=uh,lb=lb,opts=opts)
This looks like a simple QP (Quadratic Programming) problem. It may be easier to use a QP solver instead of a general purpose NLP (NonLinear Programming) solver (no need for derivatives, functions etc.). R has a QP solver called quadprog. It is not totally trivial to setup a problem for quadprog, but here is a very similar portfolio example with complete R code to show how to solve this. It has the same objective (minimize risk), the same budget constraint and the lower and upper-bounds. The example just has an extra constraint that specifies a minimum required portfolio return.
Actually I misread the question: the second constraint is ||x|| <= C. I think we can express the whole model as:
This actually looks like a convex model. I could solve it with "big" solvers like Cplex,Gurobi and Mosek. These solvers support convex Quadratically Constrained problems. I also believe this can be formulated as a cone programming problem, opening up more possibilities.
Here is an example where I use package cccp in R. cccp stands for
Cone Constrained Convex Problems and is a port of CVXOPT.
The 2-norm of weights doesn't make sense. It has to be the 1-norm. This is essentially a constraint on the leverage of the portfolio. 1-norm(w) <= 1.6 implies that the portfolio is at most 130/30 (Sorry for using finance language here). You want to read about quadratic cones though. w'COV w = w'L'Lw (Cholesky decomp) and hence w'Cov w = 2-Norm (Lw)^2. Hence you can introduce the linear constraint y - Lw = 0 and t >= 2-Norm(Lw) [This defines a quadratic cone). Now you minimize t. The 1-norm can also be replaced by cones as abs(x_i) = sqrt(x_i^2) = 2-norm(x_i). So introduce a quadratic cone for each element of the vector x.

Resources