我正在使用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)
谢谢!