def make_movie(net, force_function, duration_seconds, output_filename): w, h = 300, 100 scale = 300 / 6 cart = gz.rectangle(scale * 0.5, scale * 0.25, xy=(150, 80), stroke_width=1, fill=(0, 1, 0)) pole = gz.rectangle(scale * 0.1, scale * 1.0, xy=(150, 55), stroke_width=1, fill=(1, 1, 0)) sim = CartPole() def make_frame(t): inputs = sim.get_scaled_state() if hasattr(net, 'activate'): action = net.activate(inputs) else: action = net.advance(inputs, sim.time_step, sim.time_step) sim.step(force_function(action)) surface = gz.Surface(w, h, bg_color=(1, 1, 1)) # Convert position to display units visX = scale * sim.x # Draw cart. group = gz.Group((cart,)).translate((visX, 0)) group.draw(surface) # Draw pole. group = gz.Group((pole,)).translate((visX, 0)).rotate(sim.theta, center=(150 + visX, 80)) group.draw(surface) return surface.get_npimage() clip = mpy.VideoClip(make_frame, duration=duration_seconds) clip.write_videofile(output_filename, codec="mpeg4", fps=50)
def evaluate_population(genomes, create_func, force_func): for g in genomes: net = create_func(g) fitness = 0 for runs in range(runs_per_net): sim = CartPole() fitness += run_simulation(sim, net, force_func) # The genome's fitness is its average performance across all runs. g.fitness = fitness / float(runs_per_net)
NO_LEARNING_THRESHOLD = 20 # Time cycle of the simulation time = 0 # These variables perform bookkeeping (how many cycles was the pole # balanced for before it fell). Useful for plotting learning curves. time_steps_to_failure = [] num_failures = 0 time_at_start_of_current_trial = 0 # You should reach convergence well before this max_failures = 500 # Initialize a cart pole cart_pole = CartPole(Physics()) # Starting `state_tuple` is (0, 0, 0, 0) # x, x_dot, theta, theta_dot represents the actual continuous state vector x, x_dot, theta, theta_dot = 0.0, 0.0, 0.0, 0.0 state_tuple = (x, x_dot, theta, theta_dot) # `state` is the number given to this state, you only need to consider # this representation of the state state = cart_pole.get_state(state_tuple) # if min_trial_length_to_start_display == 0 or display_started == 1: # cart_pole.show_cart(state_tuple, pause_time) # Perform all your initializations here: # Assume no transitions or rewards have been observed. # Initialize the value function array to small random values (0 to 0.10,
# load the winner with open('winner-ctrnn', 'rb') as f: c = pickle.load(f) print('Loaded genome:') print(c) # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'config-ctrnn') config = neatfast.Config(neatfast.DefaultGenome, neatfast.DefaultReproduction, neatfast.DefaultSpeciesSet, neatfast.DefaultStagnation, config_path) sim = CartPole() net = neatfast.ctrnn.CTRNN.create(c, config, sim.time_step) 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 120 seconds. balance_time = 0.0 while sim.t < 120.0: inputs = sim.get_scaled_state()
mpc = MPC(0.5, 0, 0, 0) ports = list(serial.tools.list_ports.comports()) print(ports) for p in ports: print(p) if p[2] == "USB VID:PID=268b:0201 SNR=160045F45953": sabre_port = p[0] elif p[2] == "USB VID:PID=2341:0043 SNR=75334323935351D0D022": ard_port = p[0] motor = SabreControl(port=sabre_port) encoder = EncoderAnalyzer(port=ard_port) #image = ImageAnalyzer(0,show_image=True) cart = CartPole(motor, encoder, encoder, encoder) cart.zeroAngleAnalyzer() #encoder.setAngleZero() cart.zeroPosAnalyzer() cart.goTo(.6) fig = plt.figure() plt.autoscale(tight=True) ax = fig.add_subplot(111) ax.autoscale(enable=True, axis="y", tight=False) li_x = None command_speed = 0 last_time = time.time()
def evaluate_population(population): simulation = CartPole(population, markov = False) # comment this line to print the status simulation.print_status = False simulation.run()
from numpy import pi from cart_pole import CartPole from plot_graphs import visualize_plots from render_movie import visualize_movie from learning_system import LearningCorrection play_movie = True ideal_cart_pole = CartPole( dt=.001, init_conds=[0, 0., pi, 0.], #x, dx, phi, dphi end=10, ) ideal_cart_pole.compute_lqr_gain() ideal_data = ideal_cart_pole.integrate() dist_cart_pole = CartPole( dt=.001, init_conds=[0, 0., pi, 0.], #x, dx, phi, dphi end=10, disturb=False, ) dist_cart_pole.compute_lqr_gain() dist_data = dist_cart_pole.integrate() # ## Learn the system model lc = LearningCorrection(ideal_system=ideal_cart_pole, ideal_data=ideal_data, real_system=dist_cart_pole)
with open('winner-feedforward', 'rb') as f: c = pickle.load(f) print('Loaded genome:') print(c) # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'config-feedforward-cartpole') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) net = neat.nn.FeedForwardNetwork.create(c, config) sim = CartPole(x=2.0, dx=0, dtheta=0, theta=-30.0*math.pi/180.0) 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 120 seconds. balance_time = 0.0 while sim.t < 120.0: inputs = sim.get_scaled_state() action = net.activate(inputs)
from cart_pole import CartPole import serial.tools.list_ports ports = list(serial.tools.list_ports.comports()) print(ports) for p in ports: print(p) if p[2] == "USB VID:PID=268b:0201 SNR=160045F45953": sabre_port = p[0] elif p[2] == "USB VID:PID=2341:0043 SNR=75334323935351D0D022": ard_port = p[0] motor = SabreControl(port=sabre_port) encoder = EncoderAnalyzer(port=ard_port) image = ImageAnalyzer(0, show_image=True) cart = CartPole(motor, encoder, image, encoder) cart.zeroAngleAnalyzer() #encoder.setAngleZero() cart.zeroPosAnalyzer() cart.goTo(.5) gravity = 9.8 mass_pole = 0.1 length = 0.5 moment_of_inertia = (1. / 3.) * mass_pole * length**2 print(moment_of_inertia) A = np.array( [[0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1],
raise Exception('''This example is currently broken, it will be fixed soon. In the meantime, try the single_pole or xor examples.''') import os import sys import cPickle from cart_pole import CartPole from neatsociety.config import Config filename = 'winner_chromosome' if len(sys.argv) > 1: filename = sys.argv[1] # load genome print("loading genome {0!s}".format(filename)) with open(filename) as f: c = cPickle.load(f) # load settings file local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'dpole_config')) print("Loaded genome:\n{0!s}".format(c)) # starts the simulation simulator = CartPole([c], markov=False) simulator.run(testing=True)