1

我有一些 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)

想法?

4

1 回答 1

1

Rather than print POP_SIZE, you could print POP_SIZE() where:

def POP_SIZE():
    return random.randint(100,200)

Each time you call POP_SIZE() it will return a new random integer.

for generation in xrange(GENERATIONS):
    ...
    print POP_SIZE()
    ...
于 2012-10-12T23:51:04.557 回答