gpt4 book ai didi

c - 进化算法达到一个恒定的适应值

转载 作者:太空宇宙 更新时间:2023-11-04 04:35:05 25 4
gpt4 key购买 nike

最近我决定接受演化编程的挑战,并遵循发布在 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。

最佳答案

对您的函数的调用是:

fitness[i] = closeness (specimen[i], target, size);

大小是:

char target[] = "methinks";size = sizeof (target);

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

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

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

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

如果这解决了您的问题,请告诉我。 ;)

关于c - 进化算法达到一个恒定的适应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31552887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com