我有一个带有多个等式和不等式的线性问题。它存在无穷多的解。我想找出多重随机解这个系统的一个改进的初始种群的遗传算法。
有没有人知道怎么用R来做这个?
感谢你的时间,
查尔斯
发布于 2012-04-25 23:59:17
一些优化函数允许您指定一个起点:通过选择随机的起点,您应该有不同的解决方案。
您还可以修改问题:在目标函数中,将距离添加到某个随机点。
library(Rsolnp)
get_one_point <- function(...) {
r <- NULL
while( is.null(r) || r$convergence != 0 ) {
x <- rnorm(2)
r <- solnp(
rnorm(2),
# Minimize the distance to some point
function(u) sum((u-x)^2),
# Constraints we want to satisfy
ineqfun = function(u) c(sum(u^2), u[2] - u[1]^2),
ineqLB = c(1,0),
ineqUB = c(2,5)
)
}
r$pars
}
# Plot the points and the constraints
library(parallel) # Very slow: run the optimizations in parallel
x <- mclapply( 1:10, get_one_point, mc.cores=detectCores() )
x <- do.call(rbind, x)
plot(x,
xlim=c(-2,2), ylim=c(0,2),
pch=15, cex=1.5, asp=1, las=1,
xlab="", ylab=""
)
curve(x^2, add=TRUE)
curve(sqrt(1-x^2), add=TRUE)
curve(2*sqrt(1-x^2/4), add=TRUE)

我之所以使用Rsolnp,是因为它允许我将约束指定为函数:如果您有一个线性问题,并使用欧几里得距离,则该问题将变成二次问题,并且可以使用quadprog包中的solve.QP来解决。
您还可以使用L^1范数(即绝对值):然后可以将问题重新表示为线性问题。
https://stackoverflow.com/questions/10318678
复制相似问题