Optimization in R using multiple variables using Rsolnp

I previously asked this question and wanted to continue the observation, since I tried other things, and they didn’t quite work.

I am essentially trying to optimize a problem like NLP in R, which has binary and integer constraints. The code for it is below:

# Input Data DTM <- sample(1:30,10,replace=T) DIM <- rep(30,10) Price <- 100 - seq(0.4,1,length.out=10) # Variables that shall be changed to find optimal solution Hike <- c(1,0,0,1,0,0,0,0,0,1) Position <- c(0,1,-2,1,0,0,0,0,0,0) # Bounds for Hikes/Positions HikeLB <- rep(0,10) HikeUB <- rep(1,10) PositionLB <- rep(-2,10) PositionUB <- rep(2,10) library(Rsolnp) # x <- c(Hike, Position) # Combining two arrays into one since I want # to optimize using both these variables opt_func <- function(x) { Hike <- head(x,length(x)/2) Position <- tail(x,length(x)/2) hikes_till_now <- cumsum(Hike) - Hike PostHike <- numeric(length(Hike)) for (i in seq_along(Hike)){ PostHike[i] <- 99.60 - 0.25*(Hike[i]*(1-DTM[i]/DIM[i])) if(i>1) { PostHike[i] <- PostHike[i] - 0.25*hikes_till_now[i] } } Pnl <- Position*(PostHike-Price) return(-sum(Pnl)) # Since I want to maximize sum(Pnl) } #specify the in-equality function for Hike unequal <- function(x) { Hike <- head(x,length(x)/2) return(sum(Hike)) } #specify the equality function for Position equal <- function(x) { Position <- tail(x,length(x)/2) return(sum(Position)) } #the optimiser solnp(c(Hike,Position), opt_func, eqfun=equal, eqB=0, ineqfun=unequal, ineqUB=3, ineqLB=1, LB=c(HikeLB,PositionLB), UB=c(HikeUB,PositionUB)) 

I get the following warning / error:

 # solnp--> Solution not reliable....Problem Inverting Hessian. 

I understand that Hessian is a rare matrix, and therefore there may be problems with inversion? Also, there might be some better way to do this optimization, since it doesn't seem like a complicated problem, and I feel like I'm missing something quite straightforward here!

A description of the problem is described in detail in this question .

Any help would be greatly appreciated.

+5
source share

Source: https://habr.com/ru/post/1257538/


All Articles