作为Differential Evolution算法实现的一部分,我需要实现'变异'步骤:
This就是我提出来的:
// Algorithm types
type Member = List[Double]
type Generation = Vector[Member]
def mutate(index: Int, generation: Generation): Member = {
// Create a random number stream with distinct values
val selector = Stream.continually(Random.nextInt(N)).distinct
// Select 3 mates from the generation
val mates = selector.filter(_ != index).take(3).map(generation(_))
// Calculate the donor member
(mates(0), mates(1), mates(2)).zipped map {
case (e1, e2, e3) => e1 + F * (e2 - e3)
}
}
(我按照here解释的那样实现了算法)
现在我的问题;有没有更好的方法来实现这一步骤?我一直试图找到一种更好的方法从矢量中选择3个列表并将它们压缩在一起,但我无法找到任何其他内容,然后手动将所选列表放入元组中。 scala编译器发出警告,而不是mates(0)
应该使用mates.head
,这表明这可以以更优雅的方式实现。
提前致谢!
答案 0 :(得分:0)
您可Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile
execfile(filename, namespace)
File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/python/crawl_zhihu.py", line 78, in <module>
print 'nickname:', zansoup.get('title'), ' ',
AttributeError: 'NoneType' object has no attribute 'get'
使用transpose
提取mates
而不是map
:
Seq
这将是mates.transpose map {
case Seq(e1, e2, e3) => e1 + F * (e2 - e3)
}
,因此要获得Stream[Double]
,您必须在其上调用Member
,或使用toList