我正在尝试在遗传编程中编写一个适应度函数来扩大多边形内点所占据的区域。多边形中心附近有一些点,我想将这些点从中心扩展到多边形内部。
我正在使用多边形内的点所占据的面积与多边形外的面积之间的差异,并试图在适应度函数中将其最小化。但我不知道如何使用它来改变点的坐标,然后重新计算面积的差异并迭代地进行。我已经给出了输入和输出的样子。
提前致谢。
这是我到目前为止写的代码。
require(rgp)
require(splancs)
require(grDevices)
functionSet1 <- functionSet("+", "*", "-", "/","^")
inputVariableSet1 <- inputVariableSet("x","y")
constantFactorySet1 <- constantFactorySet(function() rnorm(1))
outpolygon<-matrix(c(3.061188,2.517408,0.523754,-0.258800,0.981104,4.036885,
3.061188,4.069070,4.069070,2.695074,0.485581,-2.129055,
-2.653607,4.069070),nrow=7,byrow=F)
inpoints<-matrix(c(2.637644,-0.4456578,2.160003,0.8553066,1.501256,1.3137518,2.352020,-0.2643815,
1.254139,1.2241712,1.918191,0.6595725,1.453478,0.9153824,1.900110,1.0607272,
1.648038,0.6847361,2.194931,2.2842159),nrow=10,byrow=T)
plot(-10:10,xlim=c(-5,5),ylim=c(-5,5))
polygon(outpolygon[,1],outpolygon[,2])
points(inpoints[,1],inpoints[,2])
fitnessFunction1 <- function(f){
if(all(point.in.polygon(inpoints[,1],inpoints[,2],outpolygon[,1],outpolygon[,2])!=0)){
rmse(areapl(inpoints[chull(inpoints[,1],inpoints[,2]),]),areapl(cbind(outpolygon[,1],outpolygon[,2])[chull(outpolygon[,1],outpolygon[,2]),]))
}else{
rmse(1000,0)
}
}
gpResult1 <- geneticProgramming(functionSet=functionSet1,
inputVariables=inputVariableSet1,
constantSet=constantFactorySet1,
fitnessFunction=fitnessFunction1,
stopCondition=makeTimeStopCondition(10))
best1 <- gpResult1$population[[which.min(sapply(gpResult1$population,
fitnessFunction1))]]