我试图建立一个简单的遗传算法,将优化到输入字符串,但我有麻烦建设[个人x基因组]矩阵(第n行是个人n的基因组)。我希望能够改变人口规模,变异率和其他参数,以研究如何影响收敛速度和程序效率。矩阵的可变大小[ixj](Python,Numpy)
这是我到目前为止有:
import random
import itertools
import numpy as np
def evolve():
goal = 'Hello, World!' #string to optimize towards
ideal = list(goal)
#converting the string into a list of integers
for i in range (0,len(ideal)):
ideal [i] = ord(ideal[i])
print(ideal)
popSize = 10 #population size
genome = len(ideal) #determineing the length of the genome to be the length of the target string
mut = 0.03 #mutation rate
S = 4 #tournament size
best = float("inf") #initial best is very large
maxVal = max(ideal)
minVal = min(ideal)
print (maxVal)
i = 0 #counting variables assigned to solve UnboundLocalError
j = 0
print(maxVal, minVal)
#constructing initial population array (individual x genome)
pop = np.empty([popSize, len(ideal)])
for i, j in itertools.product(range(i), range(j)):
pop[i, j] = [i, random.randint(minVal,maxVal)]
print(pop)
这产生了人口规模与正确的基因组长度的矩阵,但基因组是这样的:
[ 6.91364167e-310 6.91364167e-310 1.80613009e-316 1.80613009e-316
5.07224590e-317 0.00000000e+000 6.04100487e+151 3.13149876e-120
1.11787892e+253 1.47872844e-028 7.34486815e+223 1.26594941e-118
7.63858409e+228]
我需要他们为与随机ASCII字符对应的随机整数。
我在做什么错误的这种方法? 有没有办法让这个更快?
感谢您提供任何帮助。
2015-02-10
MatthewC