def make_frame(t): global x, x_dot, theta, theta_dot, last_t # maps into [0,1] inputs = [(x + 2.4) / 4.8, (x_dot + 0.75) / 1.5, (theta + angle_limit) / 0.41, (theta_dot + 1.0) / 2.0] action = net.pactivate(inputs) action[0] += 0.4 * (np.random.random() - 0.5) # Apply action to the simulated cart-pole x, x_dot, theta, theta_dot = cart_pole(action[0], x, x_dot, theta, theta_dot) surface = gz.Surface(W, H, bg_color=(1, 1, 1)) # Convert position to display units visX = SCALE * x # Draw cart. group = gz.Group((cart,)).translate((visX, 0)) group.draw(surface) # Draw pole. group = gz.Group((pole,)).translate((visX, 0)).rotate(theta, center=(150 + visX, 80)) group.draw(surface) return surface.get_npimage()
local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'spole_config')) net = nn.create_phenotype(c) # 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): # maps into [0,1] inputs = [(x + 2.4) / 4.8, (x_dot + 0.75) / 1.5, (theta + angle_limit) / 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) if abs(x) >= 2.4 or abs(theta) >= angle_limit: print '\nFailed at step %d \n' % step sys.exit(1) print '\nPole balanced for 10^5 time steps!'