Пример #1
0
def evaluate_population(population):
    
    twelve_degrees = 0.2094384 #radians
    num_steps = 10**5
    
    for chromo in population:
        
        net = nn.create_phenotype(chromo)
        
        # initial conditions (as used by Stanley)        
        x         = random.randint(0, 4799)/1000.0 - 2.4
        x_dot     = random.randint(0, 1999)/1000.0 - 1.0
        theta     = random.randint(0,  399)/1000.0 - 0.2
        theta_dot = random.randint(0, 2999)/1000.0 - 1.5
        #x = 0.0
        #x_dot = 0.0
        #theta = 0.0
        #theta_dot = 0.0
        
        fitness = 0

        for trials in xrange(num_steps):
        
            # maps into [0,1]
            inputs = [(x + 2.4)/4.8, 
                      (x_dot + 0.75)/1.5,
                      (theta + twelve_degrees)/0.41,
                      (theta_dot + 1.0)/2.0]
            
            # a normalizacao so acontece para estas condicoes iniciais
            # nada garante que a evolucao do sistema leve a outros
            # valores de x, x_dot e etc...
                      
            action = net.pactivate(inputs)
            
            # Apply action to the simulated cart-pole
            x, x_dot, theta, theta_dot = cart_pole(action[0], x, x_dot, theta, theta_dot)
            
            # Check for failure.  If so, return steps
            # the number of steps indicates the fitness: higher = better
            fitness += 1
            if (abs(x) >= 2.4 or abs(theta) >= twelve_degrees):
            #if abs(theta) > twelve_degrees: # Igel (p. 5) uses theta criteria only
                # the cart/pole has run/inclined out of the limits
                break
                
        chromo.fitness = fitness
Пример #2
0
from random import randint
import cPickle as pickle
import single_pole

chromosome.node_gene_type = genome2.NodeGene

# load the winner
file = open('winner_chromosome', 'r')
c = pickle.load(file)
file.close()

print 'Loaded chromosome:'
print c

config.load('spole_config')
net = nn.create_phenotype(c)


#x = 0.0
#x_dot = 0.0
#theta = 0.0
#theta_dot = 0.0

# initial conditions (as used by Stanley)
x         = randint(0, 4799)/1000.0 - 2.4
x_dot     = randint(0, 1999)/1000.0 - 1.0
theta     = randint(0,  399)/1000.0 - 0.2
theta_dot = randint(0, 2999)/1000.0 - 1.5
        
print "\nInitial conditions:"
print "%2.4f   %2.4f   %2.4f   %2.4f" %(x, x_dot, theta, theta_dot)
Пример #3
0
from random import randint
import cPickle as pickle
import single_pole

chromosome.node_gene_type = genome2.NodeGene

# load the winner
file = open('winner_chromosome', 'r')
c = pickle.load(file)
file.close()

print 'Loaded chromosome:'
print c

config.load('spole_config')
net = nn.create_phenotype(c)

#x = 0.0
#x_dot = 0.0
#theta = 0.0
#theta_dot = 0.0

# initial conditions (as used by Stanley)
x = randint(0, 4799) / 1000.0 - 2.4
x_dot = randint(0, 1999) / 1000.0 - 1.0
theta = randint(0, 399) / 1000.0 - 0.2
theta_dot = randint(0, 2999) / 1000.0 - 1.5

print "\nInitial conditions:"
print "%2.4f   %2.4f   %2.4f   %2.4f" % (x, x_dot, theta, theta_dot)
for step in xrange(10**5):