我有这个循环作为更大程序的一部分(它是开源的,可以下载并在逗号内运行,配置在那里):
my $promise = start react whenever $channel-one -> @crew {
Algorithm::Evolutionary::LogTimelineSchema::Evolutionary.log: {
my %fitness-of;
Algorithm::Evolutionary::LogTimelineSchema::Frequencies
.log( :@crew );
my @unpacked-pop = generate-by-frequencies( $population-size, @crew );
my $population = evaluate( population => @unpacked-pop,
:%fitness-of,
evaluator => &leading-ones);
Algorithm::Evolutionary::LogTimelineSchema::GenerationsStart
.log( :population-size(@unpacked-pop.elems),
:distinct-elements( %fitness-of.keys.elems) );
my atomicint $count = 0;
while ($count⚛++ < $generations) &&
(best-fitness($population) < $max-fitness) {
LAST {
if best-fitness($population) >= $max-fitness {
Algorithm::Evolutionary::LogTimelineSchema::SolutionFound
.log(
{
id => $*THREAD.id,
best => best-fitness($population),
found => True,
finishing-at => DateTime.now.Str
}
);
say "Solution found" => $evaluations;
Algorithm::Evolutionary::LogTimelineSchema::SolutionFound
.log( :$evaluations );
$channel-one.close;
} else {
say "Emitting after $count generations in thread ",
$*THREAD.id, " Best fitness ",best-fitness($population);
if $count < $generations {
Algorithm::Evolutionary::LogTimelineSchema::Weird
.log( id => $*THREAD.id,
best => best-fitness($population),
:$count,
:population-size(@unpacked-pop.elems),
:distinct-elements( %fitness-of.keys.elems)
);
} else {
Algorithm::Evolutionary::LogTimelineSchema::Events.log(
id => $*THREAD.id,
best => best-fitness($population),
:$count
);
}
$to-mix.send( frequencies-best($population, 8) );
}
};
$population = generation( :$population, :%fitness-of,
evaluator => &leading-ones,
:$population-size
);
$evaluations += $population.elems;
}
Algorithm::Evolutionary::LogTimelineSchema::Generations
.log( :generations($count),
individuals => %fitness-of.keys.elems);
$evaluations;
};
};
查看循环条件:
while ($count⚛++ < $generations) &&
(best-fitness($population) < $max-fitness) {
$count
是一个局部变量,此外,它是原子的,超过该数量或达到最佳适应度似乎是唯一的出路。
只是它不是。循环自行结束,不满足任何条件。我已经设置了两个不同的日志事件,“Weird”和“EndRun”(您可以在上面看到),当循环结束时调用“Weird”而没有达到最佳适应度或代数;几乎总是调用那个,正如在这个逗号可视化中看到的那样 ,顶部的(稀疏)三角形显示循环何时结束,下面的紫色三角形标记“奇怪”的结束。打印的日志也显示相同的内容:
Emitting after 8 generations in thread 10 Best fitness 48
Mixing in 12
Emitting after 5 generations in thread 7 Best fitness 47
Mixing in 12
Emitting after 2 generations in thread 8 Best fitness 48
Mixing in 12
Emitting after 1 generations in thread 10 Best fitness 48
Mixing in 8
它几乎从未达到 16 代,这是 $generations 的价值。我正在使用 Raku 2019.11,但这种事情以前也发生过。我不确定我是否遇到了错误,或者我只是触发了一些我并不真正了解的机制。任何想法?
更新:它也时不时崩溃......
/home/jmerelo/.rakudobrew/moar-2020.01/install/bin/rakudo /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6
Use of Nil in numeric context
in block at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 68
An operation first awaited:
in sub MAIN at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 139
in block <unit> at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 21
Died with the exception:
A react block:
in code at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 55
Died because of the exception:
Type check failed in binding to parameter '@chromosome1'; expected Positional but got Any (Any)
in sub crossover at /home/jmerelo/.rakudobrew/moar-2020.01/install/share/perl6/site/sources/C405508803395DA88D77FC9B113DB20EFC0C1C4C (Algorithm::Evolutionary::Simple) line 96
in sub produce-offspring at /home/jmerelo/.rakudobrew/moar-2020.01/install/share/perl6/site/sources/C405508803395DA88D77FC9B113DB20EFC0C1C4C (Algorithm::Evolutionary::Simple) line 115
in sub generation at /home/jmerelo/.rakudobrew/moar-2020.01/install/share/perl6/site/sources/C405508803395DA88D77FC9B113DB20EFC0C1C4C (Algorithm::Evolutionary::Simple) line 149
in block at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 107
in block at /home/jmerelo/.rakudobrew/moar-2020.01/install/share/perl6/site/sources/0F5CDA46B783643CA4E3D746E2329D0C256CB46B (Log::Timeline) line 95
in method log at /home/jmerelo/.rakudobrew/moar-2020.01/install/share/perl6/site/sources/0F5CDA46B783643CA4E3D746E2329D0C256CB46B (Log::Timeline) line 90
in block at /home/jmerelo/Papers/2020/2020-evostar-concurrent-eas/code/concurrent-ea-leading-ones.p6 line 56
更新 2尝试按要求打高尔夫球,我已将其更改while
为for
循环,它就像一个魅力。所以至少这对我有用,但我仍然想知道隧道的处理是什么while