質問

I have some code for a GA, that starts like this

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 = []

The important part is print POP_SIZE.

It prints what it needs to, and then the POP_SIZE stays the same, but is randomized ONLY if I exit the program and start it up again.

I want it to vary betweeen the paramaters I set at the beginning, which was

POP_SIZE    = random.randint(100,200)

Thoughts?

役に立ちましたか?

解決

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()
    ...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top