我也对使用线性排名选择时如何计算概率的各种来源感到有些困惑,有时也称为“排名选择”,如此处所述。至少我希望这两个指的是同一件事。
对我来说难以捉摸的部分是似乎在大多数资料中被省略或至少没有明确说明的等级总和。在这里,我展示了一个简短但冗长的 Python 示例,说明如何计算概率分布(您经常看到的那些漂亮的图表)。
假设这些是一些示例个体 fintesses:10、9、3、15、85、7。
排序后,按升序分配排名:第 1:3,第 2:7,第 3:9,第 4:10 ,第5:15,第6:85
所有等级的总和为 1+2+3+4+5+6 或使用高斯公式 (6+1)*6/2 = 21。
因此,我们将概率计算为:1/21、2/21、3/21、4/21、5/21、6/21,然后您可以将其表示为百分比:

请注意,这不是在遗传算法的实际实现中使用的,只是一个帮助脚本,可以为您提供更好的直觉。
您可以使用以下方法获取此脚本:
curl -o ranksel.py https://gist.githubusercontent.com/kburnik/3fe766b65f7f7427d3423d233d02cd39/raw/5c2e569189eca48212c34b3ea8a8328cb8d07ea5/ranksel.py
#!/usr/bin/env python
"""
Assumed name of script: ranksel.py
Sample program to estimate individual's selection probability using the Linear
Ranking Selection algorithm - a selection method in the field of Genetic
Algorithms. This should work with Python 2.7 and 3.5+.
Usage:
./ranksel.py f1 f2 ... fN
Where fK is the scalar fitness of the Kth individual. Any ordering is accepted.
Example:
$ python -u ranksel.py 10 9 3 15 85 7
Rank Fitness Sel.prob.
1 3.00 4.76%
2 7.00 9.52%
3 9.00 14.29%
4 10.00 19.05%
5 15.00 23.81%
6 85.00 28.57%
"""
from __future__ import print_function
import sys
def compute_sel_prob(population_fitness):
"""Computes and generates tuples of (rank, individual_fitness,
selection_probability) for each individual's fitness, using the Linear
Ranking Selection algorithm."""
# Get the number of individuals in the population.
n = len(population_fitness)
# Use the gauss formula to get the sum of all ranks (sum of integers 1 to N).
rank_sum = n * (n + 1) / 2
# Sort and go through all individual fitnesses; enumerate ranks from 1.
for rank, ind_fitness in enumerate(sorted(population_fitness), 1):
yield rank, ind_fitness, float(rank) / rank_sum
if __name__ == "__main__":
# Read the fitnesses from the command line arguments.
population_fitness = list(map(float, sys.argv[1:]))
print ("Rank Fitness Sel.prob.")
# Iterate through the computed tuples and print the table rows.
for rank, ind_fitness, sel_prob in compute_sel_prob(population_fitness):
print("%4d %7.2f %8.2f%%" % (rank, ind_fitness, sel_prob * 100))