我正在使用带有r的ompr包,但我不知道如何根据需要更改目标函数。第一个模型正在运行,但目标并不是我真正需要的。
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)谢谢!
发布于 2017-10-29 21:21:32
最小化方差在ompr中不起作用,因为它只能处理线性目标函数。您可以尝试使用带有二次目标函数的ROI包(并使用可以处理二次目标函数的求解器)。
另一种选择是最小化偏离平均值的线性和的absolute value,而不是平方。我相信这一切都可以用线性等式来表示。但我不确定这对你的用例是否有意义。
https://stackoverflow.com/questions/46748027
复制相似问题