我有一些 GA 的代码,它是这样开始
的import random
#
# Global variables
# Setup optimal string and GA input variables.
POP_SIZE = random.randint(100,200)
# random code that doesn't really matter...
# Simulate all of the generations.
for generation in xrange(GENERATIONS):
print "Generation %s... Random sample: '%s'" % (generation, population[0])
print POP_SIZE
weighted_population = []
重要的部分是print POP_SIZE
.
它打印它需要的内容,然后POP_SIZE
保持不变,但随机化仅当我退出程序并重新启动它时。
我希望它在我开始时设置的参数之间有所不同,这是
POP_SIZE = random.randint(100,200)
思潮?
与其print POP_SIZE
,不如print POP_SIZE()
:
def POP_SIZE():
return random.randint(100,200)
每次调用POP_SIZE()
时,它都会返回一个新的随机整数。
for generation in xrange(GENERATIONS):
...
print POP_SIZE()
...