gpt4 book ai didi

r - R中具有等式和不等式约束的二次优化

转载 作者:行者123 更新时间:2023-12-03 16:57:28 25 4
gpt4 key购买 nike

我试图找到如何用等式和不等式约束以及上下界解决 R 中的二次问题:

min 0.5*x'*H*x + f'*x  
subject to: A*x <= b
Aeq*x = beq
LB <= x <= UB

我已经检查过“quadprog”和“kernlab”包,但是......我一定遗漏了一些东西,因为我不知道如何为solve.QP()或ipop()同时指定“A”和“Aeq”

最佳答案

这是一个工作示例:

library('quadprog')

# min
# -8 x1 -16 x2 + x1^2 + 4 x2^2
#
# s.t.
#
# x1 + 2 x2 == 12 # equalities
# x1 + x2 <= 10 # inequalities (N.B. you need to turn it into "greater-equal" form )
# 1 <= x1 <= 3 # bounds
# 1 <= x2 <= 6 # bounds


H <- rbind(c(2, 0),
c(0, 8))

f <- c(8,16)

# equalities
A.eq <- rbind(c(1,2))
b.eq <- c(12)

# inequalities
A.ge <- rbind(c(-1,-1))
b.ge <- c(-10)

# lower-bounds
A.lbs <- rbind(c( 1, 0),
c( 0, 1))
b.lbs <- c(1, 1)

# upper-bounds on variables
A.ubs <- rbind(c(-1, 0),
c( 0,-1))
b.ubs <- c(-3, -6)

# solve
sol <- solve.QP(Dmat = H,
dvec = f,
Amat = t(rbind(A.eq, A.ge, A.lbs, A.ubs)),
bvec = c(b.eq, b.ge, b.lbs, b.ubs),
meq = 1) # this argument says the first "meq" rows of Amat are equalities

sol
> sol
$solution
[1] 3.0 4.5

$value
[1] -6

$unconstrained.solution
[1] 4 2

$iterations
[1] 3 0

$Lagrangian
[1] 10 0 0 0 12 0

$iact
[1] 1 5

关于r - R中具有等式和不等式约束的二次优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com