【发布时间】:2018-10-20 17:59:10
【问题描述】:
我正在使用 python-3.x,我正在尝试生成一个初始总体,其中包含 0 到 1 之间的随机实数,其中这些数字应为以下之一: 0、0.33333、0.666667 或 1
这意味着这些数字之间的差异是 0.33333 (1/3)。我尝试以多种方式修改此代码,但没有运气
import numpy as np
import random
from random import randint
from itertools import product
pop_size = 7
i_length = 2
i_min = 0
i_max = 1
level = 2
step = ((1/((2**level)-1))*(i_max-i_min))
def individual(length, min, max):
return [ randint(min,max) for x in range(length) ]
def population(count, length, min, max):
return [ individual(length, min, max) for x in range(count) ]
population = population(pop_size, i_length, i_min, i_max)
##count: the number of individuals in the population
##length: the number of values per individual
##min: the minimum possible value in an individual's list of values
##max: the maximum possible value in an individual's list of values
##this code was taken from :https://lethain.com/genetic-algorithms-cool-name-damn-simple/
我做了这几行,对我来说效果很好:
population2 = np.array(list(product(np.linspace(i_min, i_max, 2**level), repeat=2)))
population3 = [j for j in product(np.arange(i_min, i_max+step, step), repeat=2)]
但问题是它会列出所有可能不是我想要的值。我想要给出人口规模的随机数
我想看到的结果是 smailar to (numpy array or list):
population = [[0, 1],
[0, 0.3333],
[0.3333, 1],
[1, 0.6667],
[0.3333, 0.6667],
[0.6667, 0],
[0.3333, 0.3333]]
请记住:
level = 2
我可以在哪里计算步长值:
step = ((1/((2**level)-1))*(i_max-i_min))
例如,如果我将 level = 2 更改为 level = 3,则不再使用 0.3333,它将更改为 0.1428 1/7),我将获得不同的值。
任何建议将不胜感激
【问题讨论】:
标签: python python-3.x list numpy random