def evaluate_genome(g): net = ctrnn.create_phenotype(g) fitnesses = [] for runs in range(runs_per_net): sim = cart_pole.CartPole() # Run the given simulation for up to num_steps time steps. fitness = 0.0 for s in range(num_steps): inputs = sim.get_scaled_state() action = net.parallel_activate(inputs) # Apply action to the simulated cart-pole force = cart_pole.discrete_actuator_force(action) sim.step(force) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs( sim.theta) >= sim.angle_limit_radians: break fitness += 1.0 fitnesses.append(fitness) # The genome's fitness is its worst performance across all runs. return min(fitnesses)
def evaluate_genome(g): net = ctrnn.create_phenotype(g) fitnesses = [] for runs in range(runs_per_net): sim = cart_pole.CartPole() # Run the given simulation for up to num_steps time steps. fitness = 0.0 for s in range(num_steps): inputs = sim.get_scaled_state() action = net.parallel_activate(inputs) # Apply action to the simulated cart-pole force = cart_pole.discrete_actuator_force(action) sim.step(force) # Stop if the network fails to keep the cart within the position or angle limits. # The per-run fitness is the number of time steps the network can balance the pole # without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break fitness += 1.0 fitnesses.append(fitness) # The genome's fitness is its worst performance across all runs. return min(fitnesses)
def evaluate_genome(genomes): for g in genomes: net = ctrnn.create_phenotype(g) fitness = 0.0 for t in test_values: net.reset() output = net.serial_activate([t]) expected = t ** 2 error = output[0] - expected fitness -= error ** 2 g.fitness = fitness
def evaluate_population(population): twelve_degrees = 0.2094384 #radians num_steps = 10**5 for chromo in population: net = ctrnn.create_phenotype(chromo) # old way (harder!) #x = random.uniform(-2.4, 2.4) # cart position, meters #x_dot = random.uniform(-1.0, 1.0) # cart velocity #theta = random.uniform(-0.2, 0.2) # pole angle, radians #theta_dot = random.uniform(-1.5, 1.5) # pole angular velocity # initial conditions (as used by Stanley) x = (random.randint(0, 2**31)%4800)/1000.0 - 2.4 x_dot = (random.randint(0, 2**31)%2000)/1000.0 - 1; theta = (random.randint(0, 2**31)%400)/1000.0 - .2 theta_dot = (random.randint(0, 2**31)%3000)/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] 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.5 or abs(theta) >= twelve_degrees): #if abs(theta) > twelve_degrees: # Igel (p. 5) # the cart/pole has run/inclined out of the limits break chromo.fitness = fitness
def evaluate_population(population): twelve_degrees = 0.2094384 #radians num_steps = 10**5 for chromo in population: net = ctrnn.create_phenotype(chromo) # old way (harder!) #x = random.uniform(-2.4, 2.4) # cart position, meters #x_dot = random.uniform(-1.0, 1.0) # cart velocity #theta = random.uniform(-0.2, 0.2) # pole angle, radians #theta_dot = random.uniform(-1.5, 1.5) # pole angular velocity # initial conditions (as used by Stanley) x = (random.randint(0, 2**31) % 4800) / 1000.0 - 2.4 x_dot = (random.randint(0, 2**31) % 2000) / 1000.0 - 1 theta = (random.randint(0, 2**31) % 400) / 1000.0 - .2 theta_dot = (random.randint(0, 2**31) % 3000) / 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] 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.5 or abs(theta) >= twelve_degrees): #if abs(theta) > twelve_degrees: # Igel (p. 5) # the cart/pole has run/inclined out of the limits break chromo.fitness = fitness
import pickle from cart_pole import CartPole, discrete_actuator_force from movie import make_movie from neat import ctrnn # load the winner with open('ctrnn_winner_genome', 'rb') as f: c = pickle.load(f) print('Loaded genome:') print(c) net = ctrnn.create_phenotype(c) sim = CartPole() print() print("Initial conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() # Run the given simulation for up to 100k time steps. num_balanced = 0 for s in range(10 ** 5): inputs = sim.get_scaled_state() action = net.parallel_activate(inputs)
import pickle from cart_pole import CartPole, discrete_actuator_force from movie import make_movie from neat import ctrnn # load the winner with open('ctrnn_winner_genome', 'rb') as f: c = pickle.load(f) print('Loaded genome:') print(c) net = ctrnn.create_phenotype(c) sim = CartPole() print() print("Initial conditions:") print(" x = {0:.4f}".format(sim.x)) print(" x_dot = {0:.4f}".format(sim.dx)) print(" theta = {0:.4f}".format(sim.theta)) print("theta_dot = {0:.4f}".format(sim.dtheta)) print() # Run the given simulation for up to 100k time steps. num_balanced = 0 for s in range(10**5): inputs = sim.get_scaled_state() action = net.parallel_activate(inputs)