Algorithm 轮盘赌轮选择算法
有人能为轮盘赌选择函数提供一些伪代码吗?我该如何实现这一点:我真的不知道如何读取这个数学符号。我想用一般算法来实现这一点。有两个步骤:首先创建一个包含轮子上所有值的数组。这可以是一个带有颜色和数字的二维数组,也可以选择将100添加到红色数字中 然后简单地生成一个介于0或1(取决于您的语言是从0还是1开始为数组索引编号)和数组中最后一个元素之间的随机数 大多数语言都有内置的随机数函数。在VB和Algorithm 轮盘赌轮选择算法,algorithm,genetic-algorithm,evolutionary-algorithm,roulette-wheel-selection,Algorithm,Genetic Algorithm,Evolutionary Algorithm,Roulette Wheel Selection,有人能为轮盘赌选择函数提供一些伪代码吗?我该如何实现这一点:我真的不知道如何读取这个数学符号。我想用一般算法来实现这一点。有两个步骤:首先创建一个包含轮子上所有值的数组。这可以是一个带有颜色和数字的二维数组,也可以选择将100添加到红色数字中 然后简单地生成一个介于0或1(取决于您的语言是从0还是1开始为数组索引编号)和数组中最后一个元素之间的随机数 大多数语言都有内置的随机数函数。在VB和VBScript中,函数为RND()。在Javascript中,它是Math.random() 从数组中的
VBScript
中,函数为RND()
。在Javascript中,它是Math.random()
从数组中的该位置获取值,就得到了随机轮盘号码
最后注意事项:别忘了为随机数生成器设置种子,否则每次运行程序时都会得到相同的绘图序列。随机数生成器伪代码
- 向顺序计数器添加一个
- 获取顺序计数器的当前值
- 通过计算机滴答计数或其他小间隔计时器值添加计数器值
- 可以选择添加加法,例如来自外部硬件(如等离子发生器)的数字或其他某种随机现象
- 将结果除以一个非常大的素数 例如,3593340859668622831041960188598043661065388726959079837
- 从结果小数点的最右边获取一些数字
- 将这些数字用作随机数
使用随机数数字为轮盘赌创建1到38(或37个欧洲)之间的随机数。对于美国轮盘赌,您需要生成1到38之间的随机整数。有36个数字,0和00 然而,在美国轮盘赌游戏中,有很多值得考虑的事情是,他们可以做出很多不同的赌注。一次下注可以覆盖1、2、3、4、5、6、两个不同的12秒或18秒。您可能希望创建一个列表,其中每个数字都有额外的鞭毛来简化它,或者在编程中完成这一切
如果我用Python实现它,我只需要创建一个0、00和1到36的元组,并对每个旋转使用random.choice()。其他答案似乎假设您正在尝试实现一个轮盘赌游戏。我想你是在问进化算法中轮盘赌的选择 实现轮盘赌轮选择的 假设您有10项可供选择,并通过生成一个介于0和1之间的随机数进行选择。将范围0到1向上划分为十个不重叠的段,每个段与十个项目之一的适合度成比例。例如,这可能如下所示:
0 - 0.3 is item 1
0.3 - 0.4 is item 2
0.4 - 0.5 is item 3
0.5 - 0.57 is item 4
0.57 - 0.63 is item 5
0.63 - 0.68 is item 6
0.68 - 0.8 is item 7
0.8 - 0.85 is item 8
0.85 - 0.98 is item 9
0.98 - 1 is item 10
Map<A, B> roulette_wheel_schema = new LinkedHashMap<A, B>()
这是你的轮盘赌。你在0和1之间的随机数是你的自旋。如果随机数为0.46,则所选项目为项目3。如果它是0.92,那么它就是第9项。首先,生成一个分配的百分比数组,比如说
p[1..n]
假设总数是所有百分比的总和
然后得到一个介于1到总数之间的随机数,比如说r
现在,lua中的算法:
local c = 0
for i = 1,n do
c = c + p[i]
if r <= c then
return i
end
end
localc=0
对于i=1,n do
c=c+p[i]
如果r这假设某个类“分类器”,它只有一个字符串条件、字符串消息和双重强度。只需遵循逻辑即可
--保罗
公共静态列表轮盘选择(int分类器){
List classifierList=新建LinkedList();
双强度sum=0.0;
双重概率总和=0.0;
//把地图的优点加起来
Set keySet=ClassifierMap.CLASSIFIER_MAP.keySet();
用于(字符串键:键集){
/*用于调试以确保控制盘正常工作。
如果(强度sum==0.0){
分类器映射。分类器映射。获取(键)。设置强度(8000.0);
}
*/
分类器=分类器映射。分类器映射。获取(键);
double-strength=分类器。getStrength();
strengthSum=strengthSum+强度;
}
System.out.println(“strengthSum:+strengthSum”);
//计算总概率。这将是1.00或接近它。
用于(字符串键:键集){
分类器=分类器映射。分类器映射。获取(键);
双概率=(分类器.getStrength()/strengthSum);
概率总和=概率总和+概率;
}
System.out.println(“probabilitySum:+probabilitySum”);
while(classifierList.size()轮盘赌&&(winnerFound==false)){
System.out.println(“获胜者发现:+概率”);
添加(分类器);
winnerFound=true;
}
}
}
返回分类器列表;
}
以下是一些python代码:
def roulette_select(population, fitnesses, num):
""" Roulette selection, implemented according to:
<http://stackoverflow.com/questions/177271/roulette
-selection-in-genetic-algorithms/177278#177278>
"""
total_fitness = float(sum(fitnesses))
rel_fitness = [f/total_fitness for f in fitnesses]
# Generate probability intervals for each individual
probs = [sum(rel_fitness[:i+1]) for i in range(len(rel_fitness))]
# Draw new population
new_population = []
for n in xrange(num):
r = rand()
for (i, individual) in enumerate(population):
if r <= probs[i]:
new_population.append(individual)
break
return new_population
def轮盘赌_选择(人口、适合度、数量):
“”“轮盘赌选择,根据以下内容实施:
"""
总适应度=浮动(总适应度)
相对适合度=[f/适合度中f的总适合度]
#为每个个体生成概率区间
probs=[i在范围内(len(rel_fitness))的总和(rel_fitness[:i+1])]
#吸引新的人口
新人口=[]
对于X范围内的n(数量):
r=rand()
对于枚举(总体)中的(i,个人):
如果r可以使用如下数据结构:
0 - 0.3 is item 1
0.3 - 0.4 is item 2
0.4 - 0.5 is item 3
0.5 - 0.57 is item 4
0.57 - 0.63 is item 5
0.63 - 0.68 is item 6
0.68 - 0.8 is item 7
0.8 - 0.85 is item 8
0.85 - 0.98 is item 9
0.98 - 1 is item 10
Map<A, B> roulette_wheel_schema = new LinkedHashMap<A, B>()
Map roule
class Roulette
{
double[] c;
double total;
Random random;
public Roulette(double[] n) {
random = new Random();
total = 0;
c = new double[n.Length+1];
c[0] = 0;
// Create cumulative values for later:
for (int i = 0; i < n.Length; i++) {
c[i+1] = c[i] + n[i];
total += n[i];
}
}
public int spin() {
double r = random.NextDouble() * total; // Create a random number between 0 and 1 and times by the total we calculated earlier.
//int j; for (j = 0; j < c.Length; j++) if (c[j] > r) break; return j-1; // Don't use this - it's slower than the binary search below.
//// Binary search for efficiency. Objective is to find index of the number just above r:
int a = 0;
int b = c.Length - 1;
while (b - a > 1) {
int mid = (a + b) / 2;
if (c[mid] > r) b = mid;
else a = mid;
}
return a;
}
}
static int selectRandomWeighted(double[] wts, Random rnd) {
int selected = 0;
double total = wts[0];
for( int i = 1; i < wts.length; i++ ) {
total += wts[i];
if( rnd.nextDouble() <= (wts[i] / total)) selected = i;
}
return selected;
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Roulette-wheel Test version.
* Features a probability vector input with possibly null probability values.
* Appropriate for adaptive operator selection such as Probability Matching
* or Adaptive Pursuit, (Dynamic) Multi-armed Bandit.
* @version October 2015.
* @author Hakim Mitiche
*/
public class RouletteWheel {
/**
* Selects an element probabilistically.
* @param wheelProbabilities elements probability vector.
* @param rng random generator object
* @return selected element index
* @throws java.lang.Exception
*/
public int select(List<Double> wheelProbabilities, Random rng)
throws Exception{
double[] cumulativeProba = new double[wheelProbabilities.size()];
cumulativeProba[0] = wheelProbabilities.get(0);
for (int i = 1; i < wheelProbabilities.size(); i++)
{
double proba = wheelProbabilities.get(i);
cumulativeProba[i] = cumulativeProba[i - 1] + proba;
}
int last = wheelProbabilities.size()-1;
if (cumulativeProba[last] != 1.0)
{
throw new Exception("The probabilities does not sum up to one ("
+ "sum="+cumulativeProba[last]);
}
double r = rng.nextDouble();
int selected = Arrays.binarySearch(cumulativeProba, r);
if (selected < 0)
{
/* Convert negative insertion point to array index.
to find the correct cumulative proba range index.
*/
selected = Math.abs(selected + 1);
}
/* skip indexes of elements with Zero probability,
go backward to matching index*/
int i = selected;
while (wheelProbabilities.get(i) == 0.0){
System.out.print(i+" selected, correction");
i--;
if (i<0) i=last;
}
selected = i;
return selected;
}
public static void main(String[] args){
RouletteWheel rw = new RouletteWheel();
int rept = 20;
List<Double> P = new ArrayList<>(4);
P.add(0.2);
P.add(0.1);
P.add(0.6);
P.add(0.1);
Random rng = new Random();
for (int i = 0 ; i < rept; i++){
try {
int s = rw.select(P, rng);
System.out.println("Element selected "+s+ ", P(s)="+P.get(s));
} catch (Exception ex) {
Logger.getLogger(RouletteWheel.class.getName()).log(Level.SEVERE, null, ex);
}
}
P.clear();
P.add(0.2);
P.add(0.0);
P.add(0.5);
P.add(0.0);
P.add(0.1);
P.add(0.2);
//rng = new Random();
for (int i = 0 ; i < rept; i++){
try {
int s = rw.select(P, rng);
System.out.println("Element selected "+s+ ", P(s)="+P.get(s));
} catch (Exception ex) {
Logger.getLogger(RouletteWheel.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/**
* {@inheritDoc}
* @return
*/
@Override
public String toString()
{
return "Roulette Wheel Selection";
}
}