轮盘选择,无需排序即可找到口袋
我目前正在尝试实现java轮盘赌选择(参见http://en.wikipedia.org/wiki/ Fitness_proportionate_selection)。
我的问题是在找到我的人口中每个人的相对适合度之后出现的。
例如,假设我创建了一个概率轮盘,例如:
个体 a : 0 - 0.3 个体b:0.3-0.5 个人 c : 0.5 - 1
如果我旋转这个轮盘赌,个人 a 的被选择率有 30% 的变化,b 有 20%,c 有 50% 的被选择率。
我的问题是,维基百科文章提到“请注意,通过使用二分搜索而不是线性搜索来找到正确的口袋,可以实现性能提升。”这意味着在生成随机数后,我必须对我的口袋完成线性或二分搜索才能找到已选择的数字。
问题是,从性能的角度来看,这种每次都找到正确口袋的二分搜索似乎是不必要的,难道不可能简单地拥有某种 HashMap,而表中的每个条目都链接到如图所示的范围上面并使用该范围内的键拉动将在线性时间内拉出关联的个体,而不需要二分搜索。
我查看了树形图,但树形图必须首先对口袋进行排序,这是不行的,不能排序。
I am currently attempting to implement a java roulette wheel selection (see http://en.wikipedia.org/wiki/Fitness_proportionate_selection).
My problem comes after finding the relative fitness for each individual of my population.
For example, Say I create a probalistic roulette such as :
individual a : 0 - 0.3
individual b : 0.3 - 0.5
individual c : 0.5 - 1
Whereas individual a has a 30% change of being selected, b has 20% and c has 50% of being selected was I to spin this roulette.
My problem is, the wikipedia article mention "Note performance gains can be achieved by using a binary search rather than a linear search to find the right pocket." implying that after generating my random number, I have to complete a linear or binary search of my pockets to find which one has been selected.
The thing is, from a performance point of view this binary search to find the right pocket at each turn seems unnecessary, wouldn't it be possible to simply have some kind of HashMap whereas each entry in the table is linked to the range as shown above and pulling using a key within that range would pull out the associated individual in linear time instead of requiring a binarysearch.
Ive looked at the treemap, but the treemap has to initialy sort the pockets which is a no go, no sorting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 n=1.000 个人并选择其中 k=100,您可以进行
只有测试才能知道什么更快,性能取决于 n、k 和实现。
With n=1.000 individuals and selecting k=100 of them, you can do
Only testing will tell what is faster, performance depends on n, k and the implementations.
好吧,也许这段 Java 中的轮盘赌选择代码可以让您入门
获得所有你能得到的这里的染色体类
Chromosome.java
Well maybe this code of Roulette wheel selection in java can ghet you started
to get all you can get Chromosome class here
Chromosome.java
HashMap 允许仅通过键的精确值查找元素。您的算法需要按范围查找值。因此,使用二分搜索或 TreeMap 会给您带来 O(log(n)) 复杂度。我认为不可能有更好的复杂性。
我不明白你在说什么。看起来你是说线性搜索比 TreeMap 或二分搜索具有更好的性能。这是错误的。
HashMap allows find elements by exact value of a key only. Your algorithm requires find a value by a range. So, using a binary search or the TreeMap will give you O(log(n)) complexity. I don't think it's possible to have better complexity.
And I don't understand what are you talking about. Looks like you are saying that linear search has better performance than TreeMap or binary search. It's wrong.