3

我正在使用ompr带有 的包r,但我无法弄清楚如何将目标函数更改为我的需要。第一个模型正在运行,但目标并不是我真正需要的。

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)

谢谢!

4

1 回答 1

2

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

另一种选择是最小化与平均值的偏差的线性和的绝对值,而不是平方值。我相信这都可以表述为线性(不)等式。但我不确定这对您的用例是否有意义。

于 2017-10-29T13:21:32.187 回答