1

我有多个等式和不等式的线性问题。它存在无限的解决方案。我想找到这个系统的多重随机解来改进遗传算法的初始种群。

有谁知道如何用 R 做到这一点?

谢谢你的时间,

查尔斯

4

1 回答 1

1

一些优化功能允许您指定起点:通过选择随机起点,您应该有不同的解决方案。

您还可以修改问题:在目标函数中,添加到某个随机点的距离。

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它是因为它允许我将约束指定为函数:如果你有一个线性问题并使用欧几里得距离,那么问题就会变成二次的并且可以 solve.QPquadprog包中解决。

您还可以使用 L^1 范数(即绝对值):然后可以将问题重新表述为线性问题。

于 2012-04-25T15:59:16.887 回答