I want to solve an optimization problem in R; The dummy data for the same is given 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 <- sample(0:1,10,replace=T)
Position <- sample(-2:2,10,replace=T)
# Objective function
hikes_till_now <- cumsum(Hike) - Hike
PostHike <- numeric(10)
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)
# Snapshot of data
all.data <- data.frame(DTM,DIM,Price,Hike,Position,Pnl)
all.data
# DTM DIM Price Hike Position Pnl
# 1 3 30 99.60000 1 2 -0.4500000
# 2 11 30 99.53333 1 2 -0.6833333
# 3 29 30 99.46667 1 2 -0.7500000
# 4 16 30 99.40000 1 -2 1.3333333
# 5 25 30 99.33333 0 2 -1.4666667
# 6 5 30 99.26667 1 1 -0.8750000
# 7 3 30 99.20000 0 -1 0.8500000
# 8 8 30 99.13333 0 0 0.0000000
# 9 22 30 99.06667 0 -2 1.4333333
# 10 11 30 99.00000 0 -2 1.3000000
Variable description
So, the first section of the code contains the inputs to my system - DTM
, DIM
& Price
.
The second section consists of the variables I'd like to modify to find the optimal solution - Hike
& Position
. The values have been initiated as above.
The final section consists of the calculations I'll have to do to arrive at my objective function, which is to maximize Pnl
.
Constraints
I also have a couple of constraints -
- The sum of
Hike
should be less than 4 - The sum of
Position
should be 0, and eachPosition
value should be between -2 & +2
I have previously worked in GAMS and MATLAB, but I am just unable to get optimization to work here. I have gone through examples in the Rsymphony
package and this answer but I was unable to get them to work for multiple variable inputs.
Help appreciated.