Exemplo n.º 1
0
def test_physicality():
    """
    Test coordinate system [vertical up is 0]
        1) gravity acts in proper direction based on origin
        2) force actions behave as expected in that frame
    """
    # Apply a bunch of non-force actions, ensure that monotonically accelerate

    LEFT_FORCE = 0
    NO_FORCE = 1
    RIGHT_FORCE = 2
    domain = FiniteCartPoleBalanceModern()
    domain.force_noise_max = 0 # no stochasticity in applied force
    
    domain.int_type = 'rk4'
    
    # Slightly positive angle, just right of vertical up
    s = np.array([10.0 * np.pi/180.0, 0.0, 0.0, 0.0]) # pendulum slightly right
    domain.state = s
    
    for i in np.arange(5): # do for 5 steps and ensure works
        domain.step(NO_FORCE)
        assert np.all(domain.state[0:2] > s[0:2]) # angle and angular velocity increase
        # no energy should enter or leave system under no force action
        assert np.abs(_cartPoleEnergy(domain, s) - _cartPoleEnergy(domain, domain.state)) < 0.01

        s = domain.state
    
    # Negative angle (left)
    s = np.array([-10.0 * np.pi/180.0, 0.0, 0.0, 0.0]) # pendulum slightly right
    domain.state = s
    
    for i in np.arange(5): # do for 5 steps and ensure works
        domain.step(NO_FORCE)
        assert np.all(domain.state[0:2] < s[0:2]) # angle and angular velocity increase
        # no energy should enter or leave system under no force action
        assert np.abs(_cartPoleEnergy(domain, s) - _cartPoleEnergy(domain, domain.state)) < 0.01
        s = domain.state
    
    
    # Start vertical, ensure that force increases angular velocity in direction
    # Negative force on cart, yielding positive rotation
    s = np.array([0.0, 0.0, 0.0, 0.0])
    domain.state = s
    
    for i in np.arange(5): # do for 5 steps and ensure works
        domain.step(LEFT_FORCE)
        assert np.all(domain.state[0:2] > s[0:2]) # angle and angular velocity increase
        s = domain.state
        
    # Positive force on cart, yielding negative rotation
    s = np.array([0.0, 0.0, 0.0, 0.0])
    domain.state = s
    
    for i in np.arange(5): # do for 5 steps and ensure works
        domain.step(RIGHT_FORCE)
        assert np.all(domain.state[0:2] < s[0:2]) # angle and angular velocity increase
        s = domain.state
Exemplo n.º 2
0
def test_physicality_hanging():
    """
    Test that energy does not spontaneously enter system
    """
    # Apply a bunch of non-force actions, ensure that monotonically accelerate

    #     LEFT_FORCE = 0, RIGHT_FORCE = 1
    LEFT_FORCE = 0
    NO_FORCE = 1
    RIGHT_FORCE = 2
    domain = FiniteCartPoleBalanceModern()
    domain.force_noise_max = 0  # no stochasticity in applied force
    domain.ANGLE_LIMITS = [-np.pi, np.pi]  # We actually want to test hanging
    # Positive angle (right)
    s = np.array([179.6 * np.pi / 180.0, 0.0, -2.0,
                  0.0])  # pendulum hanging down
    domain.state = s.copy()

    for i in np.arange(5):  # do for 5 steps and ensure works
        domain.step(NO_FORCE)
        #         assert np.abs(domain.state[0]) <=179.5 # angle does not increase
        assert np.abs(domain.state[1]) <= 0.1  # angular rate does not increase
        # no energy should enter or leave system under no force action
        assert np.abs(
            _cartPoleEnergy(domain, s) -
            _cartPoleEnergy(domain, domain.state)) < 0.01
        s = domain.state

    # Ensure that running out of x bounds causes experiment to terminate
    assert domain.isTerminal(s=np.array([0.0, 0.0, 2.5, 0.0]))
    assert domain.isTerminal(s=np.array([0.0, 0.0, -2.5, 0.0]))
Exemplo n.º 3
0
def test_physicality_hanging():
    """
    Test that energy does not spontaneously enter system
    """
    # Apply a bunch of non-force actions, ensure that monotonically accelerate

#     LEFT_FORCE = 0, RIGHT_FORCE = 1
    LEFT_FORCE = 0
    NO_FORCE = 1
    RIGHT_FORCE = 2
    domain = FiniteCartPoleBalanceModern()
    domain.force_noise_max = 0 # no stochasticity in applied force
    domain.ANGLE_LIMITS = [-np.pi, np.pi] # We actually want to test hanging
    # Positive angle (right)
    s = np.array([179.6 * np.pi/180.0, 0.0, -2.0, 0.0]) # pendulum hanging down
    domain.state = s.copy()
    
    for i in np.arange(5): # do for 5 steps and ensure works
        domain.step(NO_FORCE)
#         assert np.abs(domain.state[0]) <=179.5 # angle does not increase
        assert np.abs(domain.state[1]) <= 0.1 # angular rate does not increase
        # no energy should enter or leave system under no force action
        assert np.abs(_cartPoleEnergy(domain, s) - _cartPoleEnergy(domain, domain.state)) < 0.01
        s = domain.state
    
    # Ensure that running out of x bounds causes experiment to terminate
    assert domain.isTerminal(s=np.array([0.0, 0.0, 2.5, 0.0]))
    assert domain.isTerminal(s=np.array([0.0, 0.0, -2.5, 0.0]))
Exemplo n.º 4
0
def test_physicality():
    """
    Test coordinate system [vertical up is 0]
        1) gravity acts in proper direction based on origin
        2) force actions behave as expected in that frame
    """
    # Apply a bunch of non-force actions, ensure that monotonically accelerate

    LEFT_FORCE = 0
    NO_FORCE = 1
    RIGHT_FORCE = 2
    domain = FiniteCartPoleBalanceModern()
    domain.force_noise_max = 0  # no stochasticity in applied force

    domain.int_type = 'rk4'

    # Slightly positive angle, just right of vertical up
    s = np.array([10.0 * np.pi / 180.0, 0.0, 0.0,
                  0.0])  # pendulum slightly right
    domain.state = s

    for i in np.arange(5):  # do for 5 steps and ensure works
        domain.step(NO_FORCE)
        assert np.all(
            domain.state[0:2] > s[0:2])  # angle and angular velocity increase
        # no energy should enter or leave system under no force action
        assert np.abs(
            _cartPoleEnergy(domain, s) -
            _cartPoleEnergy(domain, domain.state)) < 0.01

        s = domain.state

    # Negative angle (left)
    s = np.array([-10.0 * np.pi / 180.0, 0.0, 0.0,
                  0.0])  # pendulum slightly right
    domain.state = s

    for i in np.arange(5):  # do for 5 steps and ensure works
        domain.step(NO_FORCE)
        assert np.all(
            domain.state[0:2] < s[0:2])  # angle and angular velocity increase
        # no energy should enter or leave system under no force action
        assert np.abs(
            _cartPoleEnergy(domain, s) -
            _cartPoleEnergy(domain, domain.state)) < 0.01
        s = domain.state

    # Start vertical, ensure that force increases angular velocity in direction
    # Negative force on cart, yielding positive rotation
    s = np.array([0.0, 0.0, 0.0, 0.0])
    domain.state = s

    for i in np.arange(5):  # do for 5 steps and ensure works
        domain.step(LEFT_FORCE)
        assert np.all(
            domain.state[0:2] > s[0:2])  # angle and angular velocity increase
        s = domain.state

    # Positive force on cart, yielding negative rotation
    s = np.array([0.0, 0.0, 0.0, 0.0])
    domain.state = s

    for i in np.arange(5):  # do for 5 steps and ensure works
        domain.step(RIGHT_FORCE)
        assert np.all(
            domain.state[0:2] < s[0:2])  # angle and angular velocity increase
        s = domain.state