最近我决定接受进化编程的挑战,并按照Rosetta Code上发布的问题说明,给定目标字符串,改变随机生成的字符串,直到它与目标匹配。 我设法做了很好的突变,但我的健身功能得到了错误的值,但我不明白为什么。我的健身功能是:
int
closeness (char* string, char* target, int n)
{
int fit = 0, i, j;
if (n == 0) return 0;
if (!strcmp (string, target)) return 99999999; //if strings match, no need to iterate
for (i = n - 1; i > 0; i--)
{
if ((string[i] == target[i])) fit++; //fit gets increased by each matching letter.
}
return fit;
并且,为了不使整个帖子混乱,here's the full code (Pastebin) 任何帮助都值得赞赏。
示例输出(对于目标字符串“methinks”):
best match: ecskyzfjh in generation 1, specimen 0 with fitness 0
best match: ecsuyzfjh in generation 2, specimen 0 with fitness 0
best match: ecsuyzfjh in generation 3, specimen 0 with fitness 0
best match: vc uyzfjh in generation 4, specimen 0 with fitness 0
best match: vc uyzfjh in generation 5, specimen 0 with fitness 0
best match: ve uyzfjh in generation 6, specimen 4 with fitness 1
best match: ve lyzfjh in generation 7, specimen 4 with fitness 1
best match: ve lyzfjh in generation 8, specimen 4 with fitness 1
best match: ve lyzfjh in generation 9, specimen 4 with fitness 1
best match: ve lyzfjh in generation 10, specimen 4 with fitness 1
best match: vg lyzfjh in generation 11, specimen 4 with fitness 1
best match: vgalyzfjh in generation 12, specimen 4 with fitness 1
best match: vgalyzfjh in generation 13, specimen 4 with fitness 1
best match: vgalyzfjh in generation 14, specimen 4 with fitness 1
best match: ugalyzfjh in generation 15, specimen 4 with fitness 1
best match: ugalyzrjh in generation 16, specimen 4 with fitness 1
best match: ugalyhtjh in generation 17, specimen 4 with fitness 1
best match: ugalyhtjh in generation 18, specimen 4 with fitness 1
best match: ugalyhtjh in generation 19, specimen 4 with fitness 1
best match: ugalyhtjz in generation 20, specimen 4 with fitness 1
best match: ugalyhtjz in generation 21, specimen 4 with fitness 1
best match: ugalyht l in generation 22, specimen 4 with fitness 1
best match: ugalyht l in generation 23, specimen 4 with fitness 1
best match: ugalyht l in generation 24, specimen 4 with fitness 1
best match: galyyt l in generation 25, specimen 4 with fitness 1
best match: galyyt l in generation 26, specimen 4 with fitness 1
best match: yalyyt l in generation 27, specimen 4 with fitness 1
best match: yalyytil in generation 28, specimen 4 with fitness 1
best match: yalqytil in generation 29, specimen 4 with fitness 1
best match: pyajqytil in generation 30, specimen 4 with fitness 1
best match: pyajqytil in generation 31, specimen 4 with fitness 1
best match: pyajqytio in generation 32, specimen 4 with fitness 1
best match: pyajqytio in generation 33, specimen 4 with fitness 1
best match: pyazqytio in generation 34, specimen 4 with fitness 1
best match: pyazqytio in generation 35, specimen 4 with fitness 1
best match: pyazqytio in generation 36, specimen 4 with fitness 1
best match: pyazqptio in generation 37, specimen 4 with fitness 1
best match: pyazoztuo in generation 38, specimen 4 with fitness 1
best match: pyazoztuo in generation 39, specimen 4 with fitness 1
best match: pyazoztux in generation 40, specimen 4 with fitness 1
best match: hyazont x in generation 41, specimen 4 with fitness 1
best match: hyazont y in generation 42, specimen 4 with fitness 1
best match: hyazontay in generation 43, specimen 4 with fitness 1
best match: hyuzoltay in generation 44, specimen 4 with fitness 1
best match: hyuyoltay in generation 45, specimen 4 with fitness 1
best match: hyuyoltay in generation 46, specimen 4 with fitness 1
best match: hyuyoltay in generation 47, specimen 4 with fitness 1
best match: hvuyoltay in generation 48, specimen 4 with fitness 1
best match: hvuyoltay in generation 49, specimen 4 with fitness 1
best match: hvlyoltly in generation 50, specimen 4 with fitness 1
best match: hvlyoltly in generation 51, specimen 4 with fitness 1
best match: hvlynltlv in generation 52, specimen 4 with fitness 1
best match: hvlynltlv in generation 53, specimen 4 with fitness 1
best match: hvlynltlv in generation 54, specimen 4 with fitness 1
best match: hvlexltxv in generation 55, specimen 4 with fitness 1
best match: hvlexltev in generation 56, specimen 4 with fitness 1
best match: hvlexltev in generation 57, specimen 4 with fitness 1
best match: hvlexltev in generation 58, specimen 4 with fitness 1
best match: lvlexltev in generation 59, specimen 4 with fitness 1
best match: lvlexltem in generation 60, specimen 4 with fitness 1
best match: lvlexltem in generation 61, specimen 4 with fitness 1
best match: lqlexlpem in generation 62, specimen 4 with fitness 1
best match: lqlexdpem in generation 63, specimen 4 with fitness 1
best match: lylaxdpem in generation 64, specimen 4 with fitness 1
best match: lylaxdqem in generation 65, specimen 4 with fitness 1
best match: lylsxdqxm in generation 66, specimen 4 with fitness 1
best match: lylsidqxm in generation 67, specimen 4 with fitness 1
best match: in generation 68, specimen 4 with fitness 1
best match: in generation 69, specimen 4 with fitness 1
best match: in generation 70, specimen 4 with fitness 1
best match: in generation 71, specimen 4 with fitness 1
best match: in generation 72, specimen 4 with fitness 1
best match: ynldqxq q in generation 73, specimen 4 with fitness 1
best match: ynhdqxq q in generation 74, specimen 4 with fitness 1
best match: znhdqxqkq in generation 75, specimen 4 with fitness 1
best match: znhdqxqqq in generation 76, specimen 4 with fitness 1
best match: znbdqxqqq in generation 77, specimen 4 with fitness 1
best match: znbjqxqqq in generation 78, specimen 4 with fitness 1
best match: zbbjqxqqq in generation 79, specimen 4 with fitness 1
best match: zbbjqxqpq in generation 80, specimen 4 with fitness 1
best match: zbbjqxqpq in generation 81, specimen 4 with fitness 1
best match: bbjqxqpq in generation 82, specimen 4 with fitness 1
best match: bbjqxqpq in generation 83, specimen 4 with fitness 1
best match: bbjqxlpq in generation 84, specimen 4 with fitness 1
best match: brjqxaaq in generation 85, specimen 4 with fitness 1
best match: brjqxaaq in generation 86, specimen 4 with fitness 1
best match: prfqmaaq in generation 87, specimen 4 with fitness 1
best match: xprfqmaaq in generation 88, specimen 4 with fitness 1
best match: xprfrzaad in generation 89, specimen 4 with fitness 1
best match: xpzfrxaad in generation 90, specimen 4 with fitness 1
best match: xpefrxaad in generation 91, specimen 4 with fitness 1
best match: xgefrxaad in generation 92, specimen 4 with fitness 1
best match: xgefruaod in generation 93, specimen 4 with fitness 1
best match: dgefruaod in generation 94, specimen 4 with fitness 1
best match: dgefruaod in generation 95, specimen 4 with fitness 1
best match: dge in generation 96, specimen 4 with fitness 1
best match: in generation 97, specimen 4 with fitness 1
best match: in generation 98, specimen 4 with fitness 1
best match: in generation 99, specimen 4 with fitness 1
best match: in generation 100, specimen 4 with fitness 1
大多数人的预期适应度为0。
答案 0 :(得分:0)
对您的功能的调用是:
适应度[i] =接近度(样本[i],目标,大小);
且尺寸为:
char target [] =" methinks&#34 ;; size = sizeof(target);
c中的每个字符串都以char' \ 0'结尾,因此所有字符串函数都知道字符串结束的位置。如果你开始比较位置sizeof(目标)-1的两个字符串,它将比较两个' \ 0'首先(逻辑上相同),然后比较其余部分。
此外,我会将循环更改为:
表示(i = n - 1; i> = 0; i - )
否则,第一个角色不会被比较。
让我知道,如果这解决了你的问题。 ;)
答案 1 :(得分:0)
这里是我在上面评论中提到的快速实现。它消除了特殊情况检查,因为循环无论如何都要处理它。不需要n,因为使用strcmp
意味着字符串以空值终止。它不会检查相等性并提出一个神奇的值,因为您已经知道完全匹配将返回最大值。
我能想到的唯一潜在错误是,如果你在之前通过n = 0的情况下没有空终止字符串 - 这没有机会纾困。但是如果你的空字符串以null开头,则它是安全的。
请注意,我实际上还没有尝试编译或测试代码。
int closeness(char* string, char* target)
{
int fit = 0;
int index = 0;
while(string[index] && target[index])
{
if(string[index] == target[index])
fit++;
index++;
}
return fit;
}