Artificial intelligence 人工智能——突变和交叉点的最佳方式
我正在做一个项目,需要使用遗传算法找到有效的路径来解析地图。路径是点的列表。目前,我正在使用以下方法交叉和变异路径,但没有得到好的结果 交叉-我随机生成一些点,这些点将在1和最小路径的大小之间交叉。然后我将随机选择位置,并直接在该位置交易该点Artificial intelligence 人工智能——突变和交叉点的最佳方式,artificial-intelligence,genetic-algorithm,mutation,Artificial Intelligence,Genetic Algorithm,Mutation,我正在做一个项目,需要使用遗传算法找到有效的路径来解析地图。路径是点的列表。目前,我正在使用以下方法交叉和变异路径,但没有得到好的结果 交叉-我随机生成一些点,这些点将在1和最小路径的大小之间交叉。然后我将随机选择位置,并直接在该位置交易该点 public Chromosome[] cross(Chromosome other) { int crossPoints; Chromosome child1 = new Chromosome(); ch
public Chromosome[] cross(Chromosome other) {
int crossPoints;
Chromosome child1 = new Chromosome();
child1.setPoints(this.getPoints());
Chromosome child2 = new Chromosome();
child2.setPoints(other.getPoints());
Chromosome[] news = new Chromosome[2];
if (this.getPoints().size() >= other.getPoints().size()) {
crossPoints = other.getPoints().size() / 2;
} else {
crossPoints = this.getPoints().size() / 2;
}
for (int i = 0; i < crossPoints; i++) {
int switchedPosition = new Random().nextInt(crossPoints * 2 - 2) + 1;
IPoint temp = child1.getPoints().get(switchedPosition);
child1.getPoints().set(switchedPosition, other.getPoints().get(switchedPosition));
child2.getPoints().set(switchedPosition, temp);
news[0] = child1;
news[1] = child2;
}
return news;
}
公共染色体[]交叉(其他染色体){
int交叉点;
染色体child1=新染色体();
child1.设定点(this.getPoints());
染色体child2=新染色体();
child2.设定点(其他.getPoints());
染色体[]新闻=新染色体[2];
如果(this.getPoints().size()>=other.getPoints().size()){
交叉点=其他.getPoints().size()/2;
}否则{
交叉点=this.getPoints().size()/2;
}
对于(int i=0;i
对路径中的每个点进行变异,我将随机生成一个介于0和1之间的小数,以选择变异后的坐标是X还是Y。然后我将坐标和随机生成的数字相加
public Chromosome mutate() {
Chromosome mutatedPath = new Chromosome();
//criar lista e fazer set
for (int i = 1; i < this.getPoints().size(); i++) {
double xOuY = new Random().nextDouble();
IPoint mutatedPoint;
int mutacao;
try {
do {
mutacao = new Random().nextInt(Config.mutation_rate * 2) - Config.mutation_rate;
} while (mutacao + this.getPoints().get(i).getX() > Maps.getMap(Config.mapNumber).getWidth() || mutacao + this.getPoints().get(i).getY() > Maps.getMap(Config.mapNumber).getHeight());
if (xOuY < 0.5) {
int newX = this.getPoints().get(i).getX() + mutacao;
mutatedPoint = new Point(newX, this.getPoints().get(i).getY());
} else {
int newY = this.getPoints().get(i).getY() + mutacao;
mutatedPoint = new Point(this.getPoints().get(i).getX(), newY);
}
mutatedPath.getPoints().add(mutatedPoint);
} catch (Exception ex) {
Logger.getLogger(Chromosome.class.getName()).log(Level.SEVERE, null, ex);
}
}
return mutatedPath;
}
公共染色体突变(){
染色体突变路径=新染色体();
//criar lista e fazer集合
对于(int i=1;iMaps.getMap(Config.mapneum).getWidth()| | mutacao+this.getPoints().get(i).getY()>Maps.getMap(Config.mapneum).getHeight());
如果(xOuY<0.5){
int newX=this.getPoints().get(i).getX()+mutacao;
mutatedPoint=newpoint(newX,this.getPoints().get(i.getY());
}否则{
int newY=this.getPoints().get(i).getY()+mutacao;
mutatedPoint=新点(this.getPoints().get(i).getX(),newY);
}
mutatedPath.getPoints().add(mutatedPoint);
}捕获(例外情况除外){
Logger.getLogger(chromose.class.getName()).log(Level.SEVERE,null,ex);
}
}
返回变异路径;
}
我能得到一些反馈吗?如何改进我的突变和交叉方法