ompr目标:最小化rowums的方差

时间:2017-10-14 18:34:12

标签: r glpk mixed-integer-programming

我正在使用omprr,我无法弄清楚如何根据我的需要更改目标函数。第一个模型正在运行,但目标并不是我真正需要的。

library(ompr)
library(magrittr)
library(ROI.plugin.glpk)
library(ompr.roi)

anz_schulen <- 50
anz_sfkz <- 10


# This model works 
model <- MIPModel() %>% 
add_variable(x[i, j], i = 1:anz_schulen, j = 1:anz_sfkz, type = "binary") %>%
set_objective(sum_expr(x[i, j], i = 1:anz_schulen, j = 1:anz_sfkz), sense="max") %>%
add_constraint(sum_expr(x[i, j], i = 1:anz_schulen) <= 7, j = 1:anz_sfkz) %>% 
add_constraint(sum_expr(x[i, j], i = 1:anz_schulen) >= 1, j = 1:anz_sfkz) %>% 
add_constraint(sum_expr(x[i, j], j = 1:anz_sfkz) <= 10, i = 1:anz_schulen) %>% 
add_constraint(sum_expr(x[i, j], j = 1:anz_sfkz) >= 1, i = 1:anz_schulen)

erg <- solve_model(model, solver=with_ROI(solver = "glpk"))

我需要x行的最小化方差。谁知道怎么做?

model <- MIPModel() %>%
add_variable(x[i, j], i = 1:anz_schulen, j = 1:anz_sfkz, type = "binary") %>%
# I NEED SOMETHING LIKE: substitute(var(rowSums(x[i,j])) ... THIS IS NOT WORKING
set_objective(substitute(var(rowSums(x[i,j]))), sense="min") %>%
add_constraint(sum_expr(x[i, j], i = 1:anz_schulen) <= 7, j = 1:anz_sfkz) %>% 
add_constraint(sum_expr(x[i, j], i = 1:anz_schulen) >= 1, j = 1:anz_sfkz) %>% 
add_constraint(sum_expr(x[i, j], j = 1:anz_sfkz) <= 10, i = 1:anz_schulen) %>% 
add_constraint(sum_expr(x[i, j], j = 1:anz_sfkz) >= 1, i = 1:anz_schulen)

谢谢!

1 个答案:

答案 0 :(得分:2)

最小化方差在ompr中不起作用,因为它只能处理线性目标函数。您可以尝试使用包含二次目标函数的包ROI(并使用可处理二次目标函数的求解器)。

另一种选择是最小化与平均值的偏差的线性和的absolute value,而不是平方的偏差。我相信这一切都可以表述为线性(in)等式。但我不确定你的用例是否合理。