Ejemplo n.º 1
0
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,
def evaluate_population(pop):
    simulation = CartPole(pop, markov=False)
    # comment this line to print the status
    simulation.print_status = False
    simulation.run()
Ejemplo n.º 3
0
import pickle

from cart_pole import CartPole, discrete_actuator_force
from movie import make_movie

from neat import nn

# load the winner
with open('nn_winner_genome', 'rb') as f:
    c = pickle.load(f)

print('Loaded genome:')
print(c)

net = nn.create_feed_forward_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.serial_activate(inputs)
Ejemplo n.º 4
0
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()
Ejemplo n.º 5
0
# 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()
Ejemplo n.º 6
0
from movie import make_movie

import neat
from neat import nn

# load the winner
with open("my_network.data", 'rb') as fd:
    winner, config, stats = pickle.load(fd)

print('Loaded genome:')
print(winner)

winner_net = neat.nn.FeedForwardNetworkFPGA.create(winner, config)
#winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
#winner_net.my_create_net_layer(winner, config)
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 120 seconds.
balance_time = 0.0
# while sim.t < 120.0:
#     inputs = sim.get_scaled_state()
#     action = winner_net.activate(inputs)
#     #action = winner_net.my_activate(inputs)
Ejemplo n.º 7
0
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,
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def evaluate_population(population):

    simulation = CartPole(population, markov = False)
    # comment this line to print the status
    simulation.print_status = False
    simulation.run()
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)
import cPickle as pickle
from cart_pole import CartPole

if len(sys.argv) > 1:
    # load genome
    try:
        file = open(sys.argv[1], 'r')
    except IOError:
        print "Filename: '"+sys.argv[1]+"' not found!"
        sys.exit(0)
    else:
        c = pickle.load(file)
        file.close()
else:
    print "Loading default winner chromosome file"
    try:
        file = open('winner_chromosome', 'r')
    except IOError:
        print "Winner chromosome not found!"
        sys.exit(0)
    else:
        c = pickle.load(file)
        file.close()

# load settings file
config.load('cpExp_config')
print "Loaded genome:"
print c
# starts the simulation
simulator = CartPole([c], markov=False)
simulator.run(testing=True)
Ejemplo n.º 12
0
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.81
mass_pole = 0.15
length = 0.5
moment_of_inertia = (1. / 3.) * mass_pole * length**2


def E(x):  # energy
    return (moment_of_inertia * x[3]**2 / 2) - (np.cos(x[2]) * length *
                                                mass_pole * gravity / 2)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
from analyzer import EncoderAnalyzer, ImageAnalyzer
from motor_control import SabreControl
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)
while True:
    #print('image' , "%02.2f"%cart.angle_analyzer.getAngle(), 'encoder', "%02.2f"%cart.pos_analyzer.getAngle())
    #print('image' , "%02.2f"%cart.angle_analyzer.getAngle(),
    #	"%02.2f"%cart.angle_analyzer.getAngleVel(),
    #	'encoder' , "%02.2f"%cart.pos_analyzer.getAngle(),
    #	 "%02.2f"%cart.pos_analyzer.getAngleVel())
    print(cart.getState())