r-opt / rmpk

Mixed Integer Linear and Quadratic Programming in R
https://r-opt.github.io/rmpk/
Other
44 stars 4 forks source link

need example for parameters having more than 2 dimensions #46

Closed shuaiwang88 closed 4 years ago

shuaiwang88 commented 4 years ago

What is the data format look like for a parameter which is more than 2 dimension?

dirkdegel commented 4 years ago

Hi Shuai, Do you mean more than 2 dimensions in the decision variables or in the solver control parameters? Maybe the attached minimal example helps.

library(rmpk)
library(ROI.plugin.lpsolve)

solver <- rmpk::ROI_solver("lpsolve", control = list(presolve = "none", verbose = "full", simplextype = "primal"))

model_rmpk_MIP <- rmpk::MIPModel(solver)
x <- model_rmpk_MIP$add_variable("x", type = "continuous", i = 1:2, j = 1:3, k = 1:4, lb = 0, ub = 100)

model_rmpk_MIP

Cheers Dirk

shuaiwang88 commented 4 years ago

Hi Dirk, Not exactly. Let's say we have a parameter called demand by each product, hour, zone: demand(i,j,k). How do present this demand in a data frame?

dirkdegel commented 4 years ago

Hi Shuain,

I'm not sure if I really understand what you want to do. But in the case you want to have a 3-dimensional demand, you need to generate a 3-dimensional array that with the data. Let's say dimension 1 = product (1:2), dimension 2 = hour (1:3), dimension 3 = zone (1:4). The values in the array is the demand, e.g. demand[2, 3, 2] is the demand of product 2, in hour 3, in zone 2.

set.seed(42)
demand <- sample(100:500, 24, replace = TRUE)
dim(demand) = c(2, 3, 4)

demand[2, 3, 2]

library(rmpk)
library(ROI.plugin.lpsolve)

model_rmpk_MIP <- rmpk::MIPModel(solver)
x <- model_rmpk_MIP$add_variable("x", type = "continuous", i = 1:2, j = 1:3, k = 1:4, lb = 0, ub = 100)
model_rmpk_MIP$set_objective(sum_expr(demand[i, j, k] * x[i], i = 1:2, j = 1:3, k = 1:4), sense = "max")

model_rmpk_MIP

I hope that helps. Cheers Dirk

shuaiwang88 commented 4 years ago

Hi, Dirk, thanks for your answer. The matrix format and dim(demand) = c(2, 3, 4) is what I wanted. I am using the data frame which has character like zone1, zone2, etc., and I will convert it to the matrix instead of the dataframe.

Thank you so much!