1

最近我决定接受进化编程的挑战,并遵循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;

而且,为了不弄乱整个帖子,这里是完整的代码 (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。

4

2 回答 2

0

对您的函数的调用是:

适应度[i] = 接近度(样本[i]、目标、大小);

大小是:

字符目标[] =“我认为”;大小 = sizeof (目标);

c 中的每个字符串都以 char '\0' 结尾,因此所有字符串函数都知道字符串在哪里结束。如果您在 sizeof(target)-1 位置开始比较两个字符串,它将首先比较两个 '\0'(逻辑上相等),然后比较其余的。

此外,我会将循环更改为:

对于 (i = n - 1; i >= 0; i--)

否则,不会比较第一个字符。

让我知道,如果这解决了您的问题。;)

于 2015-08-07T17:29:37.760 回答
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;
}
于 2015-08-07T18:22:24.937 回答