首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ompr目标:最小化rowsum的方差

ompr目标:最小化rowsum的方差
EN

Stack Overflow用户
提问于 2017-10-15 02:34:12
回答 1查看 330关注 0票数 3

我正在使用带有rompr包,但我不知道如何根据需要更改目标函数。第一个模型正在运行,但目标并不是我真正需要的。

代码语言:javascript
复制
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的行和的方差。有人知道怎么做吗?

代码语言:javascript
复制
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)

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-10-29 21:21:32

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

另一种选择是最小化偏离平均值的线性和的absolute value,而不是平方。我相信这一切都可以用线性等式来表示。但我不确定这对你的用例是否有意义。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46748027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档