均匀交叉的结果比单点交叉的结果更差?
我为我的部分作业编写了一个统一的交叉算法,但它无法正常工作。它实际上返回的结果比我的一分交叉更糟糕。我只是希望有人指出我出错的地方,以便我可以修复它:)。我已经尝试了很长时间,这是我最后的手段!
private void DoUniformCrossOver(int p1id,int p2id)
{
ArrayList<Integer> p1 = population.get(p1id).GetRep();
ArrayList<Integer> p2 = population.get(p2id).GetRep();
ArrayList<Integer> c1 = new ArrayList<Integer>();
ArrayList<Integer> c2 = new ArrayList<Integer>();
for (int i=0;i<nbits;++i)
{
double selected = CS2004.UI(1,2);
if (selected ==1)
{
c1.add(p1.get(i));
c2.add(p2.get(i));
}
else
{
c1.add(p2.get(i));
c2.add(p1.get(i));
}
}
population.add(new ScalesChrome(c1));
population.add(new ScalesChrome(c2));
}
该方法将两个父代 p1id 和 p2id 作为参数。然后创建表示形式的数组列表 - p1 和 p2。
在 for 循环中,'nbits' 是数组的权重(或数组的长度)。我的单点交叉方法在 for 循环中使用它,效果很好。
然后我生成 1/2 来确定孩子将从每个父母那里获得哪个基因。
这个算法的适应度非常非常差!!任何帮助将不胜感激。
非常感谢。
I have written a uniform crossover algorithm for part of my homework but it's not working properly. It's actually returning worse results than my one point cross over. I would just like someone to point out where I am going wrong so I can fix it please :). I've been trying for ages now and this is my last resort!!
private void DoUniformCrossOver(int p1id,int p2id)
{
ArrayList<Integer> p1 = population.get(p1id).GetRep();
ArrayList<Integer> p2 = population.get(p2id).GetRep();
ArrayList<Integer> c1 = new ArrayList<Integer>();
ArrayList<Integer> c2 = new ArrayList<Integer>();
for (int i=0;i<nbits;++i)
{
double selected = CS2004.UI(1,2);
if (selected ==1)
{
c1.add(p1.get(i));
c2.add(p2.get(i));
}
else
{
c1.add(p2.get(i));
c2.add(p1.get(i));
}
}
population.add(new ScalesChrome(c1));
population.add(new ScalesChrome(c2));
}
The method takes in as paramaters the two parents, p1id and p2id. Then creates the arraylists of the representation - p1 and p2.
In the for loop, 'nbits' is the weight of the array (or the length of the array). My one-point crossover method uses it in the for loop and it works just fine.
I then generate either 1/2 to determine which gene from each parent the child will get.
The fitness of this algorithm is very very poor!! Any help at all would be greatly appreciated.
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先您正在编码什么类型的信息以及您想要发展什么?
根据您试图解决的问题,某些类型的交叉策略会阻止您找到好的解决方案。
一个简单的例子:如果您正在寻找的解决方案具有固有的对称性(例如白/黑元胞自动机多数分类器),单点交叉永远不会给您带来很好的结果,因为它破坏了任何对称性遗传算法可能偶然发现了这一点(例如,在多数分类器的情况下,它将非常擅长对黑色或白色进行分类,但不能同时对两者进行分类,因此它永远不会比给定的(相当低的)适应度更好)。
Well, first of all what kind of information are you encoding and what are you trying to evolve?
Depending on the problem you are trying to solve, some kinds of cross-over strategies will prevent you from ever finding good solutions.
A simple example: if the solution you are looking for has inherent symmetry (e.g. a white/black cellular automata majority classifier), single point cross-over will never give you very good results because it's breaking any symmetry that the genetic algorithm may have stumbled upon (e.g. so in the case of the majority classifier, it will be very good at classifying black or white but not both, so it will never get better than a given - pretty low - fitness).
(由于您没有回复我的评论,我将重复它作为答案)
您正在将双精度数与常量进行比较,这对于浮点数来说是有问题的,使用
int selected
可能会做得更好,或者如果您需要使用双精度数,请尝试使用类似以下内容的内容:(As you didn't respond to my comment, I'll repeat it as answer)
You're comparing a double with a constant which is problematic for floating points numbers, using
int selected
will probably do better, or in case you need to use a double, try using something like: