sách gpt4 ai đã đi

Tối ưu hóa/lập trình phi tuyến tính với các biến số nguyên trong R

In lại 作者:行者123 更新时间:2023-12-03 13:56:57 30 4
mua khóa gpt4 Nike

我想知道是否有人能够提出一些解决非线性优化问题的软件包的方法,而非线性优化问题可以为优化解决方案提供整数变量?问题是使具有相等约束的函数最小化,该函数受某些上下边界约束的约束。

我已经在R中使用了'nloptr'软件包来解决非线性优化问题,该问题效果很好,但现在想扩展该方法,使某些变量为整数。到目前为止,从我对nloptr的使用和理解来看,它只能返回连续的值,而不能返回整数变量以获得最佳解决方案。

我相信这类问题需要使用混合整数非线性编程来解决。

nloptr形式的问题的一个示例:

min f(x) (x-y)^2/y + (p-q)^2/q
so that (x-y)^2/y + (p-q)^2/q = 10.2

Ở đâu
x and p are positive integers not equal to 0

y and q may or may not be positive integers not equal to 0

R中的nloptr代码如下所示
library('nloptr')

x1 <- c(50,25,20,15)

fn <- function(x) {
(((x[1] - x[2])^2)/x[2]) + (((x[3] - x[4])^2)/x[4])
}

heq <- function(x) {
fn(x)-10.2
}

lower_limit <- c(0,0,0,0)
upper_limit <- c(67.314, 78, 76.11, 86)


slsqp(x1, fn, lower = lower_limit, upper = upper_limit, hin = NULL, heq = heq, control = list(xtol_rel = 1e-8, check_derivatives = FALSE))

这将输出以下内容:
$par
[1] 46.74823 29.72770 18.93794 16.22137

$value
[1] 10.2

$iter
[1] 6

$convergence
[1] 4

$message
[1] "NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was reached."

这是我要寻找的结果,但如上所述,我需要x和p为整数。

我看过 https://cran.r-project.org/web/views/Optimization.html,它有一个非常好的混合整数非线性编程软件包列表,但是想知道是否有人对它们有经验,他们认为最适合解决上述问题的方法是什么。

大约7年前在这里也发布了与此类似的问题,但最终链接到cran页面,因此认为值得再次询问。

非常感谢您的输入。

干杯,

安德鲁

1 Câu trả lời

这是一个示例,说明在没有更简单的目标函数的情况下不能与CVXR一起使用的情况。我怀疑即使有约束条件,问题也不是凸面的,因此需要替代方法。

#base example from https://cvxr.rbind.io/cvxr_examples/cvxr_gentle-intro/
#install.packages("CVXR")
library(CVXR)

#modified for Stackoverflow integer MIQP ####
#Solves, but terms not normalised by y and q respectively

# Variables minimized over
x <- Variable(1, integer=TRUE)
y <- Variable(1)
p <- Variable(1, integer=TRUE)
q <- Variable(1)

# Problem definition (terms not normalised by y and q respectively)
objective <- Minimize((x - y)^2 + (p - q)^2)
constraints <- list(x >= 0, y >= 0, p >= 0, q >= 0,
x <= 67.314, y <= 78, p <= 76.11, q <= 86)
prob2.1 <- Problem(objective, constraints)

# Problem solution
solution2.1 <- solve(prob2.1)
solution2.1$status
solution2.1$value
solution2.1$getValue(x)
solution2.1$getValue(y)
solution2.1$getValue(p)
solution2.1$getValue(q)


#modified for Stackoverflow integer NLP (not integer) ####
#Does not solve, not convex?

# Variables minimized over
x <- Variable(1)
y <- Variable(1)
p <- Variable(1)
q <- Variable(1)

# Problem definition
objective <- Minimize((x - y)^2/y + (p - q)^2/q)
constraints <- list(x >= 0, y >= 0, p >= 0, q >= 0,
x <= 67.314, y <= 78, p <= 76.11, q <= 86)
prob2.1 <- Problem(objective, constraints)

# Problem solution
solution2.1 <- solve(prob2.1, gp = TRUE)
solution2.1 <- solve(prob2.1, gp = FALSE)
# solution2.1$status
# solution2.1$value
# solution2.1$getValue(x)
# solution2.1$getValue(y)
# solution2.1$getValue(p)
# solution2.1$getValue(q)


#modified for Stackoverflow integer MINLP ####
#Does not solve

# Variables minimized over
x <- Variable(1, integer=TRUE)
y <- Variable(1)
p <- Variable(1, integer=TRUE)
q <- Variable(1)

# Problem definition
objective <- Minimize((x - y)^2/y + (p - q)^2/q)
constraints <- list(x >= 0, y >= 0, p >= 0, q >= 0,
x <= 67.314, y <= 78, p <= 76.11, q <= 86)
prob2.1 <- Problem(objective, constraints)

# Problem solution
solution2.1 <- solve(prob2.1, gp = TRUE)
solution2.1 <- solve(prob2.1, gp = FALSE)
# solution2.1$status
# solution2.1$value
# solution2.1$getValue(x)
# solution2.1$getValue(y)
# solution2.1$getValue(p)
# solution2.1$getValue(q)


#modified for Stackoverflow integer NLP (not integer) ####
#objective multiplied by y*q, Does not solve, not convex?

# Variables minimized over
x <- Variable(1)
y <- Variable(1)
p <- Variable(1)
q <- Variable(1)

# Problem definition
objective <- Minimize((x - y)^2*q + (p - q)^2*y)
constraints <- list(x >= 0, y >= 0, p >= 0, q >= 0,
x <= 67.314, y <= 78, p <= 76.11, q <= 86)
prob2.1 <- Problem(objective, constraints)

# Problem solution
solution2.1 <- solve(prob2.1, gp = TRUE)
solution2.1 <- solve(prob2.1, gp = FALSE)
# solution2.1$status
# solution2.1$value
# solution2.1$getValue(x)
# solution2.1$getValue(y)
# solution2.1$getValue(p)
# solution2.1$getValue(q)

关于r - R中具有整数变量的非线性优化/编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61047653/

30 4 0
Bài viết được đề xuất: reactjs - 导入自定义 npm 包会导致空/空对象
Bài viết được đề xuất: .net - .net程序如何 self 更新?
Bài viết được đề xuất: javascript - Redux saga debounce 而不仅仅是延迟/取消
Bài viết được đề xuất: scala - 在Scala中取消导入
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com